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 // 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_NONLINEAR_CG_DECL_HPP
00045 #define OPTIPACK_NONLINEAR_CG_DECL_HPP
00046 
00047 
00048 #include "OptiPack_Types.hpp"
00049 #include "Thyra_ModelEvaluator.hpp"
00050 #include "GlobiPack_LineSearchBase.hpp"
00051 #include "Teuchos_Describable.hpp"
00052 #include "Teuchos_VerboseObject.hpp"
00053 #include "Teuchos_ParameterListAcceptorDefaultBase.hpp"
00054 #include "Teuchos_ParameterEntryValidator.hpp"
00055 
00056 
00057 namespace OptiPack {
00058 
00059 
00060 namespace NonlinearCGUtils {
00061 
00062 
00064 enum ESolveReturn {
00065   SOLVE_SOLUTION_FOUND,      
00066   SOLVE_LINSEARCH_FAILURE,   
00067   SOLVE_MAX_ITERS_EXCEEDED   
00068 };
00069 
00070 
00072 enum ESolverTypes {
00073   NONLINEAR_CG_FR,      
00074   NONLINEAR_CG_PR_PLUS, 
00075   NONLINEAR_CG_FR_PR,   
00076   NONLINEAR_CG_HS       
00077 };
00078 
00079 
00080 } // namespace NonlinearCGUtils
00081 
00082 
00087 template<typename Scalar>
00088 class NonlinearCG
00089   : public Teuchos::Describable,
00090     public Teuchos::VerboseObject<NonlinearCG<Scalar> >,
00091     public Teuchos::ParameterListAcceptorDefaultBase
00092 {
00093 public:
00094 
00096   typedef typename ScalarTraits<Scalar>::magnitudeType ScalarMag;
00097 
00100 
00102   NonlinearCG();
00103 
00105   void initialize(
00106     const RCP<const Thyra::ModelEvaluator<Scalar> > &model,
00107     const int paramIndex,
00108     const int responseIndex,
00109     const RCP<GlobiPack::LineSearchBase<Scalar> > &linesearch
00110     );
00111 
00113   NonlinearCGUtils::ESolverTypes get_solverType() const;
00115   ScalarMag get_alpha_init() const;
00117   bool get_alpha_reinit() const;
00119   bool get_and_conv_tests() const;
00121   int get_minIters() const;
00123   int get_maxIters() const;
00125   ScalarMag get_g_reduct_tol() const;
00127   ScalarMag get_g_grad_tol() const;
00129   ScalarMag get_g_mag() const;
00130 
00132 
00135 
00137   void setParameterList(RCP<ParameterList> const& paramList);
00139   RCP<const ParameterList> getValidParameters() const;
00140 
00142 
00145 
00172   NonlinearCGUtils::ESolveReturn
00173   doSolve(
00174     const Ptr<Thyra::VectorBase<Scalar> > &p,
00175     const Ptr<ScalarMag> &g_opt,
00176     const Ptr<const ScalarMag> &g_reduct_tol = Teuchos::null,
00177     const Ptr<const ScalarMag> &g_grad_tol = Teuchos::null,
00178     const Ptr<const ScalarMag> &alpha_init = Teuchos::null,
00179     const Ptr<int> &numIters = Teuchos::null
00180     );
00181 
00183 
00184 private:
00185 
00186   // //////////////////////
00187   // Private data members
00188 
00189   RCP<const Thyra::ModelEvaluator<Scalar> > model_;
00190   int paramIndex_;
00191   int responseIndex_;
00192   RCP<GlobiPack::LineSearchBase<Scalar> > linesearch_;
00193 
00194   NonlinearCGUtils::ESolverTypes solverType_;
00195   ScalarMag alpha_init_;
00196   bool alpha_reinit_;
00197   bool and_conv_tests_;
00198   int minIters_;
00199   int maxIters_;
00200   ScalarMag g_reduct_tol_;
00201   ScalarMag g_grad_tol_;
00202   ScalarMag g_mag_;
00203   
00204   mutable int numIters_;
00205   
00206   static RCP<Teuchos::ParameterEntryValidator>
00207   solverType_validator_;
00208 
00209 };
00210 
00211 
00216 template<typename Scalar>
00217 const RCP<NonlinearCG<Scalar> >
00218 nonlinearCG()
00219 {
00220   return Teuchos::rcp(new NonlinearCG<Scalar>);
00221 }
00222 
00223 
00228 template<typename Scalar>
00229 const RCP<NonlinearCG<Scalar> >
00230 nonlinearCG(
00231   const RCP<const Thyra::ModelEvaluator<Scalar> > &model,
00232   const int paramIndex,
00233   const int responseIndex,
00234   const RCP<GlobiPack::LineSearchBase<Scalar> > &linesearch
00235   )
00236 {
00237   const RCP<NonlinearCG<Scalar> > solver = 
00238     Teuchos::rcp(new NonlinearCG<Scalar>);
00239   solver->initialize(model, paramIndex, responseIndex, linesearch);
00240   return solver;
00241 }
00242 
00243 
00244 // Default values are exposed here for unit testing purposes
00245 
00246 
00247 namespace NonlinearCGUtils {
00248 
00249 const std::string solverType_name = "Solver Type";
00250 const std::string solverType_default = "FR";
00251 const ESolverTypes solverType_default_integral_val = NONLINEAR_CG_FR;
00252 
00253 const std::string alpha_init_name = "Initial Linesearch Step Length";
00254 const double alpha_init_default = 1.0;
00255 
00256 const std::string alpha_reinit_name = "Reinitlaize Linesearch Step Length";
00257 const bool alpha_reinit_default = false;
00258 
00259 const std::string and_conv_tests_name = "AND Convergence Tests";
00260 const bool and_conv_tests_default = false;
00261 
00262 const std::string minIters_name = "Min Num Iterations";
00263 const int minIters_default = 0;
00264 
00265 const std::string maxIters_name = "Max Num Iterations";
00266 const int maxIters_default = 20;
00267 
00268 const std::string g_reduct_tol_name = "Objective Reduction Tol";
00269 const double g_reduct_tol_default = 1e-5;
00270 
00271 const std::string g_grad_tol_name = "Objective Gradient Tol";
00272 const double g_grad_tol_default = 1e-5;
00273 
00274 const std::string g_mag_name = "Objective Magnitude";
00275 const double g_mag_default = 1.0;
00276 
00277 
00278 } // namespace NonlinearCGUtils
00279 
00280 
00281 
00282 } // namespace OptiPack
00283 
00284 
00285 /* Todos:
00286 
00287 6) Implement FR-PR method
00288 
00289 7) Implement tabular output option
00290 
00291 */
00292 
00293 
00294 #endif // OPTIPACK_NONLINEAR_CG_DECL_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends