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"
55 #include "ROL_ParameterList.hpp"
56 
58 
59 #include "ROL_Stream.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 <algorithm>
67 
68 #include "example_06.hpp"
69 
70 typedef double RealT;
77 
78 int main(int argc, char *argv[]) {
79 
80  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
81 
82  auto comm = ROL::toPtr(Teuchos::DefaultComm<int>::getComm());
83 
84  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
85  int iprint = argc - 1;
86  bool print = (iprint>0);
87  ROL::Ptr<std::ostream> outStream;
88  ROL::nullstream bhs; // outputs nothing
89  if (print)
90  outStream = ROL::makePtrFromRef(std::cout);
91  else
92  outStream = ROL::makePtrFromRef(bhs);
93 
94  bool print0 = print && !comm->getRank();
95  ROL::Ptr<std::ostream> outStream0;
96  if (print0)
97  outStream0 = ROL::makePtrFromRef(std::cout);
98  else
99  outStream0 = ROL::makePtrFromRef(bhs);
100 
101  int errorFlag = 0;
102 
103  // *** Example body.
104 
105  try {
106  /*************************************************************************/
107  /************* INITIALIZE BURGERS FEM CLASS ******************************/
108  /*************************************************************************/
109  int nx = 256; // Set spatial discretization.
110  RealT alpha = 1.e-3; // Set penalty parameter.
111  RealT nl = 1.0; // Nonlinearity parameter (1 = Burgers, 0 = linear).
112  RealT cH1 = 1.0; // Scale for derivative term in H1 norm.
113  RealT cL2 = 0.0; // Scale for mass term in H1 norm.
114  ROL::Ptr<BurgersFEM<RealT> > fem
115  = ROL::makePtr<BurgersFEM<RealT>>(nx,nl,cH1,cL2);
116  fem->test_inverse_mass(*outStream0);
117  fem->test_inverse_H1(*outStream0);
118  /*************************************************************************/
119  /************* INITIALIZE SIMOPT OBJECTIVE FUNCTION **********************/
120  /*************************************************************************/
121  ROL::Ptr<std::vector<RealT> > ud_ptr
122  = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
123  ROL::Ptr<ROL::Vector<RealT> > ud
124  = ROL::makePtr<L2VectorPrimal<RealT>>(ud_ptr,fem);
125  ROL::Ptr<ROL::Objective_SimOpt<RealT> > pobj
126  = ROL::makePtr<Objective_BurgersControl<RealT>>(fem,ud,alpha);
127  /*************************************************************************/
128  /************* INITIALIZE SIMOPT EQUALITY CONSTRAINT *********************/
129  /*************************************************************************/
130  bool hess = true;
131  ROL::Ptr<ROL::Constraint_SimOpt<RealT> > pcon
132  = ROL::makePtr<Constraint_BurgersControl<RealT>>(fem,hess);
133  /*************************************************************************/
134  /************* INITIALIZE VECTOR STORAGE *********************************/
135  /*************************************************************************/
136  // INITIALIZE CONTROL VECTORS
137  ROL::Ptr<std::vector<RealT> > z_ptr
138  = ROL::makePtr<std::vector<RealT>>(nx+2, 1.0);
139  ROL::Ptr<std::vector<RealT> > gz_ptr
140  = ROL::makePtr<std::vector<RealT>>(nx+2, 1.0);
141  ROL::Ptr<std::vector<RealT> > yz_ptr
142  = ROL::makePtr<std::vector<RealT>>(nx+2, 1.0);
143  for (int i=0; i<nx+2; i++) {
144  (*yz_ptr)[i] = 2.0*random<RealT>(comm)-1.0;
145  }
146  ROL::Ptr<ROL::Vector<RealT> > zp
147  = ROL::makePtr<PrimalControlVector>(z_ptr,fem);
148  ROL::Ptr<ROL::Vector<RealT> > gzp
149  = ROL::makePtr<DualControlVector>(gz_ptr,fem);
150  ROL::Ptr<ROL::Vector<RealT> > yzp
151  = ROL::makePtr<PrimalControlVector>(yz_ptr,fem);
152  RealT zvar = 0.0*random<RealT>(comm);
153  RealT gvar = random<RealT>(comm);
154  RealT yvar = random<RealT>(comm);
155  ROL::Ptr<ROL::ParameterList> hmcrlist = ROL::makePtr<ROL::ParameterList>();
156  hmcrlist->sublist("SOL").sublist("Risk Measure").set("Name","HMCR");
157  ROL::RiskVector<RealT> z(hmcrlist,zp,zvar), g(hmcrlist,gzp,gvar), y(hmcrlist,yzp,yvar);
158  // INITIALIZE STATE VECTORS
159  ROL::Ptr<std::vector<RealT> > u_ptr
160  = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
161  ROL::Ptr<std::vector<RealT> > gu_ptr
162  = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
163  ROL::Ptr<ROL::Vector<RealT> > up
164  = ROL::makePtr<PrimalStateVector>(u_ptr,fem);
165  ROL::Ptr<ROL::Vector<RealT> > gup
166  = ROL::makePtr<DualStateVector>(gu_ptr,fem);
167  // INITIALIZE CONSTRAINT VECTORS
168  ROL::Ptr<std::vector<RealT> > c_ptr
169  = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
170  ROL::Ptr<std::vector<RealT> > l_ptr
171  = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
172  for (int i=0; i<nx; i++) {
173  (*l_ptr)[i] = random<RealT>(comm);
174  }
175  ROL::Ptr<ROL::Vector<RealT> > cp
176  = ROL::makePtr<PrimalConstraintVector>(c_ptr,fem);
177  ROL::Ptr<ROL::Vector<RealT> > lp
178  = ROL::makePtr<DualConstraintVector>(l_ptr,fem);
179  /*************************************************************************/
180  /************* INITIALIZE SAMPLE GENERATOR *******************************/
181  /*************************************************************************/
182  int dim = 4, nSamp = 1000;
183  std::vector<RealT> tmp(2,0.0); tmp[0] = -1.0; tmp[1] = 1.0;
184  std::vector<std::vector<RealT> > bounds(dim,tmp);
185  ROL::Ptr<ROL::BatchManager<RealT> > bman
186  = ROL::makePtr<L2VectorBatchManager<RealT,int>>(comm);
187  ROL::Ptr<ROL::SampleGenerator<RealT> > sampler
188  = ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(
189  nSamp,bounds,bman,false,false,100);
190  /*************************************************************************/
191  /************* INITIALIZE RISK-AVERSE OBJECTIVE FUNCTION *****************/
192  /*************************************************************************/
193  bool storage = true, fdhess = false;
194  ROL::Ptr<ROL::Objective<RealT> > robj
195  = ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(
196  pobj,pcon,up,zp,lp,gup,gzp,cp,storage,fdhess);
197  //RealT order = 2.0, prob = 0.95;
198  //ROL::Ptr<ROL::Objective<RealT> > obj
199  // = ROL::makePtr<ROL::HMCRObjective<RealT>>(
200  // robj,order,prob,sampler,storage);
201  hmcrlist->sublist("SOL").sublist("Risk Measure").sublist("HMCR").set("Order",2);
202  hmcrlist->sublist("SOL").sublist("Risk Measure").sublist("HMCR").set("Confidence Level",0.95);
203  hmcrlist->sublist("SOL").sublist("Risk Measure").sublist("HMCR").set("Convex Combination Parameter",0.0);
204  ROL::Ptr<ROL::Objective<RealT> > obj
205  = ROL::makePtr<ROL::StochasticObjective<RealT> >(robj,*hmcrlist,sampler);
206  /*************************************************************************/
207  /************* CHECK DERIVATIVES AND CONSISTENCY *************************/
208  /*************************************************************************/
209  // CHECK OBJECTIVE DERIVATIVES
210  bool derivcheck = false;
211  if (derivcheck) {
212  int nranks = sampler->numBatches();
213  for (int pid = 0; pid < nranks; pid++) {
214  if ( pid == sampler->batchID() ) {
215  for (int i = sampler->start(); i < sampler->numMySamples(); i++) {
216  *outStream << "Sample " << i << " Rank " << sampler->batchID() << "\n";
217  *outStream << "(" << sampler->getMyPoint(i)[0] << ", "
218  << sampler->getMyPoint(i)[1] << ", "
219  << sampler->getMyPoint(i)[2] << ", "
220  << sampler->getMyPoint(i)[3] << ")\n";
221  pcon->setParameter(sampler->getMyPoint(i));
222  pcon->checkSolve(*up,*zp,*cp,print,*outStream);
223  robj->setParameter(sampler->getMyPoint(i));
224  *outStream << "\n";
225  robj->checkGradient(*zp,*gzp,*yzp,print,*outStream);
226  robj->checkHessVec(*zp,*gzp,*yzp,print,*outStream);
227  *outStream << "\n\n";
228  }
229  }
230  comm->barrier();
231  }
232  }
233  obj->checkGradient(z,g,y,print0,*outStream0);
234  obj->checkHessVec(z,g,y,print0,*outStream0);
235  /*************************************************************************/
236  /************* RUN OPTIMIZATION ******************************************/
237  /*************************************************************************/
238  // READ IN XML INPUT
239  std::string filename = "input.xml";
240  auto parlist = ROL::getParametersFromXmlFile( filename );
241  // DEFINE ALGORITHM
242  ROL::Algorithm<RealT> algo("Trust Region",*parlist,false);
243  // RUN OPTIMIZATION
244  z.zero();
245  algo.run(z, g, *obj, print0, *outStream0);
246  /*************************************************************************/
247  /************* PRINT CONTROL AND STATE TO SCREEN *************************/
248  /*************************************************************************/
249  *outStream0 << "\n";
250  for ( int i = 0; i < nx+2; i++ ) {
251  *outStream0 << std::scientific << std::setprecision(10);
252  *outStream0 << std::setw(20) << std::left << (RealT)i/((RealT)nx+1.0);
253  *outStream0 << std::setw(20) << std::left << (*z_ptr)[i];
254  *outStream0 << "\n";
255  }
256  *outStream0 << "\n";
257  *outStream0 << "Scalar Parameter: " << z.getStatistic(0) << "\n";
258  }
259  catch (std::logic_error err) {
260  *outStream << err.what() << "\n";
261  errorFlag = -1000;
262  }; // end try
263 
264  comm->barrier();
265  if (errorFlag != 0)
266  std::cout << "End Result: TEST FAILED\n";
267  else
268  std::cout << "End Result: TEST PASSED\n";
269 
270  return 0;
271 }
virtual std::vector< std::string > run(Vector< Real > &x, Objective< Real > &obj, bool print=false, std::ostream &outStream=std::cout, bool printVectors=false, std::ostream &vectorStream=std::cout)
Run algorithm on unconstrained problems (Type-U). This is the primary Type-U interface.
L2VectorPrimal< RealT > PrimalControlVector
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
H1VectorDual< RealT > DualStateVector
L2VectorDual< RealT > DualControlVector
Provides an interface to run optimization algorithms.
H1VectorDual< RealT > PrimalConstraintVector
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:72
int main(int argc, char *argv[])
H1VectorPrimal< RealT > PrimalStateVector
H1VectorPrimal< RealT > DualConstraintVector