ROL
example_04.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"
51 #include "ROL_Vector_SimOpt.hpp"
52 
53 #include "Teuchos_oblackholestream.hpp"
54 #include "Teuchos_GlobalMPISession.hpp"
55 #include "Teuchos_XMLParameterListHelpers.hpp"
56 
57 #include <iostream>
58 #include <algorithm>
59 
60 #include "example_04.hpp"
61 
62 typedef double RealT;
69 
70 int main(int argc, char *argv[]) {
71 
72  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
73  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
74  int iprint = argc - 1;
75  Teuchos::RCP<std::ostream> outStream;
76  Teuchos::oblackholestream bhs; // outputs nothing
77  if (iprint > 0)
78  outStream = Teuchos::rcp(&std::cout, false);
79  else
80  outStream = Teuchos::rcp(&bhs, false);
81 
82  int errorFlag = 0;
83 
84  // *** Example body.
85  try {
86  /*************************************************************************/
87  /************* INITIALIZE BURGERS FEM CLASS ******************************/
88  /*************************************************************************/
89  int nx = 128; // Set spatial discretization.
90  RealT alpha = 1.e-3; // Set penalty parameter.
91  RealT nu = 1e-2; // Viscosity parameter.
92  RealT nl = 1.0; // Nonlinearity parameter (1 = Burgers, 0 = linear).
93  RealT u0 = 1.0; // Dirichlet boundary condition at x=0.
94  RealT u1 = 0.0; // Dirichlet boundary condition at x=1.
95  RealT f = 0.0; // Constant volumetric force.
96  RealT cH1 = 1.0; // Scale for derivative term in H1 norm.
97  RealT cL2 = 0.0; // Scale for mass term in H1 norm.
98  Teuchos::RCP<BurgersFEM<RealT> > fem
99  = Teuchos::rcp(new BurgersFEM<RealT>(nx,nu,nl,u0,u1,f,cH1,cL2));
100  fem->test_inverse_mass(*outStream);
101  fem->test_inverse_H1(*outStream);
102  /*************************************************************************/
103  /************* INITIALIZE SIMOPT OBJECTIVE FUNCTION **********************/
104  /*************************************************************************/
105  Teuchos::RCP<std::vector<RealT> > ud_rcp
106  = Teuchos::rcp( new std::vector<RealT> (nx, 1.) );
107  Teuchos::RCP<ROL::Vector<RealT> > ud
108  = Teuchos::rcp(new L2VectorPrimal<RealT>(ud_rcp,fem));
109  Objective_BurgersControl<RealT> obj(fem,ud,alpha);
110  /*************************************************************************/
111  /************* INITIALIZE SIMOPT EQUALITY CONSTRAINT *********************/
112  /*************************************************************************/
113  bool useEChessian = true;
114  EqualityConstraint_BurgersControl<RealT> con(fem, useEChessian);
115  /*************************************************************************/
116  /************* INITIALIZE BOUND CONSTRAINTS ******************************/
117  /*************************************************************************/
118  // INITIALIZE STATE CONSTRAINTS
119  std::vector<RealT> Ulo(nx, 0.), Uhi(nx, 1.);
120  //std::vector<RealT> Ulo(nx, -1.e8), Uhi(nx, 1.e8);
121  Teuchos::RCP<ROL::BoundConstraint<RealT> > Ubnd
122  = Teuchos::rcp(new H1BoundConstraint<RealT>(Ulo,Uhi,fem));
123  //Ubnd->deactivate();
124  // INITIALIZE CONTROL CONSTRAINTS
125  //std::vector<RealT> Zlo(nx+2, -1.e8), Zhi(nx+2, 1.e8);
126  std::vector<RealT> Zlo(nx+2,0.), Zhi(nx+2,2.);
127  Teuchos::RCP<ROL::BoundConstraint<RealT> > Zbnd
128  = Teuchos::rcp(new L2BoundConstraint<RealT>(Zlo,Zhi,fem));
129  //Zbnd->deactivate();
130  // INITIALIZE SIMOPT BOUND CONSTRAINTS
131  ROL::BoundConstraint_SimOpt<RealT> bnd(Ubnd,Zbnd);
132  bnd.deactivate();
133  /*************************************************************************/
134  /************* INITIALIZE VECTOR STORAGE *********************************/
135  /*************************************************************************/
136  // INITIALIZE CONTROL VECTORS
137  Teuchos::RCP<std::vector<RealT> > z_rcp
138  = Teuchos::rcp( new std::vector<RealT> (nx+2, 0.) );
139  Teuchos::RCP<std::vector<RealT> > zrand_rcp
140  = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.) );
141  Teuchos::RCP<std::vector<RealT> > gz_rcp
142  = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.) );
143  Teuchos::RCP<std::vector<RealT> > yz_rcp
144  = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.) );
145  for (int i=0; i<nx+2; i++) {
146  (*zrand_rcp)[i] = 10.*(RealT)rand()/(RealT)RAND_MAX-5.;
147  (*yz_rcp)[i] = 10.*(RealT)rand()/(RealT)RAND_MAX-5.;
148  }
149  Teuchos::RCP<ROL::Vector<RealT> > zp
150  = Teuchos::rcp(new PrimalControlVector(z_rcp,fem));
151  Teuchos::RCP<ROL::Vector<RealT> > zrandp
152  = Teuchos::rcp(new PrimalControlVector(zrand_rcp,fem));
153  Teuchos::RCP<ROL::Vector<RealT> > gzp
154  = Teuchos::rcp(new DualControlVector(gz_rcp,fem));
155  Teuchos::RCP<ROL::Vector<RealT> > yzp
156  = Teuchos::rcp(new PrimalControlVector(yz_rcp,fem));
157  // INITIALIZE STATE VECTORS
158  Teuchos::RCP<std::vector<RealT> > u_rcp
159  = Teuchos::rcp( new std::vector<RealT> (nx, 1.) );
160  Teuchos::RCP<std::vector<RealT> > gu_rcp
161  = Teuchos::rcp( new std::vector<RealT> (nx, 1.) );
162  Teuchos::RCP<std::vector<RealT> > yu_rcp
163  = Teuchos::rcp( new std::vector<RealT> (nx, 1.) );
164  for (int i=0; i<nx; i++) {
165  (*yu_rcp)[i] = 10.*(RealT)rand()/(RealT)RAND_MAX-5.;
166  }
167  Teuchos::RCP<ROL::Vector<RealT> > up
168  = Teuchos::rcp(new PrimalStateVector(u_rcp,fem));
169  Teuchos::RCP<ROL::Vector<RealT> > gup
170  = Teuchos::rcp(new DualStateVector(gu_rcp,fem));
171  Teuchos::RCP<ROL::Vector<RealT> > yup
172  = Teuchos::rcp(new PrimalStateVector(yu_rcp,fem));
173  // INITIALIZE CONSTRAINT VECTORS
174  Teuchos::RCP<std::vector<RealT> > c_rcp
175  = Teuchos::rcp( new std::vector<RealT> (nx, 1.) );
176  Teuchos::RCP<std::vector<RealT> > l_rcp
177  = Teuchos::rcp( new std::vector<RealT> (nx, 1.) );
178  for (int i=0; i<nx; i++) {
179  (*l_rcp)[i] = (RealT)rand()/(RealT)RAND_MAX;
180  }
181  PrimalConstraintVector c(c_rcp,fem);
182  DualConstraintVector l(l_rcp,fem);
183  // INITIALIZE SIMOPT VECTORS
184  ROL::Vector_SimOpt<RealT> x(up,zp);
185  ROL::Vector_SimOpt<RealT> g(gup,gzp);
186  ROL::Vector_SimOpt<RealT> y(yup,yzp);
187  // READ IN XML INPUT
188  std::string filename = "input.xml";
189  Teuchos::RCP<Teuchos::ParameterList> parlist
190  = Teuchos::rcp( new Teuchos::ParameterList() );
191  Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() );
192  /*************************************************************************/
193  /************* CHECK DERIVATIVES AND CONSISTENCY *************************/
194  /*************************************************************************/
195  zp->set(*zrandp);
196  // CHECK OBJECTIVE DERIVATIVES
197  obj.checkGradient(x,g,y,true,*outStream);
198  obj.checkHessVec(x,g,y,true,*outStream);
199  // CHECK EQUALITY CONSTRAINT DERIVATIVES
200  con.checkApplyJacobian(x,y,c,true,*outStream);
201  con.checkApplyAdjointHessian(x,*yup,y,g,true,*outStream);
202  // CHECK EQUALITY CONSTRAINT CONSISTENCY
203  con.checkSolve(*up,*zp,c,true,*outStream);
204  con.checkAdjointConsistencyJacobian_1(l,*yup,*up,*zp,true,*outStream);
205  con.checkAdjointConsistencyJacobian_2(l,*yzp,*up,*zp,true,*outStream);
206  con.checkInverseJacobian_1(c,*yup,*up,*zp,true,*outStream);
207  con.checkInverseAdjointJacobian_1(c,*yup,*up,*zp,true,*outStream);
208  *outStream << "\n";
209  // CHECK PENALTY OBJECTIVE DERIVATIVES
210  Teuchos::RCP<ROL::Objective<RealT> > obj_ptr = Teuchos::rcpFromRef(obj);
211  Teuchos::RCP<ROL::EqualityConstraint<RealT> > con_ptr = Teuchos::rcpFromRef(con);
212  Teuchos::RCP<ROL::BoundConstraint<RealT> > bnd_ptr = Teuchos::rcpFromRef(bnd);
213  ROL::MoreauYosidaPenalty<RealT> myPen(obj_ptr,bnd_ptr,x,10.0);
214  myPen.checkGradient(x, y, true, *outStream);
215  myPen.checkHessVec(x, g, y, true, *outStream);
216  ROL::AugmentedLagrangian<RealT> myAugLag(obj_ptr,con_ptr,l,1.,x,c,*parlist);
217  myAugLag.checkGradient(x, y, true, *outStream);
218  myAugLag.checkHessVec(x, g, y, true, *outStream);
219  /*************************************************************************/
220  /************* RUN OPTIMIZATION ******************************************/
221  /*************************************************************************/
222  // SOLVE USING MOREAU-YOSIDA PENALTY
223  ROL::Algorithm<RealT> algoMY("Moreau-Yosida Penalty",*parlist,false);
224  zp->set(*zrandp);
225  RealT zerotol = std::sqrt(ROL::ROL_EPSILON<RealT>());
226  con.solve(c,*up,*zp,zerotol);
227  obj.gradient_1(*gup,*up,*zp,zerotol);
228  gup->scale(-1.0);
229  con.applyInverseAdjointJacobian_1(l,*gup,*up,*zp,zerotol);
230  gup->zero(); c.zero();
231  algoMY.run(x, g, l, c, myPen, con, bnd, true, *outStream);
232  Teuchos::RCP<ROL::Vector<RealT> > xMY = x.clone();
233  xMY->set(x);
234  // SOLVE USING AUGMENTED LAGRANGIAN
235  ROL::Algorithm<RealT> algoAL("Augmented Lagrangian",*parlist,false);
236  zp->set(*zrandp);
237  con.solve(c,*up,*zp,zerotol);
238  obj.gradient_1(*gup,*up,*zp,zerotol);
239  gup->scale(-1.0);
240  con.applyInverseAdjointJacobian_1(l,*gup,*up,*zp,zerotol);
241  gup->zero(); c.zero();
242  algoAL.run(x, g, l, c, myAugLag, con, bnd, true, *outStream);
243  // COMPARE SOLUTIONS
244  Teuchos::RCP<ROL::Vector<RealT> > err = x.clone();
245  err->set(x); err->axpy(-1.,*xMY);
246  errorFlag += ((err->norm() > 1.e-7*x.norm()) ? 1 : 0);
247  }
248  catch (std::logic_error err) {
249  *outStream << err.what() << "\n";
250  errorFlag = -1000;
251  }; // end try
252 
253  if (errorFlag != 0)
254  std::cout << "End Result: TEST FAILED\n";
255  else
256  std::cout << "End Result: TEST PASSED\n";
257 
258  return 0;
259 }
virtual Real checkInverseJacobian_1(const Vector< Real > &jv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, const bool printToStream=true, std::ostream &outStream=std::cout)
Provides the interface to evaluate the augmented Lagrangian.
H1VectorDual< RealT > PrimalConstraintVector
Definition: example_04.cpp:67
Teuchos::RCP< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Defines the linear algebra or vector space interface for simulation-based optimization.
void solve(ROL::Vector< Real > &c, ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Given , solve for .
Definition: example_03.hpp:484
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:157
int main(int argc, char *argv[])
Definition: example_04.cpp:70
H1VectorPrimal< RealT > DualConstraintVector
Definition: example_04.cpp:68
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.
L2VectorDual< RealT > DualControlVector
Definition: example_04.cpp:66
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.
void gradient_1(ROL::Vector< Real > &g, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Compute gradient with respect to first component.
void applyInverseAdjointJacobian_1(ROL::Vector< Real > &iajv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply the inverse of the adjoint of the partial constraint Jacobian at , , to the vector ...
Definition: test_04.hpp:971
virtual Real checkSolve(const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, const ROL::Vector< Real > &c, const bool printToStream=true, std::ostream &outStream=std::cout)
Provides an interface to run optimization algorithms.
Provides the interface to evaluate the Moreau-Yosida penalty function.
H1VectorDual< RealT > DualStateVector
Definition: example_04.cpp:64
L2VectorPrimal< RealT > PrimalControlVector
Definition: example_04.cpp:65
Real norm() const
Returns where .
H1VectorPrimal< RealT > PrimalStateVector
Definition: example_04.cpp:63
Provides definitions of equality constraint and objective for example_04.
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 std::vector< std::string > run(Vector< Real > &x, Objective< Real > &obj, bool print=false, std::ostream &outStream=std::cout)
Run algorithm on unconstrained problems (Type-U). This is the primary Type-U interface.
virtual Real checkAdjointConsistencyJacobian_1(const Vector< Real > &w, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, const bool printToStream=true, std::ostream &outStream=std::cout)
Check the consistency of the Jacobian and its adjoint. This is the primary interface.
double RealT
virtual Real checkInverseAdjointJacobian_1(const Vector< Real > &jv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, const bool printToStream=true, std::ostream &outStream=std::cout)
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. ...
double RealT
Definition: example_04.cpp:62
void deactivate(void)
Turn off bounds.
virtual Real checkAdjointConsistencyJacobian_2(const Vector< Real > &w, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, const bool printToStream=true, std::ostream &outStream=std::cout)
Check the consistency of the Jacobian and its adjoint. This is the primary interface.