ROL
example_08.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 
54 
55 #include "Teuchos_oblackholestream.hpp"
56 #include "Teuchos_XMLParameterListHelpers.hpp"
57 #include "Teuchos_GlobalMPISession.hpp"
58 #include "Teuchos_Comm.hpp"
59 #include "Teuchos_DefaultComm.hpp"
60 #include "Teuchos_CommHelpers.hpp"
61 
62 #include <iostream>
63 #include <fstream>
64 #include <algorithm>
65 
66 #include "example_08.hpp"
67 
68 typedef double RealT;
75 
76 int main(int argc, char *argv[]) {
77 
78  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
79  Teuchos::RCP<const Teuchos::Comm<int> > comm
80  = Teuchos::DefaultComm<int>::getComm();
81 
82  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
83  int iprint = argc - 1;
84  bool print = (iprint>0); // && !(comm->getRank());
85  Teuchos::RCP<std::ostream> outStream;
86  Teuchos::oblackholestream bhs; // outputs nothing
87  if (print)
88  outStream = Teuchos::rcp(&std::cout, false);
89  else
90  outStream = Teuchos::rcp(&bhs, false);
91 
92  bool print0 = print && !comm->getRank();
93  Teuchos::RCP<std::ostream> outStream0;
94  if (print0)
95  outStream0 = Teuchos::rcp(&std::cout, false);
96  else
97  outStream0 = Teuchos::rcp(&bhs, false);
98 
99  int errorFlag = 0;
100 
101  // *** Example body.
102 
103  try {
104  /*************************************************************************/
105  /************* INITIALIZE BURGERS FEM CLASS ******************************/
106  /*************************************************************************/
107  int nx = 512; // Set spatial discretization.
108  RealT alpha = 1.e-3; // Set penalty parameter.
109  RealT nl = 1.0; // Nonlinearity parameter (1 = Burgers, 0 = linear).
110  RealT cH1 = 1.0; // Scale for derivative term in H1 norm.
111  RealT cL2 = 0.0; // Scale for mass term in H1 norm.
112  Teuchos::RCP<BurgersFEM<RealT> > fem
113  = Teuchos::rcp(new BurgersFEM<RealT>(nx,nl,cH1,cL2));
114  fem->test_inverse_mass(*outStream0);
115  fem->test_inverse_H1(*outStream0);
116  /*************************************************************************/
117  /************* INITIALIZE SIMOPT OBJECTIVE FUNCTION **********************/
118  /*************************************************************************/
119  Teuchos::RCP<std::vector<RealT> > ud_rcp
120  = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) );
121  Teuchos::RCP<ROL::Vector<RealT> > ud
122  = Teuchos::rcp(new L2VectorPrimal<RealT>(ud_rcp,fem));
123  Teuchos::RCP<ROL::ParametrizedObjective_SimOpt<RealT> > pobj
124  = Teuchos::rcp(new Objective_BurgersControl<RealT>(fem,ud,alpha));
125  /*************************************************************************/
126  /************* INITIALIZE SIMOPT EQUALITY CONSTRAINT *********************/
127  /*************************************************************************/
128  bool hess = true;
129  Teuchos::RCP<ROL::ParametrizedEqualityConstraint_SimOpt<RealT> > pcon
130  = Teuchos::rcp(new EqualityConstraint_BurgersControl<RealT>(fem,hess));
131  /*************************************************************************/
132  /************* INITIALIZE VECTOR STORAGE *********************************/
133  /*************************************************************************/
134  // INITIALIZE CONTROL VECTORS
135  Teuchos::RCP<std::vector<RealT> > z_rcp
136  = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.0) );
137  Teuchos::RCP<std::vector<RealT> > gz_rcp
138  = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.0) );
139  Teuchos::RCP<std::vector<RealT> > yz_rcp
140  = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.0) );
141  for (int i=0; i<nx+2; i++) {
142  (*yz_rcp)[i] = 2.0*random<RealT>(comm)-1.0;
143  }
144  Teuchos::RCP<ROL::Vector<RealT> > zp
145  = Teuchos::rcp(new PrimalControlVector(z_rcp,fem));
146  Teuchos::RCP<ROL::Vector<RealT> > gzp
147  = Teuchos::rcp(new DualControlVector(gz_rcp,fem));
148  Teuchos::RCP<ROL::Vector<RealT> > yzp
149  = Teuchos::rcp(new PrimalControlVector(yz_rcp,fem));
150  // INITIALIZE STATE VECTORS
151  Teuchos::RCP<std::vector<RealT> > u_rcp
152  = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) );
153  Teuchos::RCP<std::vector<RealT> > gu_rcp
154  = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) );
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 = 1000;
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 REDUCED 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  /*************************************************************************/
190  /************* INITIALIZE BOUND CONSTRAINTS ******************************/
191  /*************************************************************************/
192  std::vector<RealT> Zlo(nx+2,0.0), Zhi(nx+2,10.0);
193  for (int i = 0; i < nx+2; i++) {
194  if ( i < (int)((nx+2)/3) ) {
195  Zlo[i] = -1.0;
196  Zhi[i] = 1.0;
197  }
198  if ( i >= (int)((nx+2)/3) && i < (int)(2*(nx+2)/3) ) {
199  Zlo[i] = 1.0;
200  Zhi[i] = 5.0;
201  }
202  if ( i >= (int)(2*(nx+2)/3) ) {
203  Zlo[i] = 5.0;
204  Zhi[i] = 10.0;
205  }
206  }
207  Teuchos::RCP<ROL::BoundConstraint<RealT> > Zbnd
208  = Teuchos::rcp(new L2BoundConstraint<RealT>(Zlo,Zhi,fem));
209  /*************************************************************************/
210  /************* INITIALIZE OPTIMIZATION PROBLEM ***************************/
211  /*************************************************************************/
212  Teuchos::ParameterList SOLlist;
213  SOLlist.sublist("SOL").set("Stochastic Optimization Type","Risk Averse");
214  SOLlist.sublist("SOL").set("Store Sampled Value and Gradient",storage);
215  SOLlist.sublist("SOL").sublist("Risk Measure").set("Name","KL Divergence");
216  SOLlist.sublist("SOL").sublist("Risk Measure").sublist("KL Divergence").set("Threshold",1.e-2);
217  ROL::StochasticProblem<RealT> optProb(SOLlist,robj,sampler,zp,Zbnd);
218  /*************************************************************************/
219  /************* CHECK DERIVATIVES AND CONSISTENCY *************************/
220  /*************************************************************************/
221  // CHECK OBJECTIVE DERIVATIVES
222  bool derivcheck = false;
223  if (derivcheck) {
224  int nranks = sampler->numBatches();
225  for (int pid = 0; pid < nranks; pid++) {
226  if ( pid == sampler->batchID() ) {
227  for (int i = sampler->start(); i < sampler->numMySamples(); i++) {
228  *outStream << "Sample " << i << " Rank " << sampler->batchID() << "\n";
229  *outStream << "(" << sampler->getMyPoint(i)[0] << ", "
230  << sampler->getMyPoint(i)[1] << ", "
231  << sampler->getMyPoint(i)[2] << ", "
232  << sampler->getMyPoint(i)[3] << ")\n";
233  pcon->setParameter(sampler->getMyPoint(i));
234  pcon->checkSolve(*up,*zp,*cp,print,*outStream);
235  robj->setParameter(sampler->getMyPoint(i));
236  *outStream << "\n";
237  robj->checkGradient(*zp,*gzp,*yzp,print,*outStream);
238  robj->checkHessVec(*zp,*gzp,*yzp,print,*outStream);
239  *outStream << "\n\n";
240  }
241  }
242  comm->barrier();
243  }
244  }
245  optProb.checkObjectiveGradient(*yzp,print0,*outStream0);
246  optProb.checkObjectiveHessVec(*yzp,print0,*outStream0);
247  /*************************************************************************/
248  /************* RUN OPTIMIZATION ******************************************/
249  /*************************************************************************/
250  // READ IN XML INPUT
251  std::string filename = "input.xml";
252  Teuchos::RCP<Teuchos::ParameterList> parlist
253  = Teuchos::rcp( new Teuchos::ParameterList() );
254  Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() );
255  // RUN OPTIMIZATION
256  ROL::Algorithm<RealT> algo("Trust Region",*parlist,false);
257  zp->zero();
258  algo.run(optProb,print0,*outStream0);
259  /*************************************************************************/
260  /************* PRINT CONTROL AND STATE TO SCREEN *************************/
261  /*************************************************************************/
262  if ( print0 ) {
263  std::ofstream ofs;
264  ofs.open("output_example_08.txt",std::ofstream::out);
265  for ( int i = 0; i < nx+2; i++ ) {
266  ofs << std::scientific << std::setprecision(10);
267  ofs << std::setw(20) << std::left << (RealT)i/((RealT)nx+1.0);
268  ofs << std::setw(20) << std::left << (*z_rcp)[i];
269  ofs << "\n";
270  }
271  ofs.close();
272  }
273  *outStream0 << "Scalar Parameter: " << optProb.getSolutionStatistic() << "\n\n";
274  }
275  catch (std::logic_error err) {
276  *outStream << err.what() << "\n";
277  errorFlag = -1000;
278  }; // end try
279 
280  comm->barrier();
281  if (errorFlag != 0)
282  std::cout << "End Result: TEST FAILED\n";
283  else
284  std::cout << "End Result: TEST PASSED\n";
285 
286  return 0;
287 }
int main(int argc, char *argv[])
Definition: example_08.cpp:76
H1VectorPrimal< RealT > DualConstraintVector
Definition: example_08.cpp:74
L2VectorDual< RealT > DualControlVector
Definition: example_08.cpp:72
std::vector< std::vector< Real > > checkObjectiveGradient(const Vector< Real > &d, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
H1VectorDual< RealT > DualStateVector
Definition: example_08.cpp:70
Provides an interface to run optimization algorithms.
std::vector< std::vector< Real > > checkObjectiveHessVec(const Vector< Real > &v, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
H1VectorPrimal< RealT > PrimalStateVector
Definition: example_08.cpp:69
double RealT
Definition: example_08.cpp:68
L2VectorPrimal< RealT > PrimalControlVector
Definition: example_08.cpp:71
double RealT
H1VectorDual< RealT > PrimalConstraintVector
Definition: example_08.cpp:73