ROL
ROL_TypeE_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_TYPE_ALGORITHM_DEF_H
11#define ROL_TYPE_ALGORITHM_DEF_H
12
13#include "ROL_Types.hpp"
17
18namespace ROL {
19namespace TypeE {
20
21template<typename Real>
23 : status_(makePtr<CombinedStatusTest<Real>>()),
24 state_(makePtr<AlgorithmState<Real>>()) {
25 status_->reset();
26 status_->add(makePtr<ConstraintStatusTest<Real>>());
27}
28
29template<typename Real>
31 const Vector<Real> &g,
32 const Vector<Real> &mul,
33 const Vector<Real> &c) {
34 if (state_->iterateVec == nullPtr) {
35 state_->iterateVec = x.clone();
36 }
37 state_->iterateVec->set(x);
38 if (state_->lagmultVec == nullPtr) {
39 state_->lagmultVec = mul.clone();
40 }
41 state_->lagmultVec->set(mul);
42 if (state_->stepVec == nullPtr) {
43 state_->stepVec = x.clone();
44 }
45 state_->stepVec->zero();
46 if (state_->gradientVec == nullPtr) {
47 state_->gradientVec = g.clone();
48 }
49 state_->gradientVec->set(g);
50 if (state_->constraintVec == nullPtr) {
51 state_->constraintVec = c.clone();
52 }
53 state_->constraintVec->zero();
54 if (state_->minIterVec == nullPtr) {
55 state_->minIterVec = x.clone();
56 }
57 state_->minIterVec->set(x);
58 state_->minIter = state_->iter;
59 state_->minValue = state_->value;
60}
61
62template<typename Real>
64 const bool combineStatus) {
65 if (!combineStatus) { // Do not combine status tests
66 status_->reset();
67 }
68 status_->add(status); // Add user-defined StatusTest
69}
70
71template<typename Real>
73 std::ostream &outStream ) {
74 if (problem.getProblemType() == TYPE_E) {
75 run(*problem.getPrimalOptimizationVector(),
77 *problem.getObjective(),
78 *problem.getConstraint(),
79 *problem.getMultiplierVector(),
80 *problem.getResidualVector(),
81 outStream);
82 problem.finalizeIteration();
83 }
84 else {
85 throw Exception::NotImplemented(">>> ROL::Algorithm::run : Optimization problem is not Type E!");
86 }
87}
88
89template<typename Real>
91 Objective<Real> &obj,
92 Constraint<Real> &econ,
93 Vector<Real> &emul,
94 std::ostream &outStream ) {
95 Problem<Real> problem(makePtrFromRef(obj), makePtrFromRef(x));
96 problem.addConstraint("NEC",makePtrFromRef(econ),makePtrFromRef(emul));
97 problem.finalize(false,false,outStream);
98 run(problem,outStream);
99 //run(x,x.dual(),obj,econ,emul,emul.dual(),outStream);
100}
101
102template<typename Real>
104 Objective<Real> &obj,
105 Constraint<Real> &econ,
106 Vector<Real> &emul,
107 Constraint<Real> &linear_econ,
108 Vector<Real> &linear_emul,
109 std::ostream &outStream ) {
110 Problem<Real> problem(makePtrFromRef(obj), makePtrFromRef(x));
111 problem.addConstraint("NEC",makePtrFromRef(econ),makePtrFromRef(emul));
112 problem.addLinearConstraint("LEC",makePtrFromRef(linear_econ),makePtrFromRef(linear_emul));
113 problem.finalize(false,false,outStream);
114 run(problem,outStream);
115 //run(x,x.dual(),obj,econ,emul,emul.dual(),linear_econ,linear_emul,linear_emul.dual(),outStream);
116}
117
118template<typename Real>
120 const Vector<Real> &g,
121 Objective<Real> &obj,
122 Constraint<Real> &econ,
123 Vector<Real> &emul,
124 const Vector<Real> &eres,
125 Constraint<Real> &linear_econ,
126 Vector<Real> &linear_emul,
127 const Vector<Real> &linear_eres,
128 std::ostream &outStream ) {
129 Ptr<Vector<Real>> gp = g.clone(), erp = eres.clone(), lerp = linear_eres.clone();
130 Problem<Real> problem(makePtrFromRef(obj), makePtrFromRef(x), gp);
131 problem.addConstraint("NEC",makePtrFromRef(econ),makePtrFromRef(emul),erp,false);
132 problem.addLinearConstraint("LEC",makePtrFromRef(linear_econ),makePtrFromRef(linear_emul),lerp,false);
133 problem.finalize(false,false,outStream);
134 run(problem,outStream);
135 //Ptr<Vector<Real>> xfeas = x.clone(); xfeas->set(x);
136 //ReduceLinearConstraint<Real> rlc(makePtrFromRef(linear_econ),xfeas,makePtrFromRef(linear_eres));
137 //Ptr<Vector<Real>> s = x.clone(); s->zero();
138 //void output = run(*s,g,*rlc.transform(makePtrFromRef(obj)),
139 // *rlc.transform(makePtrFromRef(econ)),emul,eres,outStream);
140 //rlc.project(x,*s);
141 //x.plus(*rlc.getFeasibleVector());
142 //return output;
143}
144
145template<typename Real>
146void Algorithm<Real>::writeHeader( std::ostream& os ) const {
147 std::ios_base::fmtflags osFlags(os.flags());
148 os << " ";
149 os << std::setw(6) << std::left << "iter";
150 os << std::setw(15) << std::left << "value";
151 os << std::setw(15) << std::left << "cnorm";
152 os << std::setw(15) << std::left << "gLnorm";
153 os << std::setw(15) << std::left << "snorm";
154 os << std::setw(10) << std::left << "#fval";
155 os << std::setw(10) << std::left << "#grad";
156 os << std::endl;
157 os.flags(osFlags);
158}
159
160template<typename Real>
161void Algorithm<Real>::writeName( std::ostream& os ) const {
162 throw Exception::NotImplemented(">>> ROL::TypeE::Algorithm::writeName() is not implemented!");
163}
164
165template<typename Real>
166void Algorithm<Real>::writeOutput( std::ostream& os, bool write_header ) const {
167 std::ios_base::fmtflags osFlags(os.flags());
168 os << std::scientific << std::setprecision(6);
169 if ( write_header ) writeHeader(os);
170 if ( state_->iter == 0 ) {
171 os << " ";
172 os << std::setw(6) << std::left << state_->iter;
173 os << std::setw(15) << std::left << state_->value;
174 os << std::setw(15) << std::left << state_->cnorm;
175 os << std::setw(15) << std::left << state_->gnorm;
176 os << std::endl;
177 }
178 else {
179 os << " ";
180 os << std::setw(6) << std::left << state_->iter;
181 os << std::setw(15) << std::left << state_->value;
182 os << std::setw(15) << std::left << state_->cnorm;
183 os << std::setw(15) << std::left << state_->gnorm;
184 os << std::setw(15) << std::left << state_->snorm;
185 os << std::setw(10) << std::left << state_->nfval;
186 os << std::setw(10) << std::left << state_->ngrad;
187 os << std::endl;
188 }
189 os.flags(osFlags);
190}
191
192template<typename Real>
193void Algorithm<Real>::writeExitStatus( std::ostream& os ) const {
194 std::ios_base::fmtflags osFlags(os.flags());
195 os << "Optimization Terminated with Status: ";
196 os << EExitStatusToString(state_->statusFlag);
197 os << std::endl;
198 os.flags(osFlags);
199}
200
201template<typename Real>
202Ptr<const AlgorithmState<Real>> Algorithm<Real>::getState() const {
203//Ptr<const AlgorithmState<Real>>& Algorithm<Real>::getState() const {
204 return state_;
205}
206
207template<typename Real>
209 state_->reset();
210}
211
212} // namespace TypeE
213
214} // namespace ROL
215
216#endif
Contains definitions of custom data types in ROL.
Provides an interface to check two status tests of optimization algorithms.
Provides an interface to check status of optimization algorithms for problems with equality constrain...
Defines the general constraint operator interface.
Provides the interface to evaluate objective functions.
const Ptr< Vector< Real > > & getPrimalOptimizationVector()
Get the primal optimization space vector.
const Ptr< Vector< Real > > & getDualOptimizationVector()
Get the dual optimization space vector.
const Ptr< Vector< Real > > & getMultiplierVector()
Get the dual constraint space vector.
const Ptr< Constraint< Real > > & getConstraint()
Get the equality constraint.
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 finalizeIteration()
Transform the optimization variables to the native parameterization after an optimization algorithm h...
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.
const Ptr< Objective< Real > > & getObjective()
Get the objective function.
const Ptr< Vector< Real > > & getResidualVector()
Get the primal constraint space vector.
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 writeName(std::ostream &os) const
Print step name.
void initialize(const Vector< Real > &x, const Vector< Real > &g, const Vector< Real > &mul, const Vector< Real > &c)
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.
virtual void writeExitStatus(std::ostream &os) const
virtual void writeHeader(std::ostream &os) const
Print iterate header.
Ptr< const AlgorithmState< Real > > getState() const
Algorithm()
Constructor, given a step and a status test.
virtual void writeOutput(std::ostream &os, const bool write_header=false) const
Print iterate status.
void setStatusTest(const Ptr< StatusTest< Real > > &status, bool combineStatus=false)
const Ptr< CombinedStatusTest< Real > > status_
Defines the linear algebra or vector space interface.
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
@ TYPE_E
std::string EExitStatusToString(EExitStatus tr)
Definition ROL_Types.hpp:92