44#ifndef ROL_TYPE_ALGORITHM_DEF_H
45#define ROL_TYPE_ALGORITHM_DEF_H
55template<
typename Real>
57 : status_(makePtr<CombinedStatusTest<Real>>()),
60 status_->add(makePtr<ConstraintStatusTest<Real>>());
63template<
typename Real>
64void Algorithm<Real>::initialize(
const Vector<Real> &x,
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();
71 state_->iterateVec->set(x);
72 if (state_->lagmultVec == nullPtr) {
73 state_->lagmultVec = mul.clone();
75 state_->lagmultVec->set(mul);
76 if (state_->stepVec == nullPtr) {
77 state_->stepVec = x.clone();
79 state_->stepVec->zero();
80 if (state_->gradientVec == nullPtr) {
81 state_->gradientVec = g.clone();
83 state_->gradientVec->set(g);
84 if (state_->constraintVec == nullPtr) {
85 state_->constraintVec = c.clone();
87 state_->constraintVec->zero();
88 if (state_->minIterVec == nullPtr) {
89 state_->minIterVec = x.clone();
91 state_->minIterVec->set(x);
92 state_->minIter = state_->iter;
93 state_->minValue = state_->value;
96template<
typename Real>
97void Algorithm<Real>::setStatusTest(
const Ptr<StatusTest<Real>> &status,
98 const bool combineStatus) {
102 status_->add(status);
105template<
typename Real>
106void Algorithm<Real>::run( Problem<Real> &problem,
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(),
116 problem.finalizeIteration();
119 throw Exception::NotImplemented(
">>> ROL::Algorithm::run : Optimization problem is not Type E!");
123template<
typename Real>
124void Algorithm<Real>::run( Vector<Real> &x,
125 Objective<Real> &obj,
126 Constraint<Real> &econ,
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);
136template<
typename Real>
137void Algorithm<Real>::run( Vector<Real> &x,
138 Objective<Real> &obj,
139 Constraint<Real> &econ,
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);
152template<
typename Real>
153void Algorithm<Real>::run( Vector<Real> &x,
154 const Vector<Real> &g,
155 Objective<Real> &obj,
156 Constraint<Real> &econ,
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);
179template<
typename Real>
180void Algorithm<Real>::writeHeader( std::ostream& os )
const {
181 std::stringstream hist;
183 hist << std::setw(6) << std::left <<
"iter";
184 hist << std::setw(15) << std::left <<
"value";
185 hist << std::setw(15) << std::left <<
"cnorm";
186 hist << std::setw(15) << std::left <<
"gLnorm";
187 hist << std::setw(15) << std::left <<
"snorm";
188 hist << std::setw(10) << std::left <<
"#fval";
189 hist << std::setw(10) << std::left <<
"#grad";
194template<
typename Real>
195void Algorithm<Real>::writeName( std::ostream& os )
const {
196 throw Exception::NotImplemented(
">>> ROL::TypeE::Algorithm::writeName() is not implemented!");
199template<
typename Real>
200void Algorithm<Real>::writeOutput( std::ostream& os,
bool write_header )
const {
201 std::stringstream hist;
202 hist << std::scientific << std::setprecision(6);
203 if ( write_header ) writeHeader(os);
204 if ( state_->iter == 0 ) {
206 hist << std::setw(6) << std::left << state_->iter;
207 hist << std::setw(15) << std::left << state_->value;
208 hist << std::setw(15) << std::left << state_->cnorm;
209 hist << std::setw(15) << std::left << state_->gnorm;
214 hist << std::setw(6) << std::left << state_->iter;
215 hist << std::setw(15) << std::left << state_->value;
216 hist << std::setw(15) << std::left << state_->cnorm;
217 hist << std::setw(15) << std::left << state_->gnorm;
218 hist << std::setw(15) << std::left << state_->snorm;
219 hist << std::setw(10) << std::left << state_->nfval;
220 hist << std::setw(10) << std::left << state_->ngrad;
226template<
typename Real>
227void Algorithm<Real>::writeExitStatus( std::ostream& os )
const {
228 std::stringstream hist;
229 hist <<
"Optimization Terminated with Status: ";
235template<
typename Real>
236Ptr<const AlgorithmState<Real>> Algorithm<Real>::getState()
const {
241template<
typename Real>
242void Algorithm<Real>::reset() {
Contains definitions of custom data types in ROL.
Algorithm()
Constructor, given a step and a status test.
std::string EExitStatusToString(EExitStatus tr)