ROL
ROL_Algorithm.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_ALGORITHM_H
11#define ROL_ALGORITHM_H
12
13#include "ROL_Types.hpp"
14#include "ROL_Step.hpp"
15#include "ROL_StatusTest.hpp"
16#include "ROL_Objective.hpp"
18#include "ROL_Constraint.hpp"
20
26namespace ROL {
27
28template <class Real>
29class Algorithm {
30private:
31 ROL::Ptr<Step<Real> > step_;
32 ROL::Ptr<StatusTest<Real> > status_;
33 ROL::Ptr<AlgorithmState<Real> > state_;
34
36
37public:
38
39 virtual ~Algorithm() {}
40
43 Algorithm( const ROL::Ptr<Step<Real> > & step,
44 const ROL::Ptr<StatusTest<Real> > & status,
45 bool printHeader = false ) {
46 step_ = step;
47 status_ = status;
48 state_ = ROL::makePtr<AlgorithmState<Real>>();
49 printHeader_ = printHeader;
50 }
51
55 Algorithm( const ROL::Ptr<Step<Real> > & step,
56 const ROL::Ptr<StatusTest<Real> > & status,
57 const ROL::Ptr<AlgorithmState<Real> > & state,
58 bool printHeader = false ) {
59 step_ = step;
60 status_ = status;
61 state_ = state;
62 printHeader_ = printHeader;
63 }
64
68 virtual std::vector<std::string> run( Vector<Real> &x,
69 Objective<Real> &obj,
70 bool print = false,
71 std::ostream &outStream = std::cout,
72 bool printVectors = false,
73 std::ostream &vectorStream = std::cout ) {
75 bnd.deactivate();
76 return run(x,x.dual(),obj,bnd,print,outStream,printVectors,vectorStream);
77 }
78
83 virtual std::vector<std::string> run( Vector<Real> &x,
84 const Vector<Real> &g,
85 Objective<Real> &obj,
86 bool print = false,
87 std::ostream &outStream = std::cout,
88 bool printVectors = false,
89 std::ostream &vectorStream = std::cout ) {
91 bnd.deactivate();
92 return run(x,g,obj,bnd,print,outStream,printVectors,vectorStream);
93 }
94
98 virtual std::vector<std::string> run( Vector<Real> &x,
99 Objective<Real> &obj,
101 bool print = false,
102 std::ostream &outStream = std::cout,
103 bool printVectors = false,
104 std::ostream &vectorStream = std::cout ) {
105 return run(x,x.dual(),obj,bnd,print,outStream,printVectors,vectorStream);
106 }
107
112 virtual std::vector<std::string> run( Vector<Real> &x,
113 const Vector<Real> &g,
114 Objective<Real> &obj,
116 bool print = false,
117 std::ostream &outStream = std::cout,
118 bool printVectors = false,
119 std::ostream &vectorStream = std::cout ) {
120 if(printVectors) {
121 x.print(vectorStream);
122 }
123
124 std::vector<std::string> output;
125
126 // Initialize Current Iterate Container
127 if ( state_->iterateVec == ROL::nullPtr ) {
128 state_->iterateVec = x.clone();
129 }
130 state_->iterateVec->set(x);
131
132 // Initialize Step Container
133 ROL::Ptr<Vector<Real> > s = x.clone();
134
135 // Initialize Step
136 step_->initialize(x, g, obj, bnd, *state_);
137 output.push_back(step_->print(*state_,true));
138 if ( print ) {
139 outStream << step_->print(*state_,true);
140 }
141
142 // Initialize Minimum Value and Vector
143 if ( state_->minIterVec == ROL::nullPtr ) {
144 state_->minIterVec = x.clone();
145 }
146 state_->minIterVec->set(x);
147 state_->minIter = state_->iter;
148 state_->minValue = state_->value;
149
150 // Run Algorithm
151 while (status_->check(*state_)) {
152 step_->compute(*s, x, obj, bnd, *state_);
153 step_->update(x, *s, obj, bnd, *state_);
154
155 if( printVectors ) {
156 x.print(vectorStream);
157 }
158
159 // Store Minimal Value and Vector
160 if ( state_->minValue > state_->value ) {
161 state_->minIterVec->set(*(state_->iterateVec));
162 state_->minValue = state_->value;
163 state_->minIter = state_->iter;
164 }
165 // Update Output
166 output.push_back(step_->print(*state_,printHeader_));
167 if ( print ) {
168 outStream << step_->print(*state_,printHeader_);
169 }
170 }
171 std::stringstream hist;
172 hist << "Optimization Terminated with Status: ";
173 hist << EExitStatusToString(state_->statusFlag);
174 hist << "\n";
175 output.push_back(hist.str());
176 if ( print ) {
177 outStream << hist.str();
178 }
179 return output;
180 }
181
182
186 virtual std::vector<std::string> run( Vector<Real> &x,
187 Vector<Real> &l,
188 Objective<Real> &obj,
189 Constraint<Real> &con,
190 bool print = false,
191 std::ostream &outStream = std::cout,
192 bool printVectors = false,
193 std::ostream &vectorStream = std::cout ) {
194
195 return run(x, x.dual(), l, l.dual(), obj, con, print, outStream, printVectors, vectorStream);
196
197 }
198
199
204 virtual std::vector<std::string> run( Vector<Real> &x,
205 const Vector<Real> &g,
206 Vector<Real> &l,
207 const Vector<Real> &c,
208 Objective<Real> &obj,
209 Constraint<Real> &con,
210 bool print = false,
211 std::ostream &outStream = std::cout,
212 bool printVectors = false,
213 std::ostream &vectorStream = std::cout ) {
214 if( printVectors ) {
215 x.print(vectorStream);
216 }
217
218 std::vector<std::string> output;
219
220 // Initialize Current Iterate Container
221 if ( state_->iterateVec == ROL::nullPtr ) {
222 state_->iterateVec = x.clone();
223 }
224 state_->iterateVec->set(x);
225
226 // Initialize Current Lagrange Multiplier Container
227 if ( state_->lagmultVec == ROL::nullPtr ) {
228 state_->lagmultVec = l.clone();
229 }
230 state_->lagmultVec->set(l);
231
232 // Initialize Step Container
233 ROL::Ptr<Vector<Real> > s = x.clone();
234
235 // Initialize Step
236 step_->initialize(x, g, l, c, obj, con, *state_);
237 output.push_back(step_->print(*state_,true));
238 if ( print ) {
239 outStream << step_->print(*state_,true);
240 }
241
242 // Initialize Minimum Value and Vector
243 if ( state_->minIterVec == ROL::nullPtr ) {
244 state_->minIterVec = x.clone();
245 }
246 state_->minIterVec->set(x);
247 state_->minIter = state_->iter;
248 state_->minValue = state_->value;
249
250 // Run Algorithm
251 while (status_->check(*state_)) {
252 step_->compute(*s, x, l, obj, con, *state_);
253 step_->update(x, l, *s, obj, con, *state_);
254
255 if( printVectors ) {
256 x.print(vectorStream);
257 }
258
259 output.push_back(step_->print(*state_,printHeader_));
260 if ( print ) {
261 outStream << step_->print(*state_,printHeader_);
262 }
263 }
264 std::stringstream hist;
265 hist << "Optimization Terminated with Status: ";
266 hist << EExitStatusToString(state_->statusFlag);
267 hist << "\n";
268 output.push_back(hist.str());
269 if ( print ) {
270 outStream << hist.str();
271 }
272 return output;
273 }
274
278 virtual std::vector<std::string> run( Vector<Real> &x,
279 Vector<Real> &l,
280 Objective<Real> &obj,
281 Constraint<Real> &con,
283 bool print = false,
284 std::ostream &outStream = std::cout,
285 bool printVectors = false,
286 std::ostream &vectorStream = std::cout) {
287 return run(x,x.dual(),l,l.dual(),obj,con,bnd,print,outStream,printVectors,vectorStream);
288 }
289
294 virtual std::vector<std::string> run( Vector<Real> &x,
295 const Vector<Real> &g,
296 Vector<Real> &l,
297 const Vector<Real> &c,
298 Objective<Real> &obj,
299 Constraint<Real> &con,
301 bool print = false,
302 std::ostream &outStream = std::cout,
303 bool printVectors = false,
304 std::ostream &vectorStream = std::cout ) {
305 if(printVectors) {
306 x.print(vectorStream);
307 }
308
309 std::vector<std::string> output;
310
311 // Initialize Current Iterate Container
312 if ( state_->iterateVec == ROL::nullPtr ) {
313 state_->iterateVec = x.clone();
314 }
315 state_->iterateVec->set(x);
316
317 // Initialize Current Lagrange Multiplier Container
318 if ( state_->lagmultVec == ROL::nullPtr ) {
319 state_->lagmultVec = l.clone();
320 }
321 state_->lagmultVec->set(l);
322
323 // Initialize Step Container
324 ROL::Ptr<Vector<Real> > s = x.clone();
325
326 // Initialize Step
327 step_->initialize(x, g, l, c, obj, con, bnd, *state_);
328 output.push_back(step_->print(*state_,true));
329 if ( print ) {
330 outStream << step_->print(*state_,true);
331 }
332
333 // Initialize Minimum Value and Vector
334 if ( state_->minIterVec == ROL::nullPtr ) {
335 state_->minIterVec = x.clone();
336 }
337 state_->minIterVec->set(x);
338 state_->minIter = state_->iter;
339 state_->minValue = state_->value;
340
341 // Run Algorithm
342 while (status_->check(*state_)) {
343 step_->compute(*s, x, l, obj, con, bnd, *state_);
344 step_->update(x, l, *s, obj, con, bnd, *state_);
345 if( printVectors ) {
346 x.print(vectorStream);
347 }
348 output.push_back(step_->print(*state_,printHeader_));
349 if ( print ) {
350 outStream << step_->print(*state_,printHeader_);
351 }
352 }
353 std::stringstream hist;
354 hist << "Optimization Terminated with Status: ";
355 hist << EExitStatusToString(state_->statusFlag);
356 hist << "\n";
357 output.push_back(hist.str());
358 if ( print ) {
359 outStream << hist.str();
360 }
361 return output;
362 }
363
364 std::string getIterHeader(void) {
365 return step_->printHeader();
366 }
367
368 std::string getIterInfo(bool withHeader = false) {
369 return step_->print(*state_,withHeader);
370 }
371
372 ROL::Ptr<const AlgorithmState<Real> > getState(void) const {
373 return state_;
374 }
375
376 void reset(void) {
377 state_->reset();
378 }
379
380
381
382
383
384
385}; // class Algorithm
386
387
388} // namespace ROL
389
390#endif
Contains definitions of custom data types in ROL.
Provides an interface to run optimization algorithms.
ROL::Ptr< Step< Real > > step_
ROL::Ptr< StatusTest< Real > > status_
std::string getIterInfo(bool withHeader=false)
virtual ~Algorithm()
Algorithm(const ROL::Ptr< Step< Real > > &step, const ROL::Ptr< StatusTest< Real > > &status, const ROL::Ptr< AlgorithmState< Real > > &state, bool printHeader=false)
Constructor, given a step, a status test, and a previously defined algorithm state.
virtual std::vector< std::string > run(Vector< Real > &x, Objective< Real > &obj, bool print=false, std::ostream &outStream=std::cout, bool printVectors=false, std::ostream &vectorStream=std::cout)
Run algorithm on unconstrained problems (Type-U). This is the primary Type-U interface.
ROL::Ptr< AlgorithmState< Real > > state_
virtual std::vector< std::string > run(Vector< Real > &x, Vector< Real > &l, Objective< Real > &obj, Constraint< Real > &con, bool print=false, std::ostream &outStream=std::cout, bool printVectors=false, std::ostream &vectorStream=std::cout)
Run algorithm on equality constrained problems (Type-E). This is the primary Type-E interface.
Algorithm(const ROL::Ptr< Step< Real > > &step, const ROL::Ptr< StatusTest< Real > > &status, bool printHeader=false)
Constructor, given a step and a status test.
ROL::Ptr< const AlgorithmState< Real > > getState(void) const
std::string getIterHeader(void)
virtual std::vector< std::string > run(Vector< Real > &x, Vector< Real > &l, Objective< Real > &obj, Constraint< Real > &con, BoundConstraint< Real > &bnd, bool print=false, std::ostream &outStream=std::cout, bool printVectors=false, std::ostream &vectorStream=std::cout)
Run algorithm on equality and bound constrained problems (Type-EB). This is the primary Type-EB inter...
virtual std::vector< std::string > run(Vector< Real > &x, const Vector< Real > &g, Vector< Real > &l, const Vector< Real > &c, Objective< Real > &obj, Constraint< Real > &con, BoundConstraint< Real > &bnd, bool print=false, std::ostream &outStream=std::cout, bool printVectors=false, std::ostream &vectorStream=std::cout)
Run algorithm on equality and bound constrained problems (Type-EB). This general interface supports t...
virtual std::vector< std::string > run(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &obj, BoundConstraint< Real > &bnd, bool print=false, std::ostream &outStream=std::cout, bool printVectors=false, std::ostream &vectorStream=std::cout)
Run algorithm on bound constrained problems (Type-B). This general interface supports the use of dual...
virtual std::vector< std::string > run(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &obj, bool print=false, std::ostream &outStream=std::cout, bool printVectors=false, std::ostream &vectorStream=std::cout)
Run algorithm on unconstrained problems (Type-U). This general interface supports the use of dual opt...
virtual std::vector< std::string > run(Vector< Real > &x, const Vector< Real > &g, Vector< Real > &l, const Vector< Real > &c, Objective< Real > &obj, Constraint< Real > &con, bool print=false, std::ostream &outStream=std::cout, bool printVectors=false, std::ostream &vectorStream=std::cout)
Run algorithm on equality constrained problems (Type-E). This general interface supports the use of d...
virtual std::vector< std::string > run(Vector< Real > &x, Objective< Real > &obj, BoundConstraint< Real > &bnd, bool print=false, std::ostream &outStream=std::cout, bool printVectors=false, std::ostream &vectorStream=std::cout)
Run algorithm on bound constrained problems (Type-B). This is the primary Type-B interface.
Provides the interface to apply upper and lower bound constraints.
void deactivate(void)
Turn off bounds.
Defines the general constraint operator interface.
Provides the interface to evaluate objective functions.
Provides an interface to check status of optimization algorithms.
Provides the interface to compute optimization steps.
Definition ROL_Step.hpp:34
Defines the linear algebra or vector space interface.
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis,...
virtual void print(std::ostream &outStream) const
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
std::string EExitStatusToString(EExitStatus tr)
Definition ROL_Types.hpp:92
State for algorithm class. Will be used for restarts.