ROL
ROL_TypeP_SpectralGradientAlgorithm_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_SPECTRALGRADIENTALGORITHM_DEF_HPP
11#define ROL_TYPEP_SPECTRALGRADIENTALGORITHM_DEF_HPP
12
13namespace ROL {
14namespace TypeP {
15
16template<typename Real>
18 // Set status test
19 status_->reset();
20 status_->add(makePtr<StatusTest<Real>>(list));
21
22 // Parse parameter list
23 ParameterList &lslist = list.sublist("Step").sublist("Spectral Gradient");
24 maxit_ = lslist.get("Function Evaluation Limit", 20);
25 lambda_ = lslist.get("Initial Spectral Step Size", -1.0);
26 lambdaMin_ = lslist.get("Minimum Spectral Step Size", 1e-8);
27 lambdaMax_ = lslist.get("Maximum Spectral Step Size", 1e8);
28 sigma1_ = lslist.get("Lower Step Size Safeguard", 0.1);
29 sigma2_ = lslist.get("Upper Step Size Safeguard", 0.9);
30 rhodec_ = lslist.get("Backtracking Rate", 1e-1);
31 gamma_ = lslist.get("Sufficient Decrease Tolerance", 1e-4);
32 maxSize_ = lslist.get("Maximum Storage Size", 10);
33 initProx_ = lslist.get("Apply Prox to Initial Guess", false);
34 t0_ = list.sublist("Status Test").get("Gradient Scale" , 1.0);
35 verbosity_ = list.sublist("General").get("Output Level", 0);
37}
38
39template<typename Real>
41 const Vector<Real> &g,
42 Objective<Real> &sobj,
43 Objective<Real> &nobj,
44 Vector<Real> &px,
45 Vector<Real> &dg,
46 std::ostream &outStream) {
47 const Real zero(0);
48 Real ftol = std::sqrt(ROL_EPSILON<Real>());
49 // Initialize data
51 // Update approximate gradient and approximate objective function.
52 if (initProx_) {
53 nobj.prox(*state_->iterateVec,x,t0_,ftol); state_->nprox++;
54 x.set(*state_->iterateVec);
55 }
56 sobj.update(x,UpdateType::Initial,state_->iter);
57 state_->svalue = sobj.value(x,ftol); state_->nsval++;
58 nobj.update(x,UpdateType::Initial,state_->iter);
59 state_->nvalue = nobj.value(x,ftol); state_->nnval++;
60 state_->value = state_->svalue + state_->nvalue;
61 sobj.gradient(*state_->gradientVec,x,ftol); state_->ngrad++;
62 dg.set(state_->gradientVec->dual());
63 if (lambda_ <= zero && state_->gnorm != zero)
64 lambda_ = std::max(lambdaMin_,std::min(t0_,lambdaMax_));
65 pgstep(*state_->iterateVec, *state_->stepVec, nobj, x, dg, lambda_, ftol);
66 state_->snorm = state_->stepVec->norm();
67 state_->gnorm = state_->snorm / lambda_;
68}
69
70template<typename Real>
72 const Vector<Real> &g,
73 Objective<Real> &sobj,
74 Objective<Real> &nobj,
75 std::ostream &outStream ) {
76 const Real half(0.5), one(1), eps(std::sqrt(ROL_EPSILON<Real>()));
77 // Initialize trust-region data
78 Ptr<Vector<Real>> s = x.clone(), px = x.clone(), dg = x.clone(), y = g.clone(), xmin = x.clone();
79 initialize(x,g,sobj,nobj,*s,*dg,outStream);
80 Real strial(0), ntrial(0), Ftrial(0), Fmin(0), Fmax(0), Qk(0), alpha(1), rhoTmp(1);
81 Real gs(0), ys(0), snorm(state_->snorm), ss(0), tol(std::sqrt(ROL_EPSILON<Real>()));
82 int ls_nfval = 0;
83 std::deque<Real> Fqueue; Fqueue.push_back(state_->value);
84
85 Fmin = state_->value;
86 xmin->set(x);
87
88 // Output
89 if (verbosity_ > 0) writeOutput(outStream, true);
90
91 // Iterate spectral projected gradient
92 while (status_->check(*state_)) {
93 // Nonmonotone Linesearch
94 ls_nfval = 0;
95 sobj.update(*state_->iterateVec,UpdateType::Trial);
96 strial = sobj.value(*state_->iterateVec,tol);
97 nobj.update(*state_->iterateVec,UpdateType::Trial);
98 ntrial = nobj.value(*state_->iterateVec,tol);
99 Ftrial = strial + ntrial;
100 ls_nfval++;
101 alpha = one;
102 Fmax = *std::max_element(Fqueue.begin(),Fqueue.end());
103 gs = state_->gradientVec->apply(*state_->stepVec);
104 Qk = gs + ntrial - state_->nvalue;
105 if (verbosity_ > 1) {
106 outStream << " In TypeP::SpectralGradientAlgorithm Line Search" << std::endl;
107 outStream << " Step size: " << alpha << std::endl;
108 outStream << " Trial objective value: " << Ftrial << std::endl;
109 outStream << " Max stored objective value: " << Fmax << std::endl;
110 outStream << " Computed reduction: " << Fmax-Ftrial << std::endl;
111 outStream << " Dot product of gradient and step: " << Qk << std::endl;
112 outStream << " Sufficient decrease bound: " << -Qk*gamma_ << std::endl;
113 outStream << " Number of function evaluations: " << ls_nfval << std::endl;
114 }
115 while (Ftrial > Fmax + gamma_*Qk && ls_nfval < maxit_) {
116 // Compute reduction factor by minimizing 1D quadratic model
117 rhoTmp = std::min(one,-half*Qk/(strial-state_->svalue-alpha*gs));
118 // Safeguard step size selection with back tracking
119 alpha = ((sigma1_ <= rhoTmp && rhoTmp <= sigma2_) ? rhoTmp : rhodec_)*alpha;
120 // Update iterate vector
121 state_->iterateVec->set(x);
122 state_->iterateVec->axpy(alpha,*state_->stepVec);
123 // Recompute objective function values
124 sobj.update(*state_->iterateVec,UpdateType::Trial);
125 strial = sobj.value(*state_->iterateVec,tol);
126 nobj.update(*state_->iterateVec,UpdateType::Trial);
127 ntrial = nobj.value(*state_->iterateVec,tol);
128 Ftrial = strial + ntrial;
129 ls_nfval++;
130 Qk = alpha * gs + ntrial - state_->nvalue;
131 if (verbosity_ > 1) {
132 outStream << " In TypeP::SpectralGradientAlgorithm: Line Search" << std::endl;
133 outStream << " Step size: " << alpha << std::endl;
134 outStream << " Trial objective value: " << Ftrial << std::endl;
135 outStream << " Max stored objective value: " << Fmax << std::endl;
136 outStream << " Computed reduction: " << Fmax-Ftrial << std::endl;
137 outStream << " Dot product of gradient and step: " << Qk << std::endl;
138 outStream << " Sufficient decrease bound: " << -Qk*gamma_ << std::endl;
139 outStream << " Number of function evaluations: " << ls_nfval << std::endl;
140 }
141 }
142 state_->nsval += ls_nfval;
143 state_->nnval += ls_nfval;
144 if (static_cast<int>(Fqueue.size()) == maxSize_) Fqueue.pop_front();
145 Fqueue.push_back(Ftrial);
146
147 // Update state
148 state_->iter++;
149 state_->value = Ftrial;
150 state_->svalue = strial;
151 state_->nvalue = ntrial;
152 state_->searchSize = alpha;
153 state_->snorm = alpha * snorm;
154 state_->stepVec->scale(alpha);
155 x.set(*state_->iterateVec);
156 sobj.update(x,UpdateType::Accept,state_->iter);
157 nobj.update(x,UpdateType::Accept,state_->iter);
158
159 // Store the best iterate
160 if (state_->value <= Fmin) {
161 Fmin = state_->value;
162 xmin->set(x);
163 }
164
165 // Compute spectral step length
166 y->set(*state_->gradientVec);
167 y->scale(-one);
168 sobj.gradient(*state_->gradientVec,x,tol); state_->ngrad++;
169 dg->set(state_->gradientVec->dual());
170 y->plus(*state_->gradientVec);
171 ys = y->apply(*state_->stepVec);
172 ss = state_->snorm * state_->snorm;
173 lambda_ = (ys<=eps*state_->snorm ? lambdaMax_ : std::max(lambdaMin_,std::min(ss/ys,lambdaMax_)));
174
175 // Compute spectral proximal gradient step
176 pgstep(*state_->iterateVec, *state_->stepVec, nobj, x, *dg, lambda_, tol);
177 snorm = state_->stepVec->norm();
178 state_->gnorm = snorm / lambda_;
179
180 // Update Output
181 if (verbosity_ > 0) writeOutput(outStream,writeHeader_);
182 }
183 x.set(*xmin);
184 state_->value = Fmin;
186}
187
188template<typename Real>
189void SpectralGradientAlgorithm<Real>::writeHeader( std::ostream& os ) const {
190 std::ios_base::fmtflags osFlags(os.flags());
191 if (verbosity_ > 1) {
192 os << std::string(109,'-') << std::endl;
193 os << "Spectral proximal gradient with nonmonotone line search";
194 os << " status output definitions" << std::endl << std::endl;
195 os << " iter - Number of iterates (steps taken)" << std::endl;
196 os << " value - Objective function value" << std::endl;
197 os << " gnorm - Norm of the proximal gradient with parameter lambda" << std::endl;
198 os << " snorm - Norm of the step (update to optimization vector)" << std::endl;
199 os << " alpha - Line search step length" << std::endl;
200 os << " lambda - Spectral step length" << std::endl;
201 os << " #sval - Cumulative number of times the smooth objective function was evaluated" << std::endl;
202 os << " #nval - Cumulative number of times the nonsmooth objective function was evaluated" << std::endl;
203 os << " #grad - Cumulative number of times the gradient was computed" << std::endl;
204 os << " #prox - Cumulative number of times the proximal operator was computed" << std::endl;
205 os << std::string(109,'-') << std::endl;
206 }
207
208 os << " ";
209 os << std::setw(6) << std::left << "iter";
210 os << std::setw(15) << std::left << "value";
211 os << std::setw(15) << std::left << "gnorm";
212 os << std::setw(15) << std::left << "snorm";
213 os << std::setw(15) << std::left << "alpha";
214 os << std::setw(15) << std::left << "lambda";
215 os << std::setw(10) << std::left << "#sval";
216 os << std::setw(10) << std::left << "#nval";
217 os << std::setw(10) << std::left << "#grad";
218 os << std::setw(10) << std::left << "#nprox";
219 os << std::endl;
220 os.flags(osFlags);
221}
222
223template<typename Real>
224void SpectralGradientAlgorithm<Real>::writeName( std::ostream& os ) const {
225 std::ios_base::fmtflags osFlags(os.flags());
226 os << std::endl << "Spectral Proximal Gradient with Nonmonotone Line Search (Type P)" << std::endl;
227 os.flags(osFlags);
228}
229
230template<typename Real>
231void SpectralGradientAlgorithm<Real>::writeOutput( std::ostream& os, bool write_header ) const {
232 std::ios_base::fmtflags osFlags(os.flags());
233 os << std::scientific << std::setprecision(6);
234 if ( state_->iter == 0 ) writeName(os);
235 if ( write_header ) writeHeader(os);
236 if ( state_->iter == 0 ) {
237 os << " ";
238 os << std::setw(6) << std::left << state_->iter;
239 os << std::setw(15) << std::left << state_->value;
240 os << std::setw(15) << std::left << state_->gnorm;
241 os << std::setw(15) << std::left << "---";
242 os << std::setw(15) << std::left << "---";
243 os << std::setw(15) << std::left << lambda_;
244 os << std::setw(10) << std::left << state_->nsval;
245 os << std::setw(10) << std::left << state_->nnval;
246 os << std::setw(10) << std::left << state_->ngrad;
247 os << std::setw(10) << std::left << state_->nprox;
248 os << std::endl;
249 }
250 else {
251 os << " ";
252 os << std::setw(6) << std::left << state_->iter;
253 os << std::setw(15) << std::left << state_->value;
254 os << std::setw(15) << std::left << state_->gnorm;
255 os << std::setw(15) << std::left << state_->snorm;
256 os << std::setw(15) << std::left << state_->searchSize;
257 os << std::setw(15) << std::left << lambda_;
258 os << std::setw(10) << std::left << state_->nsval;
259 os << std::setw(10) << std::left << state_->nnval;
260 os << std::setw(10) << std::left << state_->ngrad;
261 os << std::setw(10) << std::left << state_->nprox;
262 os << std::endl;
263 }
264 os.flags(osFlags);
265}
266
267} // namespace TypeP
268} // namespace ROL
269
270#endif
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0 zero)()
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 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)
void writeOutput(std::ostream &os, bool write_header=false) const override
Print iterate status.
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.
void initialize(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &sobj, Objective< Real > &nobj, Vector< Real > &px, Vector< Real > &dg, std::ostream &outStream=std::cout)
void writeHeader(std::ostream &os) const override
Print iterate header.
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.
Real ROL_EPSILON(void)
Platform-dependent machine epsilon.
Definition ROL_Types.hpp:57