ROL
step/interiorpoint/test_02.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 #define OPTIMIZATION_PROBLEM_REFACTOR
45 
46 #include "ROL_RandomVector.hpp"
47 #include "ROL_StdVector.hpp"
48 #include "ROL_NonlinearProgram.hpp"
52 
53 #include "HS_Problem_041.hpp"
54 
55 #include <iomanip>
56 
67 template<class Real>
68 void printVector( const ROL::Vector<Real> &x, std::ostream &outStream ) {
69 
70  try {
71  Teuchos::RCP<const std::vector<Real> > xp =
72  Teuchos::dyn_cast<const ROL::StdVector<Real> >(x).getVector();
73 
74  outStream << "Standard Vector" << std::endl;
75  for( size_t i=0; i<xp->size(); ++i ) {
76  outStream << (*xp)[i] << std::endl;
77  }
78  }
79  catch( const std::bad_cast& e ) {
80  outStream << "Partitioned Vector" << std::endl;
81 
83  typedef typename PV::size_type size_type;
84 
85  const PV &xpv = Teuchos::dyn_cast<const PV>(x);
86 
87  for( size_type i=0; i<xpv.numVectors(); ++i ) {
88  outStream << "--------------------" << std::endl;
89  printVector( *(xpv.get(i)), outStream );
90  }
91  outStream << "--------------------" << std::endl;
92  }
93 
94 }
95 
96 
97 // Exact residual for H&S Problem 41
98 template<class Real>
99 void value( ROL::Vector<Real> &c, const ROL::Vector<Real> &sol, const Real &mu ) {
100 
101  typedef std::vector<Real> vector;
102  typedef ROL::StdVector<Real> SV;
104 
105  typedef typename PV::size_type size_type;
106 
107  using Teuchos::RCP;
108  using Teuchos::dyn_cast;
109  using Teuchos::rcp_dynamic_cast;
110 
111  const size_type OPT = 0;
112  const size_type EQUAL = 1;
113  const size_type LOWER = 2;
114  const size_type UPPER = 3;
115 
116  const PV &sol_pv = dyn_cast<const PV>(sol);
117  const vector &x = *(rcp_dynamic_cast<const SV>(sol_pv.get(OPT))->getVector());
118  const vector &l = *(rcp_dynamic_cast<const SV>(sol_pv.get(EQUAL))->getVector());
119  const vector &zl = *(rcp_dynamic_cast<const SV>(sol_pv.get(LOWER))->getVector());
120  const vector &zu = *(rcp_dynamic_cast<const SV>(sol_pv.get(UPPER))->getVector());
121 
122  PV &c_pv = dyn_cast<PV>(c);
123  vector &cx = *(rcp_dynamic_cast<SV>(c_pv.get(OPT))->getVector());
124  vector &cl = *(rcp_dynamic_cast<SV>(c_pv.get(EQUAL))->getVector());
125  vector &czl = *(rcp_dynamic_cast<SV>(c_pv.get(LOWER))->getVector());
126  vector &czu = *(rcp_dynamic_cast<SV>(c_pv.get(UPPER))->getVector());
127 
128  cx[0] = -x[1]*x[2] + l[0] - zl[0] + zu[0];
129  cx[1] = -x[0]*x[1] + 2*l[0] - zl[1] + zu[1];
130  cx[2] = -x[0]*x[1] + 2*l[0] - zl[2] + zu[2];
131  cx[3] = - l[0] - zl[3] + zu[3];
132 
133  cl[0] = x[0] + 2*x[1] + 2*x[2] - x[3];
134 
135  czl[0] = x[0]*zl[0] - mu;
136  czl[1] = x[1]*zl[1] - mu;
137  czl[2] = x[2]*zl[2] - mu;
138  czl[3] = x[3]*zl[3] - mu;
139 
140  czu[0] = (1.0-x[0])*zu[0] - mu;
141  czu[1] = (1.0-x[1])*zu[1] - mu;
142  czu[2] = (1.0-x[2])*zl[2] - mu;
143  czu[3] = (2.0-x[3])*zl[3] - mu;
144 }
145 
146 
147 
148 // Exact residual for H&S Problem 41
149 template<class Real>
151 
152  typedef std::vector<Real> vector;
153  typedef ROL::StdVector<Real> SV;
155 
156  typedef typename PV::size_type size_type;
157 
158  using Teuchos::RCP;
159  using Teuchos::dyn_cast;
160  using Teuchos::rcp_dynamic_cast;
161 
162  const size_type OPT = 0;
163  const size_type EQUAL = 1;
164  const size_type LOWER = 2;
165  const size_type UPPER = 3;
166 
167  const PV &sol_pv = dyn_cast<const PV>(sol);
168  const vector &x = *(rcp_dynamic_cast<const SV>(sol_pv.get(OPT))->getVector());
169 //const vector &l = *(rcp_dynamic_cast<const SV>(sol_pv.get(EQUAL))->getVector());
170  const vector &zl = *(rcp_dynamic_cast<const SV>(sol_pv.get(LOWER))->getVector());
171  const vector &zu = *(rcp_dynamic_cast<const SV>(sol_pv.get(UPPER))->getVector());
172 
173  const PV &v_pv = dyn_cast<const PV>(v);
174  const vector &vx = *(rcp_dynamic_cast<const SV>(v_pv.get(OPT))->getVector());
175  const vector &vl = *(rcp_dynamic_cast<const SV>(v_pv.get(EQUAL))->getVector());
176  const vector &vzl = *(rcp_dynamic_cast<const SV>(v_pv.get(LOWER))->getVector());
177  const vector &vzu = *(rcp_dynamic_cast<const SV>(v_pv.get(UPPER))->getVector());
178 
179  PV &jv_pv = dyn_cast<PV>(jv);
180  vector &jvx = *(rcp_dynamic_cast<SV>(jv_pv.get(OPT))->getVector());
181  vector &jvl = *(rcp_dynamic_cast<SV>(jv_pv.get(EQUAL))->getVector());
182  vector &jvzl = *(rcp_dynamic_cast<SV>(jv_pv.get(LOWER))->getVector());
183  vector &jvzu = *(rcp_dynamic_cast<SV>(jv_pv.get(UPPER))->getVector());
184 
185  jvx[0] = -x[1]*vx[2] - x[2]*vx[1] + vl[0] - vzl[0] + vzu[0];
186  jvx[1] = -x[0]*vx[2] - x[2]*vx[0] + 2*vl[0] - vzl[1] + vzu[1];
187  jvx[2] = -x[0]*vx[1] - x[1]*vx[0] + 2*vl[0] - vzl[2] + vzu[2];
188  jvx[3] = - vl[0] - vzl[3] + vzu[3];
189 
190  jvl[0] = vx[0] + 2*vx[1] + 2*vx[2] - vx[3];
191 
192  jvzl[0] = zl[0]*vx[0] + vzl[0]*x[0];
193  jvzl[1] = zl[1]*vx[1] + vzl[1]*x[1];
194  jvzl[2] = zl[2]*vx[2] + vzl[2]*x[2];
195  jvzl[3] = zl[3]*vx[3] + vzl[3]*x[3];
196 
197  jvzu[0] = -zu[0]*vx[0] + vzu[0]*(1.0-x[0]);
198  jvzu[1] = -zu[1]*vx[1] + vzu[1]*(1.0-x[1]);
199  jvzu[2] = -zu[2]*vx[2] + vzu[2]*(1.0-x[2]);
200  jvzu[3] = -zu[3]*vx[3] + vzu[3]*(2.0-x[3]);
201 
202 }
203 
204 
205 typedef double RealT;
206 
207 int main(int argc, char *argv[]) {
208 
209 // typedef std::vector<RealT> vector;
210 
211  typedef Teuchos::ParameterList PL;
212 
213  typedef ROL::Vector<RealT> V;
215  typedef ROL::Objective<RealT> OBJ;
216  typedef ROL::EqualityConstraint<RealT> CON;
217  typedef ROL::BoundConstraint<RealT> BND;
219  typedef ROL::NonlinearProgram<RealT> NLP;
220 
221  typedef ROL::InteriorPointPenalty<RealT> PENALTY;
223 
224  using Teuchos::RCP; using Teuchos::rcp;
225 
226  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
227 
228  int iprint = argc - 1;
229  RCP<std::ostream> outStream;
230  Teuchos::oblackholestream bhs;
231  if( iprint > 0 )
232  outStream = rcp(&std::cout,false);
233  else
234  outStream = rcp(&bhs,false);
235 
236  int errorFlag = 0;
237 
238  try {
239 
240  RealT mu = 0.1;
241 
242  RealT tol = std::sqrt(ROL::ROL_EPSILON<RealT>());
243 
244  PL parlist;
245 
246  PL &iplist = parlist.sublist("Step").sublist("Primal Dual Interior Point");
247  PL &lblist = iplist.sublist("Barrier Objective");
248 
249  iplist.set("Symmetrize Primal Dual System",false);
250 
251  lblist.set("Use Linear Damping", true);
252  lblist.set("Linear Damping Coefficient",1.e-4);
253  lblist.set("Initial Barrier Parameter", mu);
254 
255  // Need an example problem that satisfies the following criteria:
256  // 1) Has an equality constraint
257  // 2) Has a bound constraint where all variables have finite upper and lower bounds
258 
259  RCP<NLP> nlp = rcp( new HS::Problem_041<RealT>() );
260  RCP<OPT> opt = nlp->getOptimizationProblem();
261 
262  RCP<V> x = opt->getSolutionVector();
263  RCP<V> l = opt->getMultiplierVector();
264  RCP<V> zl = x->clone();
265  RCP<V> zu = x->clone();
266 
267  RCP<V> scratch = x->clone();
268 
269  RCP<PV> x_pv = Teuchos::rcp_dynamic_cast<PV>(x);
270 
271  RCP<V> sol = CreatePartitionedVector(x,l,zl,zu);
272 
273  RCP<V> c = sol->clone();
274  RCP<V> v = sol->clone();
275  RCP<V> jv = sol->clone();
276 
277  RCP<V> c_exact = c->clone();
278  RCP<V> jv_exact = jv->clone();
279 
280  ROL::RandomizeVector(*l, -1.0, 1.0);
281  ROL::RandomizeVector(*v, 0.0, 1.0);
282 
283 
284  RCP<OBJ> obj = opt->getObjective();
285  RCP<CON> con = opt->getEqualityConstraint();
286  RCP<BND> bnd = opt->getBoundConstraint();
287 
288  PENALTY penalty(obj,bnd,parlist);
289 
290  RCP<const V> maskL = penalty.getLowerMask();
291  RCP<const V> maskU = penalty.getUpperMask();
292 
293  zl->set(*maskL);
294  zu->set(*maskU);
295 
296  RCP<CON> res = rcp( new RESIDUAL(obj,con,bnd,*sol,maskL,maskU,scratch,mu,false) );
297 
298 
299  *outStream << "\n[x|lambda|zl|zu]" << std::endl;
300  printVector(*sol,*outStream);
301 
302  res->value(*c,*sol,tol);
303  value(*c_exact,*sol,mu);
304 
305  *outStream << "\n[cx|clambda|czl|czu]" << std::endl;
306 
307  printVector(*c,*outStream);
308 
309  c->axpy(-1.0,*c_exact);
310 
311  RealT cerror = c->norm();
312 
313  if( cerror>tol ) {
314  ++errorFlag;
315  }
316 
317  *outStream << "\n\n||c-c_exact|| = " << cerror << std::endl;
318 
319  res->applyJacobian(*jv,*v,*sol,tol);
320  applyJacobian(*jv_exact,*v,*sol);
321 
322  *outStream << "\n[vx|vlambda|vzl|vzu]" << std::endl;
323 
324  printVector(*v,*outStream);
325 
326  *outStream << "\n[jvx|jvlambda|jvzl|jvzu]" << std::endl;
327 
328  printVector(*jv,*outStream);
329 
330  jv->axpy(-1.0,*jv_exact);
331 
332  RealT jverror = jv->norm();
333 
334  if( jverror > tol ) {
335  ++errorFlag;
336  }
337 
338  *outStream << "\n\n||jv-jv_exact|| = " << jverror << std::endl;
339 
340  *outStream << "Residual Jacobian Finite Difference Check" << std::endl;
341  res->checkApplyJacobian(*sol,*v,*v);
342 
343 
344 
345  }
346  catch (std::logic_error err) {
347  *outStream << err.what() << std::endl;
348  errorFlag = -1000;
349  }
350 
351  if (errorFlag != 0)
352  std::cout << "End Result: TEST FAILED" << std::endl;
353  else
354  std::cout << "End Result: TEST PASSED" << std::endl;
355 
356  return 0;
357 }
358 
Provides the interface to evaluate objective functions.
PartitionedVector< Real > PV
Defines the linear algebra of vector space on a generic partitioned vector.
int main(int argc, char *argv[])
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].
Symmetrized form of the KKT operator for the Type-EB problem with equality and bound multipliers...
Teuchos::RCP< Vector< Real > > CreatePartitionedVector(const Teuchos::RCP< Vector< Real > > &a)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:76
void applyJacobian(ROL::Vector< Real > &jv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &sol)
Vector< Real > V
Provides the interface to evaluate the Interior Pointy log barrier penalty function with upper and lo...
Defines the equality constraint operator interface.
Provides the interface to apply upper and lower bound constraints.
void printVector(const ROL::Vector< Real > &x, std::ostream &outStream)
void value(ROL::Vector< Real > &c, const ROL::Vector< Real > &sol, const Real &mu)