// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #include "Teuchos_GlobalMPISession.hpp" #include "Teuchos_Comm.hpp" #include "Teuchos_DefaultComm.hpp" #include "Teuchos_CommHelpers.hpp" #include "ROL_ParameterList.hpp" #include "ROL_Stream.hpp" #include "ROL_StdVector.hpp" #include "ROL_StdBoundConstraint.hpp" #include "ROL_Types.hpp" #include "ROL_MonteCarloGenerator.hpp" #include "ROL_StdTeuchosBatchManager.hpp" #include "ROL_OptimizationSolver.hpp" typedef double RealT; template class ParametrizedObjectiveEx7 : public ROL::Objective { public: Real value( const ROL::Vector &x, Real &tol ) { ROL::Ptr > ex = dynamic_cast&>(x).getVector(); Real quad(0), lin(0); std::vector p = ROL::Objective::getParameter(); unsigned size = static_cast(ex->size()); for ( unsigned i = 0; i < size; i++ ) { quad += (*ex)[i]*(*ex)[i]; lin += (*ex)[i]*p[i+1]; } return std::exp(p[0])*quad + lin + p[size+1]; } void gradient( ROL::Vector &g, const ROL::Vector &x, Real &tol ) { ROL::Ptr > ex = dynamic_cast&>(x).getVector(); ROL::Ptr > eg = dynamic_cast&>(g).getVector(); std::vector p = ROL::Objective::getParameter(); unsigned size = static_cast(ex->size()); const Real two(2); for ( unsigned i = 0; i < size; i++ ) { (*eg)[i] = two*std::exp(p[0])*(*ex)[i] + p[i+1]; } } void hessVec( ROL::Vector &hv, const ROL::Vector &v, const ROL::Vector &x, Real &tol ) { ROL::Ptr > ev = dynamic_cast&>(v).getVector(); ROL::Ptr > ehv = dynamic_cast&>(hv).getVector(); std::vector p = ROL::Objective::getParameter(); unsigned size = static_cast(ev->size()); const Real two(2); for ( unsigned i = 0; i < size; i++ ) { (*ehv)[i] = two*std::exp(p[0])*(*ev)[i]; } } }; void setUpAndSolve(ROL::ParameterList &list, ROL::Ptr > &pObj, ROL::Ptr > &sampler, ROL::Ptr > &x, ROL::Ptr > &bnd, std::ostream & outStream) { ROL::OptimizationProblem opt(pObj,x,bnd); opt.setStochasticObjective(list,sampler); outStream << "\nCheck Derivatives of Stochastic Objective Function\n"; opt.check(outStream); // Run ROL algorithm list.sublist("Step").set("Type","Trust Region"); ROL::OptimizationSolver solver(opt,list); solver.solve(outStream); } template Real random(const ROL::Ptr > &commptr) { Real val(0); if ( Teuchos::rank(*commptr)==0 ) { //srand(time(NULL)); srand(10); val = (Real)rand()/(Real)RAND_MAX; } Teuchos::broadcast(*commptr,0,&val); return val; } void setRandomVector(std::vector &x, const ROL::Ptr > &commptr) { unsigned dim = static_cast(x.size()); for ( unsigned i = 0; i < dim; i++ ) { x[i] = random(commptr); } } void printSolution(const std::vector &x, std::ostream & outStream) { unsigned dim = static_cast(x.size()); outStream << "x = ("; for ( unsigned i = 0; i < dim-1; i++ ) { outStream << x[i] << ", "; } outStream << x[dim-1] << ")\n"; } int main(int argc, char* argv[]) { Teuchos::GlobalMPISession mpiSession(&argc, &argv); ROL::Ptr > commptr = ROL::toPtr(Teuchos::DefaultComm::getComm()); // 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 && commptr->getRank()==0) outStream = ROL::makePtrFromRef(std::cout); else outStream = ROL::makePtrFromRef(bhs); int errorFlag = 0; try { /**********************************************************************************************/ /************************* CONSTRUCT ROL ALGORITHM ********************************************/ /**********************************************************************************************/ // Get ROL parameterlist std::string filename = "input_07.xml"; auto parlist = ROL::getParametersFromXmlFile( filename ); ROL::ParameterList list = *parlist; // Build ROL algorithm ROL::Ptr > algo; /**********************************************************************************************/ /************************* CONSTRUCT SOL COMPONENTS *******************************************/ /**********************************************************************************************/ // Build vectors const unsigned dim = 4; ROL::Ptr > x_ptr = ROL::makePtr>(dim); ROL::Ptr > d_ptr = ROL::makePtr>(dim); ROL::Ptr > x = ROL::makePtr>(x_ptr); ROL::Ptr > d = ROL::makePtr>(d_ptr); setRandomVector(*x_ptr,commptr); setRandomVector(*d_ptr,commptr); // Build samplers const RealT zero(0), one(1); const int nSamp = 1000; const unsigned sdim = dim + 2; std::vector tmp = {-one, one}; std::vector > bounds(sdim,tmp); ROL::Ptr > bman = ROL::makePtr>(commptr); ROL::Ptr > sampler = ROL::makePtr>(nSamp,bounds,bman,false,false,100); // Build risk-averse objective function ROL::Ptr > pObj = ROL::makePtr>(); // Build bound constraints std::vector l(dim,zero); std::vector u(dim,one); ROL::Ptr > bnd = ROL::makePtr>(l,u); bnd->deactivate(); // Test parametrized objective functions *outStream << "Check Derivatives of Parametrized Objective Function\n"; pObj->setParameter(sampler->getMyPoint(0)); pObj->checkGradient(*x,*d,true,*outStream); pObj->checkHessVec(*x,*d,true,*outStream); /**********************************************************************************************/ /************************* MEAN PLUS HMCR *****************************************************/ /**********************************************************************************************/ *outStream << "\nMEAN PLUS HIGHER MOMENT COHERENT RISK MEASURE\n"; setRandomVector(*x_ptr,commptr); setUpAndSolve(list,pObj,sampler,x,bnd,*outStream); printSolution(*x_ptr,*outStream); } 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; }