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 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 // 
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 // 
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA
00025 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00026 // 
00027 // ***********************************************************************
00028 // @HEADER
00029 */
00030 
00031 #ifndef OPTIPACK_UNCONSTRAINED_OPT_MERIT_FUNC_1D_DEF_HPP
00032 #define OPTIPACK_UNCONSTRAINED_OPT_MERIT_FUNC_1D_DEF_HPP
00033 
00034 
00035 #include "OptiPack_Types.hpp"
00036 #include "OptiPack_LineSearchPointEvaluatorBase.hpp"
00037 #include "Thyra_ModelEvaluatorHelpers.hpp"
00038 #include "Thyra_VectorStdOps.hpp"
00039 #include "GlobiPack_MeritFunc1DBase.hpp"
00040 #include "Teuchos_Assert.hpp"
00041 
00042 
00043 namespace OptiPack {
00044 
00045 
00046 // Constructor/Initializers/Accessors
00047 
00048 
00049 template<typename Scalar>
00050 UnconstrainedOptMeritFunc1D<Scalar>::UnconstrainedOptMeritFunc1D()
00051   : paramIndex_(-1),
00052     responseIndex_(-1)
00053 {}
00054 
00055 
00056 template<typename Scalar>
00057 void UnconstrainedOptMeritFunc1D<Scalar>::setModel(
00058   const RCP<const Thyra::ModelEvaluator<Scalar> > &model,
00059   const int paramIndex,
00060   const int responseIndex
00061   )
00062 {
00063 #ifdef TEUCHOS_DEBUG
00064   model.assert_not_null();
00065   TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE( paramIndex, 0, model->Np() );
00066   TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE( responseIndex, 0, model->Ng() );
00067 #endif
00068   model_ = model;
00069   paramIndex_ = paramIndex;
00070   responseIndex_ = responseIndex;
00071 }
00072 
00073 
00074 template<typename Scalar>
00075 void UnconstrainedOptMeritFunc1D<Scalar>::setEvaluationQuantities(
00076   const RCP<const LineSearchPointEvaluatorBase<Scalar> > &pointEvaluator,
00077   const RCP<Thyra::VectorBase<Scalar> > &p,
00078   const RCP<Thyra::VectorBase<Scalar> > &g_vec,
00079   const RCP<Thyra::VectorBase<Scalar> > &g_grad_vec
00080   )
00081 {
00082 #ifdef TEUCHOS_DEBUG
00083   pointEvaluator.assert_not_null();
00084   p.assert_not_null();
00085   g_vec.assert_not_null();
00086   // ToDo: Check that pointEvaluator, p, g_vec, and g_grad_vec are compatible!
00087 #endif
00088   pointEvaluator_ = pointEvaluator;
00089   p_ = p;
00090   g_vec_ = g_vec;
00091   g_grad_vec_ = g_grad_vec; // Can be null and that is okay
00092 }
00093 
00094 
00095 // Overridden from MeritFunc1DBase
00096 
00097 
00098 template<typename Scalar>
00099 bool UnconstrainedOptMeritFunc1D<Scalar>::supportsDerivEvals() const
00100 {
00101   return !is_null(g_grad_vec_);
00102 }
00103 
00104 
00105 template<typename Scalar>
00106 void UnconstrainedOptMeritFunc1D<Scalar>::eval(
00107   const ScalarMag &alpha, const Ptr<ScalarMag> &phi,
00108   const Ptr<ScalarMag> &Dphi ) const
00109 {
00110   typedef Thyra::ModelEvaluatorBase MEB;
00111   using Thyra::get_ele;
00112   using Thyra::eval_g;
00113   using Thyra::eval_g_DgDp;
00114   pointEvaluator_->computePoint(alpha, p_.ptr());
00115   if (!is_null(g_grad_vec_)) {
00116     TEST_FOR_EXCEPT_MSG( true,
00117       "Error, g_grad_vec has not been implemented yet!.");
00118   }
00119   else {
00120 #ifdef TEUCHOS_DEBUG
00121     TEUCHOS_ASSERT(is_null(Dphi));
00122 #endif
00123     eval_g( *model_, paramIndex_, *p_, responseIndex_, g_vec_.ptr() );
00124     *phi = get_ele(*g_vec_, 0);
00125   }
00126 }
00127 
00128 
00129 } // namespace OptiPack
00130 
00131 
00132 #endif // OPTIPACK_UNCONSTRAINED_OPT_MERIT_FUNC_1D_DEF_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends