ROL
ROL_OptimizationSolver.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 #ifndef ROL_OPTIMIZATIONSOLVER_HPP
45 #define ROL_OPTIMIZATIONSOLVER_HPP
46 
47 #include "ROL_Algorithm.hpp"
48 #include "ROL_StepFactory.hpp"
52 
53 #include "ROL_Stream.hpp"
54 
60 namespace ROL {
61 
62 template<class Real>
64 private:
65 
66  ROL::Ptr<Algorithm<Real> > algo_;
67  ROL::Ptr<Step<Real> > step_;
68  ROL::Ptr<StatusTest<Real> > status0_;
69  ROL::Ptr<CombinedStatusTest<Real> > status_;
70  ROL::Ptr<AlgorithmState<Real> > state_;
71 
72  ROL::Ptr<Vector<Real> > x_;
73  ROL::Ptr<Vector<Real> > g_;
74  ROL::Ptr<Vector<Real> > l_;
75  ROL::Ptr<Vector<Real> > c_;
76 
77  ROL::Ptr<Objective<Real> > obj_;
78  ROL::Ptr<BoundConstraint<Real> > bnd_;
79  ROL::Ptr<Constraint<Real> > con_;
80 
81  std::vector<std::string> output_;
82 
85  std::string stepname_;
86 
87  Real pen_;
88 
89 public:
90 
99  ROL::ParameterList &parlist ) {
100 
101  // Get optimization problem type: U, E, B, EB
103 
104  // Initialize AlgorithmState
105  state_ = ROL::makePtr<AlgorithmState<Real>>();
106 
107  // Get step name from parameterlist
108  stepname_ = parlist.sublist("Step").get("Type","Last Type (Dummy)");
109  stepType_ = StringToEStep(stepname_);
110 
111  // Set default algorithm if provided step is incompatible with problem type
113  switch ( problemType_ ) {
114  case TYPE_U:
115  stepType_ = STEP_TRUSTREGION; break;
116  case TYPE_B:
117  stepType_ = STEP_TRUSTREGION; break;
118  case TYPE_E:
119  stepType_ = STEP_COMPOSITESTEP; break;
120  case TYPE_EB:
122  case TYPE_LAST:
123  default:
124  throw Exception::NotImplemented(">>> ROL::OptimizationSolver: Unknown problem type!");
125  }
126  }
127  stepname_ = EStepToString(stepType_);
128 
129  // Build status test
130  StatusTestFactory<Real> statusTestFactory;
131  status0_ = statusTestFactory.getStatusTest(stepname_,parlist);
132  status_ = ROL::makePtr<CombinedStatusTest<Real>>();
133 
134  // Get optimization vector and a vector for the gradient
135  x_ = opt.getSolutionVector();
136  g_ = x_->dual().clone();
137 
138  // Initialize Step
140  step_ = stepFactory.getStep(stepname_,parlist);
141 
142  // If there is an equality constraint, get the multiplier and create a constraint vector
143  if( problemType_ == TYPE_E || problemType_ == TYPE_EB ) {
144  l_ = opt.getMultiplierVector();
145  c_ = l_->dual().clone();
146  }
147 
148  // Create modified objectives if needed
149  const Real one(1), ten(10);
151  ROL::Ptr<Objective<Real> > raw_obj = opt.getObjective();
152  con_ = opt.getConstraint();
153  // TODO: Provide access to change initial penalty
154  obj_ = ROL::makePtr<AugmentedLagrangian<Real>>(raw_obj,con_,*l_,1.0,*x_,*c_,parlist);
155  bnd_ = opt.getBoundConstraint();
156  pen_ = parlist.sublist("Step").sublist("Augmented Lagrangian").get("Initial Penalty Parameter",ten);
157  }
158  else if( stepType_ == STEP_MOREAUYOSIDAPENALTY ) {
159  ROL::Ptr<Objective<Real> > raw_obj = opt.getObjective();
160  bnd_ = opt.getBoundConstraint();
161  con_ = opt.getConstraint();
162  // TODO: Provide access to change initial penalty
163  obj_ = ROL::makePtr<MoreauYosidaPenalty<Real>>(raw_obj,bnd_,*x_,parlist);
164  pen_ = parlist.sublist("Step").sublist("Moreau-Yosida Penalty").get("Initial Penalty Parameter",ten);
165  }
166  else if( stepType_ == STEP_INTERIORPOINT ) {
167  ROL::Ptr<Objective<Real> > raw_obj = opt.getObjective();
168  bnd_ = opt.getBoundConstraint();
169  con_ = opt.getConstraint();
170  // TODO: Provide access to change initial penalty
171  obj_ = ROL::makePtr<InteriorPoint::PenalizedObjective<Real>>(raw_obj,bnd_,*x_,parlist);
172  pen_ = parlist.sublist("Step").sublist("Interior Point").get("Initial Barrier Parameter",ten);
173  }
174  else if( stepType_ == STEP_FLETCHER ) {
175  ROL::Ptr<Objective<Real> > raw_obj = opt.getObjective();
176  bnd_ = opt.getBoundConstraint();
177  con_ = opt.getConstraint();
178  if( bnd_->isActivated() ) {
179  obj_ = ROL::makePtr<BoundFletcher<Real> >(raw_obj,con_,bnd_,*x_,*c_,parlist);
180  }
181  else {
182  obj_ = ROL::makePtr<Fletcher<Real> >(raw_obj,con_,*x_,*c_,parlist);
183  }
184  pen_ = parlist.sublist("Step").sublist("Fletcher").get("Penalty Parameter",one);
185  }
186  else {
187  obj_ = opt.getObjective();
188  bnd_ = opt.getBoundConstraint();
189  con_ = opt.getConstraint();
190  if( stepType_ == STEP_TRUSTREGION ) {
191  pen_ = parlist.sublist("Step").sublist("Trust Region").get("Initial Radius",ten);
192  }
193  else if( stepType_ == STEP_BUNDLE ) {
194  pen_ = parlist.sublist("Step").sublist("Bundle").get("Initial Trust-Region Parameter",ten);
195  }
196  }
197  }
198 
203  std::vector<std::string> getOutput(void) const {
204  return output_;
205  }
206 
214  int solve(const ROL::Ptr<StatusTest<Real> > &status = ROL::nullPtr,
215  const bool combineStatus = true) {
216  ROL::nullstream bhs;
217  return solve(bhs,status,combineStatus);
218  }
219 
228  int solve( std::ostream &outStream,
229  const ROL::Ptr<StatusTest<Real> > &status = ROL::nullPtr,
230  const bool combineStatus = true ) {
231  // Build algorithm
232  status_->reset(); // Clear previous status test
233  status_->add(status0_); // Default StatusTest
234  if (status != ROL::nullPtr) {
235  if (!combineStatus) { // Use only user-defined StatusTest
236  status_->reset();
237  }
238  status_->add(status); // Add user-defined StatusTest
239  }
240  algo_ = ROL::makePtr<Algorithm<Real>>( step_, status_, state_ );
241 
242  switch(problemType_) {
243  case TYPE_U:
244  output_ = algo_->run(*x_,*g_,*obj_,true,outStream);
245  break;
246  case TYPE_B:
247  output_ = algo_->run(*x_,*g_,*obj_,*bnd_,true,outStream);
248  break;
249  case TYPE_E:
250  output_ = algo_->run(*x_,*g_,*l_,*c_,*obj_,*con_,true,outStream);
251  break;
252  case TYPE_EB:
253  output_ = algo_->run(*x_,*g_,*l_,*c_,*obj_,*con_,*bnd_,true,outStream);
254  break;
255  case TYPE_LAST:
256  ROL_TEST_FOR_EXCEPTION(true,std::invalid_argument,
257  "Error in OptimizationSolver::solve() : Unsupported problem type");
258  }
259 
260  // TODO: Interrogate AlgorithmState and StatusTest to generate a return code
261  // that indicates why the solver has stopped
262 
263  // Return an integer code
264  return 0;
265  }
266 
271  ROL::Ptr<const AlgorithmState<Real> > getAlgorithmState(void) const {
272  return state_;
273  }
274 
281  void resetAlgorithmState(void) {
282  state_ = ROL::makePtr<AlgorithmState<Real>>();
283  }
284 
296  void reset(const bool resetAlgo = true) {
297  // Reset AlgorithmState
298  if (resetAlgo) {
300  }
301  // Reset StepState
302  step_->reset(pen_);
303  // Reset penalty objectives
305  ROL::dynamicPtrCast<AugmentedLagrangian<Real> >(obj_)->reset(*l_,pen_);
306  }
307  else if( stepType_ == STEP_MOREAUYOSIDAPENALTY ) {
308  ROL::dynamicPtrCast<MoreauYosidaPenalty<Real> >(obj_)->reset(pen_);
309  }
310  else if( stepType_ == STEP_INTERIORPOINT ) {
311  ROL::dynamicPtrCast<InteriorPoint::PenalizedObjective<Real> >(obj_)->updatePenalty(pen_);
312  }
313  }
314 
323  std::string getStepName(void) const {
324  return stepname_;
325  }
326 
327 }; // class OptimizationSolver
328 
329 
330 template<typename Real>
331 inline Ptr<OptimizationSolver<Real>>
333  ParameterList& parlist ) {
334  return makePtr<OptimizationSolver<Real>>(opt,parlist);
335 }
336 
337 template<typename Real>
338 inline Ptr<OptimizationSolver<Real>>
340  ParameterList& parlist ) {
341  return makePtr<OptimizationSolver<Real>>(*opt,parlist);
342 }
343 
344 template<typename Real>
345 inline Ptr<OptimizationSolver<Real>>
347  const Ptr<ParameterList>& parlist ) {
348  return makePtr<OptimizationSolver<Real>>(opt,*parlist);
349 }
350 
351 template<typename Real>
352 inline Ptr<OptimizationSolver<Real>>
354  const Ptr<ParameterList>& parlist ) {
355  return makePtr<OptimizationSolver<Real>>(*opt,*parlist);
356 }
357 
358 } // namespace ROL
359 
360 #endif // ROL_OPTIMIZATIONSOLVER_HPP
361 
362 
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:389
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...
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).
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:72
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:287
EStep
Enumeration of step types.
Definition: ROL_Types.hpp:274
ROL::Ptr< AlgorithmState< Real > > state_
EProblem
Definition: ROL_Types.hpp:255
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:305