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
50namespace ROL {
51namespace TypeB {
52
53template<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
62template<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
84template<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
97template<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
106template<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
123template<typename Real>
125 Objective<Real> &obj,
127 std::ostream &outStream ) {
128 run(x,x.dual(),obj,bnd,outStream);
129}
130
131template<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
141template<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
155template<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
166template<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
199template<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
213template<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
259template<typename Real>
260void Algorithm<Real>::writeHeader( std::ostream& os ) const {
261 std::stringstream hist;
262 hist << " ";
263 hist << std::setw(6) << std::left << "iter";
264 hist << std::setw(15) << std::left << "value";
265 hist << std::setw(15) << std::left << "gnorm";
266 hist << std::setw(15) << std::left << "snorm";
267 hist << std::setw(10) << std::left << "#fval";
268 hist << std::setw(10) << std::left << "#grad";
269 hist << std::endl;
270 os << hist.str();
271}
272
273template<typename Real>
274void Algorithm<Real>::writeName( std::ostream &os ) const {
275 throw Exception::NotImplemented(">>> ROL::TypeU::Algorithm::writeName() is not implemented!");
276}
277
278template<typename Real>
279void Algorithm<Real>::writeOutput( std::ostream& os, bool write_header ) const {
280 std::stringstream hist;
281 hist << std::scientific << std::setprecision(6);
282 if ( write_header ) writeHeader(os);
283 if ( state_->iter == 0 ) {
284 hist << " ";
285 hist << std::setw(6) << std::left << state_->iter;
286 hist << std::setw(15) << std::left << state_->value;
287 hist << std::setw(15) << std::left << state_->gnorm;
288 hist << std::endl;
289 }
290 else {
291 hist << " ";
292 hist << std::setw(6) << std::left << state_->iter;
293 hist << std::setw(15) << std::left << state_->value;
294 hist << std::setw(15) << std::left << state_->gnorm;
295 hist << std::setw(15) << std::left << state_->snorm;
296 hist << std::setw(10) << std::left << state_->nfval;
297 hist << std::setw(10) << std::left << state_->ngrad;
298 hist << std::endl;
299 }
300 os << hist.str();
301}
302
303template<typename Real>
304void Algorithm<Real>::writeExitStatus( std::ostream& os ) const {
305 std::stringstream hist;
306 hist << "Optimization Terminated with Status: ";
307 hist << EExitStatusToString(state_->statusFlag);
308 hist << std::endl;
309 os << hist.str();
310}
311
312template<typename Real>
313//Ptr<const AlgorithmState<Real>>& Algorithm<Real>::getState() const {
314Ptr<const AlgorithmState<Real>> Algorithm<Real>::getState() const {
315 return state_;
316}
317
318template<typename Real>
320 state_->reset();
321}
322
323} // namespace TypeB
324} // namespace ROL
325
326#endif
Provides the interface to apply upper and lower bound constraints.
Provides an interface to check two status tests of optimization algorithms.
Defines the general constraint operator interface.
Provides the interface to evaluate objective functions.
const Ptr< PolyhedralProjection< Real > > & getPolyhedralProjection()
Get the polyhedral projection object. This is a null pointer if no linear constraints and/or bounds a...
const Ptr< Vector< Real > > & getPrimalOptimizationVector()
Get the primal optimization space vector.
const Ptr< Vector< Real > > & getDualOptimizationVector()
Get the dual optimization space vector.
EProblem getProblemType()
Get the optimization problem type (U, B, E, or G).
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.
void addBoundConstraint(const Ptr< BoundConstraint< Real > > &bnd)
Add a bound constraint.
void finalizeIteration()
Transform the optimization variables to the native parameterization after an optimization algorithm h...
const Ptr< Objective< Real > > & getObjective()
Get the objective function.
const Ptr< BoundConstraint< Real > > & getBoundConstraint()
Get the bound constraint.
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...
Provides an interface to check status of optimization algorithms.
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.
void initialize(const Vector< Real > &x, const Vector< Real > &g)
virtual void writeHeader(std::ostream &os) const
Print iterate header.
Real optimalityCriterion(const Vector< Real > &x, const Vector< Real > &g, Vector< Real > &primal, std::ostream &outStream=std::cout) const
Algorithm()
Constructor, given a step and a status test.
virtual void writeExitStatus(std::ostream &os) const
virtual void writeOutput(std::ostream &os, bool write_header=false) const
Print iterate status.
virtual void writeName(std::ostream &os) const
Print step name.
const Ptr< CombinedStatusTest< Real > > status_
Ptr< const AlgorithmState< Real > > getState() const
void setStatusTest(const Ptr< StatusTest< Real > > &status, const bool combineStatus=false)
Defines the linear algebra or vector space interface.
virtual Real norm() const =0
Returns where .
virtual void set(const Vector &x)
Set where .
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis,...
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
@ TYPE_B
std::string EExitStatusToString(EExitStatus tr)