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
26namespace ROL {
27
28template<class Real>
30private:
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
55public:
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)");
76
77 // Set default algorithm if provided step is incompatible with problem type
79 switch ( problemType_ ) {
80 case TYPE_U:
82 case TYPE_B:
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 }
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
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 default:
223 ROL_TEST_FOR_EXCEPTION(true,std::invalid_argument,
224 "Error in OptimizationSolver::solve() : Unsupported problem type");
225 }
226
227 // TODO: Interrogate AlgorithmState and StatusTest to generate a return code
228 // that indicates why the solver has stopped
229
230 // Return an integer code
231 return 0;
232 }
233
238 ROL::Ptr<const AlgorithmState<Real> > getAlgorithmState(void) const {
239 return state_;
240 }
241
249 state_ = ROL::makePtr<AlgorithmState<Real>>();
250 }
251
263 void reset(const bool resetAlgo = true) {
264 // Reset AlgorithmState
265 if (resetAlgo) {
267 }
268 // Reset StepState
269 step_->reset(pen_);
270 // Reset penalty objectives
272 ROL::dynamicPtrCast<AugmentedLagrangian<Real> >(obj_)->reset(*l_,pen_);
273 }
274 else if( stepType_ == STEP_MOREAUYOSIDAPENALTY ) {
275 ROL::dynamicPtrCast<MoreauYosidaPenalty<Real> >(obj_)->reset(pen_);
276 }
277 else if( stepType_ == STEP_INTERIORPOINT ) {
278 ROL::dynamicPtrCast<InteriorPoint::PenalizedObjective<Real> >(obj_)->updatePenalty(pen_);
279 }
280 }
281
290 std::string getStepName(void) const {
291 return stepname_;
292 }
293
294}; // class OptimizationSolver
295
296
297template<typename Real>
298inline Ptr<OptimizationSolver<Real>>
300 ParameterList& parlist ) {
301 return makePtr<OptimizationSolver<Real>>(opt,parlist);
302}
303
304template<typename Real>
305inline Ptr<OptimizationSolver<Real>>
307 ParameterList& parlist ) {
308 return makePtr<OptimizationSolver<Real>>(*opt,parlist);
309}
310
311template<typename Real>
312inline Ptr<OptimizationSolver<Real>>
314 const Ptr<ParameterList>& parlist ) {
315 return makePtr<OptimizationSolver<Real>>(opt,*parlist);
316}
317
318template<typename Real>
319inline Ptr<OptimizationSolver<Real>>
321 const Ptr<ParameterList>& parlist ) {
322 return makePtr<OptimizationSolver<Real>>(*opt,*parlist);
323}
324
325} // namespace ROL
326
327#endif // ROL_OPTIMIZATIONSOLVER_HPP
328
329
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
virtual Ptr< Vector< Real > > getMultiplierVector(void)
virtual Ptr< Vector< Real > > getSolutionVector(void)
virtual Ptr< BoundConstraint< Real > > getBoundConstraint(void)
virtual Ptr< Constraint< Real > > getConstraint(void)
virtual Ptr< Objective< Real > > getObjective(void)
Provides a simplified interface for solving a wide range of optimization problems.
ROL::Ptr< Step< Real > > step_
ROL::Ptr< StatusTest< Real > > status0_
ROL::Ptr< Algorithm< Real > > algo_
ROL::Ptr< AlgorithmState< Real > > state_
ROL::Ptr< Vector< Real > > g_
ROL::Ptr< Vector< Real > > c_
ROL::Ptr< Vector< Real > > x_
std::string getStepName(void) const
Grab step name (after check for consistency).
OptimizationSolver(OptimizationProblem< Real > &opt, ROL::ParameterList &parlist)
Constructor.
int solve(std::ostream &outStream, const ROL::Ptr< StatusTest< Real > > &status=ROL::nullPtr, const bool combineStatus=true)
Solve optimization problem.
ROL::Ptr< const AlgorithmState< Real > > getAlgorithmState(void) const
Return the AlgorithmState.
ROL::Ptr< Vector< Real > > l_
std::vector< std::string > output_
void reset(const bool resetAlgo=true)
Reset both Algorithm and Step.
void resetAlgorithmState(void)
Reset the AlgorithmState.
int solve(const ROL::Ptr< StatusTest< Real > > &status=ROL::nullPtr, const bool combineStatus=true)
Solve optimization problem with no iteration output.
ROL::Ptr< Constraint< Real > > con_
ROL::Ptr< Objective< Real > > obj_
ROL::Ptr< BoundConstraint< Real > > bnd_
ROL::Ptr< CombinedStatusTest< Real > > status_
std::vector< std::string > getOutput(void) const
Returns iteration history as a vector of strings.
ROL::Ptr< StatusTest< Real > > getStatusTest(const std::string step, ROL::ParameterList &parlist)
Provides an interface to check status of optimization algorithms.
ROL::Ptr< Step< Real > > getStep(const std::string &type, ROL::ParameterList &parlist) const
std::string EStepToString(EStep tr)
Ptr< OptimizationSolver< Real > > make_OptimizationSolver(OptimizationProblem< Real > &opt, ParameterList &parlist)
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...
@ TYPE_U
@ TYPE_E
@ TYPE_EB
@ TYPE_B
@ TYPE_LAST
bool isCompatibleStep(EProblem p, EStep s)
@ STEP_BUNDLE
@ STEP_AUGMENTEDLAGRANGIAN
@ STEP_COMPOSITESTEP
@ STEP_INTERIORPOINT
@ STEP_FLETCHER
@ STEP_MOREAUYOSIDAPENALTY
@ STEP_TRUSTREGION
EStep StringToEStep(std::string s)