// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /*! \file example_01.cpp \brief Shows how to solve the equality constrained NLP from Nocedal/Wright, 2nd edition, page 574, example 18.2. */ #include "ROL_SimpleEqConstrained.hpp" #include "ROL_Solver.hpp" #include "ROL_Stream.hpp" #include "Teuchos_GlobalMPISession.hpp" #include //#include typedef double RealT; /*** Declare four vector spaces. ***/ // Forward declarations: template class OptStdVector; // Optimization space. template class OptDualStdVector; // Dual optimization space. template class ConStdVector; // Constraint space. template class ConDualStdVector; // Dual constraint space. // Vector space definitions: // Optimization space. template class OptStdVector : public ROL::Vector { typedef std::vector vector; typedef ROL::Vector V; typedef typename vector::size_type uint; private: ROL::Ptr > std_vec_; mutable ROL::Ptr > dual_vec_; public: OptStdVector(const ROL::Ptr > & std_vec) : std_vec_(std_vec), dual_vec_(ROL::nullPtr) {} void plus( const ROL::Vector &x ) { ROL::Ptr xp = dynamic_cast(x).getVector(); uint dimension = std_vec_->size(); for (uint i=0; isize(); for (uint i=0; i &x ) const { ROL::Ptr xp = dynamic_cast(x).getVector(); Real val = 0; uint dimension = std_vec_->size(); for (uint i=0; i > clone() const { return ROL::makePtr( ROL::makePtr>(std_vec_->size()) ); } ROL::Ptr > getVector() const { return std_vec_; } ROL::Ptr > getVector() { return std_vec_; } ROL::Ptr > basis( const int i ) const { ROL::Ptr e_ptr = ROL::makePtr(std_vec_->size(),0.0); ROL::Ptr e = ROL::makePtr( e_ptr ); (*e_ptr)[i] = 1.0; return e; } int dimension() const {return static_cast(std_vec_->size());} const ROL::Vector & dual() const { dual_vec_ = ROL::makePtr>( ROL::makePtr>(*std_vec_) ); return *dual_vec_; } Real apply( const ROL::Vector &x ) const { Real val = 0; ROL::Ptr xvalptr = dynamic_cast&>(x).getVector(); uint dimension = std_vec_->size(); for (uint i=0; i class OptDualStdVector : public ROL::Vector { typedef std::vector vector; typedef ROL::Vector V; typedef typename vector::size_type uint; private: ROL::Ptr > std_vec_; mutable ROL::Ptr > dual_vec_; public: OptDualStdVector(const ROL::Ptr > & std_vec) : std_vec_(std_vec), dual_vec_(ROL::nullPtr) {} void plus( const ROL::Vector &x ) { ROL::Ptr xp = dynamic_cast(x).getVector(); uint dimension = std_vec_->size(); for (uint i=0; isize(); for (uint i=0; i &x ) const { ROL::Ptr xp = dynamic_cast(x).getVector(); Real val = 0; uint dimension = std_vec_->size(); for (uint i=0; i > clone() const { return ROL::makePtr( ROL::makePtr>(std_vec_->size()) ); } ROL::Ptr > getVector() const { return std_vec_; } ROL::Ptr > getVector() { return std_vec_; } ROL::Ptr > basis( const int i ) const { ROL::Ptr e_ptr = ROL::makePtr( std_vec_->size(), 0.0 ); ROL::Ptr e = ROL::makePtr( e_ptr ); (*e_ptr)[i] = 1.0; return e; } int dimension() const {return static_cast(std_vec_->size());} const ROL::Vector & dual() const { dual_vec_ = ROL::makePtr>( ROL::makePtr>(*std_vec_) ); return *dual_vec_; } Real apply( const ROL::Vector &x ) const { Real val = 0; ROL::Ptr xvalptr = dynamic_cast&>(x).getVector(); uint dimension = std_vec_->size(); for (uint i=0; i class ConStdVector : public ROL::Vector { typedef std::vector vector; typedef ROL::Vector V; typedef typename vector::size_type uint; private: ROL::Ptr > std_vec_; mutable ROL::Ptr > dual_vec_; public: ConStdVector(const ROL::Ptr > & std_vec) : std_vec_(std_vec), dual_vec_(ROL::nullPtr) {} void plus( const ROL::Vector &x ) { ROL::Ptr xp = dynamic_cast(x).getVector(); uint dimension = std_vec_->size(); for (uint i=0; isize(); for (uint i=0; i &x ) const { ROL::Ptr xp = dynamic_cast(x).getVector(); Real val = 0; uint dimension = std_vec_->size(); for (uint i=0; i > clone() const { return ROL::makePtr( ROL::makePtr>(std_vec_->size())); } ROL::Ptr > getVector() const { return std_vec_; } ROL::Ptr > getVector() { return std_vec_; } ROL::Ptr > basis( const int i ) const { ROL::Ptr e_ptr = ROL::makePtr(std_vec_->size(), 0.0); ROL::Ptr e = ROL::makePtr( e_ptr ); (*e_ptr)[i] = 1.0; return e; } int dimension() const {return static_cast(std_vec_->size());} const ROL::Vector & dual() const { dual_vec_ = ROL::makePtr>( ROL::makePtr>(*std_vec_) ); return *dual_vec_; } Real apply( const ROL::Vector &x ) const { Real val = 0; ROL::Ptr xvalptr = dynamic_cast&>(x).getVector(); uint dimension = std_vec_->size(); for (uint i=0; i class ConDualStdVector : public ROL::Vector { typedef std::vector vector; typedef ROL::Vector V; typedef typename vector::size_type uint; private: ROL::Ptr > std_vec_; mutable ROL::Ptr > dual_vec_; public: ConDualStdVector(const ROL::Ptr > & std_vec) : std_vec_(std_vec), dual_vec_(ROL::nullPtr) {} void plus( const ROL::Vector &x ) { ROL::Ptr xp = dynamic_cast(x).getVector(); uint dimension = std_vec_->size(); for (uint i=0; isize(); for (uint i=0; i &x ) const { ROL::Ptr xp = dynamic_cast(x).getVector(); Real val = 0; uint dimension = std_vec_->size(); for (uint i=0; i > clone() const { return ROL::makePtr( ROL::makePtr>(std_vec_->size())); } ROL::Ptr > getVector() const { return std_vec_; } ROL::Ptr > getVector() { return std_vec_; } ROL::Ptr > basis( const int i ) const { ROL::Ptr e_ptr = ROL::makePtr(std_vec_->size(),0.0); ROL::Ptr e = ROL::makePtr(e_ptr); (*e_ptr)[i] = 1.0; return e; } int dimension() const {return static_cast(std_vec_->size());} const ROL::Vector & dual() const { dual_vec_ = ROL::makePtr>( ROL::makePtr>(*std_vec_) ); return *dual_vec_; } Real apply( const ROL::Vector &x ) const { Real val = 0; ROL::Ptr xvalptr = dynamic_cast&>(x).getVector(); uint dimension = std_vec_->size(); for (uint i=0; i vector; typedef vector::size_type uint; Teuchos::GlobalMPISession mpiSession(&argc, &argv); // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. int iprint = argc - 1; ROL::Ptr outStream; ROL::nullstream bhs; // outputs nothing if (iprint > 0) outStream = ROL::makePtrFromRef(std::cout); else outStream = ROL::makePtrFromRef(bhs); int errorFlag = 0; // *** Example body. try { uint dim = 5; uint nc = 3; ROL::Ptr > obj; ROL::Ptr > constr; ROL::Ptr x_ptr = ROL::makePtr(dim, 0.0); ROL::Ptr sol_ptr = ROL::makePtr(dim, 0.0); ROL::Ptr> x = ROL::makePtr>(x_ptr); // Iteration vector. ROL::Ptr> sol = ROL::makePtr>(sol_ptr); // Reference solution vector. // Retrieve objective, constraint, iteration vector, solution vector. ROL::ZOO::getSimpleEqConstrained , OptDualStdVector, ConStdVector, ConDualStdVector > SEC; obj = SEC.getObjective(); constr = SEC.getEqualityConstraint(); x->set(*SEC.getInitialGuess()); sol->set(*SEC.getSolution()); // Run derivative checks, etc. RealT left = -1e0, right = 1e0; ROL::Ptr xtest_ptr = ROL::makePtr(dim, 0.0); ROL::Ptr g_ptr = ROL::makePtr(dim, 0.0); ROL::Ptr d_ptr = ROL::makePtr(dim, 0.0); ROL::Ptr gd_ptr = ROL::makePtr(dim, 0.0); ROL::Ptr v_ptr = ROL::makePtr(dim, 0.0); ROL::Ptr vc_ptr = ROL::makePtr(nc, 0.0); ROL::Ptr vl_ptr = ROL::makePtr(nc, 0.0); ROL::Ptr> xtest = ROL::makePtr>(xtest_ptr); ROL::Ptr> g = ROL::makePtr>(g_ptr); ROL::Ptr> d = ROL::makePtr>(d_ptr); ROL::Ptr> gd = ROL::makePtr>(gd_ptr); ROL::Ptr> v = ROL::makePtr>(v_ptr); ROL::Ptr> vc = ROL::makePtr>(vc_ptr); ROL::Ptr> vl = ROL::makePtr>(vl_ptr); // set xtest, d, v for (uint i=0; icheckGradient(*xtest, *g, *d, true, *outStream); *outStream << std::endl; obj->checkHessVec(*xtest, *g, *v, true, *outStream); *outStream << std::endl; obj->checkHessSym(*xtest, *g, *d, *v, true, *outStream); *outStream << std::endl; constr->checkApplyJacobian(*xtest, *v, *vc, true, *outStream); *outStream << std::endl; constr->checkApplyAdjointJacobian(*xtest, *vl, *vc, *g, true, *outStream); *outStream << std::endl; constr->checkApplyAdjointHessian(*xtest, *vl, *d, *g, true, *outStream); *outStream << std::endl; ROL::Ptr v1_ptr = ROL::makePtr(dim, 0.0); ROL::Ptr v2_ptr = ROL::makePtr(nc, 0.0); ROL::Ptr> v1 = ROL::makePtr>(v1_ptr); ROL::Ptr> v2 = ROL::makePtr>(v2_ptr); RealT augtol = 1e-8; constr->solveAugmentedSystem(*v1, *v2, *gd, *vc, *xtest, augtol); // Define problem. vl->zero(); vc->zero(); g->zero(); ROL::Ptr> problem = ROL::makePtr>(obj,x,g); problem->addConstraint("Equality",constr,ROL::staticPtrCast>(vl),ROL::staticPtrCast>(vc)); problem->finalize(false,true,*outStream); // Define algorithm. ROL::ParameterList parlist; std::string stepname = "Augmented Lagrangian"; parlist.sublist("General").set("Output Level",1); parlist.sublist("Step").set("Type",stepname); parlist.sublist("Step").sublist(stepname).sublist("Optimality System Solver").set("Nominal Relative Tolerance",1e-4); parlist.sublist("Step").sublist(stepname).sublist("Optimality System Solver").set("Fix Tolerance",true); parlist.sublist("Step").sublist(stepname).sublist("Tangential Subproblem Solver").set("Iteration Limit",20); parlist.sublist("Step").sublist(stepname).sublist("Tangential Subproblem Solver").set("Relative Tolerance",1e-2); parlist.sublist("Step").sublist(stepname).set("Output Level",0); parlist.sublist("Status Test").set("Gradient Tolerance",1.e-12); parlist.sublist("Status Test").set("Constraint Tolerance",1.e-12); parlist.sublist("Status Test").set("Step Tolerance",1.e-18); parlist.sublist("Status Test").set("Iteration Limit",100); ROL::Ptr> solver = ROL::makePtr>(problem,parlist); // Run Algorithm //(*x_ptr)[0] = 3.0; (*x_ptr)[1] = 2.0; (*x_ptr)[2] = 2.0; (*x_ptr)[3] = 1.0; (*x_ptr)[4] = 1.0; //(*x_ptr)[0] = -5.0; (*x_ptr)[1] = -5.0; (*x_ptr)[2] = -5.0; (*x_ptr)[3] = -6.0; (*x_ptr)[4] = -6.0; solver->solve(*outStream); // Compute Error *outStream << "\nReference solution x_r =\n"; *outStream << std::scientific << " " << (*sol_ptr)[0] << "\n"; *outStream << std::scientific << " " << (*sol_ptr)[1] << "\n"; *outStream << std::scientific << " " << (*sol_ptr)[2] << "\n"; *outStream << std::scientific << " " << (*sol_ptr)[3] << "\n"; *outStream << std::scientific << " " << (*sol_ptr)[4] << "\n"; *outStream << "\nOptimal solution x =\n"; *outStream << std::scientific << " " << (*x_ptr)[0] << "\n"; *outStream << std::scientific << " " << (*x_ptr)[1] << "\n"; *outStream << std::scientific << " " << (*x_ptr)[2] << "\n"; *outStream << std::scientific << " " << (*x_ptr)[3] << "\n"; *outStream << std::scientific << " " << (*x_ptr)[4] << "\n"; x->axpy(-1.0, *sol); RealT abserr = x->norm(); RealT relerr = abserr/sol->norm(); *outStream << std::scientific << "\n Absolute Error: " << abserr; *outStream << std::scientific << "\n Relative Error: " << relerr << "\n"; if ( relerr > sqrt(ROL::ROL_EPSILON()) ) { errorFlag += 1; } } catch (std::logic_error& err) { *outStream << err.what() << "\n"; errorFlag = -1000; }; // end try if (errorFlag != 0) std::cout << "End Result: TEST FAILED\n"; else std::cout << "End Result: TEST PASSED\n"; return 0; }