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