ROL
function/test_15.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];
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  RealT tol = std::sqrt(ROL::ROL_EPSILON<RealT>());
58  RealT err(0);
59  ROL::Ptr<con2d<RealT>> con = ROL::makePtr<con2d<RealT>>();
60 
61  ROL::Ptr<std::vector<RealT>> yptr = ROL::makePtr<std::vector<RealT>>(2);
62  (*yptr)[0] = static_cast<RealT>(rand())/static_cast<RealT>(RAND_MAX);
63  (*yptr)[1] = static_cast<RealT>(rand())/static_cast<RealT>(RAND_MAX);
64  ROL::StdVector<RealT> y(yptr);
65 
67 
68  ROL::Ptr<std::vector<RealT>> xptr = ROL::makePtr<std::vector<RealT>>(2);
69  (*xptr)[0] = (*yptr)[0];
70  (*xptr)[1] = (*yptr)[1];
71  ROL::StdVector<RealT> x(xptr);
72 
73  ROL::Ptr<std::vector<RealT>> Pxptr = ROL::makePtr<std::vector<RealT>>(2,0.0);
74  ROL::StdVector<RealT> Px(Pxptr);
75 
76  ROL::NullSpaceOperator<RealT> ns0(con,x,r);
77  ns0.apply(Px,x,tol);
78 
79  ROL::Ptr<std::vector<RealT>> x0ptr = ROL::makePtr<std::vector<RealT>>(2);
80  (*x0ptr)[0] = ((*yptr)[0]-(*yptr)[1])/static_cast<RealT>(2);
81  (*x0ptr)[1] = -(*x0ptr)[0];
82  ROL::StdVector<RealT> x0(x0ptr);
83 
85 
86  *outStream << std::setprecision(6) << std::scientific << std::endl;
87  *outStream << " x[0] = " << (*xptr)[0] << " x[1] = " << (*xptr)[1] << std::endl;
88  *outStream << " Px[0] = " << (*Pxptr)[0] << " Px[1] = " << (*Pxptr)[1] << std::endl;
89  *outStream << " x*[0] = " << (*x0ptr)[0] << " x*[1] = " << (*x0ptr)[1] << std::endl;
90 
91  e0.set(x0); e0.axpy(static_cast<RealT>(-1),Px);
92  err = e0.norm();
93  *outStream << " Error in Euclidean Projection: " << err << std::endl;
94 
95  e0.set(x); e0.axpy(static_cast<RealT>(-1),x0);
96  *outStream << " ||x*-x||^2 = " << e0.norm() << std::endl;
97 
98  e0.set(x); e0.axpy(static_cast<RealT>(-1),Px);
99  *outStream << " ||Px-x||^2 = " << e0.norm() << std::endl << std::endl;
100 
101  errorFlag += (err > tol);
102 
103  ROL::Ptr<std::vector<RealT>> dptr = ROL::makePtr<std::vector<RealT>>(2);
104  (*dptr)[0] = static_cast<RealT>(1)+static_cast<RealT>(2)*static_cast<RealT>(rand())/static_cast<RealT>(RAND_MAX);
105  (*dptr)[1] = static_cast<RealT>(1)+static_cast<RealT>(5)*static_cast<RealT>(rand())/static_cast<RealT>(RAND_MAX);
106 
107  ROL::Ptr<std::vector<RealT>> x1ptr = ROL::makePtr<std::vector<RealT>>(2);
108  (*x1ptr)[0] = ((*dptr)[0]*(*yptr)[0]-(*dptr)[1]*(*yptr)[1])/((*dptr)[0]+(*dptr)[1]);
109  (*x1ptr)[1] = -(*x1ptr)[0];
110  ROL::PrimalScaledStdVector<RealT> x1(x1ptr,dptr);
111 
112  ROL::Ptr<std::vector<RealT>> zptr = ROL::makePtr<std::vector<RealT>>(2);
113  (*zptr)[0] = (*yptr)[0];
114  (*zptr)[1] = (*yptr)[1];
116 
117  ROL::Ptr<std::vector<RealT>> Pzptr = ROL::makePtr<std::vector<RealT>>(2,0.0);
118  ROL::PrimalScaledStdVector<RealT> Pz(Pzptr,dptr);
119 
120  ROL::NullSpaceOperator<RealT> ns1(con,z,r);
121  ns1.apply(Pz,z,tol);
122 
123  ROL::Ptr<std::vector<RealT>> e1ptr = ROL::makePtr<std::vector<RealT>>(2);
124  ROL::PrimalScaledStdVector<RealT> e1(e1ptr,dptr);
125 
126  *outStream << std::endl;
127  *outStream << " x[0] = " << (*zptr)[0] << " x[1] = " << (*zptr)[1] << std::endl;
128  *outStream << " Px[0] = " << (*Pzptr)[0] << " Px[1] = " << (*Pzptr)[1] << std::endl;
129  *outStream << " x*[0] = " << (*x1ptr)[0] << " x*[1] = " << (*x1ptr)[1] << std::endl;
130 
131  e1.set(x1); e1.axpy(static_cast<RealT>(-1),Pz);
132  err = e1.norm();
133  *outStream << " Error in Euclidean Projection: " << err << std::endl;
134 
135  e1.set(z); e1.axpy(static_cast<RealT>(-1),x1);
136  *outStream << " ||x*-x||^2 = " << e1.norm() << std::endl;
137 
138  e1.set(z); e1.axpy(static_cast<RealT>(-1),Pz);
139  *outStream << " ||Px-x||^2 = " << e1.norm() << std::endl << std::endl;
140 
141  errorFlag += (err > tol);
142  }
143 
144  catch (std::logic_error& err) {
145  *outStream << err.what() << "\n";
146  errorFlag = -1000;
147  }; // end try
148 
149  if (errorFlag != 0)
150  std::cout << "End Result: TEST FAILED\n";
151  else
152  std::cout << "End Result: TEST PASSED\n";
153 
154  return 0;
155 }
156 
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
Defines the equality constraint operator interface for StdVectors.
virtual void apply(Vector< Real > &Hv, const Vector< Real > &v, Real &tol) const
Apply linear operator.
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Projects on to the null space of a linear constraint.
Real norm() const
Returns where .
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
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 .
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:38
int main(int argc, char *argv[])
void applyAdjointJacobian(std::vector< Real > &ajv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
void value(std::vector< Real > &c, const std::vector< Real > &x, Real &tol)