ROL
ROL_TypeE_Algorithm_Def.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_TYPE_ALGORITHM_DEF_H
45 #define ROL_TYPE_ALGORITHM_DEF_H
46 
47 #include "ROL_Types.hpp"
49 #include "ROL_ValidParameters.hpp"
51 
52 namespace ROL {
53 namespace TypeE {
54 
55 template<typename Real>
57  : status_(makePtr<CombinedStatusTest<Real>>()),
58  state_(makePtr<AlgorithmState<Real>>()) {
59  status_->reset();
60  status_->add(makePtr<ConstraintStatusTest<Real>>());
61 }
62 
63 template<typename Real>
65  const Vector<Real> &g,
66  const Vector<Real> &mul,
67  const Vector<Real> &c) {
68  if (state_->iterateVec == nullPtr) {
69  state_->iterateVec = x.clone();
70  }
71  state_->iterateVec->set(x);
72  if (state_->lagmultVec == nullPtr) {
73  state_->lagmultVec = mul.clone();
74  }
75  state_->lagmultVec->set(mul);
76  if (state_->stepVec == nullPtr) {
77  state_->stepVec = x.clone();
78  }
79  state_->stepVec->zero();
80  if (state_->gradientVec == nullPtr) {
81  state_->gradientVec = g.clone();
82  }
83  state_->gradientVec->set(g);
84  if (state_->constraintVec == nullPtr) {
85  state_->constraintVec = c.clone();
86  }
87  state_->constraintVec->zero();
88  if (state_->minIterVec == nullPtr) {
89  state_->minIterVec = x.clone();
90  }
91  state_->minIterVec->set(x);
92  state_->minIter = state_->iter;
93  state_->minValue = state_->value;
94 }
95 
96 template<typename Real>
98  const bool combineStatus) {
99  if (!combineStatus) { // Do not combine status tests
100  status_->reset();
101  }
102  status_->add(status); // Add user-defined StatusTest
103 }
104 
105 template<typename Real>
107  std::ostream &outStream ) {
108  if (problem.getProblemType() == TYPE_E) {
109  run(*problem.getPrimalOptimizationVector(),
110  *problem.getDualOptimizationVector(),
111  *problem.getObjective(),
112  *problem.getConstraint(),
113  *problem.getMultiplierVector(),
114  *problem.getResidualVector(),
115  outStream);
116  problem.finalizeIteration();
117  }
118  else {
119  throw Exception::NotImplemented(">>> ROL::Algorithm::run : Optimization problem is not Type E!");
120  }
121 }
122 
123 template<typename Real>
125  Objective<Real> &obj,
126  Constraint<Real> &econ,
127  Vector<Real> &emul,
128  std::ostream &outStream ) {
129  Problem<Real> problem(makePtrFromRef(obj), makePtrFromRef(x));
130  problem.addConstraint("NEC",makePtrFromRef(econ),makePtrFromRef(emul));
131  problem.finalize(false,false,outStream);
132  run(problem,outStream);
133  //run(x,x.dual(),obj,econ,emul,emul.dual(),outStream);
134 }
135 
136 template<typename Real>
138  Objective<Real> &obj,
139  Constraint<Real> &econ,
140  Vector<Real> &emul,
141  Constraint<Real> &linear_econ,
142  Vector<Real> &linear_emul,
143  std::ostream &outStream ) {
144  Problem<Real> problem(makePtrFromRef(obj), makePtrFromRef(x));
145  problem.addConstraint("NEC",makePtrFromRef(econ),makePtrFromRef(emul));
146  problem.addLinearConstraint("LEC",makePtrFromRef(linear_econ),makePtrFromRef(linear_emul));
147  problem.finalize(false,false,outStream);
148  run(problem,outStream);
149  //run(x,x.dual(),obj,econ,emul,emul.dual(),linear_econ,linear_emul,linear_emul.dual(),outStream);
150 }
151 
152 template<typename Real>
154  const Vector<Real> &g,
155  Objective<Real> &obj,
156  Constraint<Real> &econ,
157  Vector<Real> &emul,
158  const Vector<Real> &eres,
159  Constraint<Real> &linear_econ,
160  Vector<Real> &linear_emul,
161  const Vector<Real> &linear_eres,
162  std::ostream &outStream ) {
163  Ptr<Vector<Real>> gp = g.clone(), erp = eres.clone(), lerp = linear_eres.clone();
164  Problem<Real> problem(makePtrFromRef(obj), makePtrFromRef(x), gp);
165  problem.addConstraint("NEC",makePtrFromRef(econ),makePtrFromRef(emul),erp,false);
166  problem.addLinearConstraint("LEC",makePtrFromRef(linear_econ),makePtrFromRef(linear_emul),lerp,false);
167  problem.finalize(false,false,outStream);
168  run(problem,outStream);
169  //Ptr<Vector<Real>> xfeas = x.clone(); xfeas->set(x);
170  //ReduceLinearConstraint<Real> rlc(makePtrFromRef(linear_econ),xfeas,makePtrFromRef(linear_eres));
171  //Ptr<Vector<Real>> s = x.clone(); s->zero();
172  //void output = run(*s,g,*rlc.transform(makePtrFromRef(obj)),
173  // *rlc.transform(makePtrFromRef(econ)),emul,eres,outStream);
174  //rlc.project(x,*s);
175  //x.plus(*rlc.getFeasibleVector());
176  //return output;
177 }
178 
179 template<typename Real>
180 void Algorithm<Real>::writeHeader( std::ostream& os ) const {
181  std::ios_base::fmtflags osFlags(os.flags());
182  os << " ";
183  os << std::setw(6) << std::left << "iter";
184  os << std::setw(15) << std::left << "value";
185  os << std::setw(15) << std::left << "cnorm";
186  os << std::setw(15) << std::left << "gLnorm";
187  os << std::setw(15) << std::left << "snorm";
188  os << std::setw(10) << std::left << "#fval";
189  os << std::setw(10) << std::left << "#grad";
190  os << std::endl;
191  os.flags(osFlags);
192 }
193 
194 template<typename Real>
195 void Algorithm<Real>::writeName( std::ostream& os ) const {
196  throw Exception::NotImplemented(">>> ROL::TypeE::Algorithm::writeName() is not implemented!");
197 }
198 
199 template<typename Real>
200 void Algorithm<Real>::writeOutput( std::ostream& os, bool write_header ) const {
201  std::ios_base::fmtflags osFlags(os.flags());
202  os << std::scientific << std::setprecision(6);
203  if ( write_header ) writeHeader(os);
204  if ( state_->iter == 0 ) {
205  os << " ";
206  os << std::setw(6) << std::left << state_->iter;
207  os << std::setw(15) << std::left << state_->value;
208  os << std::setw(15) << std::left << state_->cnorm;
209  os << std::setw(15) << std::left << state_->gnorm;
210  os << std::endl;
211  }
212  else {
213  os << " ";
214  os << std::setw(6) << std::left << state_->iter;
215  os << std::setw(15) << std::left << state_->value;
216  os << std::setw(15) << std::left << state_->cnorm;
217  os << std::setw(15) << std::left << state_->gnorm;
218  os << std::setw(15) << std::left << state_->snorm;
219  os << std::setw(10) << std::left << state_->nfval;
220  os << std::setw(10) << std::left << state_->ngrad;
221  os << std::endl;
222  }
223  os.flags(osFlags);
224 }
225 
226 template<typename Real>
227 void Algorithm<Real>::writeExitStatus( std::ostream& os ) const {
228  std::ios_base::fmtflags osFlags(os.flags());
229  os << "Optimization Terminated with Status: ";
230  os << EExitStatusToString(state_->statusFlag);
231  os << std::endl;
232  os.flags(osFlags);
233 }
234 
235 template<typename Real>
236 Ptr<const AlgorithmState<Real>> Algorithm<Real>::getState() const {
237 //Ptr<const AlgorithmState<Real>>& Algorithm<Real>::getState() const {
238  return state_;
239 }
240 
241 template<typename Real>
243  state_->reset();
244 }
245 
246 } // namespace TypeE
247 
248 } // namespace ROL
249 
250 #endif
Provides the interface to evaluate objective functions.
const Ptr< Constraint< Real > > & getConstraint()
Get the equality constraint.
const Ptr< Vector< Real > > & getPrimalOptimizationVector()
Get the primal optimization space vector.
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
void addLinearConstraint(std::string name, const Ptr< Constraint< Real >> &linear_econ, const Ptr< Vector< Real >> &linear_emul, const Ptr< Vector< Real >> &linear_eres=nullPtr, bool reset=false)
Add a linear equality constraint.
Contains definitions of custom data types in ROL.
virtual void writeHeader(std::ostream &os) const
Print iterate header.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
virtual void finalize(bool lumpConstraints=false, bool printToStream=false, std::ostream &outStream=std::cout)
Tranform user-supplied constraints to consist of only bounds and equalities. Optimization problem can...
virtual void run(Problem< Real > &problem, std::ostream &outStream=std::cout)
Run algorithm on equality constrained problems (Type-E). This is the primary Type-E interface...
Provides an interface to check status of optimization algorithms for problems with equality constrain...
std::string EExitStatusToString(EExitStatus tr)
Definition: ROL_Types.hpp:126
const Ptr< Objective< Real > > & getObjective()
Get the objective function.
virtual void writeName(std::ostream &os) const
Print step name.
const Ptr< Vector< Real > > & getResidualVector()
Get the primal constraint space vector.
const Ptr< Vector< Real > > & getMultiplierVector()
Get the dual constraint space vector.
void finalizeIteration()
Transform the optimization variables to the native parameterization after an optimization algorithm h...
virtual void writeOutput(std::ostream &os, const bool write_header=false) const
Print iterate status.
Provides an interface to check status of optimization algorithms.
void setStatusTest(const Ptr< StatusTest< Real >> &status, bool combineStatus=false)
const Ptr< CombinedStatusTest< Real > > status_
void addConstraint(std::string name, const Ptr< Constraint< Real >> &econ, const Ptr< Vector< Real >> &emul, const Ptr< Vector< Real >> &eres=nullPtr, bool reset=false)
Add an equality constraint.
EProblem getProblemType()
Get the optimization problem type (U, B, E, or G).
virtual void writeExitStatus(std::ostream &os) const
void initialize(const Vector< Real > &x, const Vector< Real > &g, const Vector< Real > &mul, const Vector< Real > &c)
Ptr< const AlgorithmState< Real > > getState() const
Provides an interface to check two status tests of optimization algorithms.
const Ptr< Vector< Real > > & getDualOptimizationVector()
Get the dual optimization space vector.
Algorithm()
Constructor, given a step and a status test.
Defines the general constraint operator interface.