ROL
function/test_16.cpp
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 
16 #include "ROL_Bounds.hpp"
17 #include "ROL_ScaledStdVector.hpp"
18 #include "ROL_StdConstraint.hpp"
19 
20 #include "ROL_Stream.hpp"
21 #include "Teuchos_GlobalMPISession.hpp"
22 
23 template<typename Real>
24 class con2d : public ROL::StdConstraint<Real> {
25 public:
26  void value(std::vector<Real> &c, const std::vector<Real> &x, Real &tol) {
27  c[0] = x[0]+x[1]-static_cast<Real>(1);
28  }
29  void applyJacobian(std::vector<Real> &jv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol) {
30  jv[0] = v[0]+v[1];
31  }
32  void applyAdjointJacobian(std::vector<Real> &ajv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol) {
33  ajv[0] = v[0];
34  ajv[1] = v[0];
35  }
36 };
37 
38 typedef double RealT;
39 
40 int main(int argc, char *argv[]) {
41 
42  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
43 
44  // This little trick lets us print to std::cout only if a
45  // (dummy) command-line argument is provided.
46  int iprint = argc - 1;
47  ROL::Ptr<std::ostream> outStream;
48  ROL::nullstream bhs; // outputs nothing
49  if (iprint > 0)
50  outStream = ROL::makePtrFromRef(std::cout);
51  else
52  outStream = ROL::makePtrFromRef(bhs);
53 
54  int errorFlag = 0;
55 
56  try {
57  const RealT zero(0), half(0.5), one(1);
58  RealT tol = std::sqrt(ROL::ROL_EPSILON<RealT>());
59  RealT err(0);
60  ROL::Ptr<con2d<RealT>> con = ROL::makePtr<con2d<RealT>>();
62  ROL::ParameterList list;
63  list.sublist("General").set("Output Level",2);
64  list.sublist("General").sublist("Polyhedral Projection").set("Type","Dai-Fletcher");
65  //list.sublist("General").sublist("Polyhedral Projection").set("Type","Ridders");
66  //list.sublist("General").sublist("Polyhedral Projection").set("Type","Brents");
67  //list.sublist("General").sublist("Polyhedral Projection").set("Type","Dykstra");
68  //list.sublist("General").sublist("Polyhedral Projection").set("Type","Semismooth Newton");
69  //list.sublist("General").sublist("Polyhedral Projection").set("Type","Douglas-Rachford");
70 
71  ROL::Ptr<std::vector<RealT>> yptr = ROL::makePtr<std::vector<RealT>>(2);
72  (*yptr)[0] = static_cast<RealT>(10)*(static_cast<RealT>(rand())/static_cast<RealT>(RAND_MAX)-half);
73  (*yptr)[1] = static_cast<RealT>(rand())/static_cast<RealT>(RAND_MAX);
74  //(*yptr)[1] = static_cast<RealT>(10)*(static_cast<RealT>(rand())/static_cast<RealT>(RAND_MAX)-half);
75  ROL::StdVector<RealT> y(yptr);
76 
77  ROL::Ptr<std::vector<RealT>> xptr = ROL::makePtr<std::vector<RealT>>(2);
78  (*xptr)[0] = (*yptr)[0];
79  (*xptr)[1] = (*yptr)[1];
80  ROL::StdVector<RealT> x(xptr);
81 
82  ROL::Ptr<std::vector<RealT>> Pxptr = ROL::makePtr<std::vector<RealT>>(2,0.0);
83  (*Pxptr)[0] = (*yptr)[0];
84  (*Pxptr)[1] = (*yptr)[1];
85  ROL::StdVector<RealT> Px(Pxptr);
86 
87  ROL::Ptr<ROL::Vector<RealT>> l0 = x.clone(); l0->setScalar(static_cast<RealT>(0));
88  ROL::Ptr<ROL::Vector<RealT>> u0 = x.clone(); u0->setScalar(static_cast<RealT>(1));
89  ROL::Ptr<ROL::Bounds<RealT>> bnd0 = ROL::makePtr<ROL::Bounds<RealT>>(l0,u0);
90 
91  ROL::Ptr<ROL::PolyhedralProjection<RealT>> pp0 = ROL::PolyhedralProjectionFactory<RealT>(x,x.dual(),bnd0,con,r,r.dual(),list);
92  pp0->project(Px,*outStream);
93 
94  ROL::Ptr<std::vector<RealT>> x0ptr = ROL::makePtr<std::vector<RealT>>(2);
95  RealT k0 = std::max(zero,std::min(one,half*(one+(*yptr)[0]-(*yptr)[1])));
96  (*x0ptr)[0] = k0;
97  (*x0ptr)[1] = one-k0;
98  ROL::StdVector<RealT> x0(x0ptr);
99 
100  ROL::StdVector<RealT> e0(2);
101 
102  *outStream << std::setprecision(6) << std::scientific << std::endl;
103  *outStream << " x[0] = " << (*xptr)[0] << " x[1] = " << (*xptr)[1] << std::endl;
104  *outStream << " Px[0] = " << (*Pxptr)[0] << " Px[1] = " << (*Pxptr)[1] << std::endl;
105  *outStream << " x*[0] = " << (*x0ptr)[0] << " x*[1] = " << (*x0ptr)[1] << std::endl;
106 
107  e0.set(x0); e0.axpy(static_cast<RealT>(-1),Px);
108  err = e0.norm();
109  *outStream << " Error in Euclidean Projection: " << err << std::endl;
110 
111  e0.set(x); e0.axpy(static_cast<RealT>(-1),x0);
112  *outStream << " ||x*-x||^2 = " << e0.norm() << std::endl;
113 
114  e0.set(x); e0.axpy(static_cast<RealT>(-1),Px);
115  *outStream << " ||Px-x||^2 = " << e0.norm() << std::endl << std::endl;
116 
117  errorFlag += (err > tol);
118 
119  ROL::Ptr<std::vector<RealT>> dptr = ROL::makePtr<std::vector<RealT>>(2);
120  (*dptr)[0] = static_cast<RealT>(1)+static_cast<RealT>(2)*static_cast<RealT>(rand())/static_cast<RealT>(RAND_MAX);
121  (*dptr)[1] = static_cast<RealT>(1)+static_cast<RealT>(5)*static_cast<RealT>(rand())/static_cast<RealT>(RAND_MAX);
122 
123  ROL::Ptr<std::vector<RealT>> x1ptr = ROL::makePtr<std::vector<RealT>>(2);
124  RealT k1 = std::max(zero,std::min(one,((*dptr)[1]*(one-(*yptr)[1])+(*dptr)[0]*(*yptr)[0])/((*dptr)[0]+(*dptr)[1])));
125  (*x1ptr)[0] = k1;
126  (*x1ptr)[1] = one-k1;
127  ROL::PrimalScaledStdVector<RealT> x1(x1ptr,dptr);
128 
129  ROL::Ptr<std::vector<RealT>> zptr = ROL::makePtr<std::vector<RealT>>(2);
130  (*zptr)[0] = (*yptr)[0];
131  (*zptr)[1] = (*yptr)[1];
133 
134  ROL::Ptr<std::vector<RealT>> Pzptr = ROL::makePtr<std::vector<RealT>>(2,0.0);
135  (*Pzptr)[0] = (*yptr)[0];
136  (*Pzptr)[1] = (*yptr)[1];
137  ROL::PrimalScaledStdVector<RealT> Pz(Pzptr,dptr);
138 
139  ROL::Ptr<ROL::Vector<RealT>> l1 = z.clone(); l1->setScalar(static_cast<RealT>(0));
140  ROL::Ptr<ROL::Vector<RealT>> u1 = z.clone(); u1->setScalar(static_cast<RealT>(1));
141  ROL::Ptr<ROL::Bounds<RealT>> bnd1 = ROL::makePtr<ROL::Bounds<RealT>>(l1,u1);
142 
143  ROL::Ptr<ROL::PolyhedralProjection<RealT>> pp1 = ROL::PolyhedralProjectionFactory<RealT>(z,z.dual(),bnd1,con,r,r.dual(),list);
144  pp1->project(Pz,*outStream);
145 
146  ROL::Ptr<std::vector<RealT>> e1ptr = ROL::makePtr<std::vector<RealT>>(2);
147  ROL::PrimalScaledStdVector<RealT> e1(e1ptr,dptr);
148 
149  *outStream << std::endl;
150  *outStream << " x[0] = " << (*zptr)[0] << " x[1] = " << (*zptr)[1] << std::endl;
151  *outStream << " Px[0] = " << (*Pzptr)[0] << " Px[1] = " << (*Pzptr)[1] << std::endl;
152  *outStream << " x*[0] = " << (*x1ptr)[0] << " x*[1] = " << (*x1ptr)[1] << std::endl;
153 
154  e1.set(x1); e1.axpy(static_cast<RealT>(-1),Pz);
155  err = e1.norm();
156  *outStream << " Error in Scaled Projection: " << err << std::endl;
157 
158  e1.set(z); e1.axpy(static_cast<RealT>(-1),x1);
159  *outStream << " ||x*-x||^2 = " << e1.norm() << std::endl;
160 
161  e1.set(z); e1.axpy(static_cast<RealT>(-1),Pz);
162  *outStream << " ||Px-x||^2 = " << e1.norm() << std::endl << std::endl;
163 
164  errorFlag += (err > tol);
165  }
166 
167  catch (std::logic_error& err) {
168  *outStream << err.what() << "\n";
169  errorFlag = -1000;
170  }; // end try
171 
172  if (errorFlag != 0)
173  std::cout << "End Result: TEST FAILED\n";
174  else
175  std::cout << "End Result: TEST PASSED\n";
176 
177  return 0;
178 }
179 
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Definition: ROL_Vector.hpp:192
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
Defines the equality constraint operator interface for StdVectors.
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
basic_nullstream< char, std::char_traits< char >> nullstream
Definition: ROL_Stream.hpp:36
Real norm() const
Returns where .
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Ptr< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
virtual Ptr< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
void applyJacobian(std::vector< Real > &jv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Provides the std::vector implementation of the ROL::Vector interface that handles scalings in the inn...
void set(const Vector< Real > &x)
Set where .
int main(int argc, char *argv[])
void applyAdjointJacobian(std::vector< Real > &ajv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
const Vector< Real > & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
void value(std::vector< Real > &c, const std::vector< Real > &x, Real &tol)