10 #define OPTIMIZATION_PROBLEM_REFACTOR
12 #include "Teuchos_GlobalMPISession.hpp"
16 #include "ROL_NonlinearProgram.hpp"
21 #include "HS_Problem_041.hpp"
39 ROL::Ptr<const std::vector<Real> > xp =
42 outStream <<
"Standard Vector" << std::endl;
43 for(
size_t i=0; i<xp->size(); ++i ) {
44 outStream << (*xp)[i] << std::endl;
47 catch(
const std::bad_cast& e ) {
48 outStream <<
"Partitioned Vector" << std::endl;
53 const PV &xpv =
dynamic_cast<const PV&
>(x);
55 for( size_type i=0; i<xpv.numVectors(); ++i ) {
56 outStream <<
"--------------------" << std::endl;
59 outStream <<
"--------------------" << std::endl;
69 typedef std::vector<Real> vector;
77 using ROL::dynamicPtrCast;
79 const size_type OPT = 0;
80 const size_type EQUAL = 1;
81 const size_type LOWER = 2;
82 const size_type UPPER = 3;
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());
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());
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];
101 cl[0] = x[0] + 2*x[1] + 2*x[2] - x[3];
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;
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;
120 typedef std::vector<Real> vector;
128 using ROL::dynamicPtrCast;
130 const size_type OPT = 0;
131 const size_type EQUAL = 1;
132 const size_type LOWER = 2;
133 const size_type UPPER = 3;
135 const PV &sol_pv =
dynamic_cast<const PV&
>(sol);
136 const vector &x = *(ROL::dynamicPtrCast<const SV>(sol_pv.get(OPT))->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());
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());
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());
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];
158 jvl[0] = vx[0] + 2*vx[1] + 2*vx[2] - vx[3];
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];
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]);
175 int main(
int argc,
char *argv[]) {
179 typedef ROL::ParameterList PL;
187 typedef ROL::NonlinearProgram<RealT> NLP;
194 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
196 int iprint = argc - 1;
197 ROL::Ptr<std::ostream> outStream;
200 outStream = ROL::makePtrFromRef(std::cout);
202 outStream = ROL::makePtrFromRef(bhs);
210 RealT tol = std::sqrt(ROL::ROL_EPSILON<RealT>());
214 PL &iplist = parlist.sublist(
"Step").sublist(
"Primal Dual Interior Point");
215 PL &lblist = iplist.sublist(
"Barrier Objective");
217 iplist.set(
"Symmetrize Primal Dual System",
false);
219 lblist.set(
"Use Linear Damping",
true);
220 lblist.set(
"Linear Damping Coefficient",1.e-4);
221 lblist.set(
"Initial Barrier Parameter", mu);
227 ROL::Ptr<NLP> nlp = ROL::makePtr<HS::Problem_041<RealT>>();
228 ROL::Ptr<OPT> opt = nlp->getOptimizationProblem();
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();
235 ROL::Ptr<V> scratch = x->clone();
237 ROL::Ptr<PV> x_pv = ROL::dynamicPtrCast<PV>(x);
241 ROL::Ptr<V> c = sol->clone();
242 ROL::Ptr<V> v = sol->clone();
243 ROL::Ptr<V> jv = sol->clone();
245 ROL::Ptr<V> c_exact = c->clone();
246 ROL::Ptr<V> jv_exact = jv->clone();
252 ROL::Ptr<OBJ> obj = opt->getObjective();
253 ROL::Ptr<CON> con = opt->getConstraint();
254 ROL::Ptr<BND> bnd = opt->getBoundConstraint();
256 PENALTY penalty(obj,bnd,parlist);
258 ROL::Ptr<const V> maskL = penalty.getLowerMask();
259 ROL::Ptr<const V> maskU = penalty.getUpperMask();
264 ROL::Ptr<CON> res = ROL::makePtr<RESIDUAL>(obj,con,bnd,*sol,maskL,maskU,scratch,mu,
false);
267 *outStream <<
"\n[x|lambda|zl|zu]" << std::endl;
270 res->value(*c,*sol,tol);
271 value(*c_exact,*sol,mu);
273 *outStream <<
"\n[cx|clambda|czl|czu]" << std::endl;
277 c->axpy(-1.0,*c_exact);
279 RealT cerror = c->norm();
285 *outStream <<
"\n\n||c-c_exact|| = " << cerror << std::endl;
287 res->applyJacobian(*jv,*v,*sol,tol);
290 *outStream <<
"\n[vx|vlambda|vzl|vzu]" << std::endl;
294 *outStream <<
"\n[jvx|jvlambda|jvzl|jvzu]" << std::endl;
298 jv->axpy(-1.0,*jv_exact);
300 RealT jverror = jv->norm();
302 if( jverror > tol ) {
306 *outStream <<
"\n\n||jv-jv_exact|| = " << jverror << std::endl;
308 *outStream <<
"Residual Jacobian Finite Difference Check" << std::endl;
309 res->checkApplyJacobian(*sol,*v,*v);
314 catch (std::logic_error& err) {
315 *outStream << err.what() << std::endl;
320 std::cout <<
"End Result: TEST FAILED" << std::endl;
322 std::cout <<
"End Result: TEST PASSED" << std::endl;
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.
void applyJacobian(ROL::Vector< Real > &jv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &sol)
basic_nullstream< char, std::char_traits< char >> nullstream
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[])