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-10);
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,0.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 = 1000;
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  bool storage = true;
131  double alpha = 1.e-3;
132  Teuchos::RCP<ROL::ParametrizedObjective_SimOpt<double> > pobjSimOpt
133  = Teuchos::rcp(new Objective_BurgersControl<double>(alpha,nx));
134  Teuchos::RCP<ROL::ParametrizedEqualityConstraint_SimOpt<double> > pconSimOpt
135  = Teuchos::rcp(new EqualityConstraint_BurgersControl<double>(nx));
136  Teuchos::RCP<ROL::ParametrizedObjective<double> > pObj
137  = Teuchos::rcp(new ROL::Reduced_ParametrizedObjective_SimOpt<double>(pobjSimOpt,pconSimOpt,up,pp));
138  Teuchos::RCP<ROL::Distribution<double> > dist;
139  Teuchos::RCP<ROL::PlusFunction<double> > pf;
140  Teuchos::RCP<ROL::RiskMeasure<double> > rm;
141  Teuchos::RCP<ROL::Objective<double> > obj;
142  // Test parametrized objective functions
143  *outStream << "Check Derivatives of Parametrized Objective Function\n";
144  x1.set(xr);
145  pObj->setParameter(sampler->getMyPoint(0));
146  pObj->checkGradient(x1,d,true,*outStream);
147  pObj->checkHessVec(x1,d,true,*outStream);
148  /**********************************************************************************************/
149  /************************* SMOOTHED CVAR 1.e-2 ************************************************/
150  /**********************************************************************************************/
151  *outStream << "\nSOLVE SMOOTHED CONDITIONAL VALUE AT RISK WITH TRUST REGION\n";
152  // Build CVaR objective function
153  double prob = 0.99, coeff = 1.0, gamma = 1.e-2;
154  Teuchos::ParameterList distlist;
155  distlist.sublist("SOL").sublist("Distribution").set("Name","Parabolic");
156  distlist.sublist("SOL").sublist("Distribution").sublist("Parabolic").set("Lower Bound",-0.5);
157  distlist.sublist("SOL").sublist("Distribution").sublist("Parabolic").set("Upper Bound", 0.5);
158  dist = ROL::DistributionFactory<double>(distlist);
159  pf = Teuchos::rcp( new ROL::PlusFunction<double>(dist,gamma) );
160  rm = Teuchos::rcp( new ROL::CVaR<double>(prob,coeff,pf) );
161  obj = Teuchos::rcp( new ROL::RiskAverseObjective<double>(pObj,rm,sampler,storage) );
162  // Build CVaR vectors
163  double x1v = 10.0*random<double>(comm)-5.0;
164  Teuchos::RCP<ROL::Vector<double> > x1p = Teuchos::rcp(&x1,false);
165  ROL::CVaRVector<double> x1c(x1v,x1p);
166  // Run ROL algorithm
167  algo = Teuchos::rcp(new ROL::Algorithm<double>("Trust Region",*parlist,false));
168  x1c.zero();
169  clock_t start = clock();
170  algo->run(x1c,*obj,true,*outStream);
171  *outStream << "Optimization time: " << (double)(clock()-start)/(double)CLOCKS_PER_SEC << " seconds.\n";
172  /**********************************************************************************************/
173  /************************* SMOOTHED CVAR 1.e-4 ************************************************/
174  /**********************************************************************************************/
175  *outStream << "\nSOLVE SMOOTHED CONDITIONAL VALUE AT RISK WITH TRUST REGION\n";
176  // Build CVaR objective function
177  gamma = 1.e-4;
178  pf = Teuchos::rcp( new ROL::PlusFunction<double>(dist,gamma) );
179  rm = Teuchos::rcp( new ROL::CVaR<double>(prob,coeff,pf) );
180  obj = Teuchos::rcp( new ROL::RiskAverseObjective<double>(pObj,rm,sampler,storage) );
181  // Build CVaR vectors
182  double x2v = 10.0*random<double>(comm)-5.0;
183  Teuchos::RCP<ROL::Vector<double> > x2p = Teuchos::rcp(&x2,false);
184  ROL::CVaRVector<double> x2c(x2v,x2p);
185  // Run ROL algorithm
186  algo = Teuchos::rcp(new ROL::Algorithm<double>("Trust Region",*parlist,false));
187  x2c.set(x1c);
188  start = clock();
189  algo->run(x2c,*obj,true,*outStream);
190  *outStream << "Optimization time: " << (double)(clock()-start)/(double)CLOCKS_PER_SEC << " seconds.\n";
191  /**********************************************************************************************/
192  /************************* SMOOTHED CVAR 1.e-6 ************************************************/
193  /**********************************************************************************************/
194  *outStream << "\nSOLVE SMOOTHED CONDITIONAL VALUE AT RISK WITH TRUST REGION\n";
195  // Build CVaR objective function
196  gamma = 1.e-6;
197  pf = Teuchos::rcp( new ROL::PlusFunction<double>(dist,gamma) );
198  rm = Teuchos::rcp( new ROL::CVaR<double>(prob,coeff,pf) );
199  obj = Teuchos::rcp( new ROL::RiskAverseObjective<double>(pObj,rm,sampler,storage) );
200  // Build CVaR vectors
201  double x3v = 10.0*random<double>(comm)-5.0;
202  Teuchos::RCP<ROL::Vector<double> > x3p = Teuchos::rcp(&x3,false);
203  ROL::CVaRVector<double> x3c(x3v,x3p);
204  // Run ROL algorithm
205  algo = Teuchos::rcp(new ROL::Algorithm<double>("Trust Region",*parlist,false));
206  x3c.set(x2c);
207  start = clock();
208  algo->run(x3c,*obj,true,*outStream);
209  *outStream << "Optimization time: " << (double)(clock()-start)/(double)CLOCKS_PER_SEC << " seconds.\n";
210  /**********************************************************************************************/
211  /************************* NONSMOOTH PROBLEM **************************************************/
212  /**********************************************************************************************/
213  *outStream << "\nSOLVE NONSMOOTH CVAR PROBLEM WITH BUNDLE TRUST REGION\n";
214  // Build CVaR objective function
215  distlist.sublist("SOL").sublist("Distribution").set("Name","Dirac");
216  distlist.sublist("SOL").sublist("Distribution").sublist("Dirac").set("Location",0.);
217  dist = ROL::DistributionFactory<double>(distlist);
218  pf = Teuchos::rcp( new ROL::PlusFunction<double>(dist,1.0) );
219  rm = Teuchos::rcp( new ROL::CVaR<double>(prob,coeff,pf) );
220  obj = Teuchos::rcp( new ROL::RiskAverseObjective<double>(pObj,rm,sampler,storage) );
221  // Build CVaR vector
222  double zv = 10.0*random<double>(comm)-5.0;
223  Teuchos::RCP<ROL::Vector<double> > zp = Teuchos::rcp(&z,false);
224  ROL::CVaRVector<double> zc(zv,zp);
225  // Run ROL algorithm
226  parlist->sublist("Status Test").set("Iteration Limit",10000);
227  parlist->sublist("Step").sublist("Bundle").set("Epsilon Solution Tolerance",1.e-8);
228  algo = Teuchos::rcp(new ROL::Algorithm<double>("Bundle",*parlist,false));
229  zc.set(x3c);
230  start = clock();
231  algo->run(zc,*obj,true,*outStream);
232  *outStream << "Optimization time: " << (double)(clock()-start)/(double)CLOCKS_PER_SEC << " seconds.\n";
233  /**********************************************************************************************/
234  /************************* COMPUTE ERROR ******************************************************/
235  /**********************************************************************************************/
236  *outStream << "\nSUMMARY:\n";
237  *outStream << " ---------------------------------------------\n";
238  *outStream << " True Value-At-Risk = " << zc.getVaR() << "\n";
239  *outStream << " ---------------------------------------------\n";
240  double VARerror = std::abs(zc.getVaR()-x1c.getVaR());
241  Teuchos::RCP<ROL::Vector<double> > cErr = x1.clone();
242  cErr->set(x1); cErr->axpy(-1.0,z);
243  double CTRLerror = cErr->norm();
244  cErr = x1c.clone();
245  cErr->set(x1c); cErr->axpy(-1.0,zc);
246  double TOTerror1 = cErr->norm();
247  *outStream << " Value-At-Risk (1.e-2) = " << x1c.getVaR() << "\n";
248  *outStream << " Value-At-Risk Error = " << VARerror << "\n";
249  *outStream << " Control Error = " << CTRLerror << "\n";
250  *outStream << " Total Error = " << TOTerror1 << "\n";
251  *outStream << " ---------------------------------------------\n";
252  VARerror = std::abs(zc.getVaR()-x2c.getVaR());
253  cErr = x2.clone();
254  cErr->set(x2); cErr->axpy(-1.0,z);
255  CTRLerror = cErr->norm();
256  cErr = x2c.clone();
257  cErr->set(x2c); cErr->axpy(-1.0,zc);
258  double TOTerror2 = cErr->norm();
259  *outStream << " Value-At-Risk (1.e-4) = " << x2c.getVaR() << "\n";
260  *outStream << " Value-At-Risk Error = " << VARerror << "\n";
261  *outStream << " Control Error = " << CTRLerror << "\n";
262  *outStream << " Total Error = " << TOTerror2 << "\n";
263  *outStream << " ---------------------------------------------\n";
264  VARerror = std::abs(zc.getVaR()-x3c.getVaR());
265  cErr = x3.clone();
266  cErr->set(x3); cErr->axpy(-1.0,z);
267  CTRLerror = cErr->norm();
268  cErr = x3c.clone();
269  cErr->set(x3c); cErr->axpy(-1.0,zc);
270  double TOTerror3 = cErr->norm();
271  *outStream << " Value-At-Risk (1.e-6) = " << x3c.getVaR() << "\n";
272  *outStream << " Value-At-Risk Error = " << VARerror << "\n";
273  *outStream << " Control Error = " << CTRLerror << "\n";
274  *outStream << " Total Error = " << TOTerror3 << "\n";
275  *outStream << " ---------------------------------------------\n\n";
276  // Comparison
277  errorFlag += ((TOTerror1 < 90.*TOTerror2) && (TOTerror2 < 90.*TOTerror3)) ? 1 : 0;
278  }
279  catch (std::logic_error err) {
280  *outStream << err.what() << "\n";
281  errorFlag = -1000;
282  }; // end try
283 
284  if (errorFlag != 0)
285  std::cout << "End Result: TEST FAILED\n";
286  else
287  std::cout << "End Result: TEST PASSED\n";
288 
289  return 0;
290 }
int main(int argc, char *argv[])
Definition: example_05.cpp:56
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:157
const Real getVaR() const
Teuchos::RCP< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
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.
Provides an interface to run optimization algorithms.
void set(const Vector< Real > &x)
Set where .
Teuchos::RCP< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:196