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