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"
15 #include "ROL_ValidParameters.hpp"
16 
17 namespace ROL {
18 namespace TypeU {
19 
20 template<typename Real>
22  : status_(makePtr<CombinedStatusTest<Real>>()),
23  state_(makePtr<AlgorithmState<Real>>()) {
24  status_->reset();
25  status_->add(makePtr<StatusTest<Real>>());
26 }
27 
28 template<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 
50 template<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 
59 template<typename Real>
61  std::ostream &outStream ) {
62  if (problem.getProblemType() == TYPE_U) {
63  run(*problem.getPrimalOptimizationVector(),
64  *problem.getDualOptimizationVector(),
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 
74 template<typename Real>
76  Objective<Real> &obj,
77  std::ostream &outStream ) {
78  run(x,x.dual(),obj,outStream);
79 }
80 
81 template<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 
90 template<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 
107 template<typename Real>
108 void 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 
121 template<typename Real>
122 void Algorithm<Real>::writeName( std::ostream& os ) const {
123  throw Exception::NotImplemented(">>> ROL::TypeU::Algorithm::writeName() is not implemented!");
124 }
125 
126 template<typename Real>
127 void 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 
151 template<typename Real>
152 void 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 
160 template<typename Real>
161 //Ptr<const AlgorithmState<Real>>& Algorithm<Real>::getState() const {
162 Ptr<const AlgorithmState<Real>> Algorithm<Real>::getState() const {
163  return state_;
164 }
165 
166 template<typename Real>
168  state_->reset();
169 }
170 
171 } // namespace TypeU
172 } // namespace ROL
173 
174 #endif
Provides the interface to evaluate objective functions.
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Definition: ROL_Vector.hpp:192
const Ptr< Vector< Real > > & getPrimalOptimizationVector()
Get the primal optimization space vector.
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual void plus(const Vector &x)=0
Compute , where .
void project(Vector< Real > &x, const Vector< Real > &y) const
Performs null-space transformation for reducible linear equality constraints.
Contains definitions of custom data types in ROL.
Ptr< const AlgorithmState< Real > > getState() const
void initialize(const Vector< Real > &x, const Vector< Real > &g)
Algorithm()
Constructor, given a step and a status test.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
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.
std::string EExitStatusToString(EExitStatus tr)
Definition: ROL_Types.hpp:92
const Ptr< Objective< Real > > & getObjective()
Get the objective function.
Ptr< Objective< Real > > transform(const Ptr< Objective< Real >> &obj) const
void setStatusTest(const Ptr< StatusTest< Real >> &status, bool combineStatus=false)
void finalizeIteration()
Transform the optimization variables to the native parameterization after an optimization algorithm h...
Provides an interface to check status of optimization algorithms.
EProblem getProblemType()
Get the optimization problem type (U, B, E, or G).
virtual void writeName(std::ostream &os) const
Print step name.
virtual void writeOutput(std::ostream &os, const bool write_header=false) const
Print iterate status.
Ptr< const Vector< Real > > getFeasibleVector(void) const
virtual void writeExitStatus(std::ostream &os) const
Provides an interface to check two status tests of optimization algorithms.
const Ptr< CombinedStatusTest< Real > > status_
const Ptr< Vector< Real > > & getDualOptimizationVector()
Get the dual optimization space vector.
Defines the general constraint operator interface.
virtual void writeHeader(std::ostream &os) const
Print iterate header.