ROL
ROL_TypeB_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_TYPEB_QUASINEWTONALGORITHM_DEF_HPP
11#define ROL_TYPEB_QUASINEWTONALGORITHM_DEF_HPP
12
19#include "ROL_PQNObjective.hpp"
20
21namespace ROL {
22namespace TypeB {
23
24template<typename Real>
26 const Ptr<Secant<Real>> &secant)
27 : secant_(secant), esec_(SECANT_USERDEFINED), list_(list), hasLEC_(true) {
28 // Set status test
29 status_->reset();
30 status_->add(makePtr<StatusTest<Real>>(list));
31
32 // Parse parameter list
33 ParameterList &lslist = list.sublist("Step").sublist("Line Search");
34 maxit_ = lslist.get("Function Evaluation Limit", 20);
35 c1_ = lslist.get("Sufficient Decrease Tolerance", 1e-4);
36 rhodec_ = lslist.sublist("Line-Search Method").get("Backtracking Rate", 0.5);
37 sigma1_ = lslist.sublist("PQN").get("Lower Step Size Safeguard", 0.1);
38 sigma2_ = lslist.sublist("PQN").get("Upper Step Size Safeguard", 0.9);
39 algoName_ = lslist.sublist("PQN").get("Subproblem Solver","Spectral Gradient");
40 int sp_maxit = lslist.sublist("PQN").get("Subproblem Iteration Limit", 1000);
41 sp_tol1_ = lslist.sublist("PQN").get("Subproblem Absolute Tolerance", 1e-4);
42 sp_tol2_ = lslist.sublist("PQN").get("Subproblem Relative Tolerance", 1e-2);
43 Real opt_tol = lslist.sublist("Status Test").get("Gradient Tolerance", 1e-8);
44 sp_tol_min_ = static_cast<Real>(1e-2)*opt_tol;
45 verbosity_ = list.sublist("General").get("Output Level", 0);
47
48 list_.sublist("Status Test").set("Iteration Limit", sp_maxit);
49 list_.sublist("General").set("Output Level", verbosity_>0 ? verbosity_-1 : 0);
50
51 if ( secant_ == nullPtr ) {
52 secantName_ = list.sublist("General").sublist("Secant").get("Type","Limited-Memory BFGS");
55 }
56 else {
57 secantName_ = list.sublist("General").sublist("Secant").get("User Defined Secant Name",
58 "Unspecified User Defined Secant Method");
59 }
60}
61
62
63template<typename Real>
65 const Vector<Real> &g,
66 Objective<Real> &obj,
68 std::ostream &outStream) {
69 const Real one(1);
70 if (proj_ == nullPtr) {
71 proj_ = makePtr<PolyhedralProjection<Real>>(makePtrFromRef(bnd));
72 hasLEC_ = false;
73 }
74 // Initialize data
76 // Update approximate gradient and approximate objective function.
77 Real ftol = std::sqrt(ROL_EPSILON<Real>());
78 proj_->project(x,outStream); state_->nproj++;
79 state_->iterateVec->set(x);
80 obj.update(x,UpdateType::Initial,state_->iter);
81 state_->value = obj.value(x,ftol); state_->nfval++;
82 obj.gradient(*state_->gradientVec,x,ftol); state_->ngrad++;
83 state_->stepVec->set(x);
84 state_->stepVec->axpy(-one,state_->gradientVec->dual());
85 proj_->project(*state_->stepVec,outStream); state_->nproj++;
86 state_->stepVec->axpy(-one,x);
87 state_->gnorm = state_->stepVec->norm();
88 state_->snorm = ROL_INF<Real>();
89}
90
91template<typename Real>
93 const Vector<Real> &g,
94 Objective<Real> &obj,
96 std::ostream &outStream ) {
97 const Real half(0.5), one(1);
98 // Initialize trust-region data
99 initialize(x,g,obj,bnd,outStream);
100 Ptr<Vector<Real>> s = x.clone(), gp = x.clone(), gold = g.clone(), xs = x.clone();
101 Real ftrial(0), gs(0), alphaTmp(0), tol(std::sqrt(ROL_EPSILON<Real>())), gtol(1);
102
103 Ptr<TypeB::Algorithm<Real>> algo;
104 Ptr<PQNObjective<Real>> qobj = makePtr<PQNObjective<Real>>(secant_,x,g);
105 Ptr<Problem<Real>> problem = makePtr<Problem<Real>>(qobj,xs);
106 problem->addBoundConstraint(makePtrFromRef(bnd));
107 if (hasLEC_) {
108 problem->addLinearConstraint("LEC",proj_->getLinearConstraint(),
109 proj_->getMultiplier(),
110 proj_->getResidual());
111 problem->setProjectionAlgorithm(list_);
112 }
113 problem->finalize(false,verbosity_>2,outStream);
114
115 // Output
116 if (verbosity_ > 0) writeOutput(outStream,true);
117
118 // Compute steepest descent step
119 gp->set(state_->gradientVec->dual());
120 while (status_->check(*state_)) {
121 // Compute step
122 qobj->setAnchor(x,*state_->gradientVec);
123 xs->set(x); xs->axpy(-one,*gp); proj_->project(*xs,outStream); state_->nproj++;
124 gtol = std::max(sp_tol_min_,std::min(sp_tol1_,sp_tol2_*state_->gnorm));
125 list_.sublist("Status Test").set("Gradient Tolerance",gtol);
126 if (algoName_ == "Trust Region") algo = makePtr<TypeB::LinMoreAlgorithm<Real>>(list_);
127 else if (algoName_ == "Line Search") algo = makePtr<TypeB::GradientAlgorithm<Real>>(list_);
128 else if (algoName_ == "Primal Dual Active Set") algo = makePtr<TypeB::PrimalDualActiveSetAlgorithm<Real>>(list_);
129 else if (algoName_ == "Moreau-Yosida") algo = makePtr<TypeB::MoreauYosidaAlgorithm<Real>>(list_);
130 else if (algoName_ == "Interior Point") algo = makePtr<TypeB::InteriorPointAlgorithm<Real>>(list_);
131 else algo = makePtr<TypeB::SpectralGradientAlgorithm<Real>>(list_);
132 algo->run(*problem,outStream);
133 s->set(*xs); s->axpy(-one,x);
134 spgIter_ = algo->getState()->iter;
135 state_->nproj += staticPtrCast<const TypeB::AlgorithmState<Real>>(algo->getState())->nproj;
136
137 // Perform backtracking line search
138 state_->searchSize = one;
139 x.set(*state_->iterateVec);
140 x.axpy(state_->searchSize,*s);
142 ftrial = obj.value(x,tol); ls_nfval_ = 1;
143 gs = state_->gradientVec->apply(*s);
144 if (verbosity_ > 1) {
145 outStream << " In TypeB::QuasiNewtonAlgorithm: Line Search" << std::endl;
146 outStream << " Step size: " << state_->searchSize << std::endl;
147 outStream << " Trial objective value: " << ftrial << std::endl;
148 outStream << " Computed reduction: " << state_->value-ftrial << std::endl;
149 outStream << " Dot product of gradient and step: " << gs << std::endl;
150 outStream << " Sufficient decrease bound: " << -gs*state_->searchSize*c1_ << std::endl;
151 outStream << " Number of function evaluations: " << ls_nfval_ << std::endl;
152 }
153 while ( ftrial > state_->value + c1_*state_->searchSize*gs && ls_nfval_ < maxit_ ) {
154 alphaTmp = -half*state_->searchSize*state_->searchSize*gs
155 / (ftrial-state_->value-state_->searchSize*gs);
156 state_->searchSize = (sigma1_*state_->searchSize <= alphaTmp && alphaTmp <= sigma2_*state_->searchSize)
157 ? alphaTmp : rhodec_*state_->searchSize;
158 //state_->searchSize *= rhodec_;
159 x.set(*state_->iterateVec);
160 x.axpy(state_->searchSize,*s);
162 ftrial = obj.value(x,tol); ls_nfval_++;
163 if (verbosity_ > 1) {
164 outStream << std::endl;
165 outStream << " Step size: " << state_->searchSize << std::endl;
166 outStream << " Trial objective value: " << ftrial << std::endl;
167 outStream << " Computed reduction: " << state_->value-ftrial << std::endl;
168 outStream << " Dot product of gradient and step: " << gs << std::endl;
169 outStream << " Sufficient decrease bound: " << -gs*state_->searchSize*c1_ << std::endl;
170 outStream << " Number of function evaluations: " << ls_nfval_ << std::endl;
171 }
172 }
173 state_->nfval += ls_nfval_;
174
175 // Compute norm of step
176 state_->stepVec->set(*s);
177 state_->stepVec->scale(state_->searchSize);
178 state_->snorm = state_->stepVec->norm();
179
180 // Update iterate
181 state_->iterateVec->set(x);
182
183 // Compute new value and gradient
184 state_->iter++;
185 state_->value = ftrial;
186 obj.update(x,UpdateType::Accept,state_->iter);
187 gold->set(*state_->gradientVec);
188 obj.gradient(*state_->gradientVec,x,tol); state_->ngrad++;
189 gp->set(state_->gradientVec->dual());
190
191 // Compute projected gradient norm
192 s->set(x); s->axpy(-one,*gp);
193 proj_->project(*s,outStream); state_->nproj++;
194 s->axpy(-one,x);
195 state_->gnorm = s->norm();
196
197 // Update secant
198 secant_->updateStorage(x,*state_->gradientVec,*gold,*state_->stepVec,state_->snorm,state_->iter);
199
200 // Update Output
201 if (verbosity_ > 0) writeOutput(outStream,writeHeader_);
202 }
204}
205
206template<typename Real>
207void QuasiNewtonAlgorithm<Real>::writeHeader( std::ostream& os ) const {
208 std::ios_base::fmtflags osFlags(os.flags());
209 if (verbosity_ > 1) {
210 os << std::string(114,'-') << std::endl;
211 os << "Line-Search Projected Quasi-Newton with " << secantName_ << " Hessian approximation";
212 os << " status output definitions" << std::endl << std::endl;
213 os << " iter - Number of iterates (steps taken)" << std::endl;
214 os << " value - Objective function value" << std::endl;
215 os << " gnorm - Norm of the gradient" << std::endl;
216 os << " snorm - Norm of the step (update to optimization vector)" << std::endl;
217 os << " alpha - Line search step length" << std::endl;
218 os << " #fval - Cumulative number of times the objective function was evaluated" << std::endl;
219 os << " #grad - Cumulative number of times the gradient was computed" << std::endl;
220 os << " #proj - 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 << "#fval";
233 os << std::setw(10) << std::left << "#grad";
234 os << std::setw(10) << std::left << "#proj";
235 os << std::setw(10) << std::left << "#ls_fval";
236 os << std::setw(10) << std::left << "sp_iter";
237 os << std::endl;
238 os.flags(osFlags);
239}
240
241template<typename Real>
242void QuasiNewtonAlgorithm<Real>::writeName( std::ostream& os ) const {
243 std::ios_base::fmtflags osFlags(os.flags());
244 os << std::endl << "Line-Search Projected Quasi-Newton (Type B, Bound Constraints)" << std::endl;
245 os.flags(osFlags);
246}
247
248template<typename Real>
249void QuasiNewtonAlgorithm<Real>::writeOutput( std::ostream& os, bool write_header ) const {
250 std::ios_base::fmtflags osFlags(os.flags());
251 os << std::scientific << std::setprecision(6);
252 if ( state_->iter == 0 ) writeName(os);
253 if ( write_header ) writeHeader(os);
254 if ( state_->iter == 0 ) {
255 os << " ";
256 os << std::setw(6) << std::left << state_->iter;
257 os << std::setw(15) << std::left << state_->value;
258 os << std::setw(15) << std::left << state_->gnorm;
259 os << std::setw(15) << std::left << "---";
260 os << std::setw(15) << std::left << "---";
261 os << std::setw(10) << std::left << state_->nfval;
262 os << std::setw(10) << std::left << state_->ngrad;
263 os << std::setw(10) << std::left << state_->nproj;
264 os << std::setw(10) << std::left << "---";
265 os << std::setw(10) << std::left << "---";
266 os << std::endl;
267 }
268 else {
269 os << " ";
270 os << std::setw(6) << std::left << state_->iter;
271 os << std::setw(15) << std::left << state_->value;
272 os << std::setw(15) << std::left << state_->gnorm;
273 os << std::setw(15) << std::left << state_->snorm;
274 os << std::setw(15) << std::left << state_->searchSize;
275 os << std::setw(10) << std::left << state_->nfval;
276 os << std::setw(10) << std::left << state_->ngrad;
277 os << std::setw(10) << std::left << state_->nproj;
278 os << std::setw(10) << std::left << ls_nfval_;
279 os << std::setw(10) << std::left << spgIter_;
280 os << std::endl;
281 }
282 os.flags(osFlags);
283}
284
285} // namespace TypeB
286} // namespace ROL
287
288#endif
virtual void initialize(const Vector< Real > &x)
Initialize temporary variables.
Provides the interface to apply upper and lower bound constraints.
Provides the interface to evaluate objective functions.
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.
Ptr< PolyhedralProjection< Real > > proj_
void initialize(const Vector< Real > &x, const Vector< Real > &g)
virtual void writeExitStatus(std::ostream &os) const
const Ptr< AlgorithmState< Real > > state_
const Ptr< CombinedStatusTest< Real > > status_
Real sigma2_
Upper safeguard for quadratic line search (default: 0.9).
Real c1_
Sufficient Decrease Parameter (default: 1e-4).
int maxit_
Maximum number of line search steps (default: 20).
Real sigma1_
Lower safeguard for quadratic line search (default: 0.1).
Ptr< Secant< Real > > secant_
Secant object (used for quasi-Newton).
void writeOutput(std::ostream &os, const bool write_header=false) const override
Print iterate status.
void writeHeader(std::ostream &os) const override
Print iterate header.
void writeName(std::ostream &os) const override
Print step name.
Real rhodec_
Backtracking rate (default: 0.5).
void run(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &obj, BoundConstraint< Real > &bnd, std::ostream &outStream=std::cout) override
Run algorithm on bound constrained problems (Type-B). This general interface supports the use of dual...
QuasiNewtonAlgorithm(ParameterList &list, const Ptr< Secant< Real > > &secant=nullPtr)
void initialize(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &obj, BoundConstraint< Real > &bnd, std::ostream &outStream=std::cout)
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