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