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