ROL
burgers-control/example_02.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 
50 #include "example_02.hpp"
51 
52 typedef double RealT;
53 
54 int main(int argc, char *argv[]) {
55 
56  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
57 
58  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
59  int iprint = argc - 1;
60  Teuchos::RCP<std::ostream> outStream;
61  Teuchos::oblackholestream bhs; // outputs nothing
62  if (iprint > 0)
63  outStream = Teuchos::rcp(&std::cout, false);
64  else
65  outStream = Teuchos::rcp(&bhs, false);
66 
67  int errorFlag = 0;
68 
69  // *** Example body.
70 
71  try {
72  // Initialize full objective function.
73  int nx = 256; // Set spatial discretization.
74  RealT alpha = 1.e-3; // Set penalty parameter.
75  RealT nu = 1e-2; // Viscosity parameter.
76  Objective_BurgersControl<RealT> obj(alpha,nx);
77  // Initialize equality constraints
79  // Initialize iteration vectors.
80  Teuchos::RCP<std::vector<RealT> > z_rcp = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.0) );
81  Teuchos::RCP<std::vector<RealT> > gz_rcp = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.0) );
82  Teuchos::RCP<std::vector<RealT> > yz_rcp = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.0) );
83  for (int i=0; i<nx+2; i++) {
84  (*z_rcp)[i] = (RealT)rand()/(RealT)RAND_MAX;
85  (*yz_rcp)[i] = (RealT)rand()/(RealT)RAND_MAX;
86  }
87  ROL::StdVector<RealT> z(z_rcp);
88  ROL::StdVector<RealT> gz(gz_rcp);
89  ROL::StdVector<RealT> yz(yz_rcp);
90  Teuchos::RCP<ROL::Vector<RealT> > zp = Teuchos::rcp(&z,false);
91  Teuchos::RCP<ROL::Vector<RealT> > gzp = Teuchos::rcp(&z,false);
92  Teuchos::RCP<ROL::Vector<RealT> > yzp = Teuchos::rcp(&yz,false);
93 
94  Teuchos::RCP<std::vector<RealT> > u_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) );
95  Teuchos::RCP<std::vector<RealT> > gu_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) );
96  Teuchos::RCP<std::vector<RealT> > yu_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) );
97  for (int i=0; i<nx; i++) {
98  (*u_rcp)[i] = (RealT)rand()/(RealT)RAND_MAX;
99  (*yu_rcp)[i] = (RealT)rand()/(RealT)RAND_MAX;
100  }
101  ROL::StdVector<RealT> u(u_rcp);
102  ROL::StdVector<RealT> gu(gu_rcp);
103  ROL::StdVector<RealT> yu(yu_rcp);
104  Teuchos::RCP<ROL::Vector<RealT> > up = Teuchos::rcp(&u,false);
105  Teuchos::RCP<ROL::Vector<RealT> > gup = Teuchos::rcp(&u,false);
106  Teuchos::RCP<ROL::Vector<RealT> > yup = Teuchos::rcp(&yu,false);
107 
108  Teuchos::RCP<std::vector<RealT> > c_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) );
109  Teuchos::RCP<std::vector<RealT> > l_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) );
110  ROL::StdVector<RealT> c(c_rcp);
111  ROL::StdVector<RealT> l(l_rcp);
112 
113  ROL::Vector_SimOpt<RealT> x(up,zp);
114  ROL::Vector_SimOpt<RealT> g(gup,gzp);
115  ROL::Vector_SimOpt<RealT> y(yup,yzp);
116 
117  // Check derivatives.
118  obj.checkGradient(x,x,y,true,*outStream);
119  obj.checkHessVec(x,x,y,true,*outStream);
120  con.checkApplyJacobian(x,y,c,true,*outStream);
121  con.checkApplyAdjointJacobian(x,yu,c,x,true,*outStream);
122  con.checkApplyAdjointHessian(x,yu,y,x,true,*outStream);
123 
124  // Initialize reduced objective function.
125  Teuchos::RCP<std::vector<RealT> > p_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) );
126  ROL::StdVector<RealT> p(p_rcp);
127  Teuchos::RCP<ROL::Vector<RealT> > pp = Teuchos::rcp(&p,false);
128  Teuchos::RCP<ROL::Objective_SimOpt<RealT> > pobj = Teuchos::rcp(&obj,false);
129  Teuchos::RCP<ROL::EqualityConstraint_SimOpt<RealT> > pcon = Teuchos::rcp(&con,false);
130  ROL::Reduced_Objective_SimOpt<RealT> robj(pobj,pcon,up,pp);
131  // Check derivatives.
132  robj.checkGradient(z,z,yz,true,*outStream);
133  robj.checkHessVec(z,z,yz,true,*outStream);
134 
135  // Get parameter list.
136  std::string filename = "input.xml";
137  Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() );
138  Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() );
139  parlist->sublist("Status Test").set("Gradient Tolerance",1.e-14);
140  parlist->sublist("Status Test").set("Constraint Tolerance",1.e-14);
141  parlist->sublist("Status Test").set("Step Tolerance",1.e-16);
142  parlist->sublist("Status Test").set("Iteration Limit",1000);
143  // Declare ROL algorithm pointer.
144  Teuchos::RCP<ROL::Algorithm<RealT> > algo;
145 
146  // Run optimization with Composite Step.
147  algo = Teuchos::rcp(new ROL::Algorithm<RealT>("Composite Step",*parlist,false));
148  RealT zerotol = std::sqrt(ROL::ROL_EPSILON);
149  z.zero();
150  con.solve(u,z,zerotol);
151  c.zero(); l.zero();
152  algo->run(x, g, l, c, obj, con, true, *outStream);
153  Teuchos::RCP<ROL::Vector<RealT> > zCS = z.clone();
154  zCS->set(z);
155 
156  // Run Optimization with Trust-Region algorithm.
157  algo = Teuchos::rcp(new ROL::Algorithm<RealT>("Trust Region",*parlist,false));
158  z.zero();
159  algo->run(z,robj,true,*outStream);
160 
161  // Check solutions.
162  Teuchos::RCP<ROL::Vector<RealT> > err = z.clone();
163  err->set(*zCS); err->axpy(-1.,z);
164  errorFlag += ((err->norm()) > 1.e-8) ? 1 : 0;
165  }
166  catch (std::logic_error err) {
167  *outStream << err.what() << "\n";
168  errorFlag = -1000;
169  }; // end try
170 
171  if (errorFlag != 0)
172  std::cout << "End Result: TEST FAILED\n";
173  else
174  std::cout << "End Result: TEST PASSED\n";
175 
176  return 0;
177 
178 }
179 
Defines the linear algebra or vector space interface for simulation-based optimization.
virtual std::vector< std::vector< Real > > checkApplyAdjointJacobian(const Vector< Real > &x, const Vector< Real > &v, const Vector< Real > &c, const Vector< Real > &ajv, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS)
Finite-difference check for the application of the adjoint of constraint Jacobian.
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:157
virtual std::vector< std::vector< Real > > checkGradient(const Vector< Real > &x, const Vector< Real > &d, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Finite-difference gradient check.
virtual std::vector< std::vector< Real > > checkApplyJacobian(const Vector< Real > &x, const Vector< Real > &v, const Vector< Real > &jv, const std::vector< Real > &steps, const bool printToStream=true, std::ostream &outStream=std::cout, const int order=1)
Finite-difference check for the constraint Jacobian application.
Teuchos::RCP< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Provides the interface to evaluate simulation-based reduced objective functions.
Provides the std::vector implementation of the ROL::Vector interface.
Provides an interface to run optimization algorithms.
int main(int argc, char *argv[])
void solve(ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Given , solve for .
virtual std::vector< std::vector< Real > > checkHessVec(const Vector< Real > &x, const Vector< Real > &v, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Finite-difference Hessian-applied-to-vector check.
double RealT
virtual std::vector< std::vector< Real > > checkApplyAdjointHessian(const Vector< Real > &x, const Vector< Real > &u, const Vector< Real > &v, const Vector< Real > &hv, const std::vector< Real > &step, const bool printToScreen=true, std::ostream &outStream=std::cout, const int order=1)
Finite-difference check for the application of the adjoint of constraint Hessian. ...
static const double ROL_EPSILON
Platform-dependent machine epsilon.
Definition: ROL_Types.hpp:118