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 typedef double RealT;
47 
48 template<class Real>
49 Real random(const Teuchos::RCP<const Teuchos::Comm<int> > &comm) {
50  Real val = 0.0;
51  if ( Teuchos::rank<int>(*comm)==0 ) {
52  val = (Real)rand()/(Real)RAND_MAX;
53  }
54  Teuchos::broadcast<int,Real>(*comm,0,1,&val);
55  return val;
56 }
57 
58 int main(int argc, char* argv[]) {
59 
60  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
61  Teuchos::RCP<const Teuchos::Comm<int> > comm
62  = Teuchos::DefaultComm<int>::getComm();
63 
64  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
65  int iprint = argc - 1;
66  Teuchos::RCP<std::ostream> outStream;
67  Teuchos::oblackholestream bhs; // outputs nothing
68  if (iprint > 0 && Teuchos::rank<int>(*comm)==0)
69  outStream = Teuchos::rcp(&std::cout, false);
70  else
71  outStream = Teuchos::rcp(&bhs, false);
72 
73  int errorFlag = 0;
74 
75  try {
76  /**********************************************************************************************/
77  /************************* CONSTRUCT ROL ALGORITHM ********************************************/
78  /**********************************************************************************************/
79  // Get ROL parameterlist
80  std::string filename = "input.xml";
81  Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() );
82  Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() );
83  // Build ROL algorithm
84  parlist->sublist("Status Test").set("Gradient Tolerance",1.e-7);
85  parlist->sublist("Status Test").set("Step Tolerance",1.e-14);
86  parlist->sublist("Status Test").set("Iteration Limit",100);
87  /**********************************************************************************************/
88  /************************* CONSTRUCT VECTORS **************************************************/
89  /**********************************************************************************************/
90  // Build control vectors
91  int nx = 256;
92  // Construct storage for optimal solution
93  Teuchos::RCP<std::vector<RealT> > z_rcp = Teuchos::rcp(new std::vector<RealT>(nx+2,0));
94  Teuchos::RCP<ROL::Vector<RealT> > zp = Teuchos::rcp(new ROL::StdVector<RealT>(z_rcp));
95  Teuchos::RCP<std::vector<RealT> > x1_rcp = Teuchos::rcp(new std::vector<RealT>(nx+2,0));
96  Teuchos::RCP<ROL::Vector<RealT> > x1p = Teuchos::rcp(new ROL::StdVector<RealT>(x1_rcp));
97  Teuchos::RCP<std::vector<RealT> > x2_rcp = Teuchos::rcp(new std::vector<RealT>(nx+2,0));
98  Teuchos::RCP<ROL::Vector<RealT> > x2p = Teuchos::rcp(new ROL::StdVector<RealT>(x2_rcp));
99  Teuchos::RCP<std::vector<RealT> > x3_rcp = Teuchos::rcp(new std::vector<RealT>(nx+2,0));
100  Teuchos::RCP<ROL::Vector<RealT> > x3p = Teuchos::rcp(new ROL::StdVector<RealT>(x3_rcp));
101  std::vector<Teuchos::RCP<ROL::Vector<RealT> > > xvec = {x1p, x2p, x3p};
102  // Create vectors for derivative check
103  Teuchos::RCP<std::vector<RealT> > xr_rcp = Teuchos::rcp(new std::vector<RealT>(nx+2,0));
104  ROL::StdVector<RealT> xr(xr_rcp);
105  Teuchos::RCP<std::vector<RealT> > d_rcp = Teuchos::rcp(new std::vector<RealT>(nx+2,0));
106  ROL::StdVector<RealT> d(d_rcp);
107  for ( int i = 0; i < nx+2; i++ ) {
108  (*xr_rcp)[i] = random<RealT>(comm);
109  (*d_rcp)[i] = random<RealT>(comm);
110  }
111  // Build state and adjoint vectors
112  Teuchos::RCP<std::vector<RealT> > u_rcp = Teuchos::rcp(new std::vector<RealT>(nx,1));
113  Teuchos::RCP<ROL::Vector<RealT> > up = Teuchos::rcp(new ROL::StdVector<RealT>(u_rcp));
114  Teuchos::RCP<std::vector<RealT> > p_rcp = Teuchos::rcp(new std::vector<RealT>(nx,0));
115  Teuchos::RCP<ROL::Vector<RealT> > pp = Teuchos::rcp(new ROL::StdVector<RealT>(p_rcp));
116  /**********************************************************************************************/
117  /************************* CONSTRUCT SOL COMPONENTS *******************************************/
118  /**********************************************************************************************/
119  // Build samplers
120  int dim = 4, nSamp = 100;
121  std::vector<RealT> tmp = {-1, 1};
122  std::vector<std::vector<RealT> > bounds(dim,tmp);
123  Teuchos::RCP<ROL::BatchManager<RealT> > bman
124  = Teuchos::rcp(new ROL::StdTeuchosBatchManager<RealT,int>(comm));
125  Teuchos::RCP<ROL::SampleGenerator<RealT> > sampler
126  = Teuchos::rcp(new ROL::MonteCarloGenerator<RealT>(nSamp,bounds,bman,false,false,100));
127  /**********************************************************************************************/
128  /************************* CONSTRUCT OBJECTIVE FUNCTION ***************************************/
129  /**********************************************************************************************/
130  // Build risk-averse objective function
131  RealT alpha = 1.e-3;
132  Teuchos::RCP<ROL::Objective_SimOpt<RealT> > pobjSimOpt
133  = Teuchos::rcp(new Objective_BurgersControl<RealT>(alpha,nx));
134  Teuchos::RCP<ROL::EqualityConstraint_SimOpt<RealT> > pconSimOpt
135  = Teuchos::rcp(new EqualityConstraint_BurgersControl<RealT>(nx));
136  pconSimOpt->setSolveParameters(*parlist);
137  Teuchos::RCP<ROL::Objective<RealT> > pObj
138  = Teuchos::rcp(new ROL::Reduced_Objective_SimOpt<RealT>(pobjSimOpt,pconSimOpt,up,pp));
139  // Test parametrized objective functions
140  *outStream << "Check Derivatives of Parametrized Objective Function\n";
141  xvec[0]->set(xr);
142  pObj->setParameter(sampler->getMyPoint(0));
143  pObj->checkGradient(*xvec[0],d,true,*outStream);
144  pObj->checkHessVec(*xvec[0],d,true,*outStream);
145  /**********************************************************************************************/
146  /************************* SMOOTHED CVAR 1.e-2, 1.e-4, 1.e-6 **********************************/
147  /**********************************************************************************************/
148  const RealT cl(0.9), cc(1), lb(-0.5), ub(0.5);
149  const std::string ra = "Risk Averse", rm = "CVaR", dist = "Parabolic";
150  const bool storage = true;
151  RealT eps(1.e-2);
152  std::vector<RealT> stat(3,0);
153  Teuchos::RCP<ROL::Algorithm<RealT> > algo;
154  Teuchos::RCP<ROL::StochasticProblem<RealT> > optProb;
155  for (int i = 0; i < 3; ++i) {
156  *outStream << "\nSOLVE SMOOTHED CONDITIONAL VALUE AT RISK WITH TRUST REGION\n";
157  // Build CVaR risk measure
158  Teuchos::ParameterList list;
159  list.sublist("SOL").set("Stochastic Optimization Type",ra);
160  list.sublist("SOL").set("Store Sampled Value and Gradient",storage);
161  list.sublist("SOL").sublist("Risk Measure").set("Name",rm);
162  list.sublist("SOL").sublist("Risk Measure").sublist(rm).set("Confidence Level",cl);
163  list.sublist("SOL").sublist("Risk Measure").sublist(rm).set("Convex Combination Parameter",cc);
164  list.sublist("SOL").sublist("Risk Measure").sublist(rm).set("Smoothing Parameter",eps);
165  list.sublist("SOL").sublist("Risk Measure").sublist(rm).sublist("Distribution").set("Name",dist);
166  list.sublist("SOL").sublist("Risk Measure").sublist(rm).sublist("Distribution").sublist(dist).set("Lower Bound",lb);
167  list.sublist("SOL").sublist("Risk Measure").sublist(rm).sublist("Distribution").sublist(dist).set("Upper Bound",ub);
168  // Build stochastic problem
169  if ( i==0 ) { xvec[i]->zero(); }
170  else { xvec[i]->set(*xvec[i-1]); }
171  optProb = Teuchos::rcp(new ROL::StochasticProblem<RealT>(list,pObj,sampler,xvec[i]));
172  if ( i==0 ) { optProb->setSolutionStatistic(1); }
173  else { optProb->setSolutionStatistic(stat[i-1]); }
174  optProb->checkObjectiveGradient(d,true,*outStream);
175  optProb->checkObjectiveHessVec(d,true,*outStream);
176  // Run ROL algorithm
177  algo = Teuchos::rcp(new ROL::Algorithm<RealT>("Trust Region",*parlist,false));
178  clock_t start = clock();
179  algo->run(*optProb,true,*outStream);
180  *outStream << "Optimization time: " << (RealT)(clock()-start)/(RealT)CLOCKS_PER_SEC << " seconds.\n";
181  // Get solution statistic
182  stat[i] = optProb->getSolutionStatistic();
183  // Update smoothing parameter
184  eps *= static_cast<RealT>(1.e-2);
185  }
186  /**********************************************************************************************/
187  /************************* NONSMOOTH PROBLEM **************************************************/
188  /**********************************************************************************************/
189  *outStream << "\nSOLVE NONSMOOTH CVAR PROBLEM WITH BUNDLE TRUST REGION\n";
190  Teuchos::ParameterList list;
191  list.sublist("SOL").set("Stochastic Optimization Type",ra);
192  list.sublist("SOL").set("Store Sampled Value and Gradient",storage);
193  list.sublist("SOL").sublist("Risk Measure").set("Name",rm);
194  list.sublist("SOL").sublist("Risk Measure").sublist(rm).set("Confidence Level",cl);
195  list.sublist("SOL").sublist("Risk Measure").sublist(rm).set("Convex Combination Parameter",cc);
196  list.sublist("SOL").sublist("Risk Measure").sublist(rm).set("Smoothing Parameter",0.);
197  list.sublist("SOL").sublist("Risk Measure").sublist(rm).sublist("Distribution").set("Name","Dirac");
198  list.sublist("SOL").sublist("Risk Measure").sublist(rm).sublist("Distribution").sublist("Dirac").set("Location",0.);
199  // Build stochastic problem
200  zp->set(*xvec[2]);
201  optProb = Teuchos::rcp(new ROL::StochasticProblem<RealT>(list,pObj,sampler,zp));
202  optProb->setSolutionStatistic(stat[2]);
203  optProb->checkObjectiveGradient(d,true,*outStream);
204  optProb->checkObjectiveHessVec(d,true,*outStream);
205  // Run ROL algorithm
206  parlist->sublist("Status Test").set("Iteration Limit",1000);
207  parlist->sublist("Step").sublist("Bundle").set("Epsilon Solution Tolerance",1.e-7);
208  algo = Teuchos::rcp(new ROL::Algorithm<RealT>("Bundle",*parlist,false));
209  clock_t start = clock();
210  algo->run(*optProb,true,*outStream);
211  *outStream << "Optimization time: " << (RealT)(clock()-start)/(RealT)CLOCKS_PER_SEC << " seconds.\n";
212  /**********************************************************************************************/
213  /************************* COMPUTE ERROR ******************************************************/
214  /**********************************************************************************************/
215  Teuchos::RCP<ROL::Vector<RealT> > cErr = zp->clone();
216  RealT zstat = optProb->getSolutionStatistic();
217  *outStream << "\nSUMMARY:\n";
218  *outStream << " ---------------------------------------------\n";
219  *outStream << " True Value-At-Risk = " << zstat << "\n";
220  *outStream << " ---------------------------------------------\n";
221  RealT VARerror = std::abs(zstat-stat[0]);
222  cErr->set(*xvec[0]); cErr->axpy(-1.0,*zp);
223  RealT CTRLerror = cErr->norm();
224  RealT TOTerror1 = std::sqrt(std::pow(VARerror,2)+std::pow(CTRLerror,2));
225  *outStream << " Value-At-Risk (1.e-2) = " << stat[0] << "\n";
226  *outStream << " Value-At-Risk Error = " << VARerror << "\n";
227  *outStream << " Control Error = " << CTRLerror << "\n";
228  *outStream << " Total Error = " << TOTerror1 << "\n";
229  *outStream << " ---------------------------------------------\n";
230  VARerror = std::abs(zstat-stat[1]);
231  cErr->set(*xvec[1]); cErr->axpy(-1.0,*zp);
232  CTRLerror = cErr->norm();
233  RealT TOTerror2 = std::sqrt(std::pow(VARerror,2)+std::pow(CTRLerror,2));
234  *outStream << " Value-At-Risk (1.e-4) = " << stat[1] << "\n";
235  *outStream << " Value-At-Risk Error = " << VARerror << "\n";
236  *outStream << " Control Error = " << CTRLerror << "\n";
237  *outStream << " Total Error = " << TOTerror2 << "\n";
238  *outStream << " ---------------------------------------------\n";
239  VARerror = std::abs(zstat-stat[2]);
240  cErr->set(*xvec[2]); cErr->axpy(-1.0,*zp);
241  CTRLerror = cErr->norm();
242  RealT TOTerror3 = std::sqrt(std::pow(VARerror,2)+std::pow(CTRLerror,2));
243  *outStream << " Value-At-Risk (1.e-6) = " << stat[2] << "\n";
244  *outStream << " Value-At-Risk Error = " << VARerror << "\n";
245  *outStream << " Control Error = " << CTRLerror << "\n";
246  *outStream << " Total Error = " << TOTerror3 << "\n";
247  *outStream << " ---------------------------------------------\n\n";
248  // Comparison
249  errorFlag += ((TOTerror1 < 90.*TOTerror2) && (TOTerror2 < 90.*TOTerror3)) ? 1 : 0;
250 
251  // Output controls
252  std::ofstream control;
253  control.open("example04_control.txt");
254  for (int n = 0; n < nx+2; n++) {
255  control << std::scientific << std::setprecision(15)
256  << std::setw(25) << static_cast<RealT>(n)/static_cast<RealT>(nx+1)
257  << std::setw(25) << (*z_rcp)[n]
258  << std::endl;
259  }
260  control.close();
261 
262  }
263  catch (std::logic_error err) {
264  *outStream << err.what() << "\n";
265  errorFlag = -1000;
266  }; // end try
267 
268  if (errorFlag != 0)
269  std::cout << "End Result: TEST FAILED\n";
270  else
271  std::cout << "End Result: TEST PASSED\n";
272 
273  return 0;
274 }
int main(int argc, char *argv[])
Definition: example_05.cpp:58
Real random(const Teuchos::RCP< const Teuchos::Comm< int > > &comm)
Definition: example_05.cpp:49
Provides the std::vector implementation of the ROL::Vector interface.
Provides an interface to run optimization algorithms.
double RealT
double RealT
Definition: example_05.cpp:46