Debian Codegraph @

packages/rol/adapters/epetra/test/sol/test_01.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_ParameterList.hpp"
11 #include "Teuchos_XMLParameterListHelpers.hpp"
12 #include "ROL_Stream.hpp"
13
14 #ifdef HAVE_MPI
15 #include "Epetra_MpiComm.h"
16 #else
17 #include "Epetra_SerialComm.h"
18 #endif
19
20 #include "ROL_StdVector.hpp"
21 #include "ROL_StdBoundConstraint.hpp"
22 #include "ROL_Types.hpp"
23 #include "ROL_Algorithm.hpp"
24 #include "ROL_TrustRegionStep.hpp"
25 #include "ROL_StatusTest.hpp"
26
27 #include "ROL_Objective.hpp"
28 #include "ROL_MonteCarloGenerator.hpp"
29 #include "ROL_StdEpetraBatchManager.hpp"
30
31 #include "ROL_OptimizationProblem.hpp"
32
33 typedef double RealT;
34
35 template<class Real>
36 class ParametrizedObjectiveEx3 : public ROL::Objective<Real> {
37 public:
38 Real value( const ROL::Vector<Real> &x, Real &tol ) {
39 ROL::Ptr<const std::vector<Real> > ex =
40 (dynamic_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &&>(x))).getVector();
41 Real quad = 0.0, lin = 0.0;
42 std::vector<Real> p = this->getParameter();
43 unsigned size = ex->size();
44 for ( unsigned i = 0; i < size; i++ ) {
45 quad += (*ex)[i]*(*ex)[i];
46 lin += (*ex)[i]*p[i+1];
47 }
48 return std::exp(p[0])*quad + lin + p[size+1];
49 }
50
51 void gradient( ROL::Vector<Real> &g, const ROL::Vector<Real> &x, Real &tol ) {
52 ROL::Ptr<const std::vector<Real> > ex =
53 (dynamic_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &&>(x))).getVector();
54 ROL::Ptr<std::vector<Real> > eg =
55 ROL::constPtrCast<std::vector<Real> >((dynamic_cast<ROL::StdVector<Real>&>(g)).getVector());
56 std::vector<Real> p = this->getParameter();
57 unsigned size = ex->size();
58 for ( unsigned i = 0; i < size; i++ ) {
59 (*eg)[i] = 2.0*std::exp(p[0])*(*ex)[i] + p[i+1];
60 }
61 }
62
63 void hessVec( ROL::Vector<Real> &hv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &x, Real &tol ) {
64 ROL::Ptr<const std::vector<Real> > ex =
65 (dynamic_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &&>(x))).getVector();
66 ROL::Ptr<const std::vector<Real> > ev =
67 (dynamic_cast<ROL::StdVector<Real> >(const_cast<ROL::Vector<Real> &&>(v))).getVector();
68 ROL::Ptr<std::vector<Real> > ehv =
69 ROL::constPtrCast<std::vector<Real> >((dynamic_cast<ROL::StdVector<Real>&>(hv)).getVector());
70 std::vector<Real> p = this->getParameter();
71 unsigned size = ex->size();
72 for ( unsigned i = 0; i < size; i++ ) {
73 (*ehv)[i] = 2.0*std::exp(p[0])*(*ev)[i];
74 }
75 }
76 };
77
78 void setUpAndSolve(Teuchos::ParameterList &list,
79 ROL::Ptr<ROL::Objective<RealT> > &pObj,
80 ROL::Ptr<ROL::SampleGenerator<RealT> > &sampler,
81 ROL::Ptr<ROL::Vector<RealT> > &x,
82 ROL::Ptr<ROL::Vector<RealT> > &d,
83 ROL::Ptr<ROL::BoundConstraint<RealT> > &bnd,
84 std::ostream & outStream) {
85 ROL::OptimizationProblem<RealT> opt(pObj,x,bnd);
86 opt.setStochasticObjective(list,sampler);
87 outStream << "\nCheck Derivatives of Stochastic Objective Function\n";
88 opt.checkObjectiveGradient(*d,true,outStream);
89 opt.checkObjectiveHessVec(*d,true,outStream);
90 // Run ROL algorithm
91 ROL::Ptr<ROL::Step<RealT>>
92 step = ROL::makePtr<ROL::TrustRegionStep<RealT>>(list);
93 ROL::Ptr<ROL::StatusTest<RealT>>
94 status = ROL::makePtr<ROL::StatusTest<RealT>>(list);
95 ROL::Algorithm<RealT> algo(step,status,false);
96 algo.run(opt,true,outStream);
97 }
98
99 template<class Real>
100 Real random(const ROL::Ptr<Epetra_Comm> &comm) {
101 Real val = 0.0;
102 if ( comm->MyPID()==0 ) {
103 val = (Real)rand()/(Real)RAND_MAX;
104 }
105 comm->Broadcast(&val,1,0);
106 return val;
107 }
108
109 void setRandomVector(std::vector<RealT> &x,
110 const ROL::Ptr<Epetra_Comm> &comm) {
111 unsigned dim = x.size();
112 for ( unsigned i = 0; i < dim; i++ ) {
113 x[i] = random<RealT>(comm);
114 }
115 }
116
117 void printSolution(const std::vector<RealT> &x,
118 std::ostream & outStream) {
119 unsigned dim = x.size();
120 outStream << "x = (";
121 for ( unsigned i = 0; i < dim-1; i++ ) {
122 outStream << x[i] << ", ";
123 }
124 outStream << x[dim-1] << ")\n";
125 }
126
127 int main(int argc, char* argv[]) {
128 ROL::Ptr<Epetra_Comm> comm;
129 #ifdef HAVE_MPI
130 Teuchos::GlobalMPISession mpiSession(&argc, &argv,0);
131 comm = ROL::makePtr<Epetra_MpiComm>(MPI_COMM_WORLD);
132 #else
133 comm = ROL::makePtr<Epetra_SerialComm>();
134 #endif
135
136 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
137 int iprint = argc - 1;
138 ROL::Ptr<std::ostream> outStream;
139 ROL::nullstream bhs; // outputs nothing
140 if (iprint > 0 && comm->MyPID()==0)
141 ROL::makePtrFromRef(std::cout);
142 else
143 ROL::makePtrFromRef(bhs);
144
145 int errorFlag = 0;
146
147 try {
148 /**********************************************************************************************/
149 /************************* CONSTRUCT ROL ALGORITHM ********************************************/
150 /**********************************************************************************************/
151 // Get ROL parameterlist
152 std::string filename = "input_01.xml";
153 Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() );
154 Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() );
155 Teuchos::ParameterList list = *parlist;
156 // Build ROL algorithm
157 ROL::Ptr<ROL::Algorithm<RealT> > algo;
158 /**********************************************************************************************/
159 /************************* CONSTRUCT SOL COMPONENTS *******************************************/
160 /**********************************************************************************************/
161 // Build vectors
162 unsigned dim = 4;
163 ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(dim,0.0);
164 ROL::Ptr<ROL::Vector<RealT> > x = ROL::makePtr<ROL::StdVector<RealT>>(x_ptr);
165 ROL::Ptr<std::vector<RealT> > d_ptr = ROL::makePtr<std::vector<RealT>>(dim,0.0);
166 ROL::Ptr<ROL::Vector<RealT> > d = ROL::makePtr<ROL::StdVector<RealT>>(d_ptr);
167 setRandomVector(*x_ptr,comm);
168 setRandomVector(*d_ptr,comm);
169 // Build samplers
170 int nSamp = 1000;
171 unsigned sdim = dim + 2;
172 std::vector<RealT> tmp(2,0.); tmp[0] = -1.; tmp[1] = 1.;
173 std::vector<std::vector<RealT> > bounds(sdim,tmp);
174 ROL::Ptr<ROL::BatchManager<RealT> > bman =
175 ROL::makePtr<ROL::StdEpetraBatchManager<RealT>>(comm);
176 ROL::Ptr<ROL::SampleGenerator<RealT> > sampler =
177 ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(nSamp,bounds,bman,false,false,100);
178 // Build risk-averse objective function
179 ROL::Ptr<ROL::Objective<RealT> > pObj =
180 ROL::makePtr<ParametrizedObjectiveEx3<RealT>>();
181 // Build bound constraints
182 std::vector<RealT> l(dim,0.0);
183 std::vector<RealT> u(dim,1.0);
184 ROL::Ptr<ROL::BoundConstraint<RealT> > bnd =
185 ROL::makePtr<ROL::StdBoundConstraint<RealT>>(l,u);
186 bnd->deactivate();
187 // Test parametrized objective functions
188 *outStream << "Check Derivatives of Parametrized Objective Function\n";
189 pObj->setParameter(sampler->getMyPoint(0));
190 pObj->checkGradient(*x,*d,true,*outStream);
191 pObj->checkHessVec(*x,*d,true,*outStream);
192 /**********************************************************************************************/
193 /************************* MEAN VALUE *********************************************************/
194 /**********************************************************************************************/
195 *outStream << "\nMEAN VALUE\n";
196 list.sublist("SOL").set("Type","Mean Value");
197 setRandomVector(*x_ptr,comm);
198 setUpAndSolve(list,pObj,sampler,x,d,bnd,*outStream);
199 printSolution(*x_ptr,*outStream);
200 /**********************************************************************************************/
201 /************************* RISK NEUTRAL *******************************************************/
202 /**********************************************************************************************/
203 *outStream << "\nRISK NEUTRAL\n";
204 list.sublist("SOL").set("Type","Risk Neutral");
205 setRandomVector(*x_ptr,comm);
206 setUpAndSolve(list,pObj,sampler,x,d,bnd,*outStream);
207 printSolution(*x_ptr,*outStream);
208 /**********************************************************************************************/
209 /************************* MEAN PLUS DEVIATION ************************************************/
210 /**********************************************************************************************/
211 *outStream << "\nMEAN PLUS DEVIATION\n";
212 list.sublist("SOL").set("Type","Risk Averse");
213 list.sublist("SOL").sublist("Risk Measure").set("Name","Mean Plus Deviation");
214 list.sublist("SOL").sublist("Risk Measure").sublist("Mean Plus Deviation").set("Deviation Type","Absolute");
215 setRandomVector(*x_ptr,comm);
216 setUpAndSolve(list,pObj,sampler,x,d,bnd,*outStream);
217 printSolution(*x_ptr,*outStream);
218 /**********************************************************************************************/
219 /************************* MEAN PLUS VARIANCE *************************************************/
220 /**********************************************************************************************/
221 *outStream << "\nMEAN PLUS VARIANCE\n";
222 list.sublist("SOL").set("Type","Risk Averse");
223 list.sublist("SOL").sublist("Risk Measure").set("Name","Mean Plus Variance");
224 list.sublist("SOL").sublist("Risk Measure").sublist("Mean Plus Variance").set("Deviation Type","Absolute");
225 setRandomVector(*x_ptr,comm);
226 setUpAndSolve(list,pObj,sampler,x,d,bnd,*outStream);
227 printSolution(*x_ptr,*outStream);
228 /**********************************************************************************************/
229 /************************* MEAN PLUS DEVIATION FROM TARGET ************************************/
230 /**********************************************************************************************/
231 *outStream << "\nMEAN PLUS DEVIATION FROM TARGET\n";
232 list.sublist("SOL").set("Type","Risk Averse");
233 list.sublist("SOL").sublist("Risk Measure").set("Name","Mean Plus Deviation From Target");
234 list.sublist("SOL").sublist("Risk Measure").sublist("Mean Plus Deviation From Target").set("Deviation Type","Absolute");
235 setRandomVector(*x_ptr,comm);
236 setUpAndSolve(list,pObj,sampler,x,d,bnd,*outStream);
237 printSolution(*x_ptr,*outStream);
238 /**********************************************************************************************/
239 /************************* MEAN PLUS VARIANCE FROM TARGET *************************************/
240 /**********************************************************************************************/
241 *outStream << "\nMEAN PLUS VARIANCE FROM TARGET\n";
242 list.sublist("SOL").set("Type","Risk Averse");
243 list.sublist("SOL").sublist("Risk Measure").set("Name","Mean Plus Variance From Target");
244 list.sublist("SOL").sublist("Risk Measure").sublist("Mean Plus Variance From Target").set("Deviation Type","Absolute");
245 setRandomVector(*x_ptr,comm);
246 setUpAndSolve(list,pObj,sampler,x,d,bnd,*outStream);
247 printSolution(*x_ptr,*outStream);
248 /**********************************************************************************************/
249 /************************* MEAN PLUS SEMIDEVIATION ********************************************/
250 /**********************************************************************************************/
251 *outStream << "\nMEAN PLUS SEMIDEVIATION\n";
252 list.sublist("SOL").set("Type","Risk Averse");
253 list.sublist("SOL").sublist("Risk Measure").set("Name","Mean Plus Deviation");
254 list.sublist("SOL").sublist("Risk Measure").sublist("Mean Plus Deviation").set("Deviation Type","Upper");
255 setRandomVector(*x_ptr,comm);
256 setUpAndSolve(list,pObj,sampler,x,d,bnd,*outStream);
257 printSolution(*x_ptr,*outStream);
258 /**********************************************************************************************/
259 /************************* MEAN PLUS SEMIVARIANCE *********************************************/
260 /**********************************************************************************************/
261 *outStream << "\nMEAN PLUS SEMIVARIANCE\n";
262 list.sublist("SOL").set("Type","Risk Averse");
263 list.sublist("SOL").sublist("Risk Measure").set("Name","Mean Plus Variance");
264 list.sublist("SOL").sublist("Risk Measure").sublist("Mean Plus Variance").set("Deviation Type","Upper");
265 setRandomVector(*x_ptr,comm);
266 setUpAndSolve(list,pObj,sampler,x,d,bnd,*outStream);
267 printSolution(*x_ptr,*outStream);
268 /**********************************************************************************************/
269 /************************* MEAN PLUS SEMIDEVIATION FROM TARGET ********************************/
270 /**********************************************************************************************/
271 *outStream << "\nMEAN PLUS SEMIDEVIATION FROM TARGET\n";
272 list.sublist("SOL").set("Type","Risk Averse");
273 list.sublist("SOL").sublist("Risk Measure").set("Name","Mean Plus Deviation From Target");
274 list.sublist("SOL").sublist("Risk Measure").sublist("Mean Plus Deviation From Target").set("Deviation Type","Upper");
275 setRandomVector(*x_ptr,comm);
276 setUpAndSolve(list,pObj,sampler,x,d,bnd,*outStream);
277 printSolution(*x_ptr,*outStream);
278 /**********************************************************************************************/
279 /************************* MEAN PLUS SEMIVARIANCE FROM TARGET *********************************/
280 /**********************************************************************************************/
281 *outStream << "\nMEAN PLUS SEMIVARIANCE FROM TARGET\n";
282 list.sublist("SOL").set("Type","Risk Averse");
283 list.sublist("SOL").sublist("Risk Measure").set("Name","Mean Plus Variance From Target");
284 list.sublist("SOL").sublist("Risk Measure").sublist("Mean Plus Variance From Target").set("Deviation Type","Upper");
285 setRandomVector(*x_ptr,comm);
286 setUpAndSolve(list,pObj,sampler,x,d,bnd,*outStream);
287 printSolution(*x_ptr,*outStream);
288 /**********************************************************************************************/
289 /************************* MEAN PLUS CVAR *****************************************************/
290 /**********************************************************************************************/
291 *outStream << "\nMEAN PLUS CONDITIONAL VALUE AT RISK\n";
292 list.sublist("SOL").set("Type","Risk Averse");
293 list.sublist("SOL").sublist("Risk Measure").set("Name","CVaR");
294 setRandomVector(*x_ptr,comm);
295 setUpAndSolve(list,pObj,sampler,x,d,bnd,*outStream);
296 printSolution(*x_ptr,*outStream);
297 /**********************************************************************************************/
298 /************************* MEAN PLUS HMCR *****************************************************/
299 /**********************************************************************************************/
300 *outStream << "\nMEAN PLUS HIGHER MOMENT COHERENT RISK MEASURE\n";
301 list.sublist("SOL").sublist("Risk Measure").set("Name","HMCR");
302 setRandomVector(*x_ptr,comm);
303 setUpAndSolve(list,pObj,sampler,x,d,bnd,*outStream);
304 printSolution(*x_ptr,*outStream);
305 /**********************************************************************************************/
306 /************************* EXPONENTIAL UTILITY FUNCTION ***************************************/
307 /**********************************************************************************************/
308 *outStream << "\nEXPONENTIAL UTILITY FUNCTION\n";
309 list.sublist("SOL").set("Type","Risk Averse");
310 list.sublist("SOL").sublist("Risk Measure").set("Name","Entropic Risk");
311 setRandomVector(*x_ptr,comm);
312 setUpAndSolve(list,pObj,sampler,x,d,bnd,*outStream);
313 printSolution(*x_ptr,*outStream);
314 }
315 catch (std::logic_error& err) {
316 *outStream << err.what() << "\n";
317 errorFlag = -1000;
318 }; // end try
319
320 if (errorFlag != 0)
321 std::cout << "End Result: TEST FAILED\n";
322 else
323 std::cout << "End Result: TEST PASSED\n";
324
325 return 0;
326 }