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