Debian Codegraph @

packages/rol/adapters/teuchos/test/sol/test_07.cpp

raw
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 #include "Teuchos_GlobalMPISession.hpp"
11 #include "Teuchos_Comm.hpp"
12 #include "Teuchos_DefaultComm.hpp"
13 #include "Teuchos_CommHelpers.hpp"
14
15 #include "ROL_ParameterList.hpp"
16 #include "ROL_Stream.hpp"
17
18 #include "ROL_StdVector.hpp"
19 #include "ROL_StdBoundConstraint.hpp"
20 #include "ROL_Types.hpp"
21
22 #include "ROL_MonteCarloGenerator.hpp"
23 #include "ROL_StdTeuchosBatchManager.hpp"
24
25 #include "ROL_OptimizationSolver.hpp"
26
27 typedef double RealT;
28
29 template<class Real>
30 class ParametrizedObjectiveEx7 : public ROL::Objective<Real> {
31 public:
32 Real value( const ROL::Vector<Real> &x, Real &tol ) {
33 ROL::Ptr<const std::vector<Real> > ex
34 = dynamic_cast<const ROL::StdVector<Real>&>(x).getVector();
35 Real quad(0), lin(0);
36 std::vector<Real> p = ROL::Objective<Real>::getParameter();
37 unsigned size = static_cast<unsigned>(ex->size());
38 for ( unsigned i = 0; i < size; i++ ) {
39 quad += (*ex)[i]*(*ex)[i];
40 lin += (*ex)[i]*p[i+1];
41 }
42 return std::exp(p[0])*quad + lin + p[size+1];
43 }
44
45 void gradient( ROL::Vector<Real> &g, const ROL::Vector<Real> &x, Real &tol ) {
46 ROL::Ptr<const std::vector<Real> > ex
47 = dynamic_cast<const ROL::StdVector<Real>&>(x).getVector();
48 ROL::Ptr<std::vector<Real> > eg
49 = dynamic_cast<ROL::StdVector<Real>&>(g).getVector();
50 std::vector<Real> p = ROL::Objective<Real>::getParameter();
51 unsigned size = static_cast<unsigned>(ex->size());
52 const Real two(2);
53 for ( unsigned i = 0; i < size; i++ ) {
54 (*eg)[i] = two*std::exp(p[0])*(*ex)[i] + p[i+1];
55 }
56 }
57
58 void hessVec( ROL::Vector<Real> &hv, const ROL::Vector<Real> &v,
59 const ROL::Vector<Real> &x, Real &tol ) {
60 ROL::Ptr<const std::vector<Real> > ev
61 = dynamic_cast<const ROL::StdVector<Real>&>(v).getVector();
62 ROL::Ptr<std::vector<Real> > ehv
63 = dynamic_cast<ROL::StdVector<Real>&>(hv).getVector();
64 std::vector<Real> p = ROL::Objective<Real>::getParameter();
65 unsigned size = static_cast<unsigned>(ev->size());
66 const Real two(2);
67 for ( unsigned i = 0; i < size; i++ ) {
68 (*ehv)[i] = two*std::exp(p[0])*(*ev)[i];
69 }
70 }
71 };
72
73 void setUpAndSolve(ROL::ParameterList &list,
74 ROL::Ptr<ROL::Objective<RealT> > &pObj,
75 ROL::Ptr<ROL::SampleGenerator<RealT> > &sampler,
76 ROL::Ptr<ROL::Vector<RealT> > &x,
77 ROL::Ptr<ROL::BoundConstraint<RealT> > &bnd,
78 std::ostream & outStream) {
79 ROL::OptimizationProblem<RealT> opt(pObj,x,bnd);
80 opt.setStochasticObjective(list,sampler);
81 outStream << "\nCheck Derivatives of Stochastic Objective Function\n";
82 opt.check(outStream);
83 // Run ROL algorithm
84 list.sublist("Step").set("Type","Trust Region");
85 ROL::OptimizationSolver<RealT> solver(opt,list);
86 solver.solve(outStream);
87 }
88
89 template<class Real>
90 Real random(const ROL::Ptr<const Teuchos::Comm<int> > &commptr) {
91 Real val(0);
92 if ( Teuchos::rank<int>(*commptr)==0 ) {
93 //srand(time(NULL));
94 srand(10);
95 val = (Real)rand()/(Real)RAND_MAX;
96 }
97 Teuchos::broadcast<int,Real>(*commptr,0,&val);
98 return val;
99 }
100
101 void setRandomVector(std::vector<RealT> &x,
102 const ROL::Ptr<const Teuchos::Comm<int> > &commptr) {
103 unsigned dim = static_cast<unsigned>(x.size());
104 for ( unsigned i = 0; i < dim; i++ ) {
105 x[i] = random<RealT>(commptr);
106 }
107 }
108
109 void printSolution(const std::vector<RealT> &x,
110 std::ostream & outStream) {
111 unsigned dim = static_cast<unsigned>(x.size());
112 outStream << "x = (";
113 for ( unsigned i = 0; i < dim-1; i++ ) {
114 outStream << x[i] << ", ";
115 }
116 outStream << x[dim-1] << ")\n";
117 }
118
119 int main(int argc, char* argv[]) {
120
121 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
122 ROL::Ptr<const Teuchos::Comm<int> > commptr =
123 ROL::toPtr(Teuchos::DefaultComm<int>::getComm());
124
125 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
126 int iprint = argc - 1;
127 ROL::Ptr<std::ostream> outStream;
128 ROL::nullstream bhs; // outputs nothing
129 if (iprint > 0 && commptr->getRank()==0)
130 outStream = ROL::makePtrFromRef(std::cout);
131 else
132 outStream = ROL::makePtrFromRef(bhs);
133
134 int errorFlag = 0;
135
136 try {
137 /**********************************************************************************************/
138 /************************* CONSTRUCT ROL ALGORITHM ********************************************/
139 /**********************************************************************************************/
140 // Get ROL parameterlist
141 std::string filename = "input_07.xml";
142
143 auto parlist = ROL::getParametersFromXmlFile( filename );
144 ROL::ParameterList list = *parlist;
145 // Build ROL algorithm
146 ROL::Ptr<ROL::Algorithm<RealT> > algo;
147 /**********************************************************************************************/
148 /************************* CONSTRUCT SOL COMPONENTS *******************************************/
149 /**********************************************************************************************/
150 // Build vectors
151 const unsigned dim = 4;
152 ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(dim);
153 ROL::Ptr<std::vector<RealT> > d_ptr = ROL::makePtr<std::vector<RealT>>(dim);
154 ROL::Ptr<ROL::Vector<RealT> > x = ROL::makePtr<ROL::StdVector<RealT>>(x_ptr);
155 ROL::Ptr<ROL::Vector<RealT> > d = ROL::makePtr<ROL::StdVector<RealT>>(d_ptr);
156 setRandomVector(*x_ptr,commptr);
157 setRandomVector(*d_ptr,commptr);
158 // Build samplers
159 const RealT zero(0), one(1);
160 const int nSamp = 1000;
161 const unsigned sdim = dim + 2;
162 std::vector<RealT> tmp = {-one, one};
163 std::vector<std::vector<RealT> > bounds(sdim,tmp);
164 ROL::Ptr<ROL::BatchManager<RealT> > bman =
165 ROL::makePtr<ROL::StdTeuchosBatchManager<RealT,int>>(commptr);
166 ROL::Ptr<ROL::SampleGenerator<RealT> > sampler =
167 ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(nSamp,bounds,bman,false,false,100);
168 // Build risk-averse objective function
169 ROL::Ptr<ROL::Objective<RealT> > pObj =
170 ROL::makePtr<ParametrizedObjectiveEx7<RealT>>();
171 // Build bound constraints
172 std::vector<RealT> l(dim,zero);
173 std::vector<RealT> u(dim,one);
174 ROL::Ptr<ROL::BoundConstraint<RealT> > bnd =
175 ROL::makePtr<ROL::StdBoundConstraint<RealT>>(l,u);
176 bnd->deactivate();
177 // Test parametrized objective functions
178 *outStream << "Check Derivatives of Parametrized Objective Function\n";
179 pObj->setParameter(sampler->getMyPoint(0));
180 pObj->checkGradient(*x,*d,true,*outStream);
181 pObj->checkHessVec(*x,*d,true,*outStream);
182 /**********************************************************************************************/
183 /************************* MEAN PLUS HMCR *****************************************************/
184 /**********************************************************************************************/
185 *outStream << "\nMEAN PLUS HIGHER MOMENT COHERENT RISK MEASURE\n";
186 setRandomVector(*x_ptr,commptr);
187 setUpAndSolve(list,pObj,sampler,x,bnd,*outStream);
188 printSolution(*x_ptr,*outStream);
189 }
190 catch (std::logic_error& err) {
191 *outStream << err.what() << "\n";
192 errorFlag = -1000;
193 }; // end try
194
195 if (errorFlag != 0)
196 std::cout << "End Result: TEST FAILED\n";
197 else
198 std::cout << "End Result: TEST PASSED\n";
199
200 return 0;
201 }