ROL
ROL_Algorithm.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_ALGORITHM_H
45 #define ROL_ALGORITHM_H
46 
47 #include "ROL_Types.hpp"
48 #include "ROL_Step.hpp"
49 #include "ROL_StatusTest.hpp"
50 #include "ROL_Objective.hpp"
51 #include "ROL_BoundConstraint.hpp"
53 
59 namespace ROL {
60 
61 template <class Real>
63 private:
64  Teuchos::RCP<Step<Real> > step_;
65  Teuchos::RCP<StatusTest<Real> > status_;
66  Teuchos::RCP<AlgorithmState<Real> > state_;
67 
69 
70 public:
71 
72  virtual ~DefaultAlgorithm() {}
73 
74  DefaultAlgorithm(Step<Real> & step, StatusTest<Real> & status, bool printHeader = false ) {
75  step_ = Teuchos::rcp(&step, false);
76  status_ = Teuchos::rcp(&status, false);
77  state_ = Teuchos::rcp(new AlgorithmState<Real>);
78  printHeader_ = printHeader;
79  }
80 
82  bool printHeader = false ) {
83  step_ = Teuchos::rcp(&step, false);
84  status_ = Teuchos::rcp(&status, false);
85  state_ = Teuchos::rcp(&state, false);
86  printHeader_ = printHeader;
87  }
88 
92  virtual std::vector<std::string> run( Vector<Real> &x,
93  Objective<Real> &obj,
94  bool print = false,
95  std::ostream &outStream = std::cout ) {
97  con.deactivate();
98  return run(x,x.dual(),obj,con,print,outStream);
99  }
100 
105  virtual std::vector<std::string> run( Vector<Real> &x,
106  const Vector<Real> &g,
107  Objective<Real> &obj,
108  bool print = false,
109  std::ostream &outStream = std::cout ) {
111  con.deactivate();
112  return run(x,g,obj,con,print,outStream);
113  }
114 
118  virtual std::vector<std::string> run( Vector<Real> &x,
119  Objective<Real> &obj,
121  bool print = false,
122  std::ostream &outStream = std::cout ) {
123  return run(x,x.dual(),obj,con,print,outStream);
124  }
125 
130  virtual std::vector<std::string> run( Vector<Real> &x,
131  const Vector<Real> &g,
132  Objective<Real> &obj,
134  bool print = false,
135  std::ostream &outStream = std::cout ) {
136  std::vector<std::string> output;
137 
138  // Initialize Current Iterate Container
139  if ( state_->iterateVec == Teuchos::null ) {
140  state_->iterateVec = x.clone();
141  state_->iterateVec->set(x);
142  }
143 
144  // Initialize Step Container
145  Teuchos::RCP<Vector<Real> > s = x.clone();
146 
147  // Initialize Step
148  step_->initialize(x, g, obj, con, *state_);
149  output.push_back(step_->print(*state_,true));
150  if ( print ) {
151  outStream << step_->print(*state_,true);
152  }
153 
154  // Initialize Minimum Value and Vector
155  if ( state_->minIterVec == Teuchos::null ) {
156  state_->minIterVec = x.clone();
157  state_->minIterVec->set(x);
158  state_->minIter = state_->iter;
159  state_->minValue = state_->value;
160  }
161 
162  // Run Algorithm
163  while (status_->check(*state_)) {
164  step_->compute(*s, x, obj, con, *state_);
165  step_->update(x, *s, obj, con, *state_);
166  // Store Minimal Value and Vector
167  if ( state_->minValue > state_->value ) {
168  state_->minIterVec->set(*(state_->iterateVec));
169  state_->minValue = state_->value;
170  state_->minIter = state_->iter;
171  }
172  // Update Output
173  output.push_back(step_->print(*state_,printHeader_));
174  if ( print ) {
175  outStream << step_->print(*state_,printHeader_);
176  }
177  }
178  return output;
179  }
180 
181 
185  virtual std::vector<std::string> run( Vector<Real> &x,
186  Vector<Real> &l,
187  Objective<Real> &obj,
189  bool print = false,
190  std::ostream &outStream = std::cout ) {
191 
192  return run(x, x.dual(), l, l.dual(), obj, con, print, outStream);
193 
194  }
195 
196 
201  virtual std::vector<std::string> run( Vector<Real> &x,
202  const Vector<Real> &g,
203  Vector<Real> &l,
204  const Vector<Real> &c,
205  Objective<Real> &obj,
207  bool print = false,
208  std::ostream &outStream = std::cout ) {
209  std::vector<std::string> output;
210 
211  // Initialize Current Iterate Container
212  if ( state_->iterateVec == Teuchos::null ) {
213  state_->iterateVec = x.clone();
214  state_->iterateVec->set(x);
215  }
216 
217  // Initialize Current Lagrange Multiplier Container
218  if ( state_->lagmultVec == Teuchos::null ) {
219  state_->lagmultVec = l.clone();
220  state_->lagmultVec->set(l);
221  }
222 
223  // Initialize Step Container
224  Teuchos::RCP<Vector<Real> > s = x.clone();
225 
226  // Initialize Step
227  step_->initialize(x, g, l, c, obj, con, *state_);
228  output.push_back(step_->print(*state_,true));
229  if ( print ) {
230  outStream << step_->print(*state_,true);
231  }
232 
233  // Run Algorithm
234  while (status_->check(*state_)) {
235  step_->compute(*s, x, l, obj, con, *state_);
236  step_->update(x, l, *s, obj, con, *state_);
237  output.push_back(step_->print(*state_,printHeader_));
238  if ( print ) {
239  outStream << step_->print(*state_,printHeader_);
240  }
241  }
242  return output;
243  }
244 
249  virtual std::vector<std::string> run( Vector<Real> &x,
250  const Vector<Real> &g,
251  Vector<Real> &l,
252  const Vector<Real> &c,
253  Objective<Real> &obj,
256  bool print = false,
257  std::ostream &outStream = std::cout ) {
258  std::vector<std::string> output;
259 
260  // Initialize Current Iterate Container
261  if ( state_->iterateVec == Teuchos::null ) {
262  state_->iterateVec = x.clone();
263  state_->iterateVec->set(x);
264  }
265 
266  // Initialize Current Lagrange Multiplier Container
267  if ( state_->lagmultVec == Teuchos::null ) {
268  state_->lagmultVec = l.clone();
269  state_->lagmultVec->set(l);
270  }
271 
272  // Initialize Step Container
273  Teuchos::RCP<Vector<Real> > s = x.clone();
274 
275  // Initialize Step
276  step_->initialize(x, g, l, c, obj, con, bnd, *state_);
277  output.push_back(step_->print(*state_,true));
278  if ( print ) {
279  outStream << step_->print(*state_,true);
280  }
281 
282  // Run Algorithm
283  while (status_->check(*state_)) {
284  step_->compute(*s, x, l, obj, con, bnd, *state_);
285  step_->update(x, l, *s, obj, con, bnd, *state_);
286  output.push_back(step_->print(*state_,printHeader_));
287  if ( print ) {
288  outStream << step_->print(*state_,printHeader_);
289  }
290  }
291  return output;
292  }
293 
294  std::string getIterHeader(void) {
295  return step_->printHeader();
296  }
297 
298  std::string getIterInfo(bool withHeader = false) {
299  return step_->print(*state_,withHeader);
300  }
301 
302  Teuchos::RCP<const AlgorithmState<Real> > getState(void) const {
303  return state_;
304  }
305 
306 
307 }; // class DefaultAlgorithm
308 
309 } // namespace ROL
310 
311 #endif
Provides the interface to evaluate objective functions.
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Definition: ROL_Vector.hpp:211
DefaultAlgorithm(Step< Real > &step, StatusTest< Real > &status, bool printHeader=false)
Provides the interface to compute optimization steps.
Definition: ROL_Step.hpp:63
Contains definitions of custom data types in ROL.
Teuchos::RCP< const AlgorithmState< Real > > getState(void) const
virtual Teuchos::RCP< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual std::vector< std::string > run(Vector< Real > &x, Objective< Real > &obj, BoundConstraint< Real > &con, bool print=false, std::ostream &outStream=std::cout)
Run algorithm on bound constrained problems (Type-B). This is the primary Type-B interface.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:72
DefaultAlgorithm(Step< Real > &step, StatusTest< Real > &status, AlgorithmState< Real > &state, bool printHeader=false)
virtual std::vector< std::string > run(Vector< Real > &x, Vector< Real > &l, Objective< Real > &obj, EqualityConstraint< Real > &con, bool print=false, std::ostream &outStream=std::cout)
Run algorithm on equality constrained problems (Type-E). This is the primary Type-E interface...
State for algorithm class. Will be used for restarts.
Definition: ROL_Types.hpp:76
Defines the equality constraint operator interface.
virtual std::vector< std::string > run(Vector< Real > &x, Objective< Real > &obj, bool print=false, std::ostream &outStream=std::cout)
Run algorithm on unconstrained problems (Type-U). This is the primary Type-U interface.
virtual std::vector< std::string > run(Vector< Real > &x, const Vector< Real > &g, Vector< Real > &l, const Vector< Real > &c, Objective< Real > &obj, EqualityConstraint< Real > &con, bool print=false, std::ostream &outStream=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, const Vector< Real > &g, Vector< Real > &l, const Vector< Real > &c, Objective< Real > &obj, EqualityConstraint< Real > &con, BoundConstraint< Real > &bnd, bool print=false, std::ostream &outStream=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, const Vector< Real > &g, Objective< Real > &obj, BoundConstraint< Real > &con, bool print=false, std::ostream &outStream=std::cout)
Run algorithm on bound constrained problems (Type-B). This general interface supports the use of dual...
Provides an interface to check status of optimization algorithms.
virtual std::vector< std::string > run(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &obj, bool print=false, std::ostream &outStream=std::cout)
Run algorithm on unconstrained problems (Type-U). This general interface supports the use of dual opt...
Provides the interface to apply upper and lower bound constraints.
std::string getIterHeader(void)
std::string getIterInfo(bool withHeader=false)
Teuchos::RCP< Step< Real > > step_
Teuchos::RCP< StatusTest< Real > > status_
void deactivate(void)
Turn off bounds.
Teuchos::RCP< AlgorithmState< Real > > state_