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