10 #ifndef ROL_BRENTSPROJECTION_DEF_H
11 #define ROL_BRENTSPROJECTION_DEF_H
15 template<
typename Real>
26 DEFAULT_maxit_ (5000),
27 DEFAULT_verbosity_ (0),
28 atol_ (DEFAULT_atol_),
29 rtol_ (DEFAULT_rtol_),
30 ltol_ (DEFAULT_ltol_),
31 maxit_ (DEFAULT_maxit_),
32 verbosity_ (DEFAULT_verbosity_) {
36 template<
typename Real>
48 DEFAULT_maxit_ (5000),
49 DEFAULT_verbosity_ (0),
50 atol_ (DEFAULT_atol_),
51 rtol_ (DEFAULT_rtol_),
52 ltol_ (DEFAULT_ltol_),
53 maxit_ (DEFAULT_maxit_),
54 verbosity_ (DEFAULT_verbosity_) {
55 atol_ = list.sublist(
"General").sublist(
"Polyhedral Projection").get(
"Absolute Tolerance",
DEFAULT_atol_);
56 rtol_ = list.sublist(
"General").sublist(
"Polyhedral Projection").get(
"Relative Tolerance",
DEFAULT_rtol_);
57 ltol_ = list.sublist(
"General").sublist(
"Polyhedral Projection").get(
"Multiplier Tolerance",
DEFAULT_ltol_);
58 maxit_ = list.sublist(
"General").sublist(
"Polyhedral Projection").get(
"Iteration Limit",
DEFAULT_maxit_);
63 template<
typename Real>
71 ROL_TEST_FOR_EXCEPTION(dim_!=1,std::logic_error,
72 ">>> ROL::BrentsProjection : The range of the linear constraint must be one dimensional!");
73 xnew_ = xprim.
clone();
75 mul1_ =
static_cast<Real
>(0);
76 dlam1_ =
static_cast<Real
>(2);
78 Real tol(std::sqrt(ROL_EPSILON<Real>()));
81 con_->value(*res_,*xprim_,tol);
82 b_ = res_->dot(*res_->basis(0));
83 mul_->setScalar(static_cast<Real>(1));
84 con_->applyAdjointJacobian(*xdual_,*mul_,xprim,tol);
85 xprim_->set(xdual_->dual());
86 cdot_ = xprim_->dot(*xprim_);
91 Real resl = ROL_INF<Real>(), resu = ROL_INF<Real>();
92 if (bnd_->isLowerActivated()) resl = residual(*bnd_->getLowerBound());
93 if (bnd_->isUpperActivated()) resu = residual(*bnd_->getUpperBound());
94 Real res0 = std::max(resl,resu);
95 if (res0 < atol_) res0 =
static_cast<Real
>(1);
96 ctol_ = std::min(atol_,rtol_*res0);
99 template<
typename Real>
101 if (con_ == nullPtr) {
105 mul1_ = -residual(x)/cdot_;
107 dlam1_ =
static_cast<Real
>(2);
109 project_df(x, mul1_, dlam1_, stream);
110 mul_->setScalar(mul1_);
114 template<
typename Real>
116 return xprim_->dot(x) + b_;
119 template<
typename Real>
126 template<
typename Real>
128 const Real
zero(0), one(1), c1(0.1);
129 Real lamLower(0), lamUpper(0), res(0), resLower(0), resUpper(0), s(0);
133 update_primal(*xnew_,x,lam);
134 res = residual(*xnew_);
139 std::ios_base::fmtflags streamFlags(stream.flags());
140 if (verbosity_ > 2) {
141 stream << std::scientific << std::setprecision(6);
143 stream <<
" Polyhedral Projection using Brents' Algorithm" << std::endl;
144 stream <<
" Bracketing Phase" << std::endl;
151 update_primal(*xnew_,x,lam);
152 res = residual(*xnew_);
153 if (verbosity_ > 2) {
155 stream << std::setw(6) << std::left <<
"iter";
156 stream << std::setw(15) << std::left <<
"lam";
157 stream << std::setw(15) << std::left <<
"res";
158 stream << std::setw(15) << std::left <<
"lower lam";
159 stream << std::setw(15) << std::left <<
"lower res";
162 stream << std::setw(6) << std::left << cnt;
163 stream << std::setw(15) << std::left << lam;
164 stream << std::setw(15) << std::left << res;
165 stream << std::setw(15) << std::left << lamLower;
166 stream << std::setw(15) << std::left << resLower;
169 while ( res <
zero && std::abs(res) > rtol && cnt < maxit_ ) {
170 s = std::max(resLower/res-one,c1);
175 update_primal(*xnew_,x,lam);
176 res = residual(*xnew_);
178 if (verbosity_ > 2) {
180 stream << std::setw(6) << std::left << cnt;
181 stream << std::setw(15) << std::left << lam;
182 stream << std::setw(15) << std::left << res;
183 stream << std::setw(15) << std::left << lamLower;
184 stream << std::setw(15) << std::left << resLower;
195 update_primal(*xnew_,x,lam);
196 res = residual(*xnew_);
197 if (verbosity_ > 2) {
199 stream << std::setw(6) << std::left <<
"iter";
200 stream << std::setw(15) << std::left <<
"lam";
201 stream << std::setw(15) << std::left <<
"res";
202 stream << std::setw(15) << std::left <<
"upper lam";
203 stream << std::setw(15) << std::left <<
"upper res";
206 stream << std::setw(6) << std::left << cnt;
207 stream << std::setw(15) << std::left << lam;
208 stream << std::setw(15) << std::left << res;
209 stream << std::setw(15) << std::left << lamUpper;
210 stream << std::setw(15) << std::left << resUpper;
213 while ( res >
zero && std::abs(res) > rtol && cnt < maxit_ ) {
214 s = std::max(resUpper/res-one,c1);
219 update_primal(*xnew_,x,lam);
220 res = residual(*xnew_);
222 if (verbosity_ > 2) {
224 stream << std::setw(6) << std::left << cnt;
225 stream << std::setw(15) << std::left << lam;
226 stream << std::setw(15) << std::left << res;
227 stream << std::setw(15) << std::left << lamUpper;
228 stream << std::setw(15) << std::left << resUpper;
235 if (verbosity_ > 2) {
236 stream <<
" Bracket: ";
237 stream << std::setw(15) << std::left << lamLower;
238 stream << std::setw(15) << std::left << lamUpper;
245 if (verbosity_ > 2) {
247 stream <<
" Brents' Phase" << std::endl;
249 stream << std::setw(6) << std::left <<
"iter";
250 stream << std::setw(15) << std::left <<
"rtol";
251 stream << std::setw(15) << std::left <<
"lam";
252 stream << std::setw(15) << std::left <<
"res";
253 stream << std::setw(15) << std::left <<
"lam low";
254 stream << std::setw(15) << std::left <<
"res low";
255 stream << std::setw(15) << std::left <<
"lam up";
256 stream << std::setw(15) << std::left <<
"res up";
259 const Real half(0.5), two(2), three(3);
260 const Real eps(ROL_EPSILON<Real>()), tol0(rtol);
261 Real d1(1), d2(1), tol(1);
262 Real p(0), q(0), r(0), m(0);
263 lam = lamUpper; res = resUpper;
264 update_primal(*xnew_,x,lamUpper);
265 for (cnt = 0; cnt < maxit_; cnt++) {
267 lam = lamLower; res = resLower;
268 d1 = lamUpper-lamLower; d2 = d1;
270 if (std::abs(res) < std::abs(resUpper)) {
271 lamLower = lamUpper; lamUpper = lam; lam = lamLower;
272 resLower = resUpper; resUpper = res; res = resLower;
274 tol = two*eps*std::abs(lamUpper) + half*tol0;
275 m = half*(lam - lamUpper);
276 if (std::abs(m) <= tol || std::abs(resUpper) <= rtol)
break;
277 if (std::abs(d2) < tol || std::abs(resLower) <= std::abs(resUpper)) {
281 s = resUpper/resLower;
282 if (lamLower == lam) {
289 p = s*(two*m*q*(q-r)-(lamUpper-lamLower)*(r-one));
290 q = (q-one)*(r-one)*(s-one);
292 if (p >
zero) q = -q;
294 if (two*p < three*m*q-std::abs(tol*q) && p < std::abs(half*d2*q)) {
301 lamLower = lamUpper; resLower = resUpper;
302 if (std::abs(d1) > tol) lamUpper += d1;
303 else if (m >
zero) lamUpper += tol;
304 else lamUpper -= tol;
305 update_primal(*xnew_,x,lamUpper);
306 resUpper = residual(*xnew_);
308 if (verbosity_ > 2) {
310 stream << std::setw(6) << std::left << cnt;
311 stream << std::setw(15) << std::left << rtol;
312 stream << std::setw(15) << std::left << lam;
313 stream << std::setw(15) << std::left << res;
314 stream << std::setw(15) << std::left << lamLower;
315 stream << std::setw(15) << std::left << resLower;
316 stream << std::setw(15) << std::left << lamUpper;
317 stream << std::setw(15) << std::left << resUpper;
321 if (verbosity_ > 2) {
324 stream << std::setw(6) << std::left << cnt;
325 stream << std::setw(15) << std::left << rtol;
326 stream << std::setw(15) << std::left << lam;
327 stream << std::setw(15) << std::left << res;
328 stream << std::setw(15) << std::left << lamLower;
329 stream << std::setw(15) << std::left << resLower;
330 stream << std::setw(15) << std::left << lamUpper;
331 stream << std::setw(15) << std::left << resUpper;
339 if (std::abs(res) > rtol ) {
341 stream <<
">>> ROL::PolyhedralProjection::project : Projection may be inaccurate! rnorm = ";
342 stream << std::abs(res) <<
" rtol = " << rtol << std::endl;
344 stream.flags(streamFlags);
Real residual(const Vector< Real > &x) const
void project_df(Vector< Real > &x, Real &lam, Real &dlam, std::ostream &stream=std::cout) 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)
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual int dimension() const
Return dimension of the vector space.
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Defines the linear algebra or vector space interface.
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
void project(Vector< Real > &x, std::ostream &stream=std::cout) override
Provides the interface to apply upper and lower bound constraints.
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)
virtual void set(const Vector &x)
Set where .
Real ROL_EPSILON(void)
Platform-dependent machine epsilon.
Defines the general constraint operator interface.
void update_primal(Vector< Real > &y, const Vector< Real > &x, const Real lam) const