ROL
ROL_OptimizationSolver.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef ROL_OPTIMIZATIONSOLVER_HPP
11 #define ROL_OPTIMIZATIONSOLVER_HPP
12 
13 #include "ROL_Algorithm.hpp"
14 #include "ROL_StepFactory.hpp"
18 
19 #include "ROL_Stream.hpp"
20 
26 namespace ROL {
27 
28 template<class Real>
30 private:
31 
32  ROL::Ptr<Algorithm<Real> > algo_;
33  ROL::Ptr<Step<Real> > step_;
34  ROL::Ptr<StatusTest<Real> > status0_;
35  ROL::Ptr<CombinedStatusTest<Real> > status_;
36  ROL::Ptr<AlgorithmState<Real> > state_;
37 
38  ROL::Ptr<Vector<Real> > x_;
39  ROL::Ptr<Vector<Real> > g_;
40  ROL::Ptr<Vector<Real> > l_;
41  ROL::Ptr<Vector<Real> > c_;
42 
43  ROL::Ptr<Objective<Real> > obj_;
44  ROL::Ptr<BoundConstraint<Real> > bnd_;
45  ROL::Ptr<Constraint<Real> > con_;
46 
47  std::vector<std::string> output_;
48 
51  std::string stepname_;
52 
53  Real pen_;
54 
55 public:
56 
65  ROL::ParameterList &parlist ) {
66 
67  // Get optimization problem type: U, E, B, EB
69 
70  // Initialize AlgorithmState
71  state_ = ROL::makePtr<AlgorithmState<Real>>();
72 
73  // Get step name from parameterlist
74  stepname_ = parlist.sublist("Step").get("Type","Last Type (Dummy)");
75  stepType_ = StringToEStep(stepname_);
76 
77  // Set default algorithm if provided step is incompatible with problem type
79  switch ( problemType_ ) {
80  case TYPE_U:
81  stepType_ = STEP_TRUSTREGION; break;
82  case TYPE_B:
83  stepType_ = STEP_TRUSTREGION; break;
84  case TYPE_E:
86  case TYPE_EB:
88  case TYPE_LAST:
89  default:
90  throw Exception::NotImplemented(">>> ROL::OptimizationSolver: Unknown problem type!");
91  }
92  }
93  stepname_ = EStepToString(stepType_);
94 
95  // Build status test
96  StatusTestFactory<Real> statusTestFactory;
97  status0_ = statusTestFactory.getStatusTest(stepname_,parlist);
98  status_ = ROL::makePtr<CombinedStatusTest<Real>>();
99 
100  // Get optimization vector and a vector for the gradient
101  x_ = opt.getSolutionVector();
102  g_ = x_->dual().clone();
103 
104  // Initialize Step
106  step_ = stepFactory.getStep(stepname_,parlist);
107 
108  // If there is an equality constraint, get the multiplier and create a constraint vector
109  if( problemType_ == TYPE_E || problemType_ == TYPE_EB ) {
110  l_ = opt.getMultiplierVector();
111  c_ = l_->dual().clone();
112  }
113 
114  // Create modified objectives if needed
115  const Real one(1), ten(10);
117  ROL::Ptr<Objective<Real> > raw_obj = opt.getObjective();
118  con_ = opt.getConstraint();
119  // TODO: Provide access to change initial penalty
120  obj_ = ROL::makePtr<AugmentedLagrangian<Real>>(raw_obj,con_,*l_,1.0,*x_,*c_,parlist);
121  bnd_ = opt.getBoundConstraint();
122  pen_ = parlist.sublist("Step").sublist("Augmented Lagrangian").get("Initial Penalty Parameter",ten);
123  }
124  else if( stepType_ == STEP_MOREAUYOSIDAPENALTY ) {
125  ROL::Ptr<Objective<Real> > raw_obj = opt.getObjective();
126  bnd_ = opt.getBoundConstraint();
127  con_ = opt.getConstraint();
128  // TODO: Provide access to change initial penalty
129  obj_ = ROL::makePtr<MoreauYosidaPenalty<Real>>(raw_obj,bnd_,*x_,parlist);
130  pen_ = parlist.sublist("Step").sublist("Moreau-Yosida Penalty").get("Initial Penalty Parameter",ten);
131  }
132  else if( stepType_ == STEP_INTERIORPOINT ) {
133  ROL::Ptr<Objective<Real> > raw_obj = opt.getObjective();
134  bnd_ = opt.getBoundConstraint();
135  con_ = opt.getConstraint();
136  // TODO: Provide access to change initial penalty
137  obj_ = ROL::makePtr<InteriorPoint::PenalizedObjective<Real>>(raw_obj,bnd_,*x_,parlist);
138  pen_ = parlist.sublist("Step").sublist("Interior Point").get("Initial Barrier Parameter",ten);
139  }
140  else if( stepType_ == STEP_FLETCHER ) {
141  ROL::Ptr<Objective<Real> > raw_obj = opt.getObjective();
142  bnd_ = opt.getBoundConstraint();
143  con_ = opt.getConstraint();
144  if( bnd_->isActivated() ) {
145  obj_ = ROL::makePtr<BoundFletcher<Real> >(raw_obj,con_,bnd_,*x_,*c_,parlist);
146  }
147  else {
148  obj_ = ROL::makePtr<Fletcher<Real> >(raw_obj,con_,*x_,*c_,parlist);
149  }
150  pen_ = parlist.sublist("Step").sublist("Fletcher").get("Penalty Parameter",one);
151  }
152  else {
153  obj_ = opt.getObjective();
154  bnd_ = opt.getBoundConstraint();
155  con_ = opt.getConstraint();
156  if( stepType_ == STEP_TRUSTREGION ) {
157  pen_ = parlist.sublist("Step").sublist("Trust Region").get("Initial Radius",ten);
158  }
159  else if( stepType_ == STEP_BUNDLE ) {
160  pen_ = parlist.sublist("Step").sublist("Bundle").get("Initial Trust-Region Parameter",ten);
161  }
162  }
163  }
164 
169  std::vector<std::string> getOutput(void) const {
170  return output_;
171  }
172 
180  int solve(const ROL::Ptr<StatusTest<Real> > &status = ROL::nullPtr,
181  const bool combineStatus = true) {
182  ROL::nullstream bhs;
183  return solve(bhs,status,combineStatus);
184  }
185 
194  int solve( std::ostream &outStream,
195  const ROL::Ptr<StatusTest<Real> > &status = ROL::nullPtr,
196  const bool combineStatus = true ) {
197  // Build algorithm
198  status_->reset(); // Clear previous status test
199  status_->add(status0_); // Default StatusTest
200  if (status != ROL::nullPtr) {
201  if (!combineStatus) { // Use only user-defined StatusTest
202  status_->reset();
203  }
204  status_->add(status); // Add user-defined StatusTest
205  }
206  algo_ = ROL::makePtr<Algorithm<Real>>( step_, status_, state_ );
207 
208  switch(problemType_) {
209  case TYPE_U:
210  output_ = algo_->run(*x_,*g_,*obj_,true,outStream);
211  break;
212  case TYPE_B:
213  output_ = algo_->run(*x_,*g_,*obj_,*bnd_,true,outStream);
214  break;
215  case TYPE_E:
216  output_ = algo_->run(*x_,*g_,*l_,*c_,*obj_,*con_,true,outStream);
217  break;
218  case TYPE_EB:
219  output_ = algo_->run(*x_,*g_,*l_,*c_,*obj_,*con_,*bnd_,true,outStream);
220  break;
221  case TYPE_LAST:
222  ROL_TEST_FOR_EXCEPTION(true,std::invalid_argument,
223  "Error in OptimizationSolver::solve() : Unsupported problem type");
224  }
225 
226  // TODO: Interrogate AlgorithmState and StatusTest to generate a return code
227  // that indicates why the solver has stopped
228 
229  // Return an integer code
230  return 0;
231  }
232 
237  ROL::Ptr<const AlgorithmState<Real> > getAlgorithmState(void) const {
238  return state_;
239  }
240 
247  void resetAlgorithmState(void) {
248  state_ = ROL::makePtr<AlgorithmState<Real>>();
249  }
250 
262  void reset(const bool resetAlgo = true) {
263  // Reset AlgorithmState
264  if (resetAlgo) {
266  }
267  // Reset StepState
268  step_->reset(pen_);
269  // Reset penalty objectives
271  ROL::dynamicPtrCast<AugmentedLagrangian<Real> >(obj_)->reset(*l_,pen_);
272  }
273  else if( stepType_ == STEP_MOREAUYOSIDAPENALTY ) {
274  ROL::dynamicPtrCast<MoreauYosidaPenalty<Real> >(obj_)->reset(pen_);
275  }
276  else if( stepType_ == STEP_INTERIORPOINT ) {
277  ROL::dynamicPtrCast<InteriorPoint::PenalizedObjective<Real> >(obj_)->updatePenalty(pen_);
278  }
279  }
280 
289  std::string getStepName(void) const {
290  return stepname_;
291  }
292 
293 }; // class OptimizationSolver
294 
295 
296 template<typename Real>
297 inline Ptr<OptimizationSolver<Real>>
299  ParameterList& parlist ) {
300  return makePtr<OptimizationSolver<Real>>(opt,parlist);
301 }
302 
303 template<typename Real>
304 inline Ptr<OptimizationSolver<Real>>
306  ParameterList& parlist ) {
307  return makePtr<OptimizationSolver<Real>>(*opt,parlist);
308 }
309 
310 template<typename Real>
311 inline Ptr<OptimizationSolver<Real>>
313  const Ptr<ParameterList>& parlist ) {
314  return makePtr<OptimizationSolver<Real>>(opt,*parlist);
315 }
316 
317 template<typename Real>
318 inline Ptr<OptimizationSolver<Real>>
320  const Ptr<ParameterList>& parlist ) {
321  return makePtr<OptimizationSolver<Real>>(*opt,*parlist);
322 }
323 
324 } // namespace ROL
325 
326 #endif // ROL_OPTIMIZATIONSOLVER_HPP
327 
328 
ROL::Ptr< BoundConstraint< Real > > bnd_
ROL::Ptr< Objective< Real > > obj_
ROL::Ptr< StatusTest< Real > > getStatusTest(const std::string step, ROL::ParameterList &parlist)
EStep StringToEStep(std::string s)
Definition: ROL_Types.hpp:361
ROL::Ptr< StatusTest< Real > > status0_
void stepFactory(ROL::ParameterList &parlist, ROL::Ptr< ROL::Step< Real > > &step)
A minimalist step factory which specializes the Step Type depending on whether a Trust-Region or Line...
ROL::Ptr< Step< Real > > getStep(const std::string &type, ROL::ParameterList &parlist) const
virtual Ptr< Objective< Real > > getObjective(void)
std::vector< std::string > output_
virtual Ptr< BoundConstraint< Real > > getBoundConstraint(void)
ROL::Ptr< Vector< Real > > c_
int solve(std::ostream &outStream, const ROL::Ptr< StatusTest< Real > > &status=ROL::nullPtr, const bool combineStatus=true)
Solve optimization problem.
ROL::Ptr< Step< Real > > step_
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
basic_nullstream< char, std::char_traits< char >> nullstream
Definition: ROL_Stream.hpp:36
ROL::Ptr< Vector< Real > > g_
ROL::Ptr< CombinedStatusTest< Real > > status_
ROL::Ptr< Constraint< Real > > con_
std::vector< std::string > getOutput(void) const
Returns iteration history as a vector of strings.
void reset(const bool resetAlgo=true)
Reset both Algorithm and Step.
virtual Ptr< Vector< Real > > getSolutionVector(void)
Provides an interface to check status of optimization algorithms.
virtual Ptr< Vector< Real > > getMultiplierVector(void)
ROL::Ptr< Algorithm< Real > > algo_
Provides a simplified interface for solving a wide range of optimization problems.
ROL::Ptr< Vector< Real > > x_
std::string getStepName(void) const
Grab step name (after check for consistency).
void resetAlgorithmState(void)
Reset the AlgorithmState.
ROL::Ptr< Vector< Real > > l_
int solve(const ROL::Ptr< StatusTest< Real > > &status=ROL::nullPtr, const bool combineStatus=true)
Solve optimization problem with no iteration output.
std::string EStepToString(EStep tr)
Definition: ROL_Types.hpp:256
EStep
Enumeration of step types.
Definition: ROL_Types.hpp:243
ROL::Ptr< AlgorithmState< Real > > state_
EProblem
Definition: ROL_Types.hpp:223
OptimizationSolver(OptimizationProblem< Real > &opt, ROL::ParameterList &parlist)
Constructor.
Ptr< OptimizationSolver< Real > > make_OptimizationSolver(OptimizationProblem< Real > &opt, ParameterList &parlist)
ROL::Ptr< const AlgorithmState< Real > > getAlgorithmState(void) const
Return the AlgorithmState.
virtual Ptr< Constraint< Real > > getConstraint(void)
bool isCompatibleStep(EProblem p, EStep s)
Definition: ROL_Types.hpp:274