ROL
ROL_TypeP_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_TYPEP_ALGORITHM_DEF_H
11#define ROL_TYPEP_ALGORITHM_DEF_H
12
13#include "ROL_Types.hpp"
15
16namespace ROL {
17namespace TypeP {
18
19template<typename Real>
21 : status_(makePtr<CombinedStatusTest<Real>>()),
22 state_(makePtr<AlgorithmState<Real>>()) {
23 status_->reset();
24 status_->add(makePtr<StatusTest<Real>>());
25}
26
27template<typename Real>
29 if (state_->iterateVec == nullPtr)
30 state_->iterateVec = x.clone();
31 state_->iterateVec->set(x);
32 if (state_->stepVec == nullPtr)
33 state_->stepVec = x.clone();
34 state_->stepVec->zero();
35 if (state_->gradientVec == nullPtr)
36 state_->gradientVec = g.clone();
37 state_->gradientVec->set(g);
38 if (state_->minIterVec == nullPtr)
39 state_->minIterVec = x.clone();
40 state_->minIterVec->set(x);
41 state_->minIter = state_->iter;
42 state_->minValue = state_->value;
43}
44
45template<typename Real>
48 Objective<Real> &nobj,
49 const Vector<Real> &x,
50 const Vector<Real> &dg,
51 Real t,
52 Real &tol) const {
53 pgstep.set(x);
54 pgstep.axpy(-t,dg);
55 nobj.prox(pgiter,pgstep,t,tol);
56 state_->nprox++;
57 pgstep.set(pgiter);
58 pgstep.axpy(static_cast<Real>(-1),x);
59}
60
61template<typename Real>
63 bool combineStatus) {
64 if (!combineStatus) // Do not combine status tests
65 status_->reset();
66 status_->add(status); // Add user-defined StatusTest
67}
68
69template<typename Real>
71 std::ostream &outStream ) {
72 if (problem.getProblemType() == TYPE_P) {
75 *problem.getObjective(),
76 *problem.getProximableObjective(),
77 outStream);
78 problem.finalizeIteration();
79 }
80 else {
81 throw Exception::NotImplemented(">>> ROL::TypeP::Algorithm::run : Optimization problem is not Type P!");
82 }
83}
84
85template<typename Real>
87 Objective<Real> &sobj,
88 Objective<Real> &nobj,
89 std::ostream &outStream ) {
90 run(x,x.dual(),sobj,nobj,outStream);
91}
92
93template<typename Real>
94void Algorithm<Real>::writeHeader( std::ostream& os ) const {
95 std::ios_base::fmtflags osFlags(os.flags());
96 os << " ";
97 os << std::setw(6) << std::left << "iter";
98 os << std::setw(15) << std::left << "value";
99 os << std::setw(15) << std::left << "gnorm";
100 os << std::setw(15) << std::left << "snorm";
101 os << std::setw(10) << std::left << "#fval";
102 os << std::setw(10) << std::left << "#grad";
103 os << std::setw(10) << std::left << "#prox";
104 os << std::endl;
105 os.flags(osFlags);
106}
107
108template<typename Real>
109void Algorithm<Real>::writeName( std::ostream& os ) const {
110 throw Exception::NotImplemented(">>> ROL::TypeP::Algorithm::writeName() is not implemented!");
111}
112
113template<typename Real>
114void Algorithm<Real>::writeOutput( std::ostream& os, bool write_header ) const {
115 std::ios_base::fmtflags osFlags(os.flags());
116 os << std::scientific << std::setprecision(6);
117 if ( write_header ) writeHeader(os);
118 if ( state_->iter == 0 ) {
119 os << " ";
120 os << std::setw(6) << std::left << state_->iter;
121 os << std::setw(15) << std::left << state_->value;
122 os << std::setw(15) << std::left << state_->gnorm;
123 os << std::endl;
124 }
125 else {
126 os << " ";
127 os << std::setw(6) << std::left << state_->iter;
128 os << std::setw(15) << std::left << state_->value;
129 os << std::setw(15) << std::left << state_->gnorm;
130 os << std::setw(15) << std::left << state_->snorm;
131 os << std::setw(10) << std::left << state_->nfval;
132 os << std::setw(10) << std::left << state_->ngrad;
133 os << std::setw(10) << std::left << state_->nprox;
134 os << std::endl;
135 }
136 os.flags(osFlags);
137}
138
139template<typename Real>
140void Algorithm<Real>::writeExitStatus( std::ostream& os ) const {
141 std::ios_base::fmtflags osFlags(os.flags());
142 os << "Optimization Terminated with Status: ";
143 os << EExitStatusToString(state_->statusFlag);
144 os << std::endl;
145 os.flags(osFlags);
146}
147
148template<typename Real>
149Ptr<const AlgorithmState<Real>> Algorithm<Real>::getState() const {
150 return state_;
151}
152
153template<typename Real>
155 state_->reset();
156}
157
158} // namespace TypeP
159} // namespace ROL
160
161#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)
Compute the proximity operator.
const Ptr< Vector< Real > > & getPrimalOptimizationVector()
Get the primal optimization space vector.
const Ptr< Vector< Real > > & getDualOptimizationVector()
Get the dual optimization space vector.
const Ptr< Objective< Real > > & getProximableObjective()
Get proximable objective.
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.
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.
@ TYPE_P
std::string EExitStatusToString(EExitStatus tr)
Definition ROL_Types.hpp:92