10 #define OPTIMIZATION_PROBLEM_REFACTOR
12 #include "Teuchos_GlobalMPISession.hpp"
16 #include "ROL_NonlinearProgram.hpp"
23 #include "HS_ProblemFactory.hpp"
41 ROL::Ptr<const std::vector<Real> > xp =
44 outStream <<
"Standard Vector" << std::endl;
45 for(
size_t i=0; i<xp->size(); ++i ) {
46 outStream << (*xp)[i] << std::endl;
49 catch(
const std::bad_cast& e ) {
50 outStream <<
"Partitioned Vector" << std::endl;
55 const PV &xpv =
dynamic_cast<const PV&
>(x);
57 for( size_type i=0; i<xpv.numVectors(); ++i ) {
58 outStream <<
"--------------------" << std::endl;
61 outStream <<
"--------------------" << std::endl;
68 std::ostream &outStream ) {
72 for( uint i=0; i<
dim; ++i ) {
73 for( uint j=0; j<
dim; ++j ) {
74 outStream << std::setw(6) << A[j]->dot(*(I[i]));
76 outStream << std::endl;
92 int main(
int argc,
char *argv[]) {
96 typedef ROL::ParameterList PL;
104 typedef ROL::NonlinearProgram<RealT> NLP;
115 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
117 int iprint = argc - 1;
118 ROL::Ptr<std::ostream> outStream;
121 outStream = ROL::makePtrFromRef(std::cout);
123 outStream = ROL::makePtrFromRef(bhs);
131 RealT tol = std::sqrt(ROL::ROL_EPSILON<RealT>());
135 PL &iplist = parlist.sublist(
"Step").sublist(
"Primal Dual Interior Point");
136 PL &lblist = iplist.sublist(
"Barrier Objective");
138 lblist.set(
"Use Linear Damping",
true);
139 lblist.set(
"Linear Damping Coefficient",1.e-4);
140 lblist.set(
"Initial Barrier Parameter", mu);
142 PL &krylist = parlist.sublist(
"General").sublist(
"Krylov");
144 krylist.set(
"Absolute Tolerance", 1.e-6);
145 krylist.set(
"Relative Tolerance", 1.e-6);
146 krylist.set(
"Iteration Limit", 50);
149 krylist.set(
"Type",
"Conjugate Gradients");
150 ROL::Ptr<KRYLOV> cg = ROL::KrylovFactory<RealT>(parlist);
151 HS::ProblemFactory<RealT> problemFactory;
155 ROL::Ptr<NLP> nlp = problemFactory.getProblem(16);
156 ROL::Ptr<OPT> opt = nlp->getOptimizationProblem();
158 ROL::Ptr<V> x = opt->getSolutionVector();
159 ROL::Ptr<V> l = opt->getMultiplierVector();
160 ROL::Ptr<V> zl = x->clone(); zl->zero();
161 ROL::Ptr<V> zu = x->clone(); zu->zero();
163 ROL::Ptr<V> scratch = x->clone();
165 ROL::Ptr<PV> x_pv = ROL::dynamicPtrCast<PV>(x);
169 (*ROL::dynamicPtrCast<ROL::StdVector<RealT> >(x_pv->get(1))->getVector())[0] = 1.0;
173 std::vector< ROL::Ptr<V> > I;
174 std::vector< ROL::Ptr<V> > J;
176 for(
int k=0; k<sol->dimension(); ++k ) {
177 I.push_back(sol->basis(k));
178 J.push_back(sol->clone());
181 ROL::Ptr<V> u = sol->clone();
182 ROL::Ptr<V> v = sol->clone();
184 ROL::Ptr<V> rhs = sol->clone();
185 ROL::Ptr<V> symrhs = sol->clone();
187 ROL::Ptr<V> gmres_sol = sol->clone(); gmres_sol->set(*sol);
188 ROL::Ptr<V> cg_sol = sol->clone(); cg_sol->set(*sol);
195 ROL::Ptr<OBJ> obj = opt->getObjective();
196 ROL::Ptr<CON> con = opt->getConstraint();
197 ROL::Ptr<BND> bnd = opt->getBoundConstraint();
199 PENALTY penalty(obj,bnd,parlist);
201 ROL::Ptr<const V> maskL = penalty.getLowerMask();
202 ROL::Ptr<const V> maskU = penalty.getUpperMask();
215 ROL::Ptr<CON> res = ROL::makePtr<RESIDUAL>(obj,con,bnd,*sol,maskL,maskU,scratch,mu,
false);
216 ROL::Ptr<LOP> lop = ROL::makePtr<LOPEC>( sol, res );
219 res->value(*rhs,*sol,tol);
222 krylist.set(
"Type",
"GMRES");
223 ROL::Ptr<KRYLOV> gmres = ROL::KrylovFactory<RealT>(parlist);
225 for(
int k=0; k<sol->dimension(); ++k ) {
226 res->applyJacobian(*(J[k]),*(I[k]),*sol,tol);
229 *outStream <<
"Nonsymmetric Jacobian" << std::endl;
233 gmres->run( *gmres_sol, *lop, *rhs, identity, gmres_iter, gmres_flag );
235 errorFlag += gmres_flag;
237 *outStream <<
"GMRES terminated after " << gmres_iter <<
" iterations "
238 <<
"with exit flag " << gmres_flag << std::endl;
248 ROL::Ptr<V> jv = v->clone();
249 ROL::Ptr<V> ju = u->clone();
251 iplist.set(
"Symmetrize Primal Dual System",
true);
252 ROL::Ptr<CON> symres = ROL::makePtr<RESIDUAL>(obj,con,bnd,*sol,maskL,maskU,scratch,mu,
true);
253 ROL::Ptr<LOP> symlop = ROL::makePtr<LOPEC>( sol, res );
254 symres->value(*symrhs,*sol,tol);
256 symres->applyJacobian(*jv,*v,*sol,tol);
257 symres->applyJacobian(*ju,*u,*sol,tol);
258 *outStream <<
"Symmetry check |u.dot(jv)-v.dot(ju)| = "
259 << std::abs(u->dot(*jv)-v->dot(*ju)) << std::endl;
261 cg->run( *cg_sol, *symlop, *symrhs, identity, cg_iter, cg_flag );
263 *outStream <<
"CG terminated after " << cg_iter <<
" iterations "
264 <<
"with exit flag " << cg_flag << std::endl;
266 *outStream <<
"Check that GMRES solution also is a solution to the symmetrized system"
269 symres->applyJacobian(*ju,*gmres_sol,*sol,tol);
270 ju->axpy(-1.0,*symrhs);
271 RealT mismatch = ju->norm();
275 *outStream <<
"||J_sym*sol_nonsym-rhs_sym|| = " << mismatch << std::endl;
278 catch (std::logic_error& err) {
279 *outStream << err.what() << std::endl;
284 std::cout <<
"End Result: TEST FAILED" << std::endl;
286 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 printVector(const ROL::Vector< Real > &x, std::ostream &outStream)
A simple wrapper which allows application of constraint Jacobians through the LinearOperator interfac...
Provides the interface to evaluate the Interior Pointy log barrier penalty function with upper and lo...
void printMatrix(const std::vector< ROL::Ptr< ROL::Vector< Real > > > &A, const std::vector< ROL::Ptr< ROL::Vector< Real > > > &I, std::ostream &outStream)
void apply(ROL::Vector< Real > &Hv, const ROL::Vector< Real > &v, Real &tol) const
Apply linear operator.
Provides definitions for Krylov solvers.
Provides the interface to apply a linear operator.
Provides the interface to apply upper and lower bound constraints.
basic_nullstream< char, char_traits< char >> nullstream
int main(int argc, char *argv[])
virtual void set(const Vector &x)
Set where .