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 
53 #include "ROL_Stream.hpp"
54 
55 #include "Teuchos_GlobalMPISession.hpp"
56 #include "Teuchos_LAPACK.hpp"
57 
58 #include <iostream>
59 #include <algorithm>
60 
61 #include "example_02.hpp"
62 
63 typedef double RealT;
64 
65 int main(int argc, char *argv[]) {
66 
67  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
68 
69  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
70  int iprint = argc - 1;
71  ROL::Ptr<std::ostream> outStream;
72  ROL::nullstream bhs; // outputs nothing
73  if (iprint > 0)
74  outStream = ROL::makePtrFromRef(std::cout);
75  else
76  outStream = ROL::makePtrFromRef(bhs);
77 
78  int errorFlag = 0;
79 
80  // *** Example body.
81 
82  try {
83  // Initialize full objective function.
84  int nx = 256; // Set spatial discretization.
85  RealT alpha = 1.e-3; // Set penalty parameter.
86  RealT nu = 1e-2; // Viscosity parameter.
87  Objective_BurgersControl<RealT> obj(alpha,nx);
88  // Initialize equality constraints
90  ROL::ParameterList list;
91  list.sublist("SimOpt").sublist("Solve").set("Absolute Residual Tolerance",1.e2*ROL::ROL_EPSILON<RealT>());
92  con.setSolveParameters(list);
93  // Initialize iteration vectors.
94  ROL::Ptr<std::vector<RealT> > z_ptr = ROL::makePtr<std::vector<RealT>>(nx+2, 1.0);
95  ROL::Ptr<std::vector<RealT> > gz_ptr = ROL::makePtr<std::vector<RealT>>(nx+2, 1.0);
96  ROL::Ptr<std::vector<RealT> > yz_ptr = ROL::makePtr<std::vector<RealT>>(nx+2, 1.0);
97  for (int i=0; i<nx+2; i++) {
98  (*z_ptr)[i] = (RealT)rand()/(RealT)RAND_MAX;
99  (*yz_ptr)[i] = (RealT)rand()/(RealT)RAND_MAX;
100  }
101  ROL::StdVector<RealT> z(z_ptr);
102  ROL::StdVector<RealT> gz(gz_ptr);
103  ROL::StdVector<RealT> yz(yz_ptr);
104  ROL::Ptr<ROL::Vector<RealT> > zp = ROL::makePtrFromRef(z);
105  ROL::Ptr<ROL::Vector<RealT> > gzp = ROL::makePtrFromRef(z);
106  ROL::Ptr<ROL::Vector<RealT> > yzp = ROL::makePtrFromRef(yz);
107 
108  ROL::Ptr<std::vector<RealT> > u_ptr = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
109  ROL::Ptr<std::vector<RealT> > gu_ptr = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
110  ROL::Ptr<std::vector<RealT> > yu_ptr = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
111  for (int i=0; i<nx; i++) {
112  (*u_ptr)[i] = (RealT)rand()/(RealT)RAND_MAX;
113  (*yu_ptr)[i] = (RealT)rand()/(RealT)RAND_MAX;
114  }
115  ROL::StdVector<RealT> u(u_ptr);
116  ROL::StdVector<RealT> gu(gu_ptr);
117  ROL::StdVector<RealT> yu(yu_ptr);
118  ROL::Ptr<ROL::Vector<RealT> > up = ROL::makePtrFromRef(u);
119  ROL::Ptr<ROL::Vector<RealT> > gup = ROL::makePtrFromRef(gu);
120  ROL::Ptr<ROL::Vector<RealT> > yup = ROL::makePtrFromRef(yu);
121 
122  ROL::Ptr<std::vector<RealT> > c_ptr = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
123  ROL::Ptr<std::vector<RealT> > l_ptr = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
124  ROL::StdVector<RealT> c(c_ptr);
125  ROL::StdVector<RealT> l(l_ptr);
126 
127  ROL::Vector_SimOpt<RealT> x(up,zp);
128  ROL::Vector_SimOpt<RealT> g(gup,gzp);
129  ROL::Vector_SimOpt<RealT> y(yup,yzp);
130 
131  // Check derivatives.
132  obj.checkGradient(x,x,y,true,*outStream);
133  obj.checkHessVec(x,x,y,true,*outStream);
134  con.checkApplyJacobian(x,y,c,true,*outStream);
135  con.checkApplyAdjointJacobian(x,yu,c,x,true,*outStream);
136  con.checkApplyAdjointHessian(x,yu,y,x,true,*outStream);
137 
138  // Initialize reduced objective function.
139  ROL::Ptr<std::vector<RealT> > p_ptr = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
140  ROL::StdVector<RealT> p(p_ptr);
141  ROL::Ptr<ROL::Vector<RealT> > pp = ROL::makePtrFromRef(p);
142  ROL::Ptr<ROL::Objective_SimOpt<RealT> > pobj = ROL::makePtrFromRef(obj);
143  ROL::Ptr<ROL::Constraint_SimOpt<RealT> > pcon = ROL::makePtrFromRef(con);
144  ROL::Reduced_Objective_SimOpt<RealT> robj(pobj,pcon,up,zp,pp);
145  // Check derivatives.
146  robj.checkGradient(z,z,yz,true,*outStream);
147  robj.checkHessVec(z,z,yz,true,*outStream);
148 
149  // Get parameter list.
150  std::string filename = "input.xml";
151  auto parlist = ROL::getParametersFromXmlFile( filename );
152  parlist->sublist("Status Test").set("Gradient Tolerance",1.e-14);
153  parlist->sublist("Status Test").set("Constraint Tolerance",1.e-14);
154  parlist->sublist("Status Test").set("Step Tolerance",1.e-16);
155  parlist->sublist("Status Test").set("Iteration Limit",1000);
156 
157  // Run equality-constrained optimization.
158  RealT zerotol = std::sqrt(ROL::ROL_EPSILON<RealT>());
159  z.zero();
160  con.solve(c,u,z,zerotol);
161  c.zero(); l.zero();
162  {
163  // Define algorithm.
165  // Run Algorithm
166  algo.run(x, obj, con, l, *outStream);
167  }
168  ROL::Ptr<ROL::Vector<RealT> > zCS = z.clone();
169  zCS->set(z);
170 
171  // Run unconstrained optimization.
172  z.zero();
173  {
174  // Define algorithm.
176  // Run Algorithm
177  algo.run(z, z.dual(), robj, *outStream);
178  }
179 
180  // Check solutions.
181  ROL::Ptr<ROL::Vector<RealT> > err = z.clone();
182  err->set(*zCS); err->axpy(-1.,z);
183  errorFlag += ((err->norm()) > 1.e-8) ? 1 : 0;
184  }
185  catch (std::logic_error& err) {
186  *outStream << err.what() << "\n";
187  errorFlag = -1000;
188  }; // end try
189 
190  if (errorFlag != 0)
191  std::cout << "End Result: TEST FAILED\n";
192  else
193  std::cout << "End Result: TEST PASSED\n";
194 
195  return 0;
196 
197 }
198 
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Definition: ROL_Vector.hpp:226
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.
void solve(ROL::Vector< Real > &c, ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Given , solve for .
Definition: example_03.hpp:487
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:167
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
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 void run(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &obj, Constraint< Real > &econ, Vector< Real > &emul, const Vector< Real > &eres, std::ostream &outStream=std::cout) override
Run algorithm on equality constrained problems (Type-E). This general interface supports the use of d...
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Provides an interface to run equality constrained optimization algorithms using the Composite-Step Tr...
void run(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &obj, std::ostream &outStream=std::cout) override
Run algorithm on unconstrained problems (Type-U). This general interface supports the use of dual opt...
virtual Ptr< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
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. ...
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.
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:72
int main(int argc, char *argv[])
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.
virtual void setSolveParameters(ParameterList &parlist)
Set solve parameters.
Provides an interface to run trust-region methods for unconstrained optimization algorithms.