ROL
step/interiorpoint/test_01.cpp
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 
46 #include "ROL_RandomVector.hpp"
47 #include "ROL_StdVector.hpp"
48 
49 #include <iomanip>
50 
51 
57 template<class Real>
58 class NullObjective : public ROL::Objective<Real> {
60 public:
61  Real value( const V &x, Real &tol ) {
62  return Real(0.0);
63  }
64  void gradient( V &g, const V &x, Real &tol ) {
65  g.zero();
66  }
67  void hessVec( V &hv, const V &v, const V &x, Real &tol ) {
68  hv.zero();
69  }
70 };
71 
72 template<class Real>
73 void printVector( const ROL::Vector<Real> &x, std::ostream &outStream ) {
74  Teuchos::RCP<const std::vector<Real> > xp =
75  Teuchos::dyn_cast<const ROL::StdVector<Real> >(x).getVector();
76 
77  for( size_t i=0; i<xp->size(); ++i ) {
78  outStream << (*xp)[i] << std::endl;
79  }
80 }
81 
82 
83 
84 
85 typedef double RealT;
86 
87 int main(int argc, char *argv[]) {
88 
89  typedef std::vector<RealT> vector;
90 
91  typedef ROL::Vector<RealT> V;
92  typedef ROL::StdVector<RealT> SV;
93  typedef ROL::Objective<RealT> OBJ;
94  typedef ROL::BoundConstraint<RealT> BND;
95 
96  typedef Teuchos::ParameterList PL;
97 
98  using Teuchos::RCP; using Teuchos::rcp;
99 
100  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
101 
102  int iprint = argc - 1;
103  RCP<std::ostream> outStream;
104  Teuchos::oblackholestream bhs;
105  if( iprint > 0 )
106  outStream = rcp(&std::cout,false);
107  else
108  outStream = rcp(&bhs,false);
109 
110  int errorFlag = 0;
111 
112  try {
113 
114  PL parlist;
115  PL &iplist = parlist.sublist("Step").sublist("Primal Dual Interior Point");
116  PL &lblist = iplist.sublist("Barrier Objective");
117 
118  RealT mu = 1.e2;
119  RealT kappaD = 1.e-4;
120  bool useLinearDamping = true;
121 
122  lblist.set("Use Linear Damping", useLinearDamping);
123  lblist.set("Linear Damping Coefficient",kappaD);
124  lblist.set("Initial Barrier Parameter",mu);
125 
126  RealT ninf = ROL::ROL_NINF<RealT>();
127  RealT inf = ROL::ROL_INF<RealT>();
128 
129  int dim = 4;
130  int numTestVectors = 19;
131 
132  RCP<vector> x_rcp = rcp( new vector(dim, 0.0) );
133  RCP<vector> d_rcp = rcp( new vector(dim, 0.0) );
134  RCP<vector> v_rcp = rcp( new vector(dim, 0.0) );
135  RCP<vector> l_rcp = rcp( new vector(dim, 0.0) );
136  RCP<vector> u_rcp = rcp( new vector(dim, 0.0) );
137  RCP<vector> e0_rcp = rcp( new vector(dim, 0.0) ); // First canonical vector
138 
139  (*e0_rcp)[0] = 1.0;
140 
141  SV e0(e0_rcp);
142 
143  // Lower Bounds // Upper Bounds
144  (*l_rcp)[0] = ninf; (*u_rcp)[0] = 5.0;
145  (*l_rcp)[1] = ninf; (*u_rcp)[1] = inf;
146  (*l_rcp)[2] = -5.0; (*u_rcp)[2] = inf;
147  (*l_rcp)[3] = -5.0; (*u_rcp)[3] = 5.0;
148 
149  RealT left = -1.0; RealT right = 1.0;
150 
151  RealT xmax = 4.99;
152 
153  RCP<V> x = rcp( new SV( x_rcp ) );
154  RCP<V> d = rcp( new SV( d_rcp ) );
155  RCP<V> v = rcp( new SV( v_rcp ) );
156  RCP<V> l = rcp( new SV( l_rcp ) );
157  RCP<V> u = rcp( new SV( u_rcp ) );
158 
159  RCP<const V> maskL, maskU;
160 
161  ROL::RandomizeVector(*d,left,right);
162  ROL::RandomizeVector(*v,left,right);
163 
164  std::vector<RealT> values(numTestVectors); // Computed objective value for each
165  std::vector<RealT> exact_values(numTestVectors);
166 
167  std::vector<RCP<V> > x_test;
168 
169  for(int i=0; i<numTestVectors; ++i) {
170  x_test.push_back(x->clone());
171  RealT t = static_cast<RealT>(i)/static_cast<RealT>(numTestVectors-1);
172  RealT fillValue = xmax*(2.0*t-1.0);
173  x_test[i]->applyUnary(ROL::Elementwise::Fill<RealT>(fillValue));
174  }
175 
176  RCP<OBJ> obj = rcp( new NullObjective<RealT> );
177  RCP<BND> bnd = rcp( new ROL::BoundConstraint<RealT>(l,u) );
178 
179  ROL::InteriorPointPenalty<RealT> ipobj(obj,bnd,parlist);
180 
181  maskL = ipobj.getLowerMask();
182  maskU = ipobj.getUpperMask();
183 
184  RCP<const std::vector<RealT> > maskL_rcp = Teuchos::dyn_cast<const SV>(*maskL).getVector();
185  RCP<const std::vector<RealT> > maskU_rcp = Teuchos::dyn_cast<const SV>(*maskU).getVector();
186 
187  *outStream << "\nLower bound vector" << std::endl;
188  printVector(*l,*outStream);
189 
190  *outStream << "\nUpper bound vector" << std::endl;
191  printVector(*u,*outStream);
192 
193  *outStream << "\nLower mask vector" << std::endl;
194  printVector(*maskL, *outStream);
195 
196  *outStream << "\nUpper mask vector" << std::endl;
197  printVector(*maskU, *outStream);
198 
199  *outStream << "\nChecking Objective value" << std::endl;
200 
201  RealT tol = std::sqrt(ROL::ROL_EPSILON<RealT>());
202  *outStream << std::setw(16) << "x[i], i=0,1,2,3"
203  << std::setw(20) << "Computed Objective"
204  << std::setw(20) << "Exact Objective" << std::endl;
205 
206  RealT valueError(0.0);
207 
208  for(int i=0; i<numTestVectors; ++i) {
209  values[i] = ipobj.value(*(x_test[i]),tol);
210 
211  exact_values[i] = 0;
212 
213  // Extract the value from the test vector that is in every element
214  RealT xval = x_test[i]->dot(e0);
215 
216 
217  for(int j=0; j<dim; ++j) {
218  if( (*maskL_rcp)[j] ) {
219  RealT diff = xval-(*l_rcp)[j];
220  exact_values[i] -= mu*std::log(diff);
221 
222  if( useLinearDamping && !(*maskU_rcp)[j] ) {
223  exact_values[i] += mu*kappaD*diff;
224  }
225 
226  }
227  if( (*maskU_rcp)[j] ) {
228  RealT diff = (*u_rcp)[j]-xval;
229  exact_values[i] -= mu*std::log(diff);
230 
231  if(useLinearDamping && !(*maskL_rcp)[j] ) {
232  exact_values[i] += mu*kappaD*diff;
233  }
234 
235  }
236  } // end loop over elements
237 
238  *outStream << std::setw(16) << xval
239  << std::setw(20) << values[i]
240  << std::setw(20) << exact_values[i] << std::endl;
241  RealT valDiff = exact_values[i] - values[i];
242  valueError += valDiff*valDiff;
243  } // end loop over vectors
244 
245  if(valueError>ROL::ROL_EPSILON<RealT>()) {
246  errorFlag++;
247  }
248 
249  *outStream << "\nPerforming finite difference checks" << std::endl;
250 
251  ipobj.checkGradient(*x,*v,true,*outStream); *outStream << std::endl;
252  ipobj.checkHessVec(*x,*d,true,*outStream); *outStream << std::endl;
253  ipobj.checkHessSym(*x,*d,*v,true,*outStream); *outStream << std::endl;
254 
255  }
256  catch (std::logic_error err) {
257  *outStream << err.what() << std::endl;
258  errorFlag = -1000;
259  }
260 
261  if (errorFlag != 0)
262  std::cout << "End Result: TEST FAILED" << std::endl;
263  else
264  std::cout << "End Result: TEST PASSED" << std::endl;
265 
266  return 0;
267 }
268 
Provides the interface to evaluate objective functions.
Real value(const Vector< Real > &x, Real &tol)
Compute value.
void gradient(V &g, const V &x, Real &tol)
Compute gradient.
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].
int main(int argc, char *argv[])
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:159
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:76
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.
Real value(const V &x, Real &tol)
Compute value.
Provides the interface to evaluate the Interior Pointy log barrier penalty function with upper and lo...
Teuchos::RCP< const Vector< Real > > getLowerMask(void) const
Provides the interface to apply upper and lower bound constraints.
void hessVec(V &hv, const V &v, const V &x, Real &tol)
Apply Hessian approximation to vector.
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.
ROL::Vector< Real > V
void printVector(const ROL::Vector< Real > &x, std::ostream &outStream)
Teuchos::RCP< const Vector< Real > > getUpperMask(void) const
virtual std::vector< Real > checkHessSym(const Vector< Real > &x, const Vector< Real > &v, const Vector< Real > &w, const bool printToStream=true, std::ostream &outStream=std::cout)
Hessian symmetry check.