ROL
ROL_TypeU_Algorithm_Def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 #ifndef ROL_TYPEU_ALGORITHM_DEF_H
45 #define ROL_TYPEU_ALGORITHM_DEF_H
46 
47 #include "ROL_Types.hpp"
49 #include "ROL_ValidParameters.hpp"
50 
51 namespace ROL {
52 namespace TypeU {
53 
54 template<typename Real>
56  : status_(makePtr<CombinedStatusTest<Real>>()),
57  state_(makePtr<AlgorithmState<Real>>()) {
58  status_->reset();
59  status_->add(makePtr<StatusTest<Real>>());
60 }
61 
62 template<typename Real>
64  if (state_->iterateVec == nullPtr) {
65  state_->iterateVec = x.clone();
66  }
67  state_->iterateVec->set(x);
68  if (state_->stepVec == nullPtr) {
69  state_->stepVec = x.clone();
70  }
71  state_->stepVec->zero();
72  if (state_->gradientVec == nullPtr) {
73  state_->gradientVec = g.clone();
74  }
75  state_->gradientVec->set(g);
76  if (state_->minIterVec == nullPtr) {
77  state_->minIterVec = x.clone();
78  }
79  state_->minIterVec->set(x);
80  state_->minIter = state_->iter;
81  state_->minValue = state_->value;
82 }
83 
84 template<typename Real>
86  bool combineStatus) {
87  if (!combineStatus) { // Do not combine status tests
88  status_->reset();
89  }
90  status_->add(status); // Add user-defined StatusTest
91 }
92 
93 template<typename Real>
95  std::ostream &outStream ) {
96  if (problem.getProblemType() == TYPE_U) {
97  run(*problem.getPrimalOptimizationVector(),
98  *problem.getDualOptimizationVector(),
99  *problem.getObjective(),
100  outStream);
101  problem.finalizeIteration();
102  }
103  else {
104  throw Exception::NotImplemented(">>> ROL::TypeU::Algorithm::run : Optimization problem is not Type U!");
105  }
106 }
107 
108 template<typename Real>
110  Objective<Real> &obj,
111  std::ostream &outStream ) {
112  run(x,x.dual(),obj,outStream);
113 }
114 
115 template<typename Real>
117  Objective<Real> &obj,
118  Constraint<Real> &linear_con,
119  Vector<Real> &linear_mul,
120  std::ostream &outStream ) {
121  run(x,x.dual(),obj,linear_con,linear_mul,linear_mul.dual(),outStream);
122 }
123 
124 template<typename Real>
126  const Vector<Real> &g,
127  Objective<Real> &obj,
128  Constraint<Real> &linear_con,
129  Vector<Real> &linear_mul,
130  const Vector<Real> &linear_c,
131  std::ostream &outStream ) {
132  Ptr<Vector<Real>> xfeas = x.clone(); xfeas->set(x);
133  ReduceLinearConstraint<Real> rlc(makePtrFromRef(linear_con),xfeas,makePtrFromRef(linear_c));
134  Ptr<Vector<Real>> s = x.clone(); s->zero();
135 
136  run(*s,g,*rlc.transform(makePtrFromRef(obj)),outStream);
137  rlc.project(x,*s);
138  x.plus(*rlc.getFeasibleVector());
139 }
140 
141 template<typename Real>
142 void Algorithm<Real>::writeHeader( std::ostream& os ) const {
143  std::stringstream hist;
144  hist << " ";
145  hist << std::setw(6) << std::left << "iter";
146  hist << std::setw(15) << std::left << "value";
147  hist << std::setw(15) << std::left << "gnorm";
148  hist << std::setw(15) << std::left << "snorm";
149  hist << std::setw(10) << std::left << "#fval";
150  hist << std::setw(10) << std::left << "#grad";
151  hist << std::endl;
152  os << hist.str();
153 }
154 
155 template<typename Real>
156 void Algorithm<Real>::writeName( std::ostream& os ) const {
157  throw Exception::NotImplemented(">>> ROL::TypeU::Algorithm::writeName() is not implemented!");
158 }
159 
160 template<typename Real>
161 void Algorithm<Real>::writeOutput( std::ostream& os, bool write_header ) const {
162  std::stringstream hist;
163  hist << std::scientific << std::setprecision(6);
164  if ( write_header ) writeHeader(os);
165  if ( state_->iter == 0 ) {
166  hist << " ";
167  hist << std::setw(6) << std::left << state_->iter;
168  hist << std::setw(15) << std::left << state_->value;
169  hist << std::setw(15) << std::left << state_->gnorm;
170  hist << std::endl;
171  }
172  else {
173  hist << " ";
174  hist << std::setw(6) << std::left << state_->iter;
175  hist << std::setw(15) << std::left << state_->value;
176  hist << std::setw(15) << std::left << state_->gnorm;
177  hist << std::setw(15) << std::left << state_->snorm;
178  hist << std::setw(10) << std::left << state_->nfval;
179  hist << std::setw(10) << std::left << state_->ngrad;
180  hist << std::endl;
181  }
182  os << hist.str();
183 }
184 
185 template<typename Real>
186 void Algorithm<Real>::writeExitStatus( std::ostream& os ) const {
187  std::stringstream hist;
188  hist << "Optimization Terminated with Status: ";
189  hist << EExitStatusToString(state_->statusFlag);
190  hist << std::endl;
191  os << hist.str();
192 }
193 
194 template<typename Real>
195 //Ptr<const AlgorithmState<Real>>& Algorithm<Real>::getState() const {
196 Ptr<const AlgorithmState<Real>> Algorithm<Real>::getState() const {
197  return state_;
198 }
199 
200 template<typename Real>
202  state_->reset();
203 }
204 
205 } // namespace TypeU
206 } // namespace ROL
207 
208 #endif
Provides the interface to evaluate objective functions.
Ptr< const Vector< Real > > getFeasibleVector(void) const
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 .
Performs null-space transformation for reducible linear equality constraints.
Contains definitions of custom data types in ROL.
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:80
void project(Vector< Real > &x, const Vector< Real > &y) const
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 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:226
std::string EExitStatusToString(EExitStatus tr)
Definition: ROL_Types.hpp:126
const Ptr< Objective< Real > > & getObjective()
Get the objective function.
virtual void writeOutput(std::ostream &os, bool write_header=false) const
Print iterate status.
Ptr< Objective< Real > > transform(const Ptr< Objective< Real >> &obj) const
void setStatusTest(const Ptr< StatusTest< Real >> &status, bool combineStatus=false)
Ptr< const AlgorithmState< Real > > getState() const
void finalizeIteration()
Transform the optimization variables to the native parameterization after an optimization algorithm h...
Provides an interface to check status of optimization algorithms.
virtual void writeHeader(std::ostream &os) const
Print iterate header.
virtual void writeExitStatus(std::ostream &os) const
EProblem getProblemType()
Get the optimization problem type (U, B, E, or G).
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 writeName(std::ostream &os) const
Print step name.