ROL
ROL_BrentsProjection_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
45#ifndef ROL_BRENTSPROJECTION_DEF_H
46#define ROL_BRENTSPROJECTION_DEF_H
47
48namespace ROL {
49
50template<typename Real>
52 const Vector<Real> &xdual,
53 const Ptr<BoundConstraint<Real>> &bnd,
54 const Ptr<Constraint<Real>> &con,
55 const Vector<Real> &mul,
56 const Vector<Real> &res)
57 : PolyhedralProjection<Real>(xprim,xdual,bnd,con,mul,res),
58 DEFAULT_atol_ (std::sqrt(ROL_EPSILON<Real>()*std::sqrt(ROL_EPSILON<Real>()))),
59 DEFAULT_rtol_ (std::sqrt(ROL_EPSILON<Real>())),
60 DEFAULT_ltol_ (ROL_EPSILON<Real>()),
61 DEFAULT_maxit_ (5000),
62 DEFAULT_verbosity_ (0),
63 atol_ (DEFAULT_atol_),
64 rtol_ (DEFAULT_rtol_),
65 ltol_ (DEFAULT_ltol_),
66 maxit_ (DEFAULT_maxit_),
67 verbosity_ (DEFAULT_verbosity_) {
68 initialize(xprim,xdual,bnd,con,mul,res);
69}
70
71template<typename Real>
73 const Vector<Real> &xdual,
74 const Ptr<BoundConstraint<Real>> &bnd,
75 const Ptr<Constraint<Real>> &con,
76 const Vector<Real> &mul,
77 const Vector<Real> &res,
78 ParameterList &list)
79 : PolyhedralProjection<Real>(xprim,xdual,bnd,con,mul,res),
80 DEFAULT_atol_ (std::sqrt(ROL_EPSILON<Real>()*std::sqrt(ROL_EPSILON<Real>()))),
81 DEFAULT_rtol_ (std::sqrt(ROL_EPSILON<Real>())),
82 DEFAULT_ltol_ (ROL_EPSILON<Real>()),
83 DEFAULT_maxit_ (5000),
84 DEFAULT_verbosity_ (0),
85 atol_ (DEFAULT_atol_),
86 rtol_ (DEFAULT_rtol_),
87 ltol_ (DEFAULT_ltol_),
88 maxit_ (DEFAULT_maxit_),
89 verbosity_ (DEFAULT_verbosity_) {
90 atol_ = list.sublist("General").sublist("Polyhedral Projection").get("Absolute Tolerance", DEFAULT_atol_);
91 rtol_ = list.sublist("General").sublist("Polyhedral Projection").get("Relative Tolerance", DEFAULT_rtol_);
92 ltol_ = list.sublist("General").sublist("Polyhedral Projection").get("Multiplier Tolerance", DEFAULT_ltol_);
93 maxit_ = list.sublist("General").sublist("Polyhedral Projection").get("Iteration Limit", DEFAULT_maxit_);
94 verbosity_ = list.sublist("General").get("Output Level", DEFAULT_verbosity_);
95 initialize(xprim,xdual,bnd,con,mul,res);
96}
97
98template<typename Real>
100 const Vector<Real> &xdual,
101 const Ptr<BoundConstraint<Real>> &bnd,
102 const Ptr<Constraint<Real>> &con,
103 const Vector<Real> &mul,
104 const Vector<Real> &res) {
105 dim_ = mul.dimension();
106 ROL_TEST_FOR_EXCEPTION(dim_!=1,std::logic_error,
107 ">>> ROL::BrentsProjection : The range of the linear constraint must be one dimensional!");
108 xnew_ = xprim.clone();
109 Px_ = xprim.clone();
110 mul1_ = static_cast<Real>(0);
111 dlam1_ = static_cast<Real>(2);
112 // con.value(x) = xprim_->dot(x) + b_
113 Real tol(std::sqrt(ROL_EPSILON<Real>()));
114 xprim_->zero();
115 con_->update(*xprim_,UpdateType::Temp);
116 con_->value(*res_,*xprim_,tol);
117 b_ = res_->dot(*res_->basis(0));
118 mul_->setScalar(static_cast<Real>(1));
119 con_->applyAdjointJacobian(*xdual_,*mul_,xprim,tol);
120 xprim_->set(xdual_->dual());
121 cdot_ = xprim_->dot(*xprim_);
122 // Set tolerance
123 //xnew_->zero();
124 //bnd_->project(*xnew_);
125 //Real res0 = std::abs(residual(*xnew_));
126 Real resl = ROL_INF<Real>(), resu = ROL_INF<Real>();
127 if (bnd_->isLowerActivated()) resl = residual(*bnd_->getLowerBound());
128 if (bnd_->isUpperActivated()) resu = residual(*bnd_->getUpperBound());
129 Real res0 = std::max(resl,resu);
130 if (res0 < atol_) res0 = static_cast<Real>(1);
131 ctol_ = std::min(atol_,rtol_*res0);
132}
133
134template<typename Real>
135void BrentsProjection<Real>::project(Vector<Real> &x, std::ostream &stream) {
136 if (con_ == nullPtr) {
137 bnd_->project(x);
138 }
139 else {
140 mul1_ = -residual(x)/cdot_;
141 //mul1_ = static_cast<Real>(0);
142 dlam1_ = static_cast<Real>(2);
143 //dlam1_ = static_cast<Real>(1)+std::abs(mul1_);
144 project_df(x, mul1_, dlam1_, stream);
145 mul_->setScalar(mul1_);
146 }
147}
148
149template<typename Real>
151 return xprim_->dot(x) + b_;
152}
153
154template<typename Real>
155void BrentsProjection<Real>::update_primal(Vector<Real> &y, const Vector<Real> &x, const Real lam) const {
156 y.set(x);
157 y.axpy(lam,*xprim_);
158 bnd_->project(y);
159}
160
161template<typename Real>
162void BrentsProjection<Real>::project_df(Vector<Real> &x, Real &lam, Real &dlam, std::ostream &stream) const {
163 const Real zero(0), one(1), c1(0.1);
164 Real lamLower(0), lamUpper(0), res(0), resLower(0), resUpper(0), s(0);
165 Real rtol = ctol_;
166 int cnt(0);
167 // Compute initial residual
168 update_primal(*xnew_,x,lam);
169 res = residual(*xnew_);
170 if (res == zero) {
171 x.set(*xnew_);
172 return;
173 }
174 std::ios_base::fmtflags streamFlags(stream.flags());
175 if (verbosity_ > 2) {
176 stream << std::scientific << std::setprecision(6);
177 stream << std::endl;
178 stream << " Polyhedral Projection using Brents' Algorithm" << std::endl;
179 stream << " Bracketing Phase" << std::endl;
180 }
181 // Bracketing phase
182 if ( res < zero ) {
183 lamLower = lam;
184 resLower = res;
185 lam += dlam;
186 update_primal(*xnew_,x,lam);
187 res = residual(*xnew_);
188 if (verbosity_ > 2) {
189 stream << " ";
190 stream << std::setw(6) << std::left << "iter";
191 stream << std::setw(15) << std::left << "lam";
192 stream << std::setw(15) << std::left << "res";
193 stream << std::setw(15) << std::left << "lower lam";
194 stream << std::setw(15) << std::left << "lower res";
195 stream << std::endl;
196 stream << " ";
197 stream << std::setw(6) << std::left << cnt;
198 stream << std::setw(15) << std::left << lam;
199 stream << std::setw(15) << std::left << res;
200 stream << std::setw(15) << std::left << lamLower;
201 stream << std::setw(15) << std::left << resLower;
202 stream << std::endl;
203 }
204 while ( res < zero && std::abs(res) > rtol && cnt < maxit_ ) {
205 s = std::max(resLower/res-one,c1);
206 dlam += dlam/s;
207 lamLower = lam;
208 resLower = res;
209 lam += dlam;
210 update_primal(*xnew_,x,lam);
211 res = residual(*xnew_);
212 cnt++;
213 if (verbosity_ > 2) {
214 stream << " ";
215 stream << std::setw(6) << std::left << cnt;
216 stream << std::setw(15) << std::left << lam;
217 stream << std::setw(15) << std::left << res;
218 stream << std::setw(15) << std::left << lamLower;
219 stream << std::setw(15) << std::left << resLower;
220 stream << std::endl;
221 }
222 }
223 lamUpper = lam;
224 resUpper = res;
225 }
226 else {
227 lamUpper = lam;
228 resUpper = res;
229 lam -= dlam;
230 update_primal(*xnew_,x,lam);
231 res = residual(*xnew_);
232 if (verbosity_ > 2) {
233 stream << " ";
234 stream << std::setw(6) << std::left << "iter";
235 stream << std::setw(15) << std::left << "lam";
236 stream << std::setw(15) << std::left << "res";
237 stream << std::setw(15) << std::left << "upper lam";
238 stream << std::setw(15) << std::left << "upper res";
239 stream << std::endl;
240 stream << " ";
241 stream << std::setw(6) << std::left << cnt;
242 stream << std::setw(15) << std::left << lam;
243 stream << std::setw(15) << std::left << res;
244 stream << std::setw(15) << std::left << lamUpper;
245 stream << std::setw(15) << std::left << resUpper;
246 stream << std::endl;
247 }
248 while ( res > zero && std::abs(res) > rtol && cnt < maxit_ ) {
249 s = std::max(resUpper/res-one,c1);
250 dlam += dlam/s;
251 lamUpper = lam;
252 resUpper = res;
253 lam -= dlam;
254 update_primal(*xnew_,x,lam);
255 res = residual(*xnew_);
256 cnt++;
257 if (verbosity_ > 2) {
258 stream << " ";
259 stream << std::setw(6) << std::left << cnt;
260 stream << std::setw(15) << std::left << lam;
261 stream << std::setw(15) << std::left << res;
262 stream << std::setw(15) << std::left << lamUpper;
263 stream << std::setw(15) << std::left << resUpper;
264 stream << std::endl;
265 }
266 }
267 lamLower = lam;
268 resLower = res;
269 }
270 if (verbosity_ > 2) {
271 stream << " Bracket: ";
272 stream << std::setw(15) << std::left << lamLower;
273 stream << std::setw(15) << std::left << lamUpper;
274 stream << std::endl;
275 }
276
277 // Secant phase
278 //rtol = ctol_*std::max(one,std::min(std::abs(resLower),std::abs(resUpper)));
279 cnt = 0;
280 if (verbosity_ > 2) {
281 stream << std::endl;
282 stream << " Brents' Phase" << std::endl;
283 stream << " ";
284 stream << std::setw(6) << std::left << "iter";
285 stream << std::setw(15) << std::left << "rtol";
286 stream << std::setw(15) << std::left << "lam";
287 stream << std::setw(15) << std::left << "res";
288 stream << std::setw(15) << std::left << "lam low";
289 stream << std::setw(15) << std::left << "res low";
290 stream << std::setw(15) << std::left << "lam up";
291 stream << std::setw(15) << std::left << "res up";
292 stream << std::endl;
293 }
294 const Real half(0.5), two(2), three(3);
295 const Real eps(ROL_EPSILON<Real>()), tol0(rtol); // tol0(1e1*eps);
296 Real d1(1), d2(1), tol(1);
297 Real p(0), q(0), r(0), m(0);
298 lam = lamUpper; res = resUpper;
299 update_primal(*xnew_,x,lamUpper);
300 for (cnt = 0; cnt < maxit_; cnt++) {
301 if ((resUpper > zero && res > zero) || (resUpper <= zero && res <= zero)) {
302 lam = lamLower; res = resLower;
303 d1 = lamUpper-lamLower; d2 = d1;
304 }
305 if (std::abs(res) < std::abs(resUpper)) {
306 lamLower = lamUpper; lamUpper = lam; lam = lamLower;
307 resLower = resUpper; resUpper = res; res = resLower;
308 }
309 tol = two*eps*std::abs(lamUpper) + half*tol0;
310 m = half*(lam - lamUpper);
311 if (std::abs(m) <= tol || std::abs(resUpper) <= rtol) break;
312 if (std::abs(d2) < tol || std::abs(resLower) <= std::abs(resUpper)) {
313 d1 = m; d2 = d1;
314 }
315 else {
316 s = resUpper/resLower;
317 if (lamLower == lam) {
318 p = two*m*s;
319 q = one-s;
320 }
321 else {
322 q = resLower/res;
323 r = resUpper/res;
324 p = s*(two*m*q*(q-r)-(lamUpper-lamLower)*(r-one));
325 q = (q-one)*(r-one)*(s-one);
326 }
327 if (p > zero) q = -q;
328 else p = -p;
329 if (two*p < three*m*q-std::abs(tol*q) && p < std::abs(half*d2*q)) {
330 d2 = d1; d1 = p/q;
331 }
332 else {
333 d1 = m; d2 = d1;
334 }
335 }
336 lamLower = lamUpper; resLower = resUpper;
337 if (std::abs(d1) > tol) lamUpper += d1;
338 else if (m > zero) lamUpper += tol;
339 else lamUpper -= tol;
340 update_primal(*xnew_,x,lamUpper);
341 resUpper = residual(*xnew_);
342
343 if (verbosity_ > 2) {
344 stream << " ";
345 stream << std::setw(6) << std::left << cnt;
346 stream << std::setw(15) << std::left << rtol;
347 stream << std::setw(15) << std::left << lam;
348 stream << std::setw(15) << std::left << res;
349 stream << std::setw(15) << std::left << lamLower;
350 stream << std::setw(15) << std::left << resLower;
351 stream << std::setw(15) << std::left << lamUpper;
352 stream << std::setw(15) << std::left << resUpper;
353 stream << std::endl;
354 }
355 }
356 if (verbosity_ > 2) {
357 if (cnt < maxit_) {
358 stream << " ";
359 stream << std::setw(6) << std::left << cnt;
360 stream << std::setw(15) << std::left << rtol;
361 stream << std::setw(15) << std::left << lam;
362 stream << std::setw(15) << std::left << res;
363 stream << std::setw(15) << std::left << lamLower;
364 stream << std::setw(15) << std::left << resLower;
365 stream << std::setw(15) << std::left << lamUpper;
366 stream << std::setw(15) << std::left << resUpper;
367 stream << std::endl;
368 }
369 stream << std::endl;
370 }
371 // Return projection
372 res = resUpper;
373 x.set(*xnew_);
374 if (std::abs(res) > rtol ) {
375 //throw Exception::NotImplemented(">>> ROL::PolyhedralProjection::project : Projection failed!");
376 stream << ">>> ROL::PolyhedralProjection::project : Projection may be inaccurate! rnorm = ";
377 stream << std::abs(res) << " rtol = " << rtol << std::endl;
378 }
379 stream.flags(streamFlags);
380}
381
382} // namespace ROL
383
384#endif
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
Provides the interface to apply upper and lower bound constraints.
void project_df(Vector< Real > &x, Real &lam, Real &dlam, std::ostream &stream=std::cout) const
void project(Vector< Real > &x, std::ostream &stream=std::cout) override
void initialize(const Vector< Real > &xprim, const Vector< Real > &xdual, const Ptr< BoundConstraint< Real > > &bnd, const Ptr< Constraint< Real > > &con, const Vector< Real > &mul, const Vector< Real > &res)
void update_primal(Vector< Real > &y, const Vector< Real > &x, const Real lam) const
Real residual(const Vector< Real > &x) const
BrentsProjection(const Vector< Real > &xprim, const Vector< Real > &xdual, const Ptr< BoundConstraint< Real > > &bnd, const Ptr< Constraint< Real > > &con, const Vector< Real > &mul, const Vector< Real > &res)
Defines the general constraint operator interface.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:84
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:209
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual int dimension() const
Return dimension of the vector space.
Definition: ROL_Vector.hpp:196
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:153
Real ROL_EPSILON(void)
Platform-dependent machine epsilon.
Definition: ROL_Types.hpp:91