ROL
ROL_TypeU_TrustRegionAlgorithm_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_TRUSTREGIONALGORITHM_U_DEF_H
45 #define ROL_TRUSTREGIONALGORITHM_U_DEF_H
46 
48 
49 namespace ROL {
50 namespace TypeU {
51 
52 template<typename Real>
54  const Ptr<Secant<Real>> &secant )
55  : Algorithm<Real>(), esec_(SECANT_USERDEFINED) {
56  // Set status test
57  status_->reset();
58  status_->add(makePtr<StatusTest<Real>>(parlist));
59 
60  // Trust-Region Parameters
61  ParameterList &slist = parlist.sublist("Step");
62  ParameterList &trlist = slist.sublist("Trust Region");
63  state_->searchSize = trlist.get("Initial Radius", static_cast<Real>(-1));
64  delMax_ = trlist.get("Maximum Radius", ROL_INF<Real>());
65  eta0_ = trlist.get("Step Acceptance Threshold", static_cast<Real>(0.05));
66  eta1_ = trlist.get("Radius Shrinking Threshold", static_cast<Real>(0.05));
67  eta2_ = trlist.get("Radius Growing Threshold", static_cast<Real>(0.9));
68  gamma0_ = trlist.get("Radius Shrinking Rate (Negative rho)", static_cast<Real>(0.0625));
69  gamma1_ = trlist.get("Radius Shrinking Rate (Positive rho)", static_cast<Real>(0.25));
70  gamma2_ = trlist.get("Radius Growing Rate", static_cast<Real>(2.5));
71  TRsafe_ = trlist.get("Safeguard Size", static_cast<Real>(100.0));
72  eps_ = TRsafe_*ROL_EPSILON<Real>();
73  // Nonmonotone Information
74  NMstorage_ = trlist.get("Nonmonotone Storage Limit", 0);
75  useNM_ = (NMstorage_ <= 0 ? false : true);
76  // Inexactness Information
77  ParameterList &glist = parlist.sublist("General");
78  useInexact_.clear();
79  useInexact_.push_back(glist.get("Inexact Objective Function", false));
80  useInexact_.push_back(glist.get("Inexact Gradient", false));
81  useInexact_.push_back(glist.get("Inexact Hessian-Times-A-Vector", false));
82  // Trust-Region Inexactness Parameters
83  ParameterList &ilist = trlist.sublist("Inexact").sublist("Gradient");
84  scale0_ = ilist.get("Tolerance Scaling", static_cast<Real>(0.1));
85  scale1_ = ilist.get("Relative Tolerance", static_cast<Real>(2));
86  // Inexact Function Evaluation Information
87  ParameterList &vlist = trlist.sublist("Inexact").sublist("Value");
88  scale_ = vlist.get("Tolerance Scaling", static_cast<Real>(1.e-1));
89  omega_ = vlist.get("Exponent", static_cast<Real>(0.9));
90  force_ = vlist.get("Forcing Sequence Initial Value", static_cast<Real>(1.0));
91  updateIter_ = vlist.get("Forcing Sequence Update Frequency", static_cast<int>(10));
92  forceFactor_ = vlist.get("Forcing Sequence Reduction Factor", static_cast<Real>(0.1));
93  // Initialize Trust Region Subproblem Solver Object
94  etr_ = StringToETrustRegionU(trlist.get("Subproblem Solver", "Dogleg"));
95  solver_ = TrustRegionUFactory<Real>(parlist);
96  verbosity_ = glist.get("Output Level", 0);
97  // Secant Information
98  useSecantPrecond_ = glist.sublist("Secant").get("Use as Preconditioner", false);
99  useSecantHessVec_ = glist.sublist("Secant").get("Use as Hessian", false);
100  if (secant == nullPtr) {
101  esec_ = StringToESecant(glist.sublist("Secant").get("Type","Limited-Memory BFGS"));
102  }
103  // Initialize trust region model
104  model_ = makePtr<TrustRegionModel_U<Real>>(parlist,secant);
105  printHeader_ = verbosity_ > 2;
106 }
107 
108 template<typename Real>
110  const Vector<Real> &g,
111  Vector<Real> &Bg,
112  Objective<Real> &obj,
113  std::ostream &outStream) {
114  const Real zero(0);
115  // Initialize data
117  solver_->initialize(x,g);
118  model_->initialize(x,g);
119  // Update approximate gradient and approximate objective function.
120  Real ftol = static_cast<Real>(0.1)*ROL_OVERFLOW<Real>();
121  obj.update(x,UpdateType::Initial,state_->iter);
122  state_->value = obj.value(x,ftol);
123  state_->nfval++;
124  state_->snorm = ROL_INF<Real>();
125  state_->gnorm = ROL_INF<Real>();
126  Real Delta = state_->searchSize;
127  if (Delta <= zero) state_->searchSize = 1e2*x.norm();
128  computeGradient(x,obj,true);
129  // Check if inverse Hessian is implemented for dogleg methods
130  model_->validate(obj,x,g,etr_);
131  // Compute initial trust region radius if desired.
132  if ( Delta <= zero ) {
133  int nfval = 0;
134  state_->searchSize
135  = TRUtils::initialRadius<Real>(nfval,x,*state_->gradientVec,Bg,
136  state_->value,state_->gnorm,gtol_,obj,*model_,delMax_,
137  outStream,(verbosity_>1));
138  state_->nfval += nfval;
139  }
140 }
141 
142 template<typename Real>
144  Objective<Real> &obj,
145  Real pRed) {
146  const Real one(1);
147  Real tol(std::sqrt(ROL_EPSILON<Real>())), fval(0);
148  if ( useInexact_[0] ) {
149  if ( !(state_->iter%updateIter_) && (state_->iter != 0) ) {
150  force_ *= forceFactor_;
151  }
152  Real eta = static_cast<Real>(0.999)*std::min(eta1_,one-eta2_);
153  tol = scale_*std::pow(eta*std::min(pRed,force_),one/omega_);
154  state_->value = obj.value(*state_->iterateVec,tol);
155  state_->nfval++;
156  }
157  // Evaluate objective function at new iterate
158  obj.update(x,UpdateType::Trial);
159  fval = obj.value(x,tol);
160  state_->nfval++;
161  return fval;
162 }
163 
164 template<typename Real>
166  Objective<Real> &obj,
167  bool accept) {
168  if ( useInexact_[1] ) {
169  Real gtol0 = scale0_*state_->searchSize;
170  if (accept) gtol_ = gtol0 + static_cast<Real>(1);
171  else gtol0 = scale0_*std::min(state_->gnorm,state_->searchSize);
172  while ( gtol_ > gtol0 ) {
173  gtol_ = gtol0;
174  obj.gradient(*state_->gradientVec,x,gtol_); state_->ngrad++;
175  state_->gnorm = state_->gradientVec->norm();
176  gtol0 = scale0_*std::min(state_->gnorm,state_->searchSize);
177  }
178  }
179  else {
180  if (accept) {
181  gtol_ = std::sqrt(ROL_EPSILON<Real>());
182  obj.gradient(*state_->gradientVec,x,gtol_); state_->ngrad++;
183  state_->gnorm = state_->gradientVec->norm();
184  }
185  }
186 }
187 
188 template<typename Real>
190  const Vector<Real> &g,
191  Objective<Real> &obj,
192  std::ostream &outStream ) {
193  const Real zero(0);
194  // Initialize trust-region data
195  Real ftrial(0), pRed(0), rho(0);
196  Ptr<Vector<Real>> gvec = g.clone();
197  initialize(x,g,*gvec,obj,outStream);
198  // Initialize nonmonotone data
199  Real rhoNM(0), sigmac(0), sigmar(0);
200  Real fr(state_->value), fc(state_->value), fmin(state_->value);
201  TRUtils::ETRFlag TRflagNM;
202  int L(0);
203 
204  // Output
205  if (verbosity_ > 0) writeOutput(outStream,true);
206 
207  while (status_->check(*state_)) {
208  // Build trust-region model
209  model_->setData(obj,x,*state_->gradientVec,gtol_);
210  // Minimize trust-region model over trust-region constraint
211  pRed = zero;
212  SPflag_ = 0; SPiter_ = 0;
213  solver_->solve(*state_->stepVec,state_->snorm,pRed,SPflag_,SPiter_,
214  state_->searchSize,*model_);
215  // Compute trial objective function value
216  x.plus(*state_->stepVec);
217  ftrial = computeValue(x,obj,pRed);
218  // Compute ratio of actual and predicted reduction
219  TRflag_ = TRUtils::SUCCESS;
220  TRUtils::analyzeRatio<Real>(rho,TRflag_,state_->value,ftrial,pRed,eps_,outStream,verbosity_>1);
221  if (useNM_) {
222  TRUtils::analyzeRatio<Real>(rhoNM,TRflagNM,fr,ftrial,pRed+sigmar,eps_,outStream,verbosity_>1);
223  TRflag_ = (rho < rhoNM ? TRflagNM : TRflag_);
224  rho = (rho < rhoNM ? rhoNM : rho );
225  }
226  // Update algorithm state
227  state_->iter++;
228  // Accept/reject step and update trust region radius
229  if ((rho < eta0_ && TRflag_ == TRUtils::SUCCESS)
230  || (TRflag_ >= 2)) { // Step Rejected
231  x.set(*state_->iterateVec);
232  obj.update(x,UpdateType::Revert,state_->iter);
233  if (rho < zero && TRflag_ != TRUtils::TRNAN) {
234  // Negative reduction, interpolate to find new trust-region radius
235  state_->searchSize = TRUtils::interpolateRadius<Real>(*state_->gradientVec,*state_->stepVec,
236  state_->snorm,pRed,state_->value,ftrial,state_->searchSize,gamma0_,gamma1_,eta2_,
237  outStream,verbosity_>1);
238  }
239  else { // Shrink trust-region radius
240  state_->searchSize = gamma1_*std::min(state_->snorm,state_->searchSize);
241  }
242  computeGradient(x,obj,false);
243  }
244  else if ((rho >= eta0_ && TRflag_ != TRUtils::NPOSPREDNEG)
245  || (TRflag_ == TRUtils::POSPREDNEG)) { // Step Accepted
246  state_->iterateVec->set(x);
247  state_->value = ftrial;
248  obj.update(x,UpdateType::Accept,state_->iter);
249  if (useNM_) {
250  sigmac += pRed; sigmar += pRed;
251  if (ftrial < fmin) { fmin = ftrial; fc = fmin; sigmac = zero; L = 0; }
252  else {
253  L++;
254  if (ftrial > fc) { fc = ftrial; sigmac = zero; }
255  if (L == NMstorage_) { fr = fc; sigmar = sigmac; }
256  }
257  }
258  // Increase trust-region radius
259  if (rho >= eta2_) state_->searchSize = std::min(gamma2_*state_->searchSize, delMax_);
260  // Compute gradient at new iterate
261  gvec->set(*state_->gradientVec);
262  computeGradient(x,obj,true);
263  // Update secant information in trust-region model
264  model_->update(x,*state_->stepVec,*gvec,*state_->gradientVec,
265  state_->snorm,state_->iter);
266  }
267  // Update Output
268  if (verbosity_ > 0) writeOutput(outStream,printHeader_);
269  }
270  if (verbosity_ > 0) Algorithm<Real>::writeExitStatus(outStream);
271 }
272 
273 template<typename Real>
274 void TrustRegionAlgorithm<Real>::writeHeader( std::ostream& os ) const {
275  std::ios_base::fmtflags osFlags(os.flags());
276  if(verbosity_ > 1) {
277  os << std::string(114,'-') << std::endl;
278  os << "Trust-Region status output definitions" << std::endl << std::endl;
279  os << " iter - Number of iterates (steps taken)" << std::endl;
280  os << " value - Objective function value" << std::endl;
281  os << " gnorm - Norm of the gradient" << std::endl;
282  os << " snorm - Norm of the step (update to optimization vector)" << std::endl;
283  os << " delta - Trust-Region radius" << std::endl;
284  os << " #fval - Number of times the objective function was evaluated" << std::endl;
285  os << " #grad - Number of times the gradient was computed" << std::endl;
286  os << std::endl;
287  os << " tr_flag - Trust-Region flag" << std::endl;
288  for( int flag = TRUtils::SUCCESS; flag != TRUtils::UNDEFINED; ++flag ) {
289  os << " " << NumberToString(flag) << " - "
290  << TRUtils::ETRFlagToString(static_cast<TRUtils::ETRFlag>(flag)) << std::endl;
291  }
292  if( etr_ == TRUSTREGION_U_TRUNCATEDCG ) {
293  os << std::endl;
294  os << " iterCG - Number of Truncated CG iterations" << std::endl << std::endl;
295  os << " flagGC - Trust-Region Truncated CG flag" << std::endl;
296  for( int flag = CG_FLAG_SUCCESS; flag != CG_FLAG_UNDEFINED; ++flag ) {
297  os << " " << NumberToString(flag) << " - "
298  << ECGFlagToString(static_cast<ECGFlag>(flag)) << std::endl;
299  }
300  }
301  else if( etr_ == TRUSTREGION_U_SPG ) {
302  os << std::endl;
303  os << " iterCG - Number of spectral projected gradient iterations" << std::endl << std::endl;
304  os << " flagGC - Trust-Region spectral projected gradient flag" << std::endl;
305  }
306  os << std::string(114,'-') << std::endl;
307  }
308  os << " ";
309  os << std::setw(6) << std::left << "iter";
310  os << std::setw(15) << std::left << "value";
311  os << std::setw(15) << std::left << "gnorm";
312  os << std::setw(15) << std::left << "snorm";
313  os << std::setw(15) << std::left << "delta";
314  os << std::setw(10) << std::left << "#fval";
315  os << std::setw(10) << std::left << "#grad";
316  os << std::setw(10) << std::left << "tr_flag";
317  if ( etr_ == TRUSTREGION_U_TRUNCATEDCG ) {
318  os << std::setw(10) << std::left << "iterCG";
319  os << std::setw(10) << std::left << "flagCG";
320  }
321  else if (etr_ == TRUSTREGION_U_SPG) {
322  os << std::setw(10) << std::left << "iterSPG";
323  os << std::setw(10) << std::left << "flagSPG";
324  }
325  os << std::endl;
326  os.flags(osFlags);
327 }
328 
329 template<typename Real>
330 void TrustRegionAlgorithm<Real>::writeName( std::ostream& os ) const {
331  std::ios_base::fmtflags osFlags(os.flags());
332  os << std::endl << ETrustRegionUToString(etr_) << " Trust-Region Solver";
333  if ( useSecantPrecond_ || useSecantHessVec_ ) {
334  if ( useSecantPrecond_ && !useSecantHessVec_ ) {
335  os << " with " << ESecantToString(esec_) << " Preconditioning" << std::endl;
336  }
337  else if ( !useSecantPrecond_ && useSecantHessVec_ ) {
338  os << " with " << ESecantToString(esec_) << " Hessian Approximation" << std::endl;
339  }
340  else {
341  os << " with " << ESecantToString(esec_) << " Preconditioning and Hessian Approximation" << std::endl;
342  }
343  }
344  else {
345  os << std::endl;
346  }
347  os.flags(osFlags);
348 }
349 
350 template<typename Real>
351 void TrustRegionAlgorithm<Real>::writeOutput(std::ostream& os, bool print_header) const {
352  std::ios_base::fmtflags osFlags(os.flags());
353  os << std::scientific << std::setprecision(6);
354  if ( state_->iter == 0 ) {
355  writeName(os);
356  }
357  if ( print_header ) {
358  writeHeader(os);
359  }
360  if ( state_->iter == 0 ) {
361  os << " ";
362  os << std::setw(6) << std::left << state_->iter;
363  os << std::setw(15) << std::left << state_->value;
364  os << std::setw(15) << std::left << state_->gnorm;
365  os << std::setw(15) << std::left << "---";
366  os << std::setw(15) << std::left << state_->searchSize;
367  os << std::setw(10) << std::left << state_->nfval;
368  os << std::setw(10) << std::left << state_->ngrad;
369  os << std::setw(10) << std::left << "---";
370  if ( etr_ == TRUSTREGION_U_TRUNCATEDCG || etr_ == TRUSTREGION_U_SPG ) {
371  os << std::setw(10) << std::left << "---";
372  os << std::setw(10) << std::left << "---";
373  }
374  os << std::endl;
375  }
376  else {
377  os << " ";
378  os << std::setw(6) << std::left << state_->iter;
379  os << std::setw(15) << std::left << state_->value;
380  os << std::setw(15) << std::left << state_->gnorm;
381  os << std::setw(15) << std::left << state_->snorm;
382  os << std::setw(15) << std::left << state_->searchSize;
383  os << std::setw(10) << std::left << state_->nfval;
384  os << std::setw(10) << std::left << state_->ngrad;
385  os << std::setw(10) << std::left << TRflag_;
386  if ( etr_ == TRUSTREGION_U_TRUNCATEDCG || etr_ == TRUSTREGION_U_SPG ) {
387  os << std::setw(10) << std::left << SPiter_;
388  os << std::setw(10) << std::left << SPflag_;
389  }
390  os << std::endl;
391  }
392  os.flags(osFlags);
393 }
394 } // namespace TypeU
395 } // namespace ROL
396 
397 #endif
std::string ECGFlagToString(ECGFlag cgf)
Definition: ROL_Types.hpp:831
int verbosity_
Print additional information to screen if &gt; 0.
Provides the interface to evaluate objective functions.
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
void initialize(const Vector< Real > &x, const Vector< Real > &g, Vector< Real > &Bg, Objective< Real > &obj, std::ostream &outStream=std::cout)
virtual void plus(const Vector &x)=0
Compute , where .
const Ptr< AlgorithmState< Real > > state_
Real scale1_
Scale for inexact gradient computation.
ETrustRegionU StringToETrustRegionU(std::string s)
virtual Real value(const Vector< Real > &x, Real &tol)=0
Compute value.
TrustRegionAlgorithm(ParameterList &parlist, const Ptr< Secant< Real >> &secant=nullPtr)
void initialize(const Vector< Real > &x, const Vector< Real > &g)
void computeGradient(const Vector< Real > &x, Objective< Real > &obj, bool accept)
Compute gradient to iteratively satisfy inexactness condition.
Real delMax_
Maximum trust-region radius.
ESecant StringToESecant(std::string s)
Definition: ROL_Types.hpp:543
Ptr< TrustRegion_U< Real > > solver_
Container for trust-region solver object.
bool printHeader_
Print header at every iteration.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
Contains definitions of enums for trust region algorithms.
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()
Ptr< TrustRegionModel_U< Real > > model_
Container for trust-region model.
Real gamma0_
Radius decrease rate (negative rho).
void writeOutput(std::ostream &os, const bool print_header=false) const override
Print iterate status.
ETRFlag
Enumation of flags used by trust-region solvers.
virtual void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
std::string NumberToString(T Number)
Definition: ROL_Types.hpp:81
Provides an interface to run unconstrained optimization algorithms.
void run(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &obj, 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.
Provides interface for and implements limited-memory secant operators.
Definition: ROL_Secant.hpp:79
Provides an interface to check status of optimization algorithms.
std::string ETRFlagToString(ETRFlag trf)
Real scale0_
Scale for inexact gradient computation.
void writeHeader(std::ostream &os) const override
Print iterate header.
Real TRsafe_
Safeguard size for numerically evaluating ratio.
Real gamma1_
Radius decrease rate (positive rho).
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:209
virtual Real norm() const =0
Returns where .
virtual void writeExitStatus(std::ostream &os) const
ETrustRegionU etr_
Trust-region subproblem solver type.
const Ptr< CombinedStatusTest< Real > > status_
std::vector< bool > useInexact_
Flags for inexact (0) objective function, (1) gradient, (2) Hessian.
std::string ESecantToString(ESecant tr)
Definition: ROL_Types.hpp:495
Real eps_
Safeguard for numerically evaluating ratio.
Real computeValue(const Vector< Real > &x, Objective< Real > &obj, Real pRed)
std::string ETrustRegionUToString(ETrustRegionU tr)