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_RiskVector.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);
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  bool print0 = print && !(comm->getRank());
96  Teuchos::RCP<std::ostream> outStream0;
97  if (print0)
98  outStream0 = Teuchos::rcp(&std::cout, false);
99  else
100  outStream0 = Teuchos::rcp(&bhs, false);
101 
102  int errorFlag = 0;
103 
104  // *** Example body.
105 
106  try {
107  /*************************************************************************/
108  /************* INITIALIZE BURGERS FEM CLASS ******************************/
109  /*************************************************************************/
110  int nx = 512; // Set spatial discretization.
111  RealT x = 0.0; // Set penalty parameter.
112  RealT nl = 1.0; // Nonlinearity parameter (1 = Burgers, 0 = linear).
113  RealT cH1 = 1.0; // Scale for derivative term in H1 norm.
114  RealT cL2 = 0.0; // Scale for mass term in H1 norm.
115  Teuchos::RCP<BurgersFEM<RealT> > fem
116  = Teuchos::rcp(new BurgersFEM<RealT>(nx,nl,cH1,cL2));
117  fem->test_inverse_mass(*outStream0);
118  fem->test_inverse_H1(*outStream0);
119  /*************************************************************************/
120  /************* INITIALIZE SIMOPT OBJECTIVE FUNCTION **********************/
121  /*************************************************************************/
122  Teuchos::RCP<ROL::Objective_SimOpt<RealT> > pobj
123  = Teuchos::rcp(new Objective_BurgersControl<RealT>(fem,x));
124  /*************************************************************************/
125  /************* INITIALIZE SIMOPT EQUALITY CONSTRAINT *********************/
126  /*************************************************************************/
127  bool hess = true;
128  Teuchos::RCP<ROL::EqualityConstraint_SimOpt<RealT> > pcon
129  = Teuchos::rcp(new EqualityConstraint_BurgersControl<RealT>(fem,hess));
130  /*************************************************************************/
131  /************* INITIALIZE VECTOR STORAGE *********************************/
132  /*************************************************************************/
133  // INITIALIZE CONTROL VECTORS
134  Teuchos::RCP<std::vector<RealT> > z_rcp
135  = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.0) );
136  Teuchos::RCP<std::vector<RealT> > gz_rcp
137  = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.0) );
138  Teuchos::RCP<std::vector<RealT> > yz_rcp
139  = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.0) );
140  for (int i=0; i<nx+2; i++) {
141  (*yz_rcp)[i] = 2.0*random<RealT>(comm)-1.0;
142  }
143  Teuchos::RCP<ROL::Vector<RealT> > zp
144  = Teuchos::rcp(new PrimalControlVector(z_rcp,fem));
145  Teuchos::RCP<ROL::Vector<RealT> > gzp
146  = Teuchos::rcp(new DualControlVector(gz_rcp,fem));
147  Teuchos::RCP<ROL::Vector<RealT> > yzp
148  = Teuchos::rcp(new PrimalControlVector(yz_rcp,fem));
149  std::vector<RealT> zvar(1,random<RealT>(comm));
150  std::vector<RealT> gvar(1,random<RealT>(comm));
151  std::vector<RealT> yvar(1,random<RealT>(comm));
152  ROL::RiskVector<RealT> z(zp,zvar,true), g(gzp,gvar,true), y(yzp,yvar,true);
153  // INITIALIZE STATE VECTORS
154  Teuchos::RCP<std::vector<RealT> > u_rcp
155  = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) );
156  Teuchos::RCP<std::vector<RealT> > gu_rcp
157  = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) );
158  Teuchos::RCP<ROL::Vector<RealT> > up
159  = Teuchos::rcp(new PrimalStateVector(u_rcp,fem));
160  Teuchos::RCP<ROL::Vector<RealT> > gup
161  = Teuchos::rcp(new DualStateVector(gu_rcp,fem));
162  // INITIALIZE CONSTRAINT VECTORS
163  Teuchos::RCP<std::vector<RealT> > c_rcp
164  = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) );
165  Teuchos::RCP<std::vector<RealT> > l_rcp
166  = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) );
167  for (int i=0; i<nx; i++) {
168  (*l_rcp)[i] = random<RealT>(comm);
169  }
170  Teuchos::RCP<ROL::Vector<RealT> > cp
171  = Teuchos::rcp(new PrimalConstraintVector(c_rcp,fem));
172  Teuchos::RCP<ROL::Vector<RealT> > lp
173  = Teuchos::rcp(new DualConstraintVector(l_rcp,fem));
174  /*************************************************************************/
175  /************* INITIALIZE SAMPLE GENERATOR *******************************/
176  /*************************************************************************/
177  int dim = 4, nSamp = 1000;
178  std::vector<RealT> tmp(2,0.0); tmp[0] = -1.0; tmp[1] = 1.0;
179  std::vector<std::vector<RealT> > bounds(dim,tmp);
180  Teuchos::RCP<ROL::BatchManager<RealT> > bman
181  = Teuchos::rcp(new L2VectorBatchManager<RealT,int>(comm));
182  Teuchos::RCP<ROL::SampleGenerator<RealT> > sampler
183  = Teuchos::rcp(new ROL::MonteCarloGenerator<RealT>(
184  nSamp,bounds,bman,false,false,100));
185  /*************************************************************************/
186  /************* INITIALIZE RISK-AVERSE OBJECTIVE FUNCTION *****************/
187  /*************************************************************************/
188  bool storage = true, fdhess = false;
189  Teuchos::RCP<ROL::Objective<RealT> > robj
190  = Teuchos::rcp(new ROL::Reduced_Objective_SimOpt<RealT>(
191  pobj,pcon,up,lp,gup,cp,storage,fdhess));
192  RealT order = 2.0, threshold = -0.85*(1.0-x);
193  Teuchos::RCP<ROL::Objective<RealT> > obj
194  = Teuchos::rcp(new ROL::BPOEObjective<RealT>(
195  robj,order,threshold,sampler,storage));
196  /*************************************************************************/
197  /************* INITIALIZE BOUND CONSTRAINTS ******************************/
198  /*************************************************************************/
199  std::vector<RealT> Zlo(nx+2,0.0), Zhi(nx+2,10.0);
200  for (int i = 0; i < nx+2; i++) {
201  if ( i < (int)((nx+2)/3) ) {
202  Zlo[i] = -1.0;
203  Zhi[i] = 1.0;
204  }
205  if ( i >= (int)((nx+2)/3) && i < (int)(2*(nx+2)/3) ) {
206  Zlo[i] = 1.0;
207  Zhi[i] = 5.0;
208  }
209  if ( i >= (int)(2*(nx+2)/3) ) {
210  Zlo[i] = 5.0;
211  Zhi[i] = 10.0;
212  }
213  }
214  Teuchos::RCP<ROL::BoundConstraint<RealT> > Zbnd
215  = Teuchos::rcp(new L2BoundConstraint<RealT>(Zlo,Zhi,fem));
216  Teuchos::ParameterList list;
217  list.sublist("SOL").set("Stochastic Optimization Type","BPOE");
218  Teuchos::RCP<ROL::BoundConstraint<RealT> > bnd
219  = Teuchos::rcp(new ROL::RiskBoundConstraint<RealT>(list,Zbnd));
220  /*************************************************************************/
221  /************* CHECK DERIVATIVES AND CONSISTENCY *************************/
222  /*************************************************************************/
223  // CHECK OBJECTIVE DERIVATIVES
224  bool derivcheck = false;
225  if (derivcheck) {
226  int nranks = sampler->numBatches();
227  for (int pid = 0; pid < nranks; pid++) {
228  if ( pid == sampler->batchID() ) {
229  for (int i = sampler->start(); i < sampler->numMySamples(); i++) {
230  *outStream << "Sample " << i << " Rank " << sampler->batchID() << "\n";
231  *outStream << "(" << sampler->getMyPoint(i)[0] << ", "
232  << sampler->getMyPoint(i)[1] << ", "
233  << sampler->getMyPoint(i)[2] << ", "
234  << sampler->getMyPoint(i)[3] << ")\n";
235  pcon->setParameter(sampler->getMyPoint(i));
236  pcon->checkSolve(*up,*zp,*cp,print,*outStream);
237  robj->setParameter(sampler->getMyPoint(i));
238  *outStream << "\n";
239  robj->checkGradient(*zp,*gzp,*yzp,print,*outStream);
240  robj->checkHessVec(*zp,*gzp,*yzp,print,*outStream);
241  *outStream << "\n\n";
242  }
243  }
244  comm->barrier();
245  }
246  }
247  obj->checkGradient(z,g,y,print0,*outStream0);
248  obj->checkHessVec(z,g,y,print0,*outStream0);
249  /*************************************************************************/
250  /************* RUN OPTIMIZATION ******************************************/
251  /*************************************************************************/
252  // READ IN XML INPUT
253  std::string filename = "input.xml";
254  Teuchos::RCP<Teuchos::ParameterList> parlist
255  = Teuchos::rcp( new Teuchos::ParameterList() );
256  Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() );
257  // RUN OPTIMIZATION
258  ROL::Algorithm<RealT> algo("Trust Region",*parlist,false);
259  zp->zero();
260  algo.run(z, g, *obj, *bnd, print0, *outStream0);
261  /*************************************************************************/
262  /************* PRINT CONTROL AND STATE TO SCREEN *************************/
263  /*************************************************************************/
264  if ( print0 ) {
265  std::ofstream ofs;
266  ofs.open("output_example_09.txt",std::ofstream::out);
267  for ( int i = 0; i < nx+2; i++ ) {
268  ofs << std::scientific << std::setprecision(10);
269  ofs << std::setw(20) << std::left << (RealT)i/((RealT)nx+1.0);
270  ofs << std::setw(20) << std::left << (*z_rcp)[i];
271  ofs << "\n";
272  }
273  ofs.close();
274  }
275  *outStream0 << "Scalar Parameter: " << z.getStatistic(0) << "\n\n";
276  }
277  catch (std::logic_error err) {
278  *outStream << err.what() << "\n";
279  errorFlag = -1000;
280  }; // end try
281 
282  comm->barrier();
283  if (errorFlag != 0)
284  std::cout << "End Result: TEST FAILED\n";
285  else
286  std::cout << "End Result: TEST PASSED\n";
287 
288  return 0;
289 }
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