ROL
example_06.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 
49 #include "ROL_Algorithm.hpp"
50 
52 #include "ROL_HMCRObjective.hpp"
53 #include "ROL_CVaRVector.hpp"
54 
56 
57 #include "Teuchos_oblackholestream.hpp"
58 #include "Teuchos_XMLParameterListHelpers.hpp"
59 #include "Teuchos_GlobalMPISession.hpp"
60 #include "Teuchos_Comm.hpp"
61 #include "Teuchos_DefaultComm.hpp"
62 #include "Teuchos_CommHelpers.hpp"
63 
64 #include <iostream>
65 #include <algorithm>
66 
67 #include "example_06.hpp"
68 
69 typedef double RealT;
76 
77 int main(int argc, char *argv[]) {
78 
79  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
80  Teuchos::RCP<const Teuchos::Comm<int> > comm
81  = Teuchos::DefaultComm<int>::getComm();
82 
83  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
84  int iprint = argc - 1;
85  bool print = (iprint>0) && !(comm->getRank());
86  Teuchos::RCP<std::ostream> outStream;
87  Teuchos::oblackholestream bhs; // outputs nothing
88  if (print)
89  outStream = Teuchos::rcp(&std::cout, false);
90  else
91  outStream = Teuchos::rcp(&bhs, false);
92 
93  int errorFlag = 0;
94 
95  // *** Example body.
96 
97  try {
98  /*************************************************************************/
99  /************* INITIALIZE BURGERS FEM CLASS ******************************/
100  /*************************************************************************/
101  int nx = 256; // Set spatial discretization.
102  RealT alpha = 1.e-3; // Set penalty parameter.
103  RealT nl = 1.0; // Nonlinearity parameter (1 = Burgers, 0 = linear).
104  RealT cH1 = 1.0; // Scale for derivative term in H1 norm.
105  RealT cL2 = 0.0; // Scale for mass term in H1 norm.
106  Teuchos::RCP<BurgersFEM<RealT> > fem
107  = Teuchos::rcp(new BurgersFEM<RealT>(nx,nl,cH1,cL2));
108  fem->test_inverse_mass(*outStream);
109  fem->test_inverse_H1(*outStream);
110  /*************************************************************************/
111  /************* INITIALIZE SIMOPT OBJECTIVE FUNCTION **********************/
112  /*************************************************************************/
113  Teuchos::RCP<std::vector<RealT> > ud_rcp
114  = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) );
115  Teuchos::RCP<ROL::Vector<RealT> > ud
116  = Teuchos::rcp(new L2VectorPrimal<RealT>(ud_rcp,fem));
117  Teuchos::RCP<ROL::ParametrizedObjective_SimOpt<RealT> > pobj
118  = Teuchos::rcp(new Objective_BurgersControl<RealT>(fem,ud,alpha));
119  /*************************************************************************/
120  /************* INITIALIZE SIMOPT EQUALITY CONSTRAINT *********************/
121  /*************************************************************************/
122  bool hess = true;
123  Teuchos::RCP<ROL::ParametrizedEqualityConstraint_SimOpt<RealT> > pcon
124  = Teuchos::rcp(new EqualityConstraint_BurgersControl<RealT>(fem,hess));
125  /*************************************************************************/
126  /************* INITIALIZE VECTOR STORAGE *********************************/
127  /*************************************************************************/
128  // INITIALIZE CONTROL VECTORS
129  Teuchos::RCP<std::vector<RealT> > z_rcp
130  = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.0) );
131  Teuchos::RCP<std::vector<RealT> > gz_rcp
132  = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.0) );
133  Teuchos::RCP<std::vector<RealT> > yz_rcp
134  = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.0) );
135  for (int i=0; i<nx+2; i++) {
136  (*z_rcp)[i] = 2.0*random<RealT>(comm)-1.0;
137  (*yz_rcp)[i] = 2.0*random<RealT>(comm)-1.0;
138  }
139  Teuchos::RCP<ROL::Vector<RealT> > zp
140  = Teuchos::rcp(new PrimalControlVector(z_rcp,fem));
141  Teuchos::RCP<ROL::Vector<RealT> > gzp
142  = Teuchos::rcp(new DualControlVector(gz_rcp,fem));
143  Teuchos::RCP<ROL::Vector<RealT> > yzp
144  = Teuchos::rcp(new PrimalControlVector(yz_rcp,fem));
145  RealT zvar = 0.0*random<RealT>(comm);
146  RealT gvar = random<RealT>(comm);
147  RealT yvar = random<RealT>(comm);
148  ROL::CVaRVector<RealT> z(zvar,zp), g(gvar,gzp), y(yvar,yzp);
149  // INITIALIZE STATE VECTORS
150  Teuchos::RCP<std::vector<RealT> > u_rcp
151  = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) );
152  Teuchos::RCP<std::vector<RealT> > gu_rcp
153  = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) );
154  for (int i=0; i<nx; i++) {
155  (*u_rcp)[i] = 2.0*random<RealT>(comm)-1.0;
156  }
157  Teuchos::RCP<ROL::Vector<RealT> > up
158  = Teuchos::rcp(new PrimalStateVector(u_rcp,fem));
159  Teuchos::RCP<ROL::Vector<RealT> > gup
160  = Teuchos::rcp(new DualStateVector(gu_rcp,fem));
161  // INITIALIZE CONSTRAINT VECTORS
162  Teuchos::RCP<std::vector<RealT> > c_rcp
163  = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) );
164  Teuchos::RCP<std::vector<RealT> > l_rcp
165  = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) );
166  for (int i=0; i<nx; i++) {
167  (*l_rcp)[i] = random<RealT>(comm);
168  }
169  Teuchos::RCP<ROL::Vector<RealT> > cp
170  = Teuchos::rcp(new PrimalConstraintVector(c_rcp,fem));
171  Teuchos::RCP<ROL::Vector<RealT> > lp
172  = Teuchos::rcp(new DualConstraintVector(l_rcp,fem));
173  /*************************************************************************/
174  /************* INITIALIZE SAMPLE GENERATOR *******************************/
175  /*************************************************************************/
176  int dim = 4, nSamp = 10000;
177  std::vector<RealT> tmp(2,0.0); tmp[0] = -1.0; tmp[1] = 1.0;
178  std::vector<std::vector<RealT> > bounds(dim,tmp);
179  Teuchos::RCP<ROL::BatchManager<RealT> > bman
180  = Teuchos::rcp(new L2VectorBatchManager<RealT,int>(comm));
181  Teuchos::RCP<ROL::SampleGenerator<RealT> > sampler
182  = Teuchos::rcp(new ROL::MonteCarloGenerator<RealT>(
183  nSamp,bounds,bman,false,false,100));
184  /*************************************************************************/
185  /************* INITIALIZE RISK-AVERSE OBJECTIVE FUNCTION *****************/
186  /*************************************************************************/
187  bool storage = true, fdhess = false;
188  Teuchos::RCP<ROL::ParametrizedObjective<RealT> > robj
190  pobj,pcon,up,lp,gup,cp,storage,fdhess));
191  RealT order = 2.0, prob = 0.95;
192  Teuchos::RCP<ROL::Objective<RealT> > obj
193  = Teuchos::rcp(new ROL::HMCRObjective<RealT>(
194  robj,order,prob,sampler,storage));
195  /*************************************************************************/
196  /************* CHECK DERIVATIVES AND CONSISTENCY *************************/
197  /*************************************************************************/
198  // CHECK OBJECTIVE DERIVATIVES
199  bool derivcheck = false;
200  if (derivcheck) {
201  for (int i = sampler->start(); i < sampler->numMySamples(); i++) {
202  *outStream << "Sample " << i << " Rank " << sampler->batchID() << "\n";
203  *outStream << "(" << sampler->getMyPoint(i)[0] << ", "
204  << sampler->getMyPoint(i)[1] << ", "
205  << sampler->getMyPoint(i)[2] << ", "
206  << sampler->getMyPoint(i)[3] << ")\n";
207  pcon->setParameter(sampler->getMyPoint(i));
208  pcon->checkSolve(*up,*zp,*cp,print,*outStream);
209  robj->setParameter(sampler->getMyPoint(i));
210  robj->checkGradient(*zp,*gzp,*yzp,print,*outStream);
211  robj->checkHessVec(*zp,*gzp,*yzp,print,*outStream);
212  }
213  }
214  obj->checkGradient(z,g,y,print,*outStream);
215  obj->checkHessVec(z,g,y,print,*outStream);
216  /*************************************************************************/
217  /************* RUN OPTIMIZATION ******************************************/
218  /*************************************************************************/
219  // READ IN XML INPUT
220  std::string filename = "input.xml";
221  Teuchos::RCP<Teuchos::ParameterList> parlist
222  = Teuchos::rcp( new Teuchos::ParameterList() );
223  Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() );
224  // DEFINE ALGORITHM
225  ROL::Algorithm<RealT> algo("Trust Region",*parlist,false);
226  // RUN OPTIMIZATION
227  z.zero();
228  algo.run(z, g, *obj, print, *outStream);
229  /*************************************************************************/
230  /************* PRINT CONTROL AND STATE TO SCREEN *************************/
231  /*************************************************************************/
232  *outStream << "\n";
233  for ( int i = 0; i < nx+2; i++ ) {
234  *outStream << std::scientific << std::setprecision(10);
235  *outStream << std::setw(20) << std::left << (RealT)i/((RealT)nx+1.0);
236  *outStream << std::setw(20) << std::left << (*z_rcp)[i];
237  *outStream << "\n";
238  }
239  *outStream << "\n";
240  *outStream << "Scalar Parameter: " << z.getVaR() << "\n";
241  }
242  catch (std::logic_error err) {
243  *outStream << err.what() << "\n";
244  errorFlag = -1000;
245  }; // end try
246 
247  comm->barrier();
248  if (errorFlag != 0)
249  std::cout << "End Result: TEST FAILED\n";
250  else
251  std::cout << "End Result: TEST PASSED\n";
252 
253  return 0;
254 }
H1VectorDual< RealT > PrimalConstraintVector
Definition: example_06.cpp:74
int main(int argc, char *argv[])
Definition: example_06.cpp:77
H1VectorPrimal< RealT > DualConstraintVector
Definition: example_06.cpp:75
L2VectorDual< RealT > DualControlVector
Definition: example_06.cpp:73
Provides an interface to run optimization algorithms.
H1VectorDual< RealT > DualStateVector
Definition: example_06.cpp:71
L2VectorPrimal< RealT > PrimalControlVector
Definition: example_06.cpp:72
H1VectorPrimal< RealT > PrimalStateVector
Definition: example_06.cpp:70
double RealT
Definition: example_06.cpp:69
double RealT