ROL
ROL_RiddersProjection_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_RIDDERSPROJECTION_DEF_H
46 #define ROL_RIDDERSPROJECTION_DEF_H
47 
48 namespace ROL {
49 
50 template<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 
71 template<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 
98 template<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::RiddersProjection : 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 
134 template<typename Real>
135 void RiddersProjection<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 
149 template<typename Real>
151  return xprim_->dot(x) + b_;
152 }
153 
154 template<typename Real>
155 void RiddersProjection<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 
161 template<typename Real>
162 void RiddersProjection<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 Ridders' 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 << " Ridders' 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 mid";
289  stream << std::setw(15) << std::left << "res mid";
290  stream << std::setw(15) << std::left << "lam low";
291  stream << std::setw(15) << std::left << "res low";
292  stream << std::setw(15) << std::left << "lam up";
293  stream << std::setw(15) << std::left << "res up";
294  stream << std::endl;
295  }
296  const Real half(0.5); //, bsize = std::abs(lamUpper-lamLower);
297  Real lamMid(0), resMid(0);
298  for (cnt = 0; cnt < maxit_; cnt++) {
299  // Exit if residual or bracket length are sufficiently small
300  //if (std::abs(lamUpper-lamLower) < ltol_*bsize) break;
301  if (std::abs(lamUpper-lamLower) < ltol_) break;
302 
303  lamMid = half*(lamUpper+lamLower);
304  update_primal(*xnew_,x,lamMid);
305  resMid = residual(*xnew_);
306  if (std::abs(resMid) <= rtol) {
307  res = resMid;
308  lam = lamMid;
309  break;
310  }
311 
312  lam = lamMid-(lamMid-lamLower)*resMid/std::sqrt(resMid*resMid-resLower*resUpper);
313  update_primal(*xnew_,x,lam);
314  res = residual(*xnew_);
315  if (std::abs(res) <= rtol) break;
316  else {
317  if (res < -rtol) {
318  if (resMid < -rtol) {
319  resLower = (lam < lamMid ? resMid : res);
320  lamLower = (lam < lamMid ? lamMid : lam);
321  }
322  else {
323  resLower = res;
324  lamLower = lam;
325  resUpper = resMid;
326  lamUpper = lamMid;
327  }
328  }
329  else {
330  if (resMid < -rtol) {
331  resLower = resMid;
332  lamLower = lamMid;
333  resUpper = res;
334  lamUpper = lam;
335  }
336  else {
337  resUpper = (lam < lamMid ? res : resMid);
338  lamUpper = (lam < lamMid ? lam : lamMid);
339  }
340  }
341  }
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 << lamMid;
350  stream << std::setw(15) << std::left << resMid;
351  stream << std::setw(15) << std::left << lamLower;
352  stream << std::setw(15) << std::left << resLower;
353  stream << std::setw(15) << std::left << lamUpper;
354  stream << std::setw(15) << std::left << resUpper;
355  stream << std::endl;
356  }
357  }
358  if (verbosity_ > 2) {
359  if (cnt < maxit_) {
360  stream << " ";
361  stream << std::setw(6) << std::left << cnt;
362  stream << std::setw(15) << std::left << rtol;
363  stream << std::setw(15) << std::left << lam;
364  stream << std::setw(15) << std::left << res;
365  stream << std::setw(15) << std::left << lamMid;
366  stream << std::setw(15) << std::left << resMid;
367  stream << std::setw(15) << std::left << lamLower;
368  stream << std::setw(15) << std::left << resLower;
369  stream << std::setw(15) << std::left << lamUpper;
370  stream << std::setw(15) << std::left << resUpper;
371  stream << std::endl;
372  }
373  stream << std::endl;
374  }
375  // Return projection
376  x.set(*xnew_);
377  if (std::abs(res) > rtol ) {
378  //throw Exception::NotImplemented(">>> ROL::PolyhedralProjection::project : Projection failed!");
379  stream << ">>> ROL::PolyhedralProjection::project : Projection may be inaccurate! rnorm = ";
380  stream << std::abs(res) << " rtol = " << rtol << std::endl;
381  }
382  stream.flags(streamFlags);
383 }
384 
385 } // namespace ROL
386 
387 #endif
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 residual(const Vector< Real > &x) const
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
void project_df(Vector< Real > &x, Real &lam, Real &dlam, std::ostream &stream=std::cout) const
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
void project(Vector< Real > &x, std::ostream &stream=std::cout) override
RiddersProjection(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)
Provides the interface to apply upper and lower bound constraints.
void update_primal(Vector< Real > &y, const Vector< Real > &x, const Real lam) const
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:209
Real ROL_EPSILON(void)
Platform-dependent machine epsilon.
Definition: ROL_Types.hpp:91
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)
Defines the general constraint operator interface.