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"
49
50namespace ROL {
51namespace TypeP {
52
53template<typename Real>
55 : status_(makePtr<CombinedStatusTest<Real>>()),
56 state_(makePtr<AlgorithmState<Real>>()) {
57 status_->reset();
58 status_->add(makePtr<StatusTest<Real>>());
59}
60
61template<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
79template<typename Real>
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
95template<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
103template<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
119template<typename Real>
121 Objective<Real> &sobj,
122 Objective<Real> &nobj,
123 std::ostream &outStream ) {
124 run(x,x.dual(),sobj,nobj,outStream);
125}
126
127template<typename Real>
128void Algorithm<Real>::writeHeader( std::ostream& os ) const {
129 std::stringstream hist;
130 hist << " ";
131 hist << std::setw(6) << std::left << "iter";
132 hist << std::setw(15) << std::left << "value";
133 hist << std::setw(15) << std::left << "gnorm";
134 hist << std::setw(15) << std::left << "snorm";
135 hist << std::setw(10) << std::left << "#fval";
136 hist << std::setw(10) << std::left << "#grad";
137 hist << std::setw(10) << std::left << "#prox";
138 hist << std::endl;
139 os << hist.str();
140}
141
142template<typename Real>
143void Algorithm<Real>::writeName( std::ostream& os ) const {
144 throw Exception::NotImplemented(">>> ROL::TypeP::Algorithm::writeName() is not implemented!");
145}
146
147template<typename Real>
148void Algorithm<Real>::writeOutput( std::ostream& os, bool write_header ) const {
149 std::stringstream hist;
150 hist << std::scientific << std::setprecision(6);
151 if ( write_header ) writeHeader(os);
152 if ( state_->iter == 0 ) {
153 hist << " ";
154 hist << std::setw(6) << std::left << state_->iter;
155 hist << std::setw(15) << std::left << state_->value;
156 hist << std::setw(15) << std::left << state_->gnorm;
157 hist << std::endl;
158 }
159 else {
160 hist << " ";
161 hist << std::setw(6) << std::left << state_->iter;
162 hist << std::setw(15) << std::left << state_->value;
163 hist << std::setw(15) << std::left << state_->gnorm;
164 hist << std::setw(15) << std::left << state_->snorm;
165 hist << std::setw(10) << std::left << state_->nfval;
166 hist << std::setw(10) << std::left << state_->ngrad;
167 hist << std::setw(10) << std::left << state_->nprox;
168 hist << std::endl;
169 }
170 os << hist.str();
171}
172
173template<typename Real>
174void Algorithm<Real>::writeExitStatus( std::ostream& os ) const {
175 std::stringstream hist;
176 hist << "Optimization Terminated with Status: ";
177 hist << EExitStatusToString(state_->statusFlag);
178 hist << std::endl;
179 os << hist.str();
180}
181
182template<typename Real>
183Ptr<const AlgorithmState<Real>> Algorithm<Real>::getState() const {
184 return state_;
185}
186
187template<typename Real>
189 state_->reset();
190}
191
192} // namespace TypeP
193} // namespace ROL
194
195#endif
Contains definitions of custom data types in ROL.
Provides an interface to check two status tests of optimization algorithms.
Provides the interface to evaluate objective functions.
virtual void prox(Vector< Real > &Pv, const Vector< Real > &v, Real t, Real &tol)
Provides an interface to check status of optimization algorithms.
void pgstep(Vector< Real > &pgiter, Vector< Real > &pgstep, Objective< Real > &nobj, const Vector< Real > &x, const Vector< Real > &dg, Real t, Real &tol) 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 void writeName(std::ostream &os) const
Print step name.
Ptr< const AlgorithmState< Real > > getState() const
const Ptr< AlgorithmState< Real > > state_
Algorithm()
Constructor, given a step and a status test.
virtual void writeHeader(std::ostream &os) const
Print iterate header.
virtual void writeExitStatus(std::ostream &os) const
virtual void writeOutput(std::ostream &os, bool write_header=false) const
Print iterate status.
const Ptr< CombinedStatusTest< Real > > status_
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.
void setStatusTest(const Ptr< StatusTest< Real > > &status, bool combineStatus=false)
void initialize(const Vector< Real > &x, const Vector< Real > &g)
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 ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
std::string EExitStatusToString(EExitStatus tr)