OptiPack Version of the Day
OptiPack_NonlinearCG_decl.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_NONLINEAR_CG_DECL_HPP
00032 #define OPTIPACK_NONLINEAR_CG_DECL_HPP
00033 
00034 
00035 #include "OptiPack_Types.hpp"
00036 #include "Thyra_ModelEvaluator.hpp"
00037 #include "GlobiPack_LineSearchBase.hpp"
00038 #include "Teuchos_Describable.hpp"
00039 #include "Teuchos_VerboseObject.hpp"
00040 #include "Teuchos_ParameterListAcceptorDefaultBase.hpp"
00041 #include "Teuchos_ParameterEntryValidator.hpp"
00042 
00043 
00044 namespace OptiPack {
00045 
00046 
00047 namespace NonlinearCGUtils {
00048 
00049 
00051 enum ESolveReturn {
00052   SOLVE_SOLUTION_FOUND,      
00053   SOLVE_LINSEARCH_FAILURE,   
00054   SOLVE_MAX_ITERS_EXCEEDED   
00055 };
00056 
00057 
00059 enum ESolverTypes {
00060   NONLINEAR_CG_FR,      
00061   NONLINEAR_CG_PR_PLUS, 
00062   NONLINEAR_CG_FR_PR,   
00063   NONLINEAR_CG_HS       
00064 };
00065 
00066 
00067 } // namespace NonlinearCGUtils
00068 
00069 
00074 template<typename Scalar>
00075 class NonlinearCG
00076   : public Teuchos::Describable,
00077     public Teuchos::VerboseObject<NonlinearCG<Scalar> >,
00078     public Teuchos::ParameterListAcceptorDefaultBase
00079 {
00080 public:
00081 
00083   typedef typename ScalarTraits<Scalar>::magnitudeType ScalarMag;
00084 
00087 
00089   NonlinearCG();
00090 
00092   void initialize(
00093     const RCP<const Thyra::ModelEvaluator<Scalar> > &model,
00094     const int paramIndex,
00095     const int responseIndex,
00096     const RCP<GlobiPack::LineSearchBase<Scalar> > &linesearch
00097     );
00098 
00100   NonlinearCGUtils::ESolverTypes get_solverType() const;
00102   ScalarMag get_alpha_init() const;
00104   bool get_alpha_reinit() const;
00106   bool get_and_conv_tests() const;
00108   int get_minIters() const;
00110   int get_maxIters() const;
00112   ScalarMag get_g_reduct_tol() const;
00114   ScalarMag get_g_grad_tol() const;
00116   ScalarMag get_g_mag() const;
00117 
00119 
00122 
00124   void setParameterList(RCP<ParameterList> const& paramList);
00126   RCP<const ParameterList> getValidParameters() const;
00127 
00129 
00132 
00159   NonlinearCGUtils::ESolveReturn
00160   doSolve(
00161     const Ptr<Thyra::VectorBase<Scalar> > &p,
00162     const Ptr<ScalarMag> &g_opt,
00163     const Ptr<const ScalarMag> &g_reduct_tol = Teuchos::null,
00164     const Ptr<const ScalarMag> &g_grad_tol = Teuchos::null,
00165     const Ptr<const ScalarMag> &alpha_init = Teuchos::null,
00166     const Ptr<int> &numIters = Teuchos::null
00167     );
00168 
00170 
00171 private:
00172 
00173   // //////////////////////
00174   // Private data members
00175 
00176   RCP<const Thyra::ModelEvaluator<Scalar> > model_;
00177   int paramIndex_;
00178   int responseIndex_;
00179   RCP<GlobiPack::LineSearchBase<Scalar> > linesearch_;
00180 
00181   NonlinearCGUtils::ESolverTypes solverType_;
00182   ScalarMag alpha_init_;
00183   bool alpha_reinit_;
00184   bool and_conv_tests_;
00185   int minIters_;
00186   int maxIters_;
00187   ScalarMag g_reduct_tol_;
00188   ScalarMag g_grad_tol_;
00189   ScalarMag g_mag_;
00190   
00191   mutable int numIters_;
00192   
00193   static RCP<Teuchos::ParameterEntryValidator>
00194   solverType_validator_;
00195 
00196 };
00197 
00198 
00203 template<typename Scalar>
00204 const RCP<NonlinearCG<Scalar> >
00205 nonlinearCG()
00206 {
00207   return Teuchos::rcp(new NonlinearCG<Scalar>);
00208 }
00209 
00210 
00215 template<typename Scalar>
00216 const RCP<NonlinearCG<Scalar> >
00217 nonlinearCG(
00218   const RCP<const Thyra::ModelEvaluator<Scalar> > &model,
00219   const int paramIndex,
00220   const int responseIndex,
00221   const RCP<GlobiPack::LineSearchBase<Scalar> > &linesearch
00222   )
00223 {
00224   const RCP<NonlinearCG<Scalar> > solver = 
00225     Teuchos::rcp(new NonlinearCG<Scalar>);
00226   solver->initialize(model, paramIndex, responseIndex, linesearch);
00227   return solver;
00228 }
00229 
00230 
00231 // Default values are exposed here for unit testing purposes
00232 
00233 
00234 namespace NonlinearCGUtils {
00235 
00236 const std::string solverType_name = "Solver Type";
00237 const std::string solverType_default = "FR";
00238 const ESolverTypes solverType_default_integral_val = NONLINEAR_CG_FR;
00239 
00240 const std::string alpha_init_name = "Initial Linesearch Step Length";
00241 const double alpha_init_default = 1.0;
00242 
00243 const std::string alpha_reinit_name = "Reinitlaize Linesearch Step Length";
00244 const bool alpha_reinit_default = false;
00245 
00246 const std::string and_conv_tests_name = "AND Convergence Tests";
00247 const bool and_conv_tests_default = false;
00248 
00249 const std::string minIters_name = "Min Num Iterations";
00250 const int minIters_default = 0;
00251 
00252 const std::string maxIters_name = "Max Num Iterations";
00253 const int maxIters_default = 20;
00254 
00255 const std::string g_reduct_tol_name = "Objective Reduction Tol";
00256 const double g_reduct_tol_default = 1e-5;
00257 
00258 const std::string g_grad_tol_name = "Objective Gradient Tol";
00259 const double g_grad_tol_default = 1e-5;
00260 
00261 const std::string g_mag_name = "Objective Magnitude";
00262 const double g_mag_default = 1.0;
00263 
00264 
00265 } // namespace NonlinearCGUtils
00266 
00267 
00268 
00269 } // namespace OptiPack
00270 
00271 
00272 /* Todos:
00273 
00274 6) Implement FR-PR method
00275 
00276 7) Implement tabular output option
00277 
00278 */
00279 
00280 
00281 #endif // OPTIPACK_NONLINEAR_CG_DECL_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends