ROL
ROL_TypeU_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_TYPEU_ALGORITHM_DEF_H
11#define ROL_TYPEU_ALGORITHM_DEF_H
12
13#include "ROL_Types.hpp"
16
17namespace ROL {
18namespace TypeU {
19
20template<typename Real>
22 : status_(makePtr<CombinedStatusTest<Real>>()),
23 state_(makePtr<AlgorithmState<Real>>()) {
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 bool combineStatus) {
53 if (!combineStatus) { // Do not combine status tests
54 status_->reset();
55 }
56 status_->add(status); // Add user-defined StatusTest
57}
58
59template<typename Real>
61 std::ostream &outStream ) {
62 if (problem.getProblemType() == TYPE_U) {
63 run(*problem.getPrimalOptimizationVector(),
65 *problem.getObjective(),
66 outStream);
67 problem.finalizeIteration();
68 }
69 else {
70 throw Exception::NotImplemented(">>> ROL::TypeU::Algorithm::run : Optimization problem is not Type U!");
71 }
72}
73
74template<typename Real>
76 Objective<Real> &obj,
77 std::ostream &outStream ) {
78 run(x,x.dual(),obj,outStream);
79}
80
81template<typename Real>
83 Objective<Real> &obj,
84 Constraint<Real> &linear_con,
85 Vector<Real> &linear_mul,
86 std::ostream &outStream ) {
87 run(x,x.dual(),obj,linear_con,linear_mul,linear_mul.dual(),outStream);
88}
89
90template<typename Real>
92 const Vector<Real> &g,
93 Objective<Real> &obj,
94 Constraint<Real> &linear_con,
95 Vector<Real> &linear_mul,
96 const Vector<Real> &linear_c,
97 std::ostream &outStream ) {
98 Ptr<Vector<Real>> xfeas = x.clone(); xfeas->set(x);
99 ReduceLinearConstraint<Real> rlc(makePtrFromRef(linear_con),xfeas,makePtrFromRef(linear_c));
100 Ptr<Vector<Real>> s = x.clone(); s->zero();
101
102 run(*s,g,*rlc.transform(makePtrFromRef(obj)),outStream);
103 rlc.project(x,*s);
104 x.plus(*rlc.getFeasibleVector());
105}
106
107template<typename Real>
108void Algorithm<Real>::writeHeader( std::ostream& os ) const {
109 std::ios_base::fmtflags osFlags(os.flags());
110 os << " ";
111 os << std::setw(6) << std::left << "iter";
112 os << std::setw(15) << std::left << "value";
113 os << std::setw(15) << std::left << "gnorm";
114 os << std::setw(15) << std::left << "snorm";
115 os << std::setw(10) << std::left << "#fval";
116 os << std::setw(10) << std::left << "#grad";
117 os << std::endl;
118 os.flags(osFlags);
119}
120
121template<typename Real>
122void Algorithm<Real>::writeName( std::ostream& os ) const {
123 throw Exception::NotImplemented(">>> ROL::TypeU::Algorithm::writeName() is not implemented!");
124}
125
126template<typename Real>
127void Algorithm<Real>::writeOutput( std::ostream& os, bool write_header ) const {
128 std::ios_base::fmtflags osFlags(os.flags());
129 os << std::scientific << std::setprecision(6);
130 if ( write_header ) writeHeader(os);
131 if ( state_->iter == 0 ) {
132 os << " ";
133 os << std::setw(6) << std::left << state_->iter;
134 os << std::setw(15) << std::left << state_->value;
135 os << std::setw(15) << std::left << state_->gnorm;
136 os << std::endl;
137 }
138 else {
139 os << " ";
140 os << std::setw(6) << std::left << state_->iter;
141 os << std::setw(15) << std::left << state_->value;
142 os << std::setw(15) << std::left << state_->gnorm;
143 os << std::setw(15) << std::left << state_->snorm;
144 os << std::setw(10) << std::left << state_->nfval;
145 os << std::setw(10) << std::left << state_->ngrad;
146 os << std::endl;
147 }
148 os.flags(osFlags);
149}
150
151template<typename Real>
152void Algorithm<Real>::writeExitStatus( std::ostream& os ) const {
153 std::ios_base::fmtflags osFlags(os.flags());
154 os << "Optimization Terminated with Status: ";
155 os << EExitStatusToString(state_->statusFlag);
156 os << std::endl;
157 os.flags(osFlags);
158}
159
160template<typename Real>
161//Ptr<const AlgorithmState<Real>>& Algorithm<Real>::getState() const {
162Ptr<const AlgorithmState<Real>> Algorithm<Real>::getState() const {
163 return state_;
164}
165
166template<typename Real>
168 state_->reset();
169}
170
171} // namespace TypeU
172} // namespace ROL
173
174#endif
Contains definitions of custom data types in ROL.
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< 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 finalizeIteration()
Transform the optimization variables to the native parameterization after an optimization algorithm h...
const Ptr< Objective< Real > > & getObjective()
Get the objective function.
Performs null-space transformation for reducible linear equality constraints.
Ptr< const Vector< Real > > getFeasibleVector(void) const
Ptr< Objective< Real > > transform(const Ptr< Objective< Real > > &obj) const
void project(Vector< Real > &x, const Vector< Real > &y) const
Provides an interface to check status of optimization algorithms.
Algorithm()
Constructor, given a step and a status test.
const Ptr< CombinedStatusTest< Real > > status_
Ptr< const AlgorithmState< Real > > getState() const
void initialize(const Vector< Real > &x, const Vector< Real > &g)
virtual void writeExitStatus(std::ostream &os) const
virtual void writeHeader(std::ostream &os) const
Print iterate header.
virtual void writeName(std::ostream &os) const
Print step name.
void setStatusTest(const Ptr< StatusTest< Real > > &status, bool combineStatus=false)
virtual void run(Problem< Real > &problem, std::ostream &outStream=std::cout)
Run algorithm on unconstrained problems (Type-U). This is the primary Type-U interface.
virtual void writeOutput(std::ostream &os, const bool write_header=false) const
Print iterate status.
Defines the linear algebra or vector space interface.
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis,...
virtual void plus(const Vector &x)=0
Compute , where .
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
@ TYPE_U
std::string EExitStatusToString(EExitStatus tr)
Definition ROL_Types.hpp:92