ROL
function/test_02.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 
14 #include "ROL_RandomVector.hpp"
15 #include "ROL_StdVector.hpp"
16 #include "ROL_Bounds.hpp"
18 
19 #include "ROL_Stream.hpp"
20 #include "Teuchos_GlobalMPISession.hpp"
21 #include "ROL_ParameterList.hpp"
22 
23 
24 typedef double RealT;
25 
26 int main(int argc, char *argv[]) {
27 
28  typedef std::vector<RealT> vector;
29  typedef ROL::Vector<RealT> V;
30  typedef ROL::StdVector<RealT> SV;
31 
32  typedef typename vector::size_type uint;
33 
34  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
35 
36  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
37  int iprint = argc - 1;
38  ROL::Ptr<std::ostream> outStream;
39  ROL::nullstream bhs; // outputs nothing
40  if (iprint > 0)
41  outStream = ROL::makePtrFromRef(std::cout);
42  else
43  outStream = ROL::makePtrFromRef(bhs);
44 
45  // Save the format state of the original std::cout.
46  ROL::nullstream oldFormatState;
47  oldFormatState.copyfmt(std::cout);
48 
49 // RealT errtol = std::sqrt(ROL::ROL_THRESHOLD<RealT>());
50 
51  int errorFlag = 0;
52 
53  // *** Test body.
54 
55  try {
56 
57  uint dim = 30;
58  RealT xmin = 0.5;
59  RealT xmax = 2.5;
60 
61  ROL::Ptr<vector> x_ptr = ROL::makePtr<vector>(dim,0.0);
62  ROL::Ptr<vector> g_ptr = ROL::makePtr<vector>(dim,0.0);
63  ROL::Ptr<vector> v_ptr = ROL::makePtr<vector>(dim,0.0);
64  ROL::Ptr<vector> hv_ptr = ROL::makePtr<vector>(dim,0.0);
65 
66  ROL::Ptr<vector> l_ptr = ROL::makePtr<vector>(dim,1.0);
67  ROL::Ptr<vector> u_ptr = ROL::makePtr<vector>(dim,2.0);
68 
69  ROL::Ptr<vector> xlog0_ptr = ROL::makePtr<vector>(dim,0.0);
70  ROL::Ptr<vector> xlog1_ptr = ROL::makePtr<vector>(dim,0.0);
71  ROL::Ptr<vector> xlog2_ptr = ROL::makePtr<vector>(dim,0.0);
72 
73  ROL::Ptr<vector> xquad0_ptr = ROL::makePtr<vector>(dim,0.0);
74  ROL::Ptr<vector> xquad1_ptr = ROL::makePtr<vector>(dim,0.0);
75  ROL::Ptr<vector> xquad2_ptr = ROL::makePtr<vector>(dim,0.0);
76 
77  ROL::Ptr<vector> xdwell0_ptr = ROL::makePtr<vector>(dim,0.0);
78  ROL::Ptr<vector> xdwell1_ptr = ROL::makePtr<vector>(dim,0.0);
79  ROL::Ptr<vector> xdwell2_ptr = ROL::makePtr<vector>(dim,0.0);
80 
81 
82 
83  SV x(x_ptr);
84  SV g(g_ptr);
85  SV v(v_ptr);
86  SV hv(hv_ptr);
87 
88  ROL::Ptr<SV> xlog0 = ROL::makePtr<SV>(xlog0_ptr);
89  ROL::Ptr<SV> xlog1 = ROL::makePtr<SV>(xlog1_ptr);
90  ROL::Ptr<SV> xlog2 = ROL::makePtr<SV>(xlog2_ptr);
91 
92  ROL::Ptr<SV> xquad0 = ROL::makePtr<SV>(xquad0_ptr);
93  ROL::Ptr<SV> xquad1 = ROL::makePtr<SV>(xquad1_ptr);
94  ROL::Ptr<SV> xquad2 = ROL::makePtr<SV>(xquad2_ptr);
95 
96  ROL::Ptr<SV> xdwell0 = ROL::makePtr<SV>(xdwell0_ptr);
97  ROL::Ptr<SV> xdwell1 = ROL::makePtr<SV>(xdwell1_ptr);
98  ROL::Ptr<SV> xdwell2 = ROL::makePtr<SV>(xdwell2_ptr);
99 
100  ROL::Ptr<V> lo = ROL::makePtr<SV>(l_ptr);
101  ROL::Ptr<V> up = ROL::makePtr<SV>(u_ptr);
102 
103  for(uint i=0; i<dim; ++i) {
104  RealT t = static_cast<RealT>(i)/static_cast<RealT>(dim-1);
105  (*x_ptr)[i] = xmin*(1-t) + xmax*t;
106  }
107 
108  // Create bound constraint
109  ROL::Bounds<RealT> bc(lo,up);
110 
111  ROL::ParameterList logList;
112  ROL::ParameterList quadList;
113  ROL::ParameterList dwellList;
114 
115  logList.sublist("Barrier Function").set("Type","Logarithmic");
116  quadList.sublist("Barrier Function").set("Type","Quadratic");
117  dwellList.sublist("Barrier Function").set("Type","Double Well");
118 
119  ROL::ObjectiveFromBoundConstraint<RealT> logObj(bc,logList);
120  ROL::ObjectiveFromBoundConstraint<RealT> quadObj(bc,quadList);
121  ROL::ObjectiveFromBoundConstraint<RealT> dwellObj(bc,dwellList);
122 
123  RealT tol = 0.0;
124 
125 
126  logObj.value(x,tol);
127  xlog0->set(*ROL::staticPtrCast<SV>(logObj.getBarrierVector()));
128 
129  logObj.gradient(g,x,tol);
130  xlog1->set(*ROL::staticPtrCast<SV>(logObj.getBarrierVector()));
131 
132  logObj.hessVec(hv,v,x,tol);
133  xlog2->set(*ROL::staticPtrCast<SV>(logObj.getBarrierVector()));
134 
135 
136  quadObj.value(x,tol);
137  xquad0->set(*ROL::staticPtrCast<SV>(quadObj.getBarrierVector()));
138 
139  quadObj.gradient(g,x,tol);
140  xquad1->set(*ROL::staticPtrCast<SV>(quadObj.getBarrierVector()));
141 
142  quadObj.hessVec(hv,v,x,tol);
143  xquad2->set(*ROL::staticPtrCast<SV>(quadObj.getBarrierVector()));
144 
145 
146  dwellObj.value(x,tol);
147  xdwell0->set(*ROL::staticPtrCast<SV>(dwellObj.getBarrierVector()));
148 
149  dwellObj.gradient(g,x,tol);
150  xdwell1->set(*ROL::staticPtrCast<SV>(dwellObj.getBarrierVector()));
151 
152  dwellObj.hessVec(hv,v,x,tol);
153  xdwell2->set(*ROL::staticPtrCast<SV>(dwellObj.getBarrierVector()));
154 
155 
156  *outStream << std::setw(14) << "x"
157  << std::setw(14) << "log"
158  << std::setw(14) << "D(log)"
159  << std::setw(14) << "D2(log)"
160  << std::setw(14) << "quad"
161  << std::setw(14) << "D(quad)"
162  << std::setw(14) << "D2(quad)"
163  << std::setw(14) << "dwell"
164  << std::setw(14) << "D(dwell)"
165  << std::setw(14) << "D2(dwell)"
166  << std::endl;
167  *outStream << std::string(140,'-') << std::endl;
168 
169  for(uint i=0; i<dim; ++i) {
170  *outStream << std::setw(14) << (*x_ptr)[i]
171  << std::setw(14) << (*xlog0_ptr)[i]
172  << std::setw(14) << (*xlog1_ptr)[i]
173  << std::setw(14) << (*xlog2_ptr)[i]
174  << std::setw(14) << (*xquad0_ptr)[i]
175  << std::setw(14) << (*xquad1_ptr)[i]
176  << std::setw(14) << (*xquad2_ptr)[i]
177  << std::setw(14) << (*xdwell0_ptr)[i]
178  << std::setw(14) << (*xdwell1_ptr)[i]
179  << std::setw(14) << (*xdwell2_ptr)[i]
180  << std::endl;
181  }
182 
183 
184  ROL::RandomizeVector( x, 1.2, 1.8 );
185  ROL::RandomizeVector( v, -0.1, 0.1 );
186 
187  *outStream << "\n\n";
188  *outStream << "Test of logarithmic penalty objective" << std::endl;
189  logObj.checkGradient(x,v,true,*outStream); *outStream << std::endl;
190  logObj.checkHessVec(x,v,true,*outStream); *outStream << std::endl;
191 
192  ROL::RandomizeVector( x, -1.0, 1.0 );
193  ROL::RandomizeVector( v, -1.0, 1.0 );
194 
195  *outStream << "\n\n";
196  *outStream << "Test of piecewise quadratic penalty objective" << std::endl;
197  quadObj.checkGradient(x,v,true,*outStream); *outStream << std::endl;
198  quadObj.checkHessVec(x,v,true,*outStream); *outStream << std::endl;
199 
200 
201  *outStream << "\n\n";
202  *outStream << "Test of double well penalty objective" << std::endl;
203  dwellObj.checkGradient(x,v,true,*outStream); *outStream << std::endl;
204  dwellObj.checkHessVec(x,v,true,*outStream); *outStream << std::endl;
205 
206 
207 
208 
209 
210  }
211  catch (std::logic_error& err) {
212  *outStream << err.what() << "\n";
213  errorFlag = -1000;
214  }; // end try
215 
216  if (errorFlag != 0)
217  std::cout << "End Result: TEST FAILED\n";
218  else
219  std::cout << "End Result: TEST PASSED\n";
220 
221  return 0;
222 
223 
224 }
225 
typename PV< Real >::size_type size_type
ROL::Ptr< Vector< Real > > getBarrierVector(void)
void RandomizeVector(Vector< Real > &x, const Real &lower=0.0, const Real &upper=1.0)
Fill a ROL::Vector with uniformly-distributed random numbers in the interval [lower,upper].
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
virtual std::vector< std::vector< Real > > checkGradient(const Vector< Real > &x, const Vector< Real > &d, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Finite-difference gradient check.
Vector< Real > V
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Provides the elementwise interface to apply upper and lower bound constraints.
Definition: ROL_Bounds.hpp:25
Real value(const Vector< Real > &x, Real &tol)
Compute value.
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:38
int main(int argc, char *argv[])
virtual std::vector< std::vector< Real > > checkHessVec(const Vector< Real > &x, const Vector< Real > &v, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Finite-difference Hessian-applied-to-vector check.
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
constexpr auto dim
Create a penalty objective from upper and lower bound vectors.