ROL
ROL_TrustRegionUtilities.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_TRUSTREGIONUTILITIES_U_H
11#define ROL_TRUSTREGIONUTILITIES_U_H
12
14
15namespace ROL {
16namespace TRUtils {
17
37
38inline std::string ETRFlagToString(ETRFlag trf) {
39 std::string retString;
40 switch(trf) {
41 case SUCCESS:
42 retString = "Both actual and predicted reductions are positive (success)";
43 break;
44 case POSPREDNEG:
45 retString = "Actual reduction is positive and predicted reduction is negative (impossible)";
46 break;
47 case NPOSPREDPOS:
48 retString = "Actual reduction is nonpositive and predicted reduction is positive";
49 break;
50 case NPOSPREDNEG:
51 retString = "Actual reduction is nonpositive and predicted reduction is negative (impossible)";
52 break;
53 case TRNAN:
54 retString = "Actual and/or predicted reduction is a NaN";
55 break;
56 case QMINSUFDEC:
57 retString = "Subproblem solution did not produce sufficient decrease";
58 break;
59 default:
60 retString = "INVALID ETRFlag";
61 }
62 return retString;
63}
64
65template<typename Real>
66inline Real initialRadius(int &nfval,
67 const Vector<Real> &x,
68 const Vector<Real> &g,
69 Vector<Real> &Bg,
70 const Real fx,
71 const Real gnorm,
72 const Real gtol,
73 Objective<Real> &obj,
75 const Real delMax,
76 std::ostream &outStream,
77 const bool print = false) {
78 const Real zero(0), half(0.5), one(1), two(2), three(3), six(6);
79 const Real eps(ROL_EPSILON<Real>());
80 Real del(ROL_INF<Real>());
81 Real htol = gtol;
82 Ptr<Vector<Real>> xcp = x.clone();
83 model.setData(obj,x,g,htol);
84 model.hessVec(Bg,g.dual(),x,htol);
85 Real gBg = Bg.dot(g);
86 Real alpha = (gBg > eps ? gnorm*gnorm/gBg : one);
87 // Evaluate the objective function at the Cauchy point
88 xcp->set(g.dual());
89 xcp->scale(-alpha);
90 //Real gs = xcp->dot(g.dual());
91 Real gs = xcp->apply(g);
92 xcp->plus(x);
93 obj.update(*xcp,UpdateType::Temp);
94 Real ftol = static_cast<Real>(0.1)*ROL_OVERFLOW<Real>();
95 Real fnew = obj.value(*xcp,ftol); // MUST DO SOMETHING HERE WITH FTOL
96 nfval++;
97 // Perform cubic interpolation to determine initial trust region radius
98 Real a = fnew - fx - gs - half*alpha*alpha*gBg;
99 if ( std::abs(a) < eps ) {
100 // a = 0 implies the objective is quadratic in the negative gradient direction
101 del = std::min(alpha*gnorm,delMax);
102 }
103 else {
104 Real b = half*alpha*alpha*gBg;
105 Real c = gs;
106 if ( b*b-three*a*c > eps ) {
107 // There is at least one critical point
108 Real t1 = (-b-std::sqrt(b*b-three*a*c))/(three*a);
109 Real t2 = (-b+std::sqrt(b*b-three*a*c))/(three*a);
110 if ( six*a*t1 + two*b > zero ) {
111 // t1 is the minimizer
112 del = std::min(t1*alpha*gnorm,delMax);
113 }
114 else {
115 // t2 is the minimizer
116 del = std::min(t2*alpha*gnorm,delMax);
117 }
118 }
119 else {
120 del = std::min(alpha*gnorm,delMax);
121 }
122 }
123 if (del <= eps*gnorm) {
124 del = one;
125 }
127 if ( print ) {
128 outStream << " In TrustRegionUtilities::initialRadius" << std::endl;
129 outStream << " Initial radius: " << del << std::endl;
130 }
131 return del;
132}
133
134template<typename Real>
135inline void analyzeRatio(Real &rho,
136 ETRFlag &flag,
137 const Real fold,
138 const Real ftrial,
139 const Real pRed,
140 const Real epsi,
141 std::ostream &outStream = std::cout,
142 const bool print = false) {
143 const Real zero(0), one(1);
144 Real eps = epsi*std::max(one,fold);
145 Real aRed = fold - ftrial;
146 Real aRed_safe = aRed + eps, pRed_safe = pRed + eps;
147 if (((std::abs(aRed_safe) < epsi) && (std::abs(pRed_safe) < epsi)) || aRed == pRed) {
148 rho = one;
149 flag = SUCCESS;
150 }
151 else if ( std::isnan(aRed_safe) || std::isnan(pRed_safe) ) {
152 rho = -one;
153 flag = TRNAN;
154 }
155 else {
156 rho = aRed_safe/pRed_safe;
157 if (pRed_safe < zero && aRed_safe > zero) {
158 flag = POSPREDNEG;
159 }
160 else if (aRed_safe <= zero && pRed_safe > zero) {
161 flag = NPOSPREDPOS;
162 }
163 else if (aRed_safe <= zero && pRed_safe < zero) {
164 flag = NPOSPREDNEG;
165 }
166 else {
167 flag = SUCCESS;
168 }
169 }
170 if ( print ) {
171 outStream << " In TrustRegionUtilities::analyzeRatio" << std::endl;
172 outStream << " Current objective function value: " << fold << std::endl;
173 outStream << " New objective function value: " << ftrial << std::endl;
174 outStream << " Actual reduction: " << aRed << std::endl;
175 outStream << " Predicted reduction: " << pRed << std::endl;
176 outStream << " Safeguard: " << epsi << std::endl;
177 outStream << " Actual reduction with safeguard: " << aRed_safe << std::endl;
178 outStream << " Predicted reduction with safeguard: " << pRed_safe << std::endl;
179 outStream << " Ratio of actual and predicted reduction: " << rho << std::endl;
180 outStream << " Trust-region flag: " << flag << std::endl;
181 outStream << std::endl;
182 }
183}
184
185template<typename Real>
186inline Real interpolateRadius(const Vector<Real> &g,
187 const Vector<Real> &s,
188 const Real snorm,
189 const Real pRed,
190 const Real fold,
191 const Real ftrial,
192 const Real del,
193 const Real gamma0,
194 const Real gamma1,
195 const Real eta2,
196 std::ostream &outStream = std::cout,
197 const bool print = false) {
198 const Real one(1);
199 //Real gs = g.dot(s.dual());
200 Real gs = g.apply(s);
201 Real modelVal = fold - pRed;
202 Real theta = (one-eta2)*gs/((one-eta2)*(fold+gs)+eta2*modelVal-ftrial);
203 if ( print ) {
204 outStream << " In TrustRegionUtilities::interpolateRadius" << std::endl;
205 outStream << " Interpolation model value: " << modelVal << std::endl;
206 outStream << " Interpolation step length: " << theta << std::endl;
207 outStream << std::endl;
208 }
209 return std::min(gamma1*std::min(snorm,del),std::max(gamma0,theta)*del);
210}
211
212} // namespace TrustRegion
213} // namespace ROL
214
215
216#endif
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
Provides the interface to evaluate objective functions.
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 the interface to evaluate trust-region model functions.
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &s, Real &tol) override
Apply Hessian approximation to vector.
virtual void setData(Objective< Real > &obj, const Vector< Real > &x, const Vector< Real > &g, Real &tol)
Defines the linear algebra or vector space interface.
virtual Real apply(const Vector< Real > &x) const
Apply to a dual vector. This is equivalent to the call .
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis,...
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual Real dot(const Vector &x) const =0
Compute where .
void analyzeRatio(Real &rho, ETRFlag &flag, const Real fold, const Real ftrial, const Real pRed, const Real epsi, std::ostream &outStream=std::cout, const bool print=false)
std::string ETRFlagToString(ETRFlag trf)
Real interpolateRadius(const Vector< Real > &g, const Vector< Real > &s, const Real snorm, const Real pRed, const Real fold, const Real ftrial, const Real del, const Real gamma0, const Real gamma1, const Real eta2, std::ostream &outStream=std::cout, const bool print=false)
Real initialRadius(int &nfval, const Vector< Real > &x, const Vector< Real > &g, Vector< Real > &Bg, const Real fx, const Real gnorm, const Real gtol, Objective< Real > &obj, TrustRegionModel_U< Real > &model, const Real delMax, std::ostream &outStream, const bool print=false)