1 /* _______________________________________________________________________ 2 3 DAKOTA: Design Analysis Kit for Optimization and Terascale Applications 4 Copyright 2014-2020 National Technology & Engineering Solutions of Sandia, LLC (NTESS). 5 This software is distributed under the GNU Lesser General Public License. 6 For more information, see the README file in the top Dakota directory. 7 _______________________________________________________________________ */ 8 9 //- Class: NPSOLOptimizer 10 //- Description: Wrapper class for NPSOL 11 //- Owner: Mike Eldred 12 //- Checked by: 13 //- Version: $Id: NPSOLOptimizer.hpp 6786 2010-05-19 21:39:57Z dmgay $ 14 15 #ifndef NPSOL_OPTIMIZER_H 16 #define NPSOL_OPTIMIZER_H 17 18 #include "DakotaOptimizer.hpp" 19 #include "SOLBase.hpp" 20 21 22 namespace Dakota { 23 24 /// Wrapper class for the NPSOL optimization library. 25 26 /** The NPSOLOptimizer class provides a wrapper for NPSOL, a Fortran 27 77 sequential quadratic programming library from Stanford 28 University marketed by Stanford Business Associates. It uses a 29 function pointer approach for which passed functions must be 30 either global functions or static member functions. Any attribute 31 used within static member functions must be either local to that 32 function or accessed through a static pointer. 33 34 The user input mappings are as follows: \c 35 max_function_evaluations is implemented directly in 36 NPSOLOptimizer's evaluator functions since there is no NPSOL 37 parameter equivalent, and \c max_iterations, \c 38 convergence_tolerance, \c output verbosity, \c verify_level, \c 39 function_precision, and \c linesearch_tolerance are mapped into 40 NPSOL's "Major Iteration Limit", "Optimality Tolerance", "Major 41 Print Level" (\c verbose: Major Print Level = 20; \c quiet: Major 42 Print Level = 10), "Verify Level", "Function Precision", and 43 "Linesearch Tolerance" parameters, respectively, using NPSOL's 44 npoptn() subroutine (as wrapped by npoptn2() from the 45 npoptn_wrapper.f file). Refer to [Gill, P.E., Murray, W., 46 Saunders, M.A., and Wright, M.H., 1986] for information on NPSOL's 47 optional input parameters and the npoptn() subroutine. */ 48 49 /** 50 * \brief A version of TraitsBase specialized for NPSOL optimizers 51 * 52 */ 53 54 class NPSOLTraits: public TraitsBase 55 { 56 public: 57 58 /// default constructor NPSOLTraits()59 NPSOLTraits() { } 60 61 /// destructor ~NPSOLTraits()62 virtual ~NPSOLTraits() { } 63 64 /// A temporary query used in the refactor is_derived()65 virtual bool is_derived() { return true; } 66 67 /// Return the flag indicating whether method supports continuous variables supports_continuous_variables()68 bool supports_continuous_variables() { return true; } 69 70 /// Return the flag indicating whether method supports linear equalities supports_linear_equality()71 bool supports_linear_equality() { return true; } 72 73 /// Return the flag indicating whether method supports linear inequalities supports_linear_inequality()74 bool supports_linear_inequality() { return true; } 75 76 /// Return the flag indicating whether method supports nonlinear equalities supports_nonlinear_equality()77 bool supports_nonlinear_equality() { return true; } 78 79 /// Return the flag indicating whether method supports nonlinear inequalities supports_nonlinear_inequality()80 bool supports_nonlinear_inequality() { return true; } 81 82 /// Return the format used for nonlinear inequality constraints nonlinear_inequality_format()83 NONLINEAR_INEQUALITY_FORMAT nonlinear_inequality_format() 84 { return NONLINEAR_INEQUALITY_FORMAT::TWO_SIDED; } 85 86 }; 87 88 89 class NPSOLOptimizer: public Optimizer, public SOLBase 90 { 91 public: 92 93 // 94 //- Heading: Constructors and destructor 95 // 96 97 /// standard constructor 98 NPSOLOptimizer(ProblemDescDB& problem_db, Model& model); 99 100 /// alternate constructor for Iterator instantiations by name 101 NPSOLOptimizer(Model& model); 102 103 /// alternate constructor for instantiations "on the fly" 104 NPSOLOptimizer(Model& model, const int& derivative_level, 105 const Real& conv_tol); 106 107 /// alternate constructor for instantiations "on the fly" 108 NPSOLOptimizer(const RealVector& initial_point, 109 const RealVector& var_lower_bnds, const RealVector& var_upper_bnds, 110 const RealMatrix& lin_ineq_coeffs, const RealVector& lin_ineq_lower_bnds, 111 const RealVector& lin_ineq_upper_bnds, const RealMatrix& lin_eq_coeffs, 112 const RealVector& lin_eq_targets, const RealVector& nonlin_ineq_lower_bnds, 113 const RealVector& nonlin_ineq_upper_bnds, 114 const RealVector& nonlin_eq_targets, 115 void (*user_obj_eval) (int&, int&, double*, double&, double*, int&), 116 void (*user_con_eval) (int&, int&, int&, int&, int*, double*, double*, 117 double*, int&), 118 const int& derivative_level, const Real& conv_tol); 119 120 /// alternate constructor for instantiations "on the fly" with additional NPSOL settings 121 NPSOLOptimizer(const RealVector& initial_point, 122 const RealVector& var_lower_bnds, const RealVector& var_upper_bnds, 123 const RealMatrix& lin_ineq_coeffs, 124 const RealVector& lin_ineq_lower_bnds, 125 const RealVector& lin_ineq_upper_bnds, 126 const RealMatrix& lin_eq_coeffs, 127 const RealVector& lin_eq_targets, 128 const RealVector& nonlin_ineq_lower_bnds, 129 const RealVector& nonlin_ineq_upper_bnds, 130 const RealVector& nonlin_eq_targets, 131 void (*user_obj_eval) (int&, int&, double*, double&, double*, int&), 132 void (*user_con_eval) (int&, int&, int&, int&, int*, double*, double*, 133 double*, int&), 134 const int& derivative_level, const Real& conv_tol, 135 const Real function_precision, const Real feas_tol, 136 const Real lin_feas_tol, const Real nonlin_feas_tol); 137 138 ~NPSOLOptimizer(); ///< destructor 139 140 // 141 //- Heading: Member functions 142 // 143 144 void core_run(); 145 146 void declare_sources(); 147 private: 148 149 // 150 //- Heading: Convenience member functions 151 // 152 153 /// called by core_run for setUpType == "model" 154 void find_optimum_on_model(); 155 /// called by core_run for setUpType == "user_functions" 156 void find_optimum_on_user_functions(); 157 158 // 159 //- Heading: Static member functions passed by pointer to NPSOL 160 // 161 162 /// OBJFUN in NPSOL manual: computes the value and first derivatives of the 163 /// objective function (passed by function pointer to NPSOL). 164 static void objective_eval(int& mode, int& n, double* x, double& f, 165 double* gradf, int& nstate); 166 167 // 168 //- Heading: Data 169 // 170 171 /// pointer to the active object instance used within the static evaluator 172 /// functions in order to avoid the need for static data 173 static NPSOLOptimizer* npsolInstance; 174 175 /// controls iteration mode: "model" (normal usage) or "user_functions" 176 /// (user-supplied functions mode for "on the fly" instantiations). 177 /// NonDReliability currently uses the user_functions mode. 178 String setUpType; 179 /// holds initial point passed in for "user_functions" mode. 180 RealVector initialPoint; 181 /// holds variable lower bounds passed in for "user_functions" mode. 182 RealVector lowerBounds; 183 /// holds variable upper bounds passed in for "user_functions" mode. 184 RealVector upperBounds; 185 /// holds function pointer for objective function evaluator passed in for 186 /// "user_functions" mode. 187 void (*userObjectiveEval) (int&, int&, double*, double&, double*, int&); 188 /// holds function pointer for constraint function evaluator passed in for 189 /// "user_functions" mode. 190 void (*userConstraintEval) (int&, int&, int&, int&, int*, double*, double*, 191 double*, int&); 192 }; 193 194 195 #ifdef HAVE_DYNLIB_FACTORIES 196 // --------------------------------------------------------- 197 // Factory functions for dynamic loading of solver libraries 198 // --------------------------------------------------------- 199 200 NPSOLOptimizer* new_NPSOLOptimizer(ProblemDescDB& problem_db, Model& model); 201 NPSOLOptimizer* new_NPSOLOptimizer(Model& model); 202 NPSOLOptimizer* new_NPSOLOptimizer(Model& model, const int&, const Real&); 203 NPSOLOptimizer* new_NPSOLOptimizer(const RealVector& initial_point, 204 const RealVector& var_lower_bnds, 205 const RealVector& var_upper_bnds, 206 const RealMatrix& lin_ineq_coeffs, 207 const RealVector& lin_ineq_lower_bnds, 208 const RealVector& lin_ineq_upper_bnds, 209 const RealMatrix& lin_eq_coeffs, 210 const RealVector& lin_eq_targets, 211 const RealVector& nonlin_ineq_lower_bnds, 212 const RealVector& nonlin_ineq_upper_bnds, 213 const RealVector& nonlin_eq_targets, 214 void (*user_obj_eval) (int&, int&, double*, double&, double*, int&), 215 void (*user_con_eval) (int&, int&, int&, int&, int*, double*, double*, 216 double*, int&), 217 const int& derivative_level, const Real& conv_tol); 218 219 #endif // HAVE_DYNLIB_FACTORIES 220 221 } // namespace Dakota 222 223 #endif // NPSOL_OPTIMIZER_H 224