ROL
ROL_TypeP_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_TYPEP_ALGORITHM_DEF_H
45 #define ROL_TYPEP_ALGORITHM_DEF_H
46 
47 #include "ROL_Types.hpp"
48 #include "ROL_ValidParameters.hpp"
49 
50 namespace ROL {
51 namespace TypeP {
52 
53 template<typename Real>
55  : status_(makePtr<CombinedStatusTest<Real>>()),
56  state_(makePtr<AlgorithmState<Real>>()) {
57  status_->reset();
58  status_->add(makePtr<StatusTest<Real>>());
59 }
60 
61 template<typename Real>
63  if (state_->iterateVec == nullPtr)
64  state_->iterateVec = x.clone();
65  state_->iterateVec->set(x);
66  if (state_->stepVec == nullPtr)
67  state_->stepVec = x.clone();
68  state_->stepVec->zero();
69  if (state_->gradientVec == nullPtr)
70  state_->gradientVec = g.clone();
71  state_->gradientVec->set(g);
72  if (state_->minIterVec == nullPtr)
73  state_->minIterVec = x.clone();
74  state_->minIterVec->set(x);
75  state_->minIter = state_->iter;
76  state_->minValue = state_->value;
77 }
78 
79 template<typename Real>
81  Vector<Real> &pgstep,
82  Objective<Real> &nobj,
83  const Vector<Real> &x,
84  const Vector<Real> &dg,
85  Real t,
86  Real &tol) const {
87  pgstep.set(x);
88  pgstep.axpy(-t,dg);
89  nobj.prox(pgiter,pgstep,t,tol);
90  state_->nprox++;
91  pgstep.set(pgiter);
92  pgstep.axpy(static_cast<Real>(-1),x);
93 }
94 
95 template<typename Real>
97  bool combineStatus) {
98  if (!combineStatus) // Do not combine status tests
99  status_->reset();
100  status_->add(status); // Add user-defined StatusTest
101 }
102 
103 template<typename Real>
105  std::ostream &outStream ) {
106  /*if (problem.getProblemType() == TYPE_P) {
107  run(*problem.getPrimalOptimizationVector(),
108  *problem.getDualOptimizationVector(),
109  *problem.getObjective(),
110  outStream);
111  problem.finalizeIteration();
112  }
113  else {
114  throw Exception::NotImplemented(">>> ROL::TypeP::Algorithm::run : Optimization problem is not Type P!");
115  }*/
116  throw Exception::NotImplemented(">>> ROL::TypeP::Algorithm::run : Optimization problem is not available for Type P problems!");
117 }
118 
119 template<typename Real>
121  Objective<Real> &sobj,
122  Objective<Real> &nobj,
123  std::ostream &outStream ) {
124  run(x,x.dual(),sobj,nobj,outStream);
125 }
126 
127 template<typename Real>
128 void Algorithm<Real>::writeHeader( std::ostream& os ) const {
129  std::ios_base::fmtflags osFlags(os.flags());
130  os << " ";
131  os << std::setw(6) << std::left << "iter";
132  os << std::setw(15) << std::left << "value";
133  os << std::setw(15) << std::left << "gnorm";
134  os << std::setw(15) << std::left << "snorm";
135  os << std::setw(10) << std::left << "#fval";
136  os << std::setw(10) << std::left << "#grad";
137  os << std::setw(10) << std::left << "#prox";
138  os << std::endl;
139  os.flags(osFlags);
140 }
141 
142 template<typename Real>
143 void Algorithm<Real>::writeName( std::ostream& os ) const {
144  throw Exception::NotImplemented(">>> ROL::TypeP::Algorithm::writeName() is not implemented!");
145 }
146 
147 template<typename Real>
148 void Algorithm<Real>::writeOutput( std::ostream& os, bool write_header ) const {
149  std::ios_base::fmtflags osFlags(os.flags());
150  os << std::scientific << std::setprecision(6);
151  if ( write_header ) writeHeader(os);
152  if ( state_->iter == 0 ) {
153  os << " ";
154  os << std::setw(6) << std::left << state_->iter;
155  os << std::setw(15) << std::left << state_->value;
156  os << std::setw(15) << std::left << state_->gnorm;
157  os << std::endl;
158  }
159  else {
160  os << " ";
161  os << std::setw(6) << std::left << state_->iter;
162  os << std::setw(15) << std::left << state_->value;
163  os << std::setw(15) << std::left << state_->gnorm;
164  os << std::setw(15) << std::left << state_->snorm;
165  os << std::setw(10) << std::left << state_->nfval;
166  os << std::setw(10) << std::left << state_->ngrad;
167  os << std::setw(10) << std::left << state_->nprox;
168  os << std::endl;
169  }
170  os.flags(osFlags);
171 }
172 
173 template<typename Real>
174 void Algorithm<Real>::writeExitStatus( std::ostream& os ) const {
175  std::ios_base::fmtflags osFlags(os.flags());
176  os << "Optimization Terminated with Status: ";
177  os << EExitStatusToString(state_->statusFlag);
178  os << std::endl;
179  os.flags(osFlags);
180 }
181 
182 template<typename Real>
183 Ptr<const AlgorithmState<Real>> Algorithm<Real>::getState() const {
184  return state_;
185 }
186 
187 template<typename Real>
189  state_->reset();
190 }
191 
192 } // namespace TypeP
193 } // namespace ROL
194 
195 #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:226
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:153
Contains definitions of custom data types in ROL.
virtual void writeOutput(std::ostream &os, bool write_header=false) const
Print iterate status.
virtual void prox(Vector< Real > &Pv, const Vector< Real > &v, Real t, Real &tol)
void pgstep(Vector< Real > &pgiter, Vector< Real > &pgstep, Objective< Real > &nobj, const Vector< Real > &x, const Vector< Real > &dg, Real t, Real &tol) const
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
void setStatusTest(const Ptr< StatusTest< Real >> &status, bool combineStatus=false)
virtual void writeHeader(std::ostream &os) const
Print iterate header.
virtual void writeName(std::ostream &os) const
Print step name.
std::string EExitStatusToString(EExitStatus tr)
Definition: ROL_Types.hpp:126
virtual void run(Vector< Real > &x, Objective< Real > &sobj, Objective< Real > &nobj, std::ostream &outStream=std::cout)
Run algorithm on unconstrained problems (Type-U). This is the primary Type-U interface.
Provides an interface to check status of optimization algorithms.
const Ptr< CombinedStatusTest< Real > > status_
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:209
Provides an interface to check two status tests of optimization algorithms.
virtual void writeExitStatus(std::ostream &os) 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.
void initialize(const Vector< Real > &x, const Vector< Real > &g)
Algorithm()
Constructor, given a step and a status test.
Ptr< const AlgorithmState< Real > > getState() const