ROL
ROL_TypeP_QuasiNewtonAlgorithm_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_QUASINEWTONALGORITHM_DEF_HPP
11#define ROL_TYPEP_QUASINEWTONALGORITHM_DEF_HPP
12
16#include "ROL_PQNObjective.hpp"
17
18namespace ROL {
19namespace TypeP {
20
21template<typename Real>
23 const Ptr<Secant<Real>> &secant)
24 : secant_(secant), esec_(SECANT_USERDEFINED), list_(list), hasLEC_(true) {
25 // Set status test
26 status_->reset();
27 status_->add(makePtr<StatusTest<Real>>(list));
28
29 // Parse parameter list
30 ParameterList &lslist = list.sublist("Step").sublist("Line Search");
31 t0_ = list.sublist("Status Test").get("Gradient Scale" , 1.0);
32 initProx_ = lslist.get("Apply Prox to Initial Guess", false);
33 maxit_ = lslist.get("Function Evaluation Limit", 20);
34 c1_ = lslist.get("Sufficient Decrease Tolerance", 1e-4);
35 rhodec_ = lslist.sublist("Line-Search Method").get("Backtracking Rate", 0.5);
36 sigma1_ = lslist.sublist("PQN").get("Lower Step Size Safeguard", 0.1);
37 sigma2_ = lslist.sublist("PQN").get("Upper Step Size Safeguard", 0.9);
38 algoName_ = lslist.sublist("PQN").get("Subproblem Solver","Spectral Gradient");
39 int sp_maxit = lslist.sublist("PQN").get("Subproblem Iteration Limit", 1000);
40 sp_tol1_ = lslist.sublist("PQN").get("Subproblem Absolute Tolerance", 1e-4);
41 sp_tol2_ = lslist.sublist("PQN").get("Subproblem Relative Tolerance", 1e-2);
42 Real opt_tol = lslist.sublist("Status Test").get("Gradient Tolerance", 1e-8);
43 sp_tol_min_ = static_cast<Real>(1e-2)*opt_tol;
44 verbosity_ = list.sublist("General").get("Output Level", 0);
46
47 list_.sublist("Status Test").set("Iteration Limit", sp_maxit);
48 list_.sublist("General").set("Output Level", verbosity_>0 ? verbosity_-1 : 0);
49
50 if ( secant_ == nullPtr ) {
51 secantName_ = list.sublist("General").sublist("Secant").get("Type","Limited-Memory BFGS");
54 }
55 else {
56 secantName_ = list.sublist("General").sublist("Secant").get("User Defined Secant Name",
57 "Unspecified User Defined Secant Method");
58 }
59}
60
61
62template<typename Real>
64 const Vector<Real> &g,
65 Objective<Real> &sobj,
66 Objective<Real> &nobj,
67 Vector<Real> &dg,
68 std::ostream &outStream) {
69 const Real one(1);
70 Real tol(std::sqrt(ROL_EPSILON<Real>()));
71 // Initialize data
73 // Update approximate gradient and approximate objective function.
74 Real ftol = std::sqrt(ROL_EPSILON<Real>());
75 if (initProx_) {
76 state_->iterateVec->set(x);
77 nobj.prox(x,*state_->iterateVec,one,tol); state_->nprox++;
78 }
79 sobj.update(x,UpdateType::Initial,state_->iter);
80 nobj.update(x,UpdateType::Initial,state_->iter);
81 state_->svalue = sobj.value(x,ftol); state_->nsval++;
82 state_->nvalue = nobj.value(x,ftol); state_->nnval++;
83 state_->value = state_->svalue + state_->nvalue;
84 sobj.gradient(*state_->gradientVec,x,ftol); state_->ngrad++;
85 dg.set(state_->gradientVec->dual());
86 pgstep(*state_->iterateVec,*state_->stepVec,nobj,x,dg,t0_,tol);
87 state_->gnorm = state_->stepVec->norm() / t0_;
88 state_->snorm = ROL_INF<Real>();
89}
90
91template<typename Real>
93 const Vector<Real> &g,
94 Objective<Real> &sobj,
95 Objective<Real> &nobj,
96 std::ostream &outStream ) {
97 const Real half(0.5), one(1);
98 // Initialize trust-region data
99 Ptr<Vector<Real>> s = x.clone(), gp = x.clone(), gold = g.clone(), xs = x.clone();
100 initialize(x,g,sobj,nobj,*gp,outStream);
101 Real strial(0), ntrial(0), ftrial(0), gs(0), Qk(0), rhoTmp(0);
102 Real tol(std::sqrt(ROL_EPSILON<Real>())), gtol(1);
103
104 Ptr<TypeP::Algorithm<Real>> algo;
105 Ptr<PQNObjective<Real>> qobj = makePtr<PQNObjective<Real>>(secant_,x,g);
106 //Ptr<Problem<Real>> problem = makePtr<Problem<Real>>(qobj,xs);
107
108 // Output
109 if (verbosity_ > 0) writeOutput(outStream,true);
110
111 // Compute steepest descent step
112 xs->set(*state_->iterateVec);
113 state_->iterateVec->set(x);
114 while (status_->check(*state_)) {
115 // Compute step
116 qobj->setAnchor(x,*state_->gradientVec);
117 gtol = std::max(sp_tol_min_,std::min(sp_tol1_,sp_tol2_*state_->gnorm));
118 list_.sublist("Status Test").set("Gradient Tolerance",gtol);
119 if (algoName_ == "Line Search") algo = makePtr<TypeP::ProxGradientAlgorithm<Real>>(list_);
120 else if (algoName_ == "iPiano") algo = makePtr<TypeP::iPianoAlgorithm<Real>>(list_);
121 else algo = makePtr<TypeP::SpectralGradientAlgorithm<Real>>(list_);
122 algo->run(*xs,*qobj,nobj,outStream);
123 s->set(*xs); s->axpy(-one,x);
124 spgIter_ = algo->getState()->iter;
125 state_->nprox += staticPtrCast<const TypeP::AlgorithmState<Real>>(algo->getState())->nprox;
126
127 // Perform backtracking line search
128 state_->searchSize = one;
129 x.set(*state_->iterateVec);
130 x.axpy(state_->searchSize,*s);
133 strial = sobj.value(x,tol);
134 ntrial = nobj.value(x,tol);
135 ftrial = strial + ntrial;
136 ls_nfval_ = 1;
137 gs = state_->gradientVec->apply(*s);
138 Qk = gs + ntrial - state_->nvalue;
139 if (verbosity_ > 1) {
140 outStream << " In TypeP::QuasiNewtonAlgorithm: Line Search" << std::endl;
141 outStream << " Step size: " << state_->searchSize << std::endl;
142 outStream << " Trial objective value: " << ftrial << std::endl;
143 outStream << " Computed reduction: " << state_->value-ftrial << std::endl;
144 outStream << " Dot product of gradient and step: " << gs << std::endl;
145 outStream << " Sufficient decrease bound: " << -Qk*c1_ << std::endl;
146 outStream << " Number of function evaluations: " << ls_nfval_ << std::endl;
147 }
148 while ( ftrial > state_->value + c1_*Qk && ls_nfval_ < maxit_ ) {
149 rhoTmp = -half * Qk / (strial-state_->svalue-state_->searchSize*gs);
150 state_->searchSize = ((sigma1_ <= rhoTmp && rhoTmp <= sigma2_) ? rhoTmp : rhodec_) * state_->searchSize;
151 x.set(*state_->iterateVec);
152 x.axpy(state_->searchSize,*s);
155 strial = sobj.value(x,tol);
156 ntrial = nobj.value(x,tol);
157 ftrial = strial + ntrial;
158 Qk = state_->searchSize * gs + ntrial - state_->nvalue;
159 ls_nfval_++;
160 if (verbosity_ > 1) {
161 outStream << std::endl;
162 outStream << " Step size: " << state_->searchSize << std::endl;
163 outStream << " Trial objective value: " << ftrial << std::endl;
164 outStream << " Computed reduction: " << state_->value-ftrial << std::endl;
165 outStream << " Dot product of gradient and step: " << gs << std::endl;
166 outStream << " Sufficient decrease bound: " << -Qk*c1_ << std::endl;
167 outStream << " Number of function evaluations: " << ls_nfval_ << std::endl;
168 }
169 }
170 state_->nsval += ls_nfval_;
171 state_->nnval += ls_nfval_;
172
173 // Compute norm of step
174 state_->stepVec->set(*s);
175 state_->stepVec->scale(state_->searchSize);
176 state_->snorm = state_->stepVec->norm();
177
178 // Update iterate
179 state_->iterateVec->set(x);
180
181 // Compute new value and gradient
182 state_->iter++;
183 state_->value = ftrial;
184 state_->svalue = strial;
185 state_->nvalue = ntrial;
186 sobj.update(x,UpdateType::Accept,state_->iter);
187 nobj.update(x,UpdateType::Accept,state_->iter);
188 gold->set(*state_->gradientVec);
189 sobj.gradient(*state_->gradientVec,x,tol); state_->ngrad++;
190 gp->set(state_->gradientVec->dual());
191
192 // Compute projected gradient norm
193 pgstep(*xs,*s,nobj,x,*gp,t0_,tol);
194 state_->gnorm = s->norm() / t0_;
195
196 // Update secant
197 secant_->updateStorage(x,*state_->gradientVec,*gold,*state_->stepVec,state_->snorm,state_->iter);
198
199 // Update Output
200 if (verbosity_ > 0) writeOutput(outStream,writeHeader_);
201 }
203}
204
205template<typename Real>
206void QuasiNewtonAlgorithm<Real>::writeHeader( std::ostream& os ) const {
207 std::ios_base::fmtflags osFlags(os.flags());
208 if (verbosity_ > 1) {
209 os << std::string(114,'-') << std::endl;
210 os << "Line-Search Proximal Quasi-Newton with " << secantName_ << " Hessian approximation";
211 os << " status output definitions" << std::endl << std::endl;
212 os << " iter - Number of iterates (steps taken)" << std::endl;
213 os << " value - Objective function value" << std::endl;
214 os << " gnorm - Norm of the gradient" << std::endl;
215 os << " snorm - Norm of the step (update to optimization vector)" << std::endl;
216 os << " alpha - Line search step length" << std::endl;
217 os << " #sval - Cumulative number of times the smooth objective function was evaluated" << std::endl;
218 os << " #nval - Cumulative number of times the nonsmooth objective function was evaluated" << std::endl;
219 os << " #grad - Cumulative number of times the gradient was computed" << std::endl;
220 os << " #prox - Cumulative number of times the projection was computed" << std::endl;
221 os << " ls_#fval - Number of the times the objective function was evaluated during the line search" << std::endl;
222 os << " sp_iter - Number iterations to compute quasi-Newton step" << std::endl;
223 os << std::string(114,'-') << std::endl;
224 }
225
226 os << " ";
227 os << std::setw(6) << std::left << "iter";
228 os << std::setw(15) << std::left << "value";
229 os << std::setw(15) << std::left << "gnorm";
230 os << std::setw(15) << std::left << "snorm";
231 os << std::setw(15) << std::left << "alpha";
232 os << std::setw(10) << std::left << "#sval";
233 os << std::setw(10) << std::left << "#nval";
234 os << std::setw(10) << std::left << "#grad";
235 os << std::setw(10) << std::left << "#prox";
236 os << std::setw(10) << std::left << "#ls_fval";
237 os << std::setw(10) << std::left << "sp_iter";
238 os << std::endl;
239 os.flags(osFlags);
240}
241
242template<typename Real>
243void QuasiNewtonAlgorithm<Real>::writeName( std::ostream& os ) const {
244 std::ios_base::fmtflags osFlags(os.flags());
245 os << std::endl << "Line-Search Proximal Quasi-Newton (Type P)" << std::endl;
246 os.flags(osFlags);
247}
248
249template<typename Real>
250void QuasiNewtonAlgorithm<Real>::writeOutput( std::ostream& os, bool write_header ) const {
251 std::ios_base::fmtflags osFlags(os.flags());
252 os << std::scientific << std::setprecision(6);
253 if ( state_->iter == 0 ) writeName(os);
254 if ( write_header ) writeHeader(os);
255 if ( state_->iter == 0 ) {
256 os << " ";
257 os << std::setw(6) << std::left << state_->iter;
258 os << std::setw(15) << std::left << state_->value;
259 os << std::setw(15) << std::left << state_->gnorm;
260 os << std::setw(15) << std::left << "---";
261 os << std::setw(15) << std::left << "---";
262 os << std::setw(10) << std::left << state_->nsval;
263 os << std::setw(10) << std::left << state_->nnval;
264 os << std::setw(10) << std::left << state_->ngrad;
265 os << std::setw(10) << std::left << state_->nprox;
266 os << std::setw(10) << std::left << "---";
267 os << std::setw(10) << std::left << "---";
268 os << std::endl;
269 }
270 else {
271 os << " ";
272 os << std::setw(6) << std::left << state_->iter;
273 os << std::setw(15) << std::left << state_->value;
274 os << std::setw(15) << std::left << state_->gnorm;
275 os << std::setw(15) << std::left << state_->snorm;
276 os << std::setw(15) << std::left << state_->searchSize;
277 os << std::setw(10) << std::left << state_->nsval;
278 os << std::setw(10) << std::left << state_->nnval;
279 os << std::setw(10) << std::left << state_->ngrad;
280 os << std::setw(10) << std::left << state_->nprox;
281 os << std::setw(10) << std::left << ls_nfval_;
282 os << std::setw(10) << std::left << spgIter_;
283 os << std::endl;
284 }
285 os.flags(osFlags);
286}
287
288} // namespace TypeP
289} // namespace ROL
290
291#endif
virtual void initialize(const Vector< Real > &x)
Initialize temporary variables.
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.
virtual void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
virtual Real value(const Vector< Real > &x, Real &tol)=0
Compute value.
virtual void update(const Vector< Real > &x, UpdateType type, int iter=-1)
Update objective function.
Provides interface for and implements limited-memory secant operators.
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
const Ptr< AlgorithmState< Real > > state_
virtual void writeExitStatus(std::ostream &os) const
const Ptr< CombinedStatusTest< Real > > status_
void initialize(const Vector< Real > &x, const Vector< Real > &g)
Real rhodec_
Backtracking rate (default: 0.5).
Real sigma2_
Upper safeguard for quadratic line search (default: 0.9).
void writeOutput(std::ostream &os, bool write_header=false) const override
Print iterate status.
QuasiNewtonAlgorithm(ParameterList &list, const Ptr< Secant< Real > > &secant=nullPtr)
Real sigma1_
Lower safeguard for quadratic line search (default: 0.1).
Ptr< Secant< Real > > secant_
Secant object (used for quasi-Newton).
int maxit_
Maximum number of line search steps (default: 20).
Real c1_
Sufficient Decrease Parameter (default: 1e-4).
void writeHeader(std::ostream &os) const override
Print iterate header.
void initialize(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &sobj, Objective< Real > &nobj, Vector< Real > &dg, std::ostream &outStream=std::cout)
void run(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &sobj, Objective< Real > &nobj, std::ostream &outStream=std::cout) override
Run algorithm on unconstrained problems (Type-U). This general interface supports the use of dual opt...
void writeName(std::ostream &os) const override
Print step name.
Defines the linear algebra or vector space interface.
virtual void set(const Vector &x)
Set where .
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 .
Real ROL_EPSILON(void)
Platform-dependent machine epsilon.
Definition ROL_Types.hpp:57
ESecant StringToESecant(std::string s)
@ SECANT_USERDEFINED
ROL::Ptr< Secant< Real > > SecantFactory(ROL::ParameterList &parlist, ESecantMode mode=SECANTMODE_BOTH)
Real ROL_INF(void)
Definition ROL_Types.hpp:71