Debian Codegraph @

packages/rol/adapters/teuchos/test/sol/test_03.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 "ROL_ParameterList.hpp"
11 #include "ROL_Stream.hpp"
12 #include "Teuchos_GlobalMPISession.hpp"
13 #include "Teuchos_Comm.hpp"
14 #include "Teuchos_DefaultComm.hpp"
15 #include "Teuchos_CommHelpers.hpp"
16
17 #include "ROL_StdVector.hpp"
18 #include "ROL_StdBoundConstraint.hpp"
19 #include "ROL_Types.hpp"
20
21 #include "ROL_MonteCarloGenerator.hpp"
22 #include "ROL_StdTeuchosBatchManager.hpp"
23
24 #include "ROL_OptimizationSolver.hpp"
25 #include "ROL_RiskMeasureFactory.hpp"
26
27 typedef double RealT;
28
29 template<class Real>
30 class ParametrizedObjectiveEx3 : 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 x->zero();
80 ROL::OptimizationProblem<RealT> problem(pObj,x,bnd);
81 problem.setStochasticObjective(list,sampler);
82 //outStream << "\nCheck Derivatives of Stochastic Objective Function\n";
83 //problem.check(outStream);
84 ROL::OptimizationSolver<RealT> solver(problem,list);
85 solver.solve(outStream);
86 }
87
88 template<class Real>
89 Real random(const ROL::Ptr<const Teuchos::Comm<int> > &commptr) {
90 Real val(0);
91 if ( Teuchos::rank<int>(*commptr)==0 ) {
92 srand(time(NULL));
93 val = (Real)rand()/(Real)RAND_MAX;
94 }
95 Teuchos::broadcast<int,Real>(*commptr,0,&val);
96 return val;
97 }
98
99 void setRandomVector(std::vector<RealT> &x,
100 const ROL::Ptr<const Teuchos::Comm<int> > &commptr) {
101 unsigned dim = static_cast<unsigned>(x.size());
102 for ( unsigned i = 0; i < dim; i++ ) {
103 x[i] = random<RealT>(commptr);
104 }
105 }
106
107 void printSolution(const std::vector<RealT> &x,
108 std::ostream & outStream) {
109 unsigned dim = static_cast<unsigned>(x.size());
110 outStream << "x = (";
111 for ( unsigned i = 0; i < dim-1; i++ ) {
112 outStream << x[i] << ", ";
113 }
114 outStream << x[dim-1] << ")\n";
115 }
116
117 int main(int argc, char* argv[]) {
118
119 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
120 ROL::Ptr<const Teuchos::Comm<int> > commptr =
121 ROL::toPtr(Teuchos::DefaultComm<int>::getComm());
122
123 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
124 int iprint = argc - 1;
125 ROL::Ptr<std::ostream> outStream;
126 ROL::nullstream bhs; // outputs nothing
127 if (iprint > 0 && commptr->getRank()==0)
128 outStream = ROL::makePtrFromRef(std::cout);
129 else
130 outStream = ROL::makePtrFromRef(bhs);
131
132 int errorFlag = 0;
133
134 try {
135 /**********************************************************************************************/
136 /************************* CONSTRUCT ROL ALGORITHM ********************************************/
137 /**********************************************************************************************/
138 // Get ROL parameterlist
139 std::string filename = "input_03.xml";
140
141 auto parlist = ROL::getParametersFromXmlFile( filename );
142 ROL::ParameterList list = *parlist;
143 // Build ROL algorithm
144 ROL::Ptr<ROL::Algorithm<RealT> > algo;
145 /**********************************************************************************************/
146 /************************* CONSTRUCT SOL COMPONENTS *******************************************/
147 /**********************************************************************************************/
148 // Build vectors
149 unsigned dim = 4;
150 ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(dim,0.0);
151 ROL::Ptr<ROL::Vector<RealT> > x = ROL::makePtr<ROL::StdVector<RealT>>(x_ptr);
152 ROL::Ptr<std::vector<RealT> > d_ptr = ROL::makePtr<std::vector<RealT>>(dim,0.0);
153 ROL::Ptr<ROL::Vector<RealT> > d = ROL::makePtr<ROL::StdVector<RealT>>(d_ptr);
154 //setRandomVector(*x_ptr,commptr);
155 //setRandomVector(*d_ptr,commptr);
156 // Build samplers
157 int nSamp = 1000;
158 unsigned sdim = dim + 2;
159 std::vector<RealT> tmp(2,0.); tmp[0] = -1.; tmp[1] = 1.;
160 std::vector<std::vector<RealT> > bounds(sdim,tmp);
161 ROL::Ptr<ROL::BatchManager<RealT> > bman =
162 ROL::makePtr<ROL::StdTeuchosBatchManager<RealT,int>>(commptr);
163 ROL::Ptr<ROL::SampleGenerator<RealT> > sampler =
164 ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(nSamp,bounds,bman,false,false,100);
165 // Build risk-averse objective function
166 ROL::Ptr<ROL::Objective<RealT> > pObj =
167 ROL::makePtr<ParametrizedObjectiveEx3<RealT>>();
168 // Build bound constraints
169 std::vector<RealT> l(dim,0.0);
170 std::vector<RealT> u(dim,1.0);
171 ROL::Ptr<ROL::BoundConstraint<RealT> > bnd =
172 ROL::makePtr<ROL::StdBoundConstraint<RealT>>(l,u);
173 bnd->deactivate();
174 // Test parametrized objective functions
175 *outStream << "Check Derivatives of Parametrized Objective Function\n";
176 pObj->setParameter(sampler->getMyPoint(0));
177 pObj->checkGradient(*x,*d,true,*outStream);
178 pObj->checkHessVec(*x,*d,true,*outStream);
179 // Storage for solutions
180 std::vector<std::tuple<std::string,std::string,std::vector<RealT>>> solution;
181 /**********************************************************************************************/
182 /************************* MEAN VALUE *********************************************************/
183 /**********************************************************************************************/
184 *outStream << "\nMean Value\n";
185 list.sublist("SOL").set("Type","Mean Value");
186 //setRandomVector(*x_ptr,commptr);
187 setUpAndSolve(list,pObj,sampler,x,bnd,*outStream);
188 printSolution(*x_ptr,*outStream);
189 solution.push_back(std::tuple<std::string,std::string,std::vector<RealT>>("","Mean Value",*x_ptr));
190 /**********************************************************************************************/
191 /************************* RISK NEUTRAL *******************************************************/
192 /**********************************************************************************************/
193 *outStream << "\nRisk Neutral\n";
194 list.sublist("SOL").set("Type","Risk Neutral");
195 //setRandomVector(*x_ptr,commptr);
196 setUpAndSolve(list,pObj,sampler,x,bnd,*outStream);
197 printSolution(*x_ptr,*outStream);
198 solution.push_back(std::tuple<std::string,std::string,std::vector<RealT>>("","Risk Neutral",*x_ptr));
199 /**********************************************************************************************/
200 /************************* RISK AVERSE ********************************************************/
201 /**********************************************************************************************/
202 for (ROL::ERiskMeasure er = ROL::RISKMEASURE_CVAR; er != ROL::RISKMEASURE_LAST; er++) {
203 std::string name = ROL::ERiskMeasureToString(er);
204 *outStream << std::endl << name << std::endl;
205 list.sublist("SOL").set("Type","Risk Averse");
206 list.sublist("SOL").sublist("Risk Measure").set("Name",name);
207 if (er == ROL::RISKMEASURE_MEANDEVIATION ||
208 er == ROL::RISKMEASURE_MEANVARIANCE ||
209 er == ROL::RISKMEASURE_MEANDEVIATIONFROMTARGET ||
210 er == ROL::RISKMEASURE_MEANVARIANCEFROMTARGET) {
211 list.sublist("SOL").sublist("Risk Measure").sublist(name).set("Deviation Type","Absolute");
212 //setRandomVector(*x_ptr,commptr);
213 setUpAndSolve(list,pObj,sampler,x,bnd,*outStream);
214 printSolution(*x_ptr,*outStream);
215 solution.push_back(std::tuple<std::string,std::string,std::vector<RealT>>("Risk",name,*x_ptr));
216
217 list.sublist("SOL").sublist("Risk Measure").sublist(name).set("Deviation Type","Upper");
218 //setRandomVector(*x_ptr,commptr);
219 setUpAndSolve(list,pObj,sampler,x,bnd,*outStream);
220 printSolution(*x_ptr,*outStream);
221 solution.push_back(std::tuple<std::string,std::string,std::vector<RealT>>("Risk",name,*x_ptr));
222 }
223 else if (er == ROL::RISKMEASURE_CHEBYSHEVSPECTRAL) {
224 list.sublist("SOL").sublist("Risk Measure").sublist(name).set("Weight Type",1);
225 //setRandomVector(*x_ptr,commptr);
226 setUpAndSolve(list,pObj,sampler,x,bnd,*outStream);
227 printSolution(*x_ptr,*outStream);
228 solution.push_back(std::tuple<std::string,std::string,std::vector<RealT>>("Risk",name,*x_ptr));
229
230 list.sublist("SOL").sublist("Risk Measure").sublist(name).set("Weight Type",2);
231 //setRandomVector(*x_ptr,commptr);
232 setUpAndSolve(list,pObj,sampler,x,bnd,*outStream);
233 printSolution(*x_ptr,*outStream);
234 solution.push_back(std::tuple<std::string,std::string,std::vector<RealT>>("Risk",name,*x_ptr));
235
236 list.sublist("SOL").sublist("Risk Measure").sublist(name).set("Weight Type",3);
237 //setRandomVector(*x_ptr,commptr);
238 setUpAndSolve(list,pObj,sampler,x,bnd,*outStream);
239 printSolution(*x_ptr,*outStream);
240 solution.push_back(std::tuple<std::string,std::string,std::vector<RealT>>("Risk",name,*x_ptr));
241 }
242 else {
243 //setRandomVector(*x_ptr,commptr);
244 setUpAndSolve(list,pObj,sampler,x,bnd,*outStream);
245 printSolution(*x_ptr,*outStream);
246 solution.push_back(std::tuple<std::string,std::string,std::vector<RealT>>("Risk",name,*x_ptr));
247 }
248 }
249 /**********************************************************************************************/
250 /************************* CONVEX COMBINATION OF RISK MEASURES ********************************/
251 /**********************************************************************************************/
252 *outStream << "\nConvex Combination if Risk Measures\n";
253 list.sublist("SOL").set("Type","Risk Averse");
254 list.sublist("SOL").sublist("Risk Measure").set("Name","Convex Combination Risk Measure");
255 //setRandomVector(*x_ptr,commptr);
256 setUpAndSolve(list,pObj,sampler,x,bnd,*outStream);
257 printSolution(*x_ptr,*outStream);
258 solution.push_back(std::tuple<std::string,std::string,std::vector<RealT>>("Risk","Convex Combination of Risk Measures",*x_ptr));
259 /**********************************************************************************************/
260 /************************* DEVIATION **********************************************************/
261 /**********************************************************************************************/
262 for (ROL::EDeviationMeasure ed = ROL::DEVIATIONMEASURE_MEANVARIANCEQUADRANGLE; ed != ROL::DEVIATIONMEASURE_LAST; ed++) {
263 std::string name = ROL::EDeviationMeasureToString(ed);
264 *outStream << std::endl << "Deviation: " << name << std::endl;
265 list.sublist("SOL").set("Type","Deviation");
266 list.sublist("SOL").sublist("Deviation Measure").set("Name",name);
267 //setRandomVector(*x_ptr);
268 setUpAndSolve(list,pObj,sampler,x,bnd,*outStream);
269 printSolution(*x_ptr,*outStream);
270 solution.push_back(std::tuple<std::string,std::string,std::vector<RealT>>("Deviation",name,*x_ptr));
271 }
272 /**********************************************************************************************/
273 /************************* REGRET *************************************************************/
274 /**********************************************************************************************/
275 for (ROL::ERegretMeasure er = ROL::REGRETMEASURE_MEANABSOLUTELOSS; er != ROL::REGRETMEASURE_LAST; er++) {
276 std::string name = ROL::ERegretMeasureToString(er);
277 *outStream << std::endl << "Regret: " << name << std::endl;
278 list.sublist("SOL").set("Type","Regret");
279 list.sublist("SOL").sublist("Regret Measure").set("Name",name);
280 //setRandomVector(*x_ptr);
281 setUpAndSolve(list,pObj,sampler,x,bnd,*outStream);
282 printSolution(*x_ptr,*outStream);
283 solution.push_back(std::tuple<std::string,std::string,std::vector<RealT>>("Regret",name,*x_ptr));
284 }
285 /**********************************************************************************************/
286 /************************* PROBABILITY ********************************************************/
287 /**********************************************************************************************/
288 for (ROL::EProbability ep = ROL::PROBABILITY_BPOE; ep != ROL::PROBABILITY_LAST; ep++) {
289 std::string name = ROL::EProbabilityToString(ep);
290 *outStream << std::endl << "Probability: " << name << std::endl;
291 list.sublist("SOL").set("Type","Probability");
292 list.sublist("SOL").sublist("Probability").set("Name",name);
293 //setRandomVector(*x_ptr);
294 setUpAndSolve(list,pObj,sampler,x,bnd,*outStream);
295 printSolution(*x_ptr,*outStream);
296 solution.push_back(std::tuple<std::string,std::string,std::vector<RealT>>("Probability",name,*x_ptr));
297 }
298
299 std::vector<std::tuple<std::string,std::string,std::vector<RealT>>>::iterator it;
300 *outStream << std::endl << std::scientific << std::setprecision(6);
301 for (it = solution.begin(); it != solution.end(); ++it) {
302 *outStream << " ";
303 *outStream << std::setw(20) << std::left << std::get<0>(*it);
304 *outStream << std::setw(50) << std::left << std::get<1>(*it);
305 for (unsigned i = 0; i < dim; ++i) {
306 *outStream << std::setw(18) << std::left << (std::get<2>(*it))[i];
307 }
308 *outStream << std::endl;
309 }
310 *outStream << std::endl;
311 }
312 catch (std::logic_error& err) {
313 *outStream << err.what() << "\n";
314 errorFlag = -1000;
315 }; // end try
316
317 if (errorFlag != 0)
318 std::cout << "End Result: TEST FAILED\n";
319 else
320 std::cout << "End Result: TEST PASSED\n";
321
322 return 0;
323 }