ROL
burgers-control/example_01.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 "example_01.hpp"
50 
51 typedef double RealT;
52 
53 int main(int argc, char *argv[]) {
54 
55  typedef std::vector<RealT> vector;
56  typedef ROL::Vector<RealT> V;
57  typedef ROL::StdVector<RealT> SV;
58 
59  typedef typename vector::size_type uint;
60 
61 
62 
63  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
64 
65  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
66  int iprint = argc - 1;
67  ROL::Ptr<std::ostream> outStream;
68  ROL::nullstream bhs; // outputs nothing
69  if (iprint > 0)
70  outStream = ROL::makePtrFromRef(std::cout);
71  else
72  outStream = ROL::makePtrFromRef(bhs);
73 
74  int errorFlag = 0;
75 
76  // *** Example body.
77 
78  try {
79  // Initialize objective function.
80  uint nx = 1028; // Set spatial discretization.
81  RealT alpha = 1.e-3; // Set penalty parameter.
82  Objective_BurgersControl<RealT> obj(alpha,nx);
83  // Initialize iteration vectors.
84  ROL::Ptr<vector> x_ptr = ROL::makePtr<vector>(nx+2, 1.0);
85  ROL::Ptr<vector> y_ptr = ROL::makePtr<vector>(nx+2, 0.0);
86  for (uint i=0; i<nx+2; i++) {
87  (*x_ptr)[i] = (RealT)rand()/(RealT)RAND_MAX;
88  (*y_ptr)[i] = (RealT)rand()/(RealT)RAND_MAX;
89  }
90 
91  SV x(x_ptr);
92  SV y(y_ptr);
93 
94  // Check deriatives.
95  obj.checkGradient(x,x,y,true,*outStream);
96  obj.checkHessVec(x,x,y,true,*outStream);
97 
98  // Initialize Constraints
99  ROL::Ptr<vector> l_ptr = ROL::makePtr<vector>(nx+2,0.0);
100  ROL::Ptr<vector> u_ptr = ROL::makePtr<vector>(nx+2,1.0);
101  ROL::Ptr<V> lo = ROL::makePtr<SV>(l_ptr);
102  ROL::Ptr<V> up = ROL::makePtr<SV>(u_ptr);
103 
104  ROL::Bounds<RealT> icon(lo,up);
105 
106  // ROL components.
107  ROL::Ptr<ROL::Algorithm<RealT>> algo;
108  ROL::Ptr<ROL::Step<RealT>> step;
109  ROL::Ptr<ROL::StatusTest<RealT>> status;
110 
111  // Primal dual active set.
112  std::string filename = "input.xml";
113  auto parlist = ROL::getParametersFromXmlFile( filename );
114 
115  // Krylov parameters.
116  parlist->sublist("General").sublist("Krylov").set("Absolute Tolerance",1.e-8);
117  parlist->sublist("General").sublist("Krylov").set("Relative Tolerance",1.e-4);
118  parlist->sublist("General").sublist("Krylov").set("Iteration Limit",50);
119  // PDAS parameters.
120  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Relative Step Tolerance",1.e-10);
121  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Relative Gradient Tolerance",1.e-8);
122  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Iteration Limit", 10);
123  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Dual Scaling",(alpha>0.0)?alpha:1.e-4);
124  // Status test parameters.
125  parlist->sublist("Status Test").set("Gradient Tolerance",1.e-12);
126  parlist->sublist("Status Test").set("Step Tolerance",1.e-16);
127  parlist->sublist("Status Test").set("Iteration Limit",100);
128  // Define algorithm.
129  step = ROL::makePtr<ROL::PrimalDualActiveSetStep<RealT>>(*parlist);
130  status = ROL::makePtr<ROL::StatusTest<RealT>>(*parlist);
131  algo = ROL::makePtr<ROL::Algorithm<RealT>>(step,status,false);
132  // Run algorithm.
133  x.zero();
134  algo->run(x, obj, icon, true, *outStream);
135  // Output control to file.
136  std::ofstream file_pdas;
137  file_pdas.open("control_PDAS.txt");
138  for ( unsigned i = 0; i < (unsigned)nx+2; i++ ) {
139  file_pdas << (*x_ptr)[i] << "\n";
140  }
141  file_pdas.close();
142 
143  // Projected Newton.
144  parlist->sublist("General").sublist("Krylov").set("Absolute Tolerance",1.e-4);
145  parlist->sublist("General").sublist("Krylov").set("Relative Tolerance",1.e-2);
146  parlist->sublist("General").sublist("Krylov").set("Iteration Limit",50);
147  // Define algorithm.
148  step = ROL::makePtr<ROL::TrustRegionStep<RealT>>(*parlist);
149  status = ROL::makePtr<ROL::StatusTest<RealT>>(*parlist);
150  algo = ROL::makePtr<ROL::Algorithm<RealT>>(step,status,false);
151  // Run Algorithm
152  y.zero();
153  algo->run(y,obj,icon,true,*outStream);
154  // Output control to file.
155  std::ofstream file_tr;
156  file_tr.open("control_TR.txt");
157  for ( unsigned i = 0; i < (unsigned)nx+2; i++ ) {
158  file_tr << (*y_ptr)[i] << "\n";
159  }
160  file_tr.close();
161  // Output state to file.
162  std::vector<RealT> u(nx,0.0);
163  std::vector<RealT> param(4,0.0);
164  obj.solve_state(u,*x_ptr,param);
165  std::ofstream file;
166  file.open("state.txt");
167  for (unsigned i=0; i<(unsigned)nx; i++) {
168  file << i/((RealT)(nx+1)) << " " << u[i] << "\n";
169  }
170  file.close();
171  // Compute error
172  ROL::Ptr<ROL::Vector<RealT> > diff = x.clone();
173  diff->set(x);
174  diff->axpy(-1.0,y);
175  RealT error = diff->norm();
176  *outStream << "\nError between PDAS solution and TR solution is " << error << "\n";
177  errorFlag = ((error > 1e2*std::sqrt(ROL::ROL_EPSILON<RealT>())) ? 1 : 0);
178  }
179  catch (std::logic_error& err) {
180  *outStream << err.what() << "\n";
181  errorFlag = -1000;
182  }; // end try
183 
184  if (errorFlag != 0)
185  std::cout << "End Result: TEST FAILED\n";
186  else
187  std::cout << "End Result: TEST PASSED\n";
188 
189  return 0;
190 
191 }
192 
typename PV< Real >::size_type size_type
void solve_state(std::vector< Real > &u, const std::vector< Real > &z, const std::vector< Real > &param)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
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.
Vector< Real > V
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Provides the elementwise interface to apply upper and lower bound constraints.
Definition: ROL_Bounds.hpp:59
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.