ROL
example_05.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 #include "example_05.hpp"
45 
46 template<class Real>
47 Real random(const Teuchos::RCP<const Teuchos::Comm<int> > &comm) {
48  Real val = 0.0;
49  if ( Teuchos::rank<int>(*comm)==0 ) {
50  val = (Real)rand()/(Real)RAND_MAX;
51  }
52  Teuchos::broadcast<int,Real>(*comm,0,1,&val);
53  return val;
54 }
55 
56 int main(int argc, char* argv[]) {
57 
58  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
59  Teuchos::RCP<const Teuchos::Comm<int> > comm
60  = Teuchos::DefaultComm<int>::getComm();
61 
62  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
63  int iprint = argc - 1;
64  Teuchos::RCP<std::ostream> outStream;
65  Teuchos::oblackholestream bhs; // outputs nothing
66  if (iprint > 0 && Teuchos::rank<int>(*comm)==0)
67  outStream = Teuchos::rcp(&std::cout, false);
68  else
69  outStream = Teuchos::rcp(&bhs, false);
70 
71  int errorFlag = 0;
72 
73  try {
74  /**********************************************************************************************/
75  /************************* CONSTRUCT ROL ALGORITHM ********************************************/
76  /**********************************************************************************************/
77  // Get ROL parameterlist
78  std::string filename = "input.xml";
79  Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() );
80  Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() );
81  // Build ROL algorithm
82  parlist->sublist("Status Test").set("Gradient Tolerance",1.e-8);
83  parlist->sublist("Status Test").set("Step Tolerance",1.e-14);
84  parlist->sublist("Status Test").set("Iteration Limit",100);
85  Teuchos::RCP<ROL::Algorithm<double> > algo;
86  /**********************************************************************************************/
87  /************************* CONSTRUCT VECTORS **************************************************/
88  /**********************************************************************************************/
89  // Build control vectors
90  int nx = 256;
91  Teuchos::RCP<std::vector<double> > x1_rcp = Teuchos::rcp( new std::vector<double>(nx+2,0.0) );
92  ROL::StdVector<double> x1(x1_rcp);
93  Teuchos::RCP<std::vector<double> > x2_rcp = Teuchos::rcp( new std::vector<double>(nx+2,0.0) );
94  ROL::StdVector<double> x2(x2_rcp);
95  Teuchos::RCP<std::vector<double> > x3_rcp = Teuchos::rcp( new std::vector<double>(nx+2,0.0) );
96  ROL::StdVector<double> x3(x3_rcp);
97  Teuchos::RCP<std::vector<double> > z_rcp = Teuchos::rcp( new std::vector<double>(nx+2,0.0) );
98  ROL::StdVector<double> z(z_rcp);
99  Teuchos::RCP<std::vector<double> > xr_rcp = Teuchos::rcp( new std::vector<double>(nx+2,0.0) );
100  ROL::StdVector<double> xr(xr_rcp);
101  Teuchos::RCP<std::vector<double> > d_rcp = Teuchos::rcp( new std::vector<double>(nx+2,0.0) );
102  ROL::StdVector<double> d(d_rcp);
103  for ( int i = 0; i < nx+2; i++ ) {
104  (*xr_rcp)[i] = random<double>(comm);
105  (*d_rcp)[i] = random<double>(comm);
106  }
107  // Build state and adjoint vectors
108  Teuchos::RCP<std::vector<double> > u_rcp = Teuchos::rcp( new std::vector<double>(nx,1.0) );
109  ROL::StdVector<double> u(u_rcp);
110  Teuchos::RCP<std::vector<double> > p_rcp = Teuchos::rcp( new std::vector<double>(nx,0.0) );
111  ROL::StdVector<double> p(p_rcp);
112  Teuchos::RCP<ROL::Vector<double> > up = Teuchos::rcp(&u,false);
113  Teuchos::RCP<ROL::Vector<double> > pp = Teuchos::rcp(&p,false);
114  /**********************************************************************************************/
115  /************************* CONSTRUCT SOL COMPONENTS *******************************************/
116  /**********************************************************************************************/
117  // Build samplers
118  int dim = 4;
119  int nSamp = 100;
120  std::vector<double> tmp(2,0.0); tmp[0] = -1.0; tmp[1] = 1.0;
121  std::vector<std::vector<double> > bounds(dim,tmp);
122  Teuchos::RCP<ROL::BatchManager<double> > bman
123  = Teuchos::rcp(new ROL::StdTeuchosBatchManager<double,int>(comm));
124  Teuchos::RCP<ROL::SampleGenerator<double> > sampler
125  = Teuchos::rcp(new ROL::MonteCarloGenerator<double>(nSamp,bounds,bman,false,false,100));
126  /**********************************************************************************************/
127  /************************* CONSTRUCT OBJECTIVE FUNCTION ***************************************/
128  /**********************************************************************************************/
129  // Build risk-averse objective function
130  double alpha = 1.e-3;
131  Teuchos::RCP<ROL::ParametrizedObjective_SimOpt<double> > pobjSimOpt
132  = Teuchos::rcp(new Objective_BurgersControl<double>(alpha,nx));
133  Teuchos::RCP<ROL::ParametrizedEqualityConstraint_SimOpt<double> > pconSimOpt
134  = Teuchos::rcp(new EqualityConstraint_BurgersControl<double>(nx));
135  Teuchos::RCP<ROL::ParametrizedObjective<double> > pObj
136  = Teuchos::rcp(new ROL::Reduced_ParametrizedObjective_SimOpt<double>(pobjSimOpt,pconSimOpt,up,pp));
137  Teuchos::RCP<ROL::Objective<double> > obj;
138  // Test parametrized objective functions
139  *outStream << "Check Derivatives of Parametrized Objective Function\n";
140  x1.set(xr);
141  pObj->setParameter(sampler->getMyPoint(0));
142  pObj->checkGradient(x1,d,true,*outStream);
143  pObj->checkHessVec(x1,d,true,*outStream);
144  /**********************************************************************************************/
145  /************************* SMOOTHED CVAR 1.e-2 ************************************************/
146  /**********************************************************************************************/
147  *outStream << "\nSOLVE SMOOTHED CONDITIONAL VALUE AT RISK WITH TRUST REGION\n";
148  // Build CVaR objective function
149  Teuchos::ParameterList list1;
150  list1.sublist("SOL").set("Stochastic Optimization Type","Risk Averse");
151  list1.sublist("SOL").set("Store Sampled Value and Gradient",true);
152  list1.sublist("SOL").sublist("Risk Measure").set("Name","CVaR");
153  list1.sublist("SOL").sublist("Risk Measure").sublist("CVaR").set("Confidence Level",0.99);
154  list1.sublist("SOL").sublist("Risk Measure").sublist("CVaR").set("Convex Combination Parameter",1.0);
155  list1.sublist("SOL").sublist("Risk Measure").sublist("CVaR").set("Smoothing Parameter",1.e-2);
156  list1.sublist("SOL").sublist("Risk Measure").sublist("CVaR").sublist("Distribution").set("Name","Parabolic");
157  list1.sublist("SOL").sublist("Risk Measure").sublist("CVaR").sublist("Distribution").sublist("Parabolic").set("Lower Bound",-0.5);
158  list1.sublist("SOL").sublist("Risk Measure").sublist("CVaR").sublist("Distribution").sublist("Parabolic").set("Upper Bound", 0.5);
159  // Build stochastic problem
160  Teuchos::RCP<ROL::Vector<double> > x1p = Teuchos::rcp(&x1,false);
161  x1p->zero();
162  ROL::StochasticProblem<double> optProb1(list1,pObj,sampler,x1p);
163  optProb1.checkObjectiveGradient(d,true,*outStream);
164  optProb1.checkObjectiveHessVec(d,true,*outStream);
165  // Run ROL algorithm
166  algo = Teuchos::rcp(new ROL::Algorithm<double>("Trust Region",*parlist,false));
167  clock_t start = clock();
168  algo->run(optProb1,true,*outStream);
169  *outStream << "Optimization time: " << (double)(clock()-start)/(double)CLOCKS_PER_SEC << " seconds.\n";
170  /**********************************************************************************************/
171  /************************* SMOOTHED CVAR 1.e-4 ************************************************/
172  /**********************************************************************************************/
173  *outStream << "\nSOLVE SMOOTHED CONDITIONAL VALUE AT RISK WITH TRUST REGION\n";
174  Teuchos::ParameterList list2;
175  list2.sublist("SOL").set("Stochastic Optimization Type","Risk Averse");
176  list2.sublist("SOL").set("Store Sampled Value and Gradient",true);
177  list2.sublist("SOL").sublist("Risk Measure").set("Name","CVaR");
178  list2.sublist("SOL").sublist("Risk Measure").sublist("CVaR").set("Confidence Level",0.99);
179  list2.sublist("SOL").sublist("Risk Measure").sublist("CVaR").set("Convex Combination Parameter",1.0);
180  list2.sublist("SOL").sublist("Risk Measure").sublist("CVaR").set("Smoothing Parameter",1.e-4);
181  list2.sublist("SOL").sublist("Risk Measure").sublist("CVaR").sublist("Distribution").set("Name","Parabolic");
182  list2.sublist("SOL").sublist("Risk Measure").sublist("CVaR").sublist("Distribution").sublist("Parabolic").set("Lower Bound",-0.5);
183  list2.sublist("SOL").sublist("Risk Measure").sublist("CVaR").sublist("Distribution").sublist("Parabolic").set("Upper Bound", 0.5);
184  // Build stochastic problem
185  Teuchos::RCP<ROL::Vector<double> > x2p = Teuchos::rcp(&x2,false);
186  x2p->set(*x1p);
187  ROL::StochasticProblem<double> optProb2(list2,pObj,sampler,x2p);
188  optProb2.setSolutionStatistic(optProb1.getSolutionStatistic());
189  optProb2.checkObjectiveGradient(d,true,*outStream);
190  optProb2.checkObjectiveHessVec(d,true,*outStream);
191  // Run ROL algorithm
192  algo = Teuchos::rcp(new ROL::Algorithm<double>("Trust Region",*parlist,false));
193  start = clock();
194  algo->run(optProb2,true,*outStream);
195  *outStream << "Optimization time: " << (double)(clock()-start)/(double)CLOCKS_PER_SEC << " seconds.\n";
196  /**********************************************************************************************/
197  /************************* SMOOTHED CVAR 1.e-6 ************************************************/
198  /**********************************************************************************************/
199  *outStream << "\nSOLVE SMOOTHED CONDITIONAL VALUE AT RISK WITH TRUST REGION\n";
200  Teuchos::ParameterList list3;
201  list3.sublist("SOL").set("Stochastic Optimization Type","Risk Averse");
202  list3.sublist("SOL").set("Store Sampled Value and Gradient",true);
203  list3.sublist("SOL").sublist("Risk Measure").set("Name","CVaR");
204  list3.sublist("SOL").sublist("Risk Measure").sublist("CVaR").set("Confidence Level",0.99);
205  list3.sublist("SOL").sublist("Risk Measure").sublist("CVaR").set("Convex Combination Parameter",1.0);
206  list3.sublist("SOL").sublist("Risk Measure").sublist("CVaR").set("Smoothing Parameter",1.e-6);
207  list3.sublist("SOL").sublist("Risk Measure").sublist("CVaR").sublist("Distribution").set("Name","Parabolic");
208  list3.sublist("SOL").sublist("Risk Measure").sublist("CVaR").sublist("Distribution").sublist("Parabolic").set("Lower Bound",-0.5);
209  list3.sublist("SOL").sublist("Risk Measure").sublist("CVaR").sublist("Distribution").sublist("Parabolic").set("Upper Bound", 0.5);
210  // Build stochastic problem
211  Teuchos::RCP<ROL::Vector<double> > x3p = Teuchos::rcp(&x3,false);
212  x3p->set(*x2p);
213  ROL::StochasticProblem<double> optProb3(list3,pObj,sampler,x3p);
214  optProb3.setSolutionStatistic(optProb2.getSolutionStatistic());
215  optProb3.checkObjectiveGradient(d,true,*outStream);
216  optProb3.checkObjectiveHessVec(d,true,*outStream);
217  // Run ROL algorithm
218  algo = Teuchos::rcp(new ROL::Algorithm<double>("Trust Region",*parlist,false));
219  start = clock();
220  algo->run(optProb3,true,*outStream);
221  *outStream << "Optimization time: " << (double)(clock()-start)/(double)CLOCKS_PER_SEC << " seconds.\n";
222  /**********************************************************************************************/
223  /************************* NONSMOOTH PROBLEM **************************************************/
224  /**********************************************************************************************/
225  *outStream << "\nSOLVE NONSMOOTH CVAR PROBLEM WITH BUNDLE TRUST REGION\n";
226  Teuchos::ParameterList list;
227  list.sublist("SOL").set("Stochastic Optimization Type","Risk Averse");
228  list.sublist("SOL").set("Store Sampled Value and Gradient",true);
229  list.sublist("SOL").sublist("Risk Measure").set("Name","CVaR");
230  list.sublist("SOL").sublist("Risk Measure").sublist("CVaR").set("Confidence Level",0.99);
231  list.sublist("SOL").sublist("Risk Measure").sublist("CVaR").set("Convex Combination Parameter",1.0);
232  list.sublist("SOL").sublist("Risk Measure").sublist("CVaR").set("Smoothing Parameter",0.);
233  list.sublist("SOL").sublist("Risk Measure").sublist("CVaR").sublist("Distribution").set("Name","Dirac");
234  list.sublist("SOL").sublist("Risk Measure").sublist("CVaR").sublist("Distribution").sublist("Dirac").set("Location",0.);
235  // Build stochastic problem
236  Teuchos::RCP<ROL::Vector<double> > zp = Teuchos::rcp(&z,false);
237  zp->set(*x3p);
238  ROL::StochasticProblem<double> optProb(list,pObj,sampler,zp);
239  optProb.setSolutionStatistic(optProb3.getSolutionStatistic());
240  optProb.checkObjectiveGradient(d,true,*outStream);
241  optProb.checkObjectiveHessVec(d,true,*outStream);
242  // Run ROL algorithm
243  parlist->sublist("Status Test").set("Iteration Limit",1000);
244  parlist->sublist("Step").sublist("Bundle").set("Epsilon Solution Tolerance",1.e-8);
245  algo = Teuchos::rcp(new ROL::Algorithm<double>("Bundle",*parlist,false));
246  start = clock();
247  algo->run(optProb,true,*outStream);
248  *outStream << "Optimization time: " << (double)(clock()-start)/(double)CLOCKS_PER_SEC << " seconds.\n";
249  /**********************************************************************************************/
250  /************************* COMPUTE ERROR ******************************************************/
251  /**********************************************************************************************/
252  *outStream << "\nSUMMARY:\n";
253  *outStream << " ---------------------------------------------\n";
254  *outStream << " True Value-At-Risk = " << optProb.getSolutionStatistic() << "\n";
255  *outStream << " ---------------------------------------------\n";
256  double VARerror = std::abs(optProb.getSolutionStatistic()-optProb1.getSolutionStatistic());
257  Teuchos::RCP<ROL::Vector<double> > cErr = x1.clone();
258  cErr->set(x1); cErr->axpy(-1.0,z);
259  double CTRLerror = cErr->norm();
260  double TOTerror1 = std::sqrt(std::pow(VARerror,2)+std::pow(CTRLerror,2));
261  *outStream << " Value-At-Risk (1.e-2) = " << optProb1.getSolutionStatistic() << "\n";
262  *outStream << " Value-At-Risk Error = " << VARerror << "\n";
263  *outStream << " Control Error = " << CTRLerror << "\n";
264  *outStream << " Total Error = " << TOTerror1 << "\n";
265  *outStream << " ---------------------------------------------\n";
266  VARerror = std::abs(optProb.getSolutionStatistic()-optProb2.getSolutionStatistic());
267  cErr = x2.clone();
268  cErr->set(x2); cErr->axpy(-1.0,z);
269  CTRLerror = cErr->norm();
270  double TOTerror2 = std::sqrt(std::pow(VARerror,2)+std::pow(CTRLerror,2));
271  *outStream << " Value-At-Risk (1.e-4) = " << optProb2.getSolutionStatistic() << "\n";
272  *outStream << " Value-At-Risk Error = " << VARerror << "\n";
273  *outStream << " Control Error = " << CTRLerror << "\n";
274  *outStream << " Total Error = " << TOTerror2 << "\n";
275  *outStream << " ---------------------------------------------\n";
276  VARerror = std::abs(optProb.getSolutionStatistic()-optProb3.getSolutionStatistic());
277  cErr = x3.clone();
278  cErr->set(x3); cErr->axpy(-1.0,z);
279  CTRLerror = cErr->norm();
280  double TOTerror3 = std::sqrt(std::pow(VARerror,2)+std::pow(CTRLerror,2));
281  *outStream << " Value-At-Risk (1.e-6) = " << optProb3.getSolutionStatistic() << "\n";
282  *outStream << " Value-At-Risk Error = " << VARerror << "\n";
283  *outStream << " Control Error = " << CTRLerror << "\n";
284  *outStream << " Total Error = " << TOTerror3 << "\n";
285  *outStream << " ---------------------------------------------\n\n";
286  // Comparison
287  errorFlag += ((TOTerror1 < 90.*TOTerror2) && (TOTerror2 < 90.*TOTerror3)) ? 1 : 0;
288  }
289  catch (std::logic_error err) {
290  *outStream << err.what() << "\n";
291  errorFlag = -1000;
292  }; // end try
293 
294  if (errorFlag != 0)
295  std::cout << "End Result: TEST FAILED\n";
296  else
297  std::cout << "End Result: TEST PASSED\n";
298 
299  return 0;
300 }
int main(int argc, char *argv[])
Definition: example_05.cpp:56
Real random(const Teuchos::RCP< const Teuchos::Comm< int > > &comm)
Definition: example_05.cpp:47
Provides the std::vector implementation of the ROL::Vector interface.
std::vector< std::vector< Real > > checkObjectiveGradient(const Vector< Real > &d, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Provides an interface to run optimization algorithms.
virtual Teuchos::RCP< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
std::vector< std::vector< Real > > checkObjectiveHessVec(const Vector< Real > &v, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
void set(const Vector< Real > &x)
Set where .
void setSolutionStatistic(const Real stat)