ROL
ROL_TypeB_Algorithm_Def.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_TYPEB_ALGORITHM_DEF_H
11#define ROL_TYPEB_ALGORITHM_DEF_H
12
14//#include "ROL_ConstraintManager.hpp"
15
16namespace ROL {
17namespace TypeB {
18
19template<typename Real>
21 : status_(makePtr<CombinedStatusTest<Real>>()),
22 state_(makePtr<AlgorithmState<Real>>()),
23 proj_(nullPtr) {
24 status_->reset();
25 status_->add(makePtr<StatusTest<Real>>());
26}
27
28template<typename Real>
30 if (state_->iterateVec == nullPtr) {
31 state_->iterateVec = x.clone();
32 }
33 state_->iterateVec->set(x);
34 if (state_->stepVec == nullPtr) {
35 state_->stepVec = x.clone();
36 }
37 state_->stepVec->zero();
38 if (state_->gradientVec == nullPtr) {
39 state_->gradientVec = g.clone();
40 }
41 state_->gradientVec->set(g);
42 if (state_->minIterVec == nullPtr) {
43 state_->minIterVec = x.clone();
44 }
45 state_->minIterVec->set(x);
46 state_->minIter = state_->iter;
47 state_->minValue = state_->value;
48}
49
50template<typename Real>
52 const Vector<Real> &g,
53 Vector<Real> &primal,
54 std::ostream &outStream) const {
55 const Real one(1);
56 primal.set(x);
57 primal.axpy(-one,g.dual());
58 proj_->project(primal,outStream); state_->nproj++;
59 primal.axpy(-one,x);
60 return primal.norm();
61}
62
63template<typename Real>
65 bool combineStatus) {
66 if (!combineStatus) { // Do not combine status tests
67 status_->reset();
68 }
69 status_->add(status); // Add user-defined StatusTest
70}
71
72template<typename Real>
74 std::ostream &outStream ) {
75 if (problem.getProblemType() == TYPE_B) {
79 *problem.getObjective(),
80 *problem.getBoundConstraint(),
81 outStream);
82 problem.finalizeIteration();
83 }
84 else {
85 throw Exception::NotImplemented(">>> ROL::Algorithm::TypeB::run : Optimization problem is not Type B!");
86 }
87}
88
89template<typename Real>
91 Objective<Real> &obj,
93 std::ostream &outStream ) {
94 run(x,x.dual(),obj,bnd,outStream);
95}
96
97template<typename Real>
99 Objective<Real> &obj,
101 Constraint<Real> &linear_econ,
102 Vector<Real> &linear_emul,
103 std::ostream &outStream ) {
104 run(x,x.dual(),obj,bnd,linear_econ,linear_emul,linear_emul.dual(),outStream);
105}
106
107template<typename Real>
109 const Vector<Real> &g,
110 Objective<Real> &obj,
112 Constraint<Real> &linear_econ,
113 Vector<Real> &linear_emul,
114 const Vector<Real> &linear_eres,
115 std::ostream &outStream ) {
116 ParameterList parlist;
117 proj_ = PolyhedralProjectionFactory<Real>(x,g,makePtrFromRef(bnd),makePtrFromRef(linear_econ),linear_emul,linear_eres,parlist);
118 run(x,g,obj,bnd,outStream);
119}
120
121template<typename Real>
123 Objective<Real> &obj,
125 Constraint<Real> &linear_icon,
126 Vector<Real> &linear_imul,
127 BoundConstraint<Real> &linear_ibnd,
128 std::ostream &outStream ) {
129 run(x,x.dual(),obj,bnd,linear_icon,linear_imul,linear_ibnd,linear_imul.dual(),outStream);
130}
131
132template<typename Real>
134 const Vector<Real> &g,
135 Objective<Real> &obj,
137 Constraint<Real> &linear_icon,
138 Vector<Real> &linear_imul,
139 BoundConstraint<Real> &linear_ibnd,
140 const Vector<Real> &linear_ires,
141 std::ostream &outStream ) {
142 Ptr<Vector<Real>> gp = g.clone(), irp = linear_ires.clone();
143 Problem<Real> problem(makePtrFromRef(obj),
144 makePtrFromRef(x),gp);
145 problem.addBoundConstraint(makePtrFromRef(bnd));
146 problem.addLinearConstraint("LinearInequalityConstraint",
147 makePtrFromRef(linear_icon),
148 makePtrFromRef(linear_imul),
149 makePtrFromRef(linear_ibnd),irp);
150 problem.finalize(false,false,outStream);
151 run(problem,outStream);
152// ConstraintManager<Real> cm(makePtrFromRef(linear_icon),makePtrFromRef(linear_imul),
153// makePtrFromRef(linear_ibnd),makePtrFromRef(x),
154// makePtrFromRef(bnd));
155// Ptr<Constraint<Real>> linear_econ = cm.getConstraint();
156// Ptr<Vector<Real>> linear_emul = cm.getMultiplier();
157// Ptr<Vector<Real>> xvec = cm.getOptVector();
158// Ptr<BoundConstraint<Real>> xbnd = cm.getBoundConstraint();
159// Ptr<Objective<Real>> sobj = makePtr<SlacklessObjective<Real>>(makePtrFromRef(obj));
160// //run(*xvec,xvec->dual(),*sobj,*xbnd,*linear_econ,*linear_emul,linear_emul->dual(),outStream);
161// Ptr<Vector<Real>> xdual = xvec->dual().clone();
162// run(*xvec,*xdual,*sobj,*xbnd,*linear_econ,*linear_emul,linear_emul->dual(),outStream);
163}
164
165template<typename Real>
167 Objective<Real> &obj,
169 Constraint<Real> &linear_econ,
170 Vector<Real> &linear_emul,
171 Constraint<Real> &linear_icon,
172 Vector<Real> &linear_imul,
173 BoundConstraint<Real> &linear_ibnd,
174 std::ostream &outStream ) {
175 run(x,x.dual(),obj,bnd,linear_econ,linear_emul,linear_emul.dual(),
176 linear_icon,linear_imul,linear_ibnd,linear_imul.dual(),outStream);
177}
178
179template<typename Real>
181 const Vector<Real> &g,
182 Objective<Real> &obj,
184 Constraint<Real> &linear_econ,
185 Vector<Real> &linear_emul,
186 const Vector<Real> &linear_eres,
187 Constraint<Real> &linear_icon,
188 Vector<Real> &linear_imul,
189 BoundConstraint<Real> &linear_ibnd,
190 const Vector<Real> &linear_ires,
191 std::ostream &outStream ) {
192 Ptr<Vector<Real>> gp = g.clone(), erp = linear_eres.clone(), irp = linear_ires.clone();
193 Problem<Real> problem(makePtrFromRef(obj),
194 makePtrFromRef(x),gp);
195 problem.addBoundConstraint(makePtrFromRef(bnd));
196 problem.addLinearConstraint("LinearEqualityConstraint",
197 makePtrFromRef(linear_econ),
198 makePtrFromRef(linear_emul),erp);
199 problem.addLinearConstraint("LinearInequalityConstraint",
200 makePtrFromRef(linear_icon),
201 makePtrFromRef(linear_imul),
202 makePtrFromRef(linear_ibnd),irp);
203 problem.finalize(false,false,outStream);
204 run(problem,outStream);
205 //std::vector<Ptr<Constraint<Real>>> cvec;
206 //cvec.push_back(makePtrFromRef(linear_econ));
207 //cvec.push_back(makePtrFromRef(linear_icon));
208 //std::vector<Ptr<Vector<Real>>> lvec;
209 //lvec.push_back(makePtrFromRef(linear_emul));
210 //lvec.push_back(makePtrFromRef(linear_imul));
211 //std::vector<Ptr<BoundConstraint<Real>>> bvec;
212 //bvec.push_back(nullPtr);
213 //bvec.push_back(makePtrFromRef(linear_ibnd));
214 //ConstraintManager<Real> cm(cvec,lvec,bvec,makePtrFromRef(x),makePtrFromRef(bnd));
215 //Ptr<Constraint<Real>> linear_con = cm.getConstraint();
216 //Ptr<Vector<Real>> linear_mul = cm.getMultiplier();
217 //Ptr<Vector<Real>> xvec = cm.getOptVector();
218 //Ptr<BoundConstraint<Real>> xbnd = cm.getBoundConstraint();
219 //Ptr<Objective<Real>> xobj = makePtr<SlacklessObjective<Real>>(makePtrFromRef(obj));
221 //Ptr<Vector<Real>> xdual = xvec->dual().clone();
222 //run(*xvec,*xdual,*xobj,*xbnd,*linear_con,*linear_mul,linear_mul->dual(),outStream);
223}
224
225template<typename Real>
226void Algorithm<Real>::writeHeader( std::ostream& os ) const {
227 std::ios_base::fmtflags osFlags(os.flags());
228 os << " ";
229 os << std::setw(6) << std::left << "iter";
230 os << std::setw(15) << std::left << "value";
231 os << std::setw(15) << std::left << "gnorm";
232 os << std::setw(15) << std::left << "snorm";
233 os << std::setw(10) << std::left << "#fval";
234 os << std::setw(10) << std::left << "#grad";
235 os << std::endl;
236 os.flags(osFlags);
237}
238
239template<typename Real>
240void Algorithm<Real>::writeName( std::ostream &os ) const {
241 throw Exception::NotImplemented(">>> ROL::TypeU::Algorithm::writeName() is not implemented!");
242}
243
244template<typename Real>
245void Algorithm<Real>::writeOutput( std::ostream& os, bool write_header ) const {
246 std::ios_base::fmtflags osFlags(os.flags());
247 os << std::scientific << std::setprecision(6);
248 if ( write_header ) writeHeader(os);
249 if ( state_->iter == 0 ) {
250 os << " ";
251 os << std::setw(6) << std::left << state_->iter;
252 os << std::setw(15) << std::left << state_->value;
253 os << std::setw(15) << std::left << state_->gnorm;
254 os << std::endl;
255 }
256 else {
257 os << " ";
258 os << std::setw(6) << std::left << state_->iter;
259 os << std::setw(15) << std::left << state_->value;
260 os << std::setw(15) << std::left << state_->gnorm;
261 os << std::setw(15) << std::left << state_->snorm;
262 os << std::setw(10) << std::left << state_->nfval;
263 os << std::setw(10) << std::left << state_->ngrad;
264 os << std::endl;
265 }
266 os.flags(osFlags);
267}
268
269template<typename Real>
270void Algorithm<Real>::writeExitStatus( std::ostream& os ) const {
271 std::ios_base::fmtflags osFlags(os.flags());
272 os << "Optimization Terminated with Status: ";
273 os << EExitStatusToString(state_->statusFlag);
274 os << std::endl;
275 os.flags(osFlags);
276}
277
278template<typename Real>
279//Ptr<const AlgorithmState<Real>>& Algorithm<Real>::getState() const {
280Ptr<const AlgorithmState<Real>> Algorithm<Real>::getState() const {
281 return state_;
282}
283
284template<typename Real>
286 state_->reset();
287}
288
289} // namespace TypeB
290} // namespace ROL
291
292#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.
Ptr< PolyhedralProjection< Real > > proj_
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 writeName(std::ostream &os) const
Print step name.
const Ptr< AlgorithmState< Real > > state_
const Ptr< CombinedStatusTest< Real > > status_
virtual void writeOutput(std::ostream &os, const bool write_header=false) const
Print iterate 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
Ptr< PolyhedralProjection< Real > > PolyhedralProjectionFactory(const Vector< Real > &xprim, const Vector< Real > &xdual, const Ptr< BoundConstraint< Real > > &bnd, const Ptr< Constraint< Real > > &con, const Vector< Real > &mul, const Vector< Real > &res, ParameterList &list)
std::string EExitStatusToString(EExitStatus tr)
Definition ROL_Types.hpp:92