OptiPack Version of the Day
OptiPack_UnconstrainedOptMeritFunc1D_def.hpp
00001 /*
00002 // @HEADER
00003 // ***********************************************************************
00004 // 
00005 //    OptiPack: Collection of simple Thyra-based Optimization ANAs
00006 //                 Copyright (2009) Sandia Corporation
00007 // 
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 // 
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00039 // 
00040 // ***********************************************************************
00041 // @HEADER
00042 */
00043 
00044 #ifndef OPTIPACK_UNCONSTRAINED_OPT_MERIT_FUNC_1D_DEF_HPP
00045 #define OPTIPACK_UNCONSTRAINED_OPT_MERIT_FUNC_1D_DEF_HPP
00046 
00047 
00048 #include "OptiPack_Types.hpp"
00049 #include "OptiPack_LineSearchPointEvaluatorBase.hpp"
00050 #include "Thyra_ModelEvaluatorHelpers.hpp"
00051 #include "Thyra_VectorStdOps.hpp"
00052 #include "GlobiPack_MeritFunc1DBase.hpp"
00053 #include "Teuchos_Assert.hpp"
00054 
00055 
00056 namespace OptiPack {
00057 
00058 
00059 // Constructor/Initializers/Accessors
00060 
00061 
00062 template<typename Scalar>
00063 UnconstrainedOptMeritFunc1D<Scalar>::UnconstrainedOptMeritFunc1D()
00064   : paramIndex_(-1),
00065     responseIndex_(-1)
00066 {}
00067 
00068 
00069 template<typename Scalar>
00070 void UnconstrainedOptMeritFunc1D<Scalar>::setModel(
00071   const RCP<const Thyra::ModelEvaluator<Scalar> > &model,
00072   const int paramIndex,
00073   const int responseIndex
00074   )
00075 {
00076 #ifdef TEUCHOS_DEBUG
00077   model.assert_not_null();
00078   TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE( paramIndex, 0, model->Np() );
00079   TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE( responseIndex, 0, model->Ng() );
00080 #endif
00081   model_ = model;
00082   paramIndex_ = paramIndex;
00083   responseIndex_ = responseIndex;
00084 }
00085 
00086 
00087 template<typename Scalar>
00088 void UnconstrainedOptMeritFunc1D<Scalar>::setEvaluationQuantities(
00089   const RCP<const LineSearchPointEvaluatorBase<Scalar> > &pointEvaluator,
00090   const RCP<Thyra::VectorBase<Scalar> > &p,
00091   const RCP<Thyra::VectorBase<Scalar> > &g_vec,
00092   const RCP<Thyra::VectorBase<Scalar> > &g_grad_vec
00093   )
00094 {
00095 #ifdef TEUCHOS_DEBUG
00096   pointEvaluator.assert_not_null();
00097   p.assert_not_null();
00098   g_vec.assert_not_null();
00099   // ToDo: Check that pointEvaluator, p, g_vec, and g_grad_vec are compatible!
00100 #endif
00101   pointEvaluator_ = pointEvaluator;
00102   p_ = p;
00103   g_vec_ = g_vec;
00104   g_grad_vec_ = g_grad_vec; // Can be null and that is okay
00105 }
00106 
00107 
00108 // Overridden from MeritFunc1DBase
00109 
00110 
00111 template<typename Scalar>
00112 bool UnconstrainedOptMeritFunc1D<Scalar>::supportsDerivEvals() const
00113 {
00114   return !is_null(g_grad_vec_);
00115 }
00116 
00117 
00118 template<typename Scalar>
00119 void UnconstrainedOptMeritFunc1D<Scalar>::eval(
00120   const ScalarMag &alpha, const Ptr<ScalarMag> &phi,
00121   const Ptr<ScalarMag> &Dphi ) const
00122 {
00123   typedef Thyra::ModelEvaluatorBase MEB;
00124   using Thyra::get_ele;
00125   using Thyra::eval_g;
00126   using Thyra::eval_g_DgDp;
00127   pointEvaluator_->computePoint(alpha, p_.ptr());
00128   if (!is_null(g_grad_vec_)) {
00129     TEUCHOS_TEST_FOR_EXCEPT_MSG( true,
00130       "Error, g_grad_vec has not been implemented yet!.");
00131   }
00132   else {
00133 #ifdef TEUCHOS_DEBUG
00134     TEUCHOS_ASSERT(is_null(Dphi));
00135 #endif
00136     eval_g( *model_, paramIndex_, *p_, responseIndex_, g_vec_.ptr() );
00137     *phi = get_ele(*g_vec_, 0);
00138   }
00139 }
00140 
00141 
00142 } // namespace OptiPack
00143 
00144 
00145 #endif // OPTIPACK_UNCONSTRAINED_OPT_MERIT_FUNC_1D_DEF_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends