ROL
ROL_TypeP_SpectralGradientAlgorithm_Def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 #ifndef ROL_TYPEP_SPECTRALGRADIENTALGORITHM_DEF_HPP
45 #define ROL_TYPEP_SPECTRALGRADIENTALGORITHM_DEF_HPP
46 
47 namespace ROL {
48 namespace TypeP {
49 
50 template<typename Real>
52  // Set status test
53  status_->reset();
54  status_->add(makePtr<StatusTest<Real>>(list));
55 
56  // Parse parameter list
57  ParameterList &lslist = list.sublist("Step").sublist("Spectral Gradient");
58  maxit_ = lslist.get("Function Evaluation Limit", 20);
59  lambda_ = lslist.get("Initial Spectral Step Size", -1.0);
60  lambdaMin_ = lslist.get("Minimum Spectral Step Size", 1e-8);
61  lambdaMax_ = lslist.get("Maximum Spectral Step Size", 1e8);
62  sigma1_ = lslist.get("Lower Step Size Safeguard", 0.1);
63  sigma2_ = lslist.get("Upper Step Size Safeguard", 0.9);
64  rhodec_ = lslist.get("Backtracking Rate", 1e-1);
65  gamma_ = lslist.get("Sufficient Decrease Tolerance", 1e-4);
66  maxSize_ = lslist.get("Maximum Storage Size", 10);
67  initProx_ = lslist.get("Apply Prox to Initial Guess", false);
68  t0_ = list.sublist("Status Test").get("Gradient Scale" , 1.0);
69  verbosity_ = list.sublist("General").get("Output Level", 0);
70  writeHeader_ = verbosity_ > 2;
71 }
72 
73 template<typename Real>
75  const Vector<Real> &g,
76  Objective<Real> &sobj,
77  Objective<Real> &nobj,
78  Vector<Real> &px,
79  Vector<Real> &dg,
80  std::ostream &outStream) {
81  const Real zero(0);
82  Real ftol = std::sqrt(ROL_EPSILON<Real>());
83  // Initialize data
85  // Update approximate gradient and approximate objective function.
86  if (initProx_) {
87  nobj.prox(*state_->iterateVec,x,t0_,ftol); state_->nprox++;
88  x.set(*state_->iterateVec);
89  }
90  sobj.update(x,UpdateType::Initial,state_->iter);
91  state_->svalue = sobj.value(x,ftol); state_->nsval++;
92  nobj.update(x,UpdateType::Initial,state_->iter);
93  state_->nvalue = nobj.value(x,ftol); state_->nnval++;
94  state_->value = state_->svalue + state_->nvalue;
95  sobj.gradient(*state_->gradientVec,x,ftol); state_->ngrad++;
96  dg.set(state_->gradientVec->dual());
97  if (lambda_ <= zero && state_->gnorm != zero)
98  lambda_ = std::max(lambdaMin_,std::min(t0_,lambdaMax_));
99  pgstep(*state_->iterateVec, *state_->stepVec, nobj, x, dg, lambda_, ftol);
100  state_->snorm = state_->stepVec->norm();
101  state_->gnorm = state_->snorm / lambda_;
102 }
103 
104 template<typename Real>
106  const Vector<Real> &g,
107  Objective<Real> &sobj,
108  Objective<Real> &nobj,
109  std::ostream &outStream ) {
110  const Real half(0.5), one(1), eps(std::sqrt(ROL_EPSILON<Real>()));
111  // Initialize trust-region data
112  Ptr<Vector<Real>> s = x.clone(), px = x.clone(), dg = x.clone(), y = g.clone(), xmin = x.clone();
113  initialize(x,g,sobj,nobj,*s,*dg,outStream);
114  Real strial(0), ntrial(0), Ftrial(0), Fmin(0), Fmax(0), Qk(0), alpha(1), rhoTmp(1);
115  Real gs(0), ys(0), snorm(state_->snorm), ss(0), tol(std::sqrt(ROL_EPSILON<Real>()));
116  int ls_nfval = 0;
117  std::deque<Real> Fqueue; Fqueue.push_back(state_->value);
118 
119  Fmin = state_->value;
120  xmin->set(x);
121 
122  // Output
123  if (verbosity_ > 0) writeOutput(outStream, true);
124 
125  // Iterate spectral projected gradient
126  while (status_->check(*state_)) {
127  // Nonmonotone Linesearch
128  ls_nfval = 0;
129  sobj.update(*state_->iterateVec,UpdateType::Trial);
130  strial = sobj.value(*state_->iterateVec,tol);
131  nobj.update(*state_->iterateVec,UpdateType::Trial);
132  ntrial = nobj.value(*state_->iterateVec,tol);
133  Ftrial = strial + ntrial;
134  ls_nfval++;
135  alpha = one;
136  Fmax = *std::max_element(Fqueue.begin(),Fqueue.end());
137  gs = state_->gradientVec->apply(*state_->stepVec);
138  Qk = gs + ntrial - state_->nvalue;
139  if (verbosity_ > 1) {
140  outStream << " In TypeP::SpectralGradientAlgorithm Line Search" << std::endl;
141  outStream << " Step size: " << alpha << std::endl;
142  outStream << " Trial objective value: " << Ftrial << std::endl;
143  outStream << " Max stored objective value: " << Fmax << std::endl;
144  outStream << " Computed reduction: " << Fmax-Ftrial << std::endl;
145  outStream << " Dot product of gradient and step: " << Qk << std::endl;
146  outStream << " Sufficient decrease bound: " << -Qk*gamma_ << std::endl;
147  outStream << " Number of function evaluations: " << ls_nfval << std::endl;
148  }
149  while (Ftrial > Fmax + gamma_*Qk && ls_nfval < maxit_) {
150  // Compute reduction factor by minimizing 1D quadratic model
151  rhoTmp = std::min(one,-half*Qk/(strial-state_->svalue-alpha*gs));
152  // Safeguard step size selection with back tracking
153  alpha = ((sigma1_ <= rhoTmp && rhoTmp <= sigma2_) ? rhoTmp : rhodec_)*alpha;
154  // Update iterate vector
155  state_->iterateVec->set(x);
156  state_->iterateVec->axpy(alpha,*state_->stepVec);
157  // Recompute objective function values
158  sobj.update(*state_->iterateVec,UpdateType::Trial);
159  strial = sobj.value(*state_->iterateVec,tol);
160  nobj.update(*state_->iterateVec,UpdateType::Trial);
161  ntrial = nobj.value(*state_->iterateVec,tol);
162  Ftrial = strial + ntrial;
163  ls_nfval++;
164  Qk = alpha * gs + ntrial - state_->nvalue;
165  if (verbosity_ > 1) {
166  outStream << " In TypeP::SpectralGradientAlgorithm: Line Search" << std::endl;
167  outStream << " Step size: " << alpha << std::endl;
168  outStream << " Trial objective value: " << Ftrial << std::endl;
169  outStream << " Max stored objective value: " << Fmax << std::endl;
170  outStream << " Computed reduction: " << Fmax-Ftrial << std::endl;
171  outStream << " Dot product of gradient and step: " << Qk << std::endl;
172  outStream << " Sufficient decrease bound: " << -Qk*gamma_ << std::endl;
173  outStream << " Number of function evaluations: " << ls_nfval << std::endl;
174  }
175  }
176  state_->nsval += ls_nfval;
177  state_->nnval += ls_nfval;
178  if (static_cast<int>(Fqueue.size()) == maxSize_) Fqueue.pop_front();
179  Fqueue.push_back(Ftrial);
180 
181  // Update state
182  state_->iter++;
183  state_->value = Ftrial;
184  state_->svalue = strial;
185  state_->nvalue = ntrial;
186  state_->searchSize = alpha;
187  state_->snorm = alpha * snorm;
188  state_->stepVec->scale(alpha);
189  x.set(*state_->iterateVec);
190  sobj.update(x,UpdateType::Accept,state_->iter);
191  nobj.update(x,UpdateType::Accept,state_->iter);
192 
193  // Store the best iterate
194  if (state_->value <= Fmin) {
195  Fmin = state_->value;
196  xmin->set(x);
197  }
198 
199  // Compute spectral step length
200  y->set(*state_->gradientVec);
201  y->scale(-one);
202  sobj.gradient(*state_->gradientVec,x,tol); state_->ngrad++;
203  dg->set(state_->gradientVec->dual());
204  y->plus(*state_->gradientVec);
205  ys = y->apply(*state_->stepVec);
206  ss = state_->snorm * state_->snorm;
207  lambda_ = (ys<=eps*state_->snorm ? lambdaMax_ : std::max(lambdaMin_,std::min(ss/ys,lambdaMax_)));
208 
209  // Compute spectral proximal gradient step
210  pgstep(*state_->iterateVec, *state_->stepVec, nobj, x, *dg, lambda_, tol);
211  snorm = state_->stepVec->norm();
212  state_->gnorm = snorm / lambda_;
213 
214  // Update Output
215  if (verbosity_ > 0) writeOutput(outStream,writeHeader_);
216  }
217  x.set(*xmin);
218  state_->value = Fmin;
219  if (verbosity_ > 0) TypeP::Algorithm<Real>::writeExitStatus(outStream);
220 }
221 
222 template<typename Real>
223 void SpectralGradientAlgorithm<Real>::writeHeader( std::ostream& os ) const {
224  std::ios_base::fmtflags osFlags(os.flags());
225  if (verbosity_ > 1) {
226  os << std::string(109,'-') << std::endl;
227  os << "Spectral proximal gradient with nonmonotone line search";
228  os << " status output definitions" << std::endl << std::endl;
229  os << " iter - Number of iterates (steps taken)" << std::endl;
230  os << " value - Objective function value" << std::endl;
231  os << " gnorm - Norm of the proximal gradient with parameter lambda" << std::endl;
232  os << " snorm - Norm of the step (update to optimization vector)" << std::endl;
233  os << " alpha - Line search step length" << std::endl;
234  os << " lambda - Spectral step length" << std::endl;
235  os << " #sval - Cumulative number of times the smooth objective function was evaluated" << std::endl;
236  os << " #nval - Cumulative number of times the nonsmooth objective function was evaluated" << std::endl;
237  os << " #grad - Cumulative number of times the gradient was computed" << std::endl;
238  os << " #prox - Cumulative number of times the proximal operator was computed" << std::endl;
239  os << std::string(109,'-') << std::endl;
240  }
241 
242  os << " ";
243  os << std::setw(6) << std::left << "iter";
244  os << std::setw(15) << std::left << "value";
245  os << std::setw(15) << std::left << "gnorm";
246  os << std::setw(15) << std::left << "snorm";
247  os << std::setw(15) << std::left << "alpha";
248  os << std::setw(15) << std::left << "lambda";
249  os << std::setw(10) << std::left << "#sval";
250  os << std::setw(10) << std::left << "#nval";
251  os << std::setw(10) << std::left << "#grad";
252  os << std::setw(10) << std::left << "#nprox";
253  os << std::endl;
254  os.flags(osFlags);
255 }
256 
257 template<typename Real>
258 void SpectralGradientAlgorithm<Real>::writeName( std::ostream& os ) const {
259  std::ios_base::fmtflags osFlags(os.flags());
260  os << std::endl << "Spectral Proximal Gradient with Nonmonotone Line Search (Type P)" << std::endl;
261  os.flags(osFlags);
262 }
263 
264 template<typename Real>
265 void SpectralGradientAlgorithm<Real>::writeOutput( std::ostream& os, bool write_header ) const {
266  std::ios_base::fmtflags osFlags(os.flags());
267  os << std::scientific << std::setprecision(6);
268  if ( state_->iter == 0 ) writeName(os);
269  if ( write_header ) writeHeader(os);
270  if ( state_->iter == 0 ) {
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 << "---";
276  os << std::setw(15) << std::left << "---";
277  os << std::setw(15) << std::left << lambda_;
278  os << std::setw(10) << std::left << state_->nsval;
279  os << std::setw(10) << std::left << state_->nnval;
280  os << std::setw(10) << std::left << state_->ngrad;
281  os << std::setw(10) << std::left << state_->nprox;
282  os << std::endl;
283  }
284  else {
285  os << " ";
286  os << std::setw(6) << std::left << state_->iter;
287  os << std::setw(15) << std::left << state_->value;
288  os << std::setw(15) << std::left << state_->gnorm;
289  os << std::setw(15) << std::left << state_->snorm;
290  os << std::setw(15) << std::left << state_->searchSize;
291  os << std::setw(15) << std::left << lambda_;
292  os << std::setw(10) << std::left << state_->nsval;
293  os << std::setw(10) << std::left << state_->nnval;
294  os << std::setw(10) << std::left << state_->ngrad;
295  os << std::setw(10) << std::left << state_->nprox;
296  os << std::endl;
297  }
298  os.flags(osFlags);
299 }
300 
301 } // namespace TypeP
302 } // namespace ROL
303 
304 #endif
Provides the interface to evaluate objective functions.
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual Real value(const Vector< Real > &x, Real &tol)=0
Compute value.
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)
virtual void prox(Vector< Real > &Pv, const Vector< Real > &v, Real t, Real &tol)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
virtual void update(const Vector< Real > &x, UpdateType type, int iter=-1)
Update objective function.
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
void writeName(std::ostream &os) const override
Print step name.
virtual void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
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...
Provides an interface to check status of optimization algorithms.
void writeOutput(std::ostream &os, bool write_header=false) const override
Print iterate status.
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:209
void writeHeader(std::ostream &os) const override
Print iterate header.
virtual void writeExitStatus(std::ostream &os) const
void initialize(const Vector< Real > &x, const Vector< Real > &g)