ROL
ROL_TypeB_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_TYPEB_ALGORITHM_DEF_H
45 #define ROL_TYPEB_ALGORITHM_DEF_H
46 
48 //#include "ROL_ConstraintManager.hpp"
49 
50 namespace ROL {
51 namespace TypeB {
52 
53 template<typename Real>
55  : status_(makePtr<CombinedStatusTest<Real>>()),
56  state_(makePtr<AlgorithmState<Real>>()),
57  proj_(nullPtr) {
58  status_->reset();
59  status_->add(makePtr<StatusTest<Real>>());
60 }
61 
62 template<typename Real>
64  if (state_->iterateVec == nullPtr) {
65  state_->iterateVec = x.clone();
66  }
67  state_->iterateVec->set(x);
68  if (state_->stepVec == nullPtr) {
69  state_->stepVec = x.clone();
70  }
71  state_->stepVec->zero();
72  if (state_->gradientVec == nullPtr) {
73  state_->gradientVec = g.clone();
74  }
75  state_->gradientVec->set(g);
76  if (state_->minIterVec == nullPtr) {
77  state_->minIterVec = x.clone();
78  }
79  state_->minIterVec->set(x);
80  state_->minIter = state_->iter;
81  state_->minValue = state_->value;
82 }
83 
84 template<typename Real>
86  const Vector<Real> &g,
87  Vector<Real> &primal,
88  std::ostream &outStream) const {
89  const Real one(1);
90  primal.set(x);
91  primal.axpy(-one,g.dual());
92  proj_->project(primal,outStream); state_->nproj++;
93  primal.axpy(-one,x);
94  return primal.norm();
95 }
96 
97 template<typename Real>
99  bool combineStatus) {
100  if (!combineStatus) { // Do not combine status tests
101  status_->reset();
102  }
103  status_->add(status); // Add user-defined StatusTest
104 }
105 
106 template<typename Real>
108  std::ostream &outStream ) {
109  if (problem.getProblemType() == TYPE_B) {
110  proj_ = problem.getPolyhedralProjection();
111  run(*problem.getPrimalOptimizationVector(),
112  *problem.getDualOptimizationVector(),
113  *problem.getObjective(),
114  *problem.getBoundConstraint(),
115  outStream);
116  problem.finalizeIteration();
117  }
118  else {
119  throw Exception::NotImplemented(">>> ROL::Algorithm::TypeB::run : Optimization problem is not Type B!");
120  }
121 }
122 
123 template<typename Real>
125  Objective<Real> &obj,
127  std::ostream &outStream ) {
128  run(x,x.dual(),obj,bnd,outStream);
129 }
130 
131 template<typename Real>
133  Objective<Real> &obj,
135  Constraint<Real> &linear_econ,
136  Vector<Real> &linear_emul,
137  std::ostream &outStream ) {
138  run(x,x.dual(),obj,bnd,linear_econ,linear_emul,linear_emul.dual(),outStream);
139 }
140 
141 template<typename Real>
143  const Vector<Real> &g,
144  Objective<Real> &obj,
146  Constraint<Real> &linear_econ,
147  Vector<Real> &linear_emul,
148  const Vector<Real> &linear_eres,
149  std::ostream &outStream ) {
150  ParameterList parlist;
151  proj_ = PolyhedralProjectionFactory<Real>(x,g,makePtrFromRef(bnd),makePtrFromRef(linear_econ),linear_emul,linear_eres,parlist);
152  run(x,g,obj,bnd,outStream);
153 }
154 
155 template<typename Real>
157  Objective<Real> &obj,
159  Constraint<Real> &linear_icon,
160  Vector<Real> &linear_imul,
161  BoundConstraint<Real> &linear_ibnd,
162  std::ostream &outStream ) {
163  run(x,x.dual(),obj,bnd,linear_icon,linear_imul,linear_ibnd,linear_imul.dual(),outStream);
164 }
165 
166 template<typename Real>
168  const Vector<Real> &g,
169  Objective<Real> &obj,
171  Constraint<Real> &linear_icon,
172  Vector<Real> &linear_imul,
173  BoundConstraint<Real> &linear_ibnd,
174  const Vector<Real> &linear_ires,
175  std::ostream &outStream ) {
176  Ptr<Vector<Real>> gp = g.clone(), irp = linear_ires.clone();
177  Problem<Real> problem(makePtrFromRef(obj),
178  makePtrFromRef(x),gp);
179  problem.addBoundConstraint(makePtrFromRef(bnd));
180  problem.addLinearConstraint("LinearInequalityConstraint",
181  makePtrFromRef(linear_icon),
182  makePtrFromRef(linear_imul),
183  makePtrFromRef(linear_ibnd),irp);
184  problem.finalize(false,false,outStream);
185  run(problem,outStream);
186 // ConstraintManager<Real> cm(makePtrFromRef(linear_icon),makePtrFromRef(linear_imul),
187 // makePtrFromRef(linear_ibnd),makePtrFromRef(x),
188 // makePtrFromRef(bnd));
189 // Ptr<Constraint<Real>> linear_econ = cm.getConstraint();
190 // Ptr<Vector<Real>> linear_emul = cm.getMultiplier();
191 // Ptr<Vector<Real>> xvec = cm.getOptVector();
192 // Ptr<BoundConstraint<Real>> xbnd = cm.getBoundConstraint();
193 // Ptr<Objective<Real>> sobj = makePtr<SlacklessObjective<Real>>(makePtrFromRef(obj));
194 // //run(*xvec,xvec->dual(),*sobj,*xbnd,*linear_econ,*linear_emul,linear_emul->dual(),outStream);
195 // Ptr<Vector<Real>> xdual = xvec->dual().clone();
196 // run(*xvec,*xdual,*sobj,*xbnd,*linear_econ,*linear_emul,linear_emul->dual(),outStream);
197 }
198 
199 template<typename Real>
201  Objective<Real> &obj,
203  Constraint<Real> &linear_econ,
204  Vector<Real> &linear_emul,
205  Constraint<Real> &linear_icon,
206  Vector<Real> &linear_imul,
207  BoundConstraint<Real> &linear_ibnd,
208  std::ostream &outStream ) {
209  run(x,x.dual(),obj,bnd,linear_econ,linear_emul,linear_emul.dual(),
210  linear_icon,linear_imul,linear_ibnd,linear_imul.dual(),outStream);
211 }
212 
213 template<typename Real>
215  const Vector<Real> &g,
216  Objective<Real> &obj,
218  Constraint<Real> &linear_econ,
219  Vector<Real> &linear_emul,
220  const Vector<Real> &linear_eres,
221  Constraint<Real> &linear_icon,
222  Vector<Real> &linear_imul,
223  BoundConstraint<Real> &linear_ibnd,
224  const Vector<Real> &linear_ires,
225  std::ostream &outStream ) {
226  Ptr<Vector<Real>> gp = g.clone(), erp = linear_eres.clone(), irp = linear_ires.clone();
227  Problem<Real> problem(makePtrFromRef(obj),
228  makePtrFromRef(x),gp);
229  problem.addBoundConstraint(makePtrFromRef(bnd));
230  problem.addLinearConstraint("LinearEqualityConstraint",
231  makePtrFromRef(linear_econ),
232  makePtrFromRef(linear_emul),erp);
233  problem.addLinearConstraint("LinearInequalityConstraint",
234  makePtrFromRef(linear_icon),
235  makePtrFromRef(linear_imul),
236  makePtrFromRef(linear_ibnd),irp);
237  problem.finalize(false,false,outStream);
238  run(problem,outStream);
239  //std::vector<Ptr<Constraint<Real>>> cvec;
240  //cvec.push_back(makePtrFromRef(linear_econ));
241  //cvec.push_back(makePtrFromRef(linear_icon));
242  //std::vector<Ptr<Vector<Real>>> lvec;
243  //lvec.push_back(makePtrFromRef(linear_emul));
244  //lvec.push_back(makePtrFromRef(linear_imul));
245  //std::vector<Ptr<BoundConstraint<Real>>> bvec;
246  //bvec.push_back(nullPtr);
247  //bvec.push_back(makePtrFromRef(linear_ibnd));
248  //ConstraintManager<Real> cm(cvec,lvec,bvec,makePtrFromRef(x),makePtrFromRef(bnd));
249  //Ptr<Constraint<Real>> linear_con = cm.getConstraint();
250  //Ptr<Vector<Real>> linear_mul = cm.getMultiplier();
251  //Ptr<Vector<Real>> xvec = cm.getOptVector();
252  //Ptr<BoundConstraint<Real>> xbnd = cm.getBoundConstraint();
253  //Ptr<Objective<Real>> xobj = makePtr<SlacklessObjective<Real>>(makePtrFromRef(obj));
255  //Ptr<Vector<Real>> xdual = xvec->dual().clone();
256  //run(*xvec,*xdual,*xobj,*xbnd,*linear_con,*linear_mul,linear_mul->dual(),outStream);
257 }
258 
259 template<typename Real>
260 void Algorithm<Real>::writeHeader( std::ostream& os ) const {
261  std::ios_base::fmtflags osFlags(os.flags());
262  os << " ";
263  os << std::setw(6) << std::left << "iter";
264  os << std::setw(15) << std::left << "value";
265  os << std::setw(15) << std::left << "gnorm";
266  os << std::setw(15) << std::left << "snorm";
267  os << std::setw(10) << std::left << "#fval";
268  os << std::setw(10) << std::left << "#grad";
269  os << std::endl;
270  os.flags(osFlags);
271 }
272 
273 template<typename Real>
274 void Algorithm<Real>::writeName( std::ostream &os ) const {
275  throw Exception::NotImplemented(">>> ROL::TypeU::Algorithm::writeName() is not implemented!");
276 }
277 
278 template<typename Real>
279 void Algorithm<Real>::writeOutput( std::ostream& os, bool write_header ) const {
280  std::ios_base::fmtflags osFlags(os.flags());
281  os << std::scientific << std::setprecision(6);
282  if ( write_header ) writeHeader(os);
283  if ( state_->iter == 0 ) {
284  os << " ";
285  os << std::setw(6) << std::left << state_->iter;
286  os << std::setw(15) << std::left << state_->value;
287  os << std::setw(15) << std::left << state_->gnorm;
288  os << std::endl;
289  }
290  else {
291  os << " ";
292  os << std::setw(6) << std::left << state_->iter;
293  os << std::setw(15) << std::left << state_->value;
294  os << std::setw(15) << std::left << state_->gnorm;
295  os << std::setw(15) << std::left << state_->snorm;
296  os << std::setw(10) << std::left << state_->nfval;
297  os << std::setw(10) << std::left << state_->ngrad;
298  os << std::endl;
299  }
300  os.flags(osFlags);
301 }
302 
303 template<typename Real>
304 void Algorithm<Real>::writeExitStatus( std::ostream& os ) const {
305  std::ios_base::fmtflags osFlags(os.flags());
306  os << "Optimization Terminated with Status: ";
307  os << EExitStatusToString(state_->statusFlag);
308  os << std::endl;
309  os.flags(osFlags);
310 }
311 
312 template<typename Real>
313 //Ptr<const AlgorithmState<Real>>& Algorithm<Real>::getState() const {
314 Ptr<const AlgorithmState<Real>> Algorithm<Real>::getState() const {
315  return state_;
316 }
317 
318 template<typename Real>
320  state_->reset();
321 }
322 
323 } // namespace TypeB
324 } // namespace ROL
325 
326 #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:226
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 addBoundConstraint(const Ptr< BoundConstraint< Real >> &bnd)
Add a bound constraint.
virtual void writeHeader(std::ostream &os) const
Print iterate header.
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:153
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.
const Ptr< BoundConstraint< Real > > & getBoundConstraint()
Get the bound constraint.
virtual void writeExitStatus(std::ostream &os) const
void setStatusTest(const Ptr< StatusTest< Real >> &status, const bool combineStatus=false)
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 writeName(std::ostream &os) const
Print step name.
std::string EExitStatusToString(EExitStatus tr)
Definition: ROL_Types.hpp:126
const Ptr< Objective< Real > > & getObjective()
Get the objective function.
void finalizeIteration()
Transform the optimization variables to the native parameterization after an optimization algorithm h...
Provides an interface to check status of optimization algorithms.
Algorithm()
Constructor, given a step and a status test.
virtual void writeOutput(std::ostream &os, const bool write_header=false) const
Print iterate status.
Provides the interface to apply upper and lower bound constraints.
Real optimalityCriterion(const Vector< Real > &x, const Vector< Real > &g, Vector< Real > &primal, std::ostream &outStream=std::cout) const
const Ptr< PolyhedralProjection< Real > > & getPolyhedralProjection()
Get the polyhedral projection object. This is a null pointer if no linear constraints and/or bounds a...
EProblem getProblemType()
Get the optimization problem type (U, B, E, or G).
void initialize(const Vector< Real > &x, const Vector< Real > &g)
virtual void run(Problem< Real > &problem, std::ostream &outStream=std::cout)
Run algorithm on bound constrained problems (Type-B). This is the primary Type-B interface.
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:209
virtual Real norm() const =0
Returns where .
Provides an interface to check two status tests of optimization algorithms.
const Ptr< Vector< Real > > & getDualOptimizationVector()
Get the dual optimization space vector.
Defines the general constraint operator interface.
Ptr< const AlgorithmState< Real > > getState() const
const Ptr< CombinedStatusTest< Real > > status_