ROL
sacado/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 
55 #include <iostream>
56 
57 #include "ROL_Sacado_Objective.hpp"
58 #include "ROL_Sacado_EqualityConstraint.hpp"
59 
60 #include "ROL_Algorithm.hpp"
61 #include "ROL_CompositeStep.hpp"
64 
65 #include "Teuchos_oblackholestream.hpp"
66 #include "Teuchos_GlobalMPISession.hpp"
67 #include "Teuchos_XMLParameterListHelpers.hpp"
68 
69 #include "example_02.hpp"
70 
71 using namespace ROL;
72 
73 typedef double RealT;
74 
75 int main(int argc, char **argv)
76 {
77 
78 
79  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
80 
81  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
82  int iprint = argc - 1;
83  Teuchos::RCP<std::ostream> outStream;
84  Teuchos::oblackholestream bhs; // outputs nothing
85  if (iprint > 0)
86  outStream = Teuchos::rcp(&std::cout, false);
87  else
88  outStream = Teuchos::rcp(&bhs, false);
89 
90  int errorFlag = 0;
91 
92  // *** Example body.
93 
94  try {
95 
96  // Run derivative checks, etc.
97  int dim = 5;
98  int nc = 3;
99 
100  Teuchos::RCP< Sacado_Objective<RealT,Example_Objective> > obj =
101  Teuchos::rcp( new Sacado_Objective<RealT,Example_Objective> ());
102 
103  Teuchos::RCP< Sacado_EqualityConstraint<RealT,Example_Constraint > > constr =
104  Teuchos::rcp( new Sacado_EqualityConstraint<RealT,Example_Constraint > (nc));
105 
106  Teuchos::RCP<std::vector<RealT> > x_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
107 
108  Teuchos::RCP<std::vector<RealT> > sol_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
109  ROL::StdVector<RealT> x(x_rcp); // Iteration vector.
110  ROL::StdVector<RealT> sol(sol_rcp); // Reference solution vector.
111 
112  // Get initial guess
113  (*x_rcp)[0] = -1.8;
114  (*x_rcp)[1] = 1.7;
115  (*x_rcp)[2] = 1.9;
116  (*x_rcp)[3] = -0.8;
117  (*x_rcp)[4] = -0.8;
118 
119  // Get solution
120  (*sol_rcp)[0] = -1.717143570394391e+00;
121  (*sol_rcp)[1] = 1.595709690183565e+00;
122  (*sol_rcp)[2] = 1.827245752927178e+00;
123  (*sol_rcp)[3] = -7.636430781841294e-01;
124  (*sol_rcp)[4] = -7.636430781841294e-01;
125 
126  RealT left = -1e0, right = 1e0;
127  Teuchos::RCP<std::vector<RealT> > xtest_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
128  Teuchos::RCP<std::vector<RealT> > g_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
129  Teuchos::RCP<std::vector<RealT> > d_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
130  Teuchos::RCP<std::vector<RealT> > v_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
131  Teuchos::RCP<std::vector<RealT> > vc_rcp = Teuchos::rcp( new std::vector<RealT> (nc, 0.0) );
132  Teuchos::RCP<std::vector<RealT> > vl_rcp = Teuchos::rcp( new std::vector<RealT> (nc, 0.0) );
133  ROL::StdVector<RealT> xtest(xtest_rcp);
134  ROL::StdVector<RealT> g(g_rcp);
135  ROL::StdVector<RealT> d(d_rcp);
136  ROL::StdVector<RealT> v(v_rcp);
137  ROL::StdVector<RealT> vc(vc_rcp);
138  ROL::StdVector<RealT> vl(vl_rcp);
139 
140  // set xtest, d, v
141  for (int i=0; i<dim; i++) {
142  (*xtest_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
143  (*d_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
144  (*v_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
145  }
146  // set vc, vl
147  for (int i=0; i<nc; i++) {
148  (*vc_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
149  (*vl_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
150  }
151 
152  obj->checkGradient(xtest, d, true, *outStream); *outStream << "\n";
153  obj->checkHessVec(xtest, v, true, *outStream); *outStream << "\n";
154  obj->checkHessSym(xtest, d, v, true, *outStream); *outStream << "\n";
155  constr->checkApplyJacobian(xtest, v, vc, true, *outStream); *outStream << "\n";
156  constr->checkApplyAdjointJacobian(xtest, vl, vc, xtest, true, *outStream); *outStream << "\n";
157  constr->checkApplyAdjointHessian(xtest, vl, d, xtest, true, *outStream); *outStream << "\n";
158 
159  Teuchos::RCP<std::vector<RealT> > v1_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
160  Teuchos::RCP<std::vector<RealT> > v2_rcp = Teuchos::rcp( new std::vector<RealT> (nc, 0.0) );
161  ROL::StdVector<RealT> v1(v1_rcp);
162  ROL::StdVector<RealT> v2(v2_rcp);
163  RealT augtol = 1e-8;
164  constr->solveAugmentedSystem(v1, v2, d, vc, xtest, augtol);
165 
166  // Define algorithm.
167  Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp(new Teuchos::ParameterList());
168  std::string paramfile = "parameters.xml";
169  Teuchos::updateParametersFromXmlFile(paramfile,parlist.ptr());
170  ROL::Algorithm<RealT> algo("Composite Step", *parlist);
171 
172  // Run algorithm.
173  vl.zero();
174  algo.run(x, g, vl, vc, *obj, *constr, true, *outStream);
175 
176  // Compute Error
177  *outStream << "\nReference solution x_r =\n";
178  *outStream << std::scientific << " " << (*sol_rcp)[0] << "\n";
179  *outStream << std::scientific << " " << (*sol_rcp)[1] << "\n";
180  *outStream << std::scientific << " " << (*sol_rcp)[2] << "\n";
181  *outStream << std::scientific << " " << (*sol_rcp)[3] << "\n";
182  *outStream << std::scientific << " " << (*sol_rcp)[4] << "\n";
183  *outStream << "\nOptimal solution x =\n";
184  *outStream << std::scientific << " " << (*x_rcp)[0] << "\n";
185  *outStream << std::scientific << " " << (*x_rcp)[1] << "\n";
186  *outStream << std::scientific << " " << (*x_rcp)[2] << "\n";
187  *outStream << std::scientific << " " << (*x_rcp)[3] << "\n";
188  *outStream << std::scientific << " " << (*x_rcp)[4] << "\n";
189  x.axpy(-1.0, sol);
190  RealT abserr = x.norm();
191  RealT relerr = abserr/sol.norm();
192  *outStream << std::scientific << "\n Absolute Error: " << abserr;
193  *outStream << std::scientific << "\n Relative Error: " << relerr << "\n";
194  if ( relerr > sqrt(ROL::ROL_EPSILON<RealT>()) ) {
195  errorFlag += 1;
196  }
197  }
198  catch (std::logic_error err) {
199  *outStream << err.what() << "\n";
200  errorFlag = -1000;
201  }; // end try
202 
203  if (errorFlag != 0)
204  std::cout << "End Result: TEST FAILED\n";
205  else
206  std::cout << "End Result: TEST PASSED\n";
207 
208  return 0;
209 
210 
211 }
double RealT
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
int main(int argc, char **argv)
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:157
Provides the std::vector implementation of the ROL::Vector interface.
Provides an interface to run optimization algorithms.
double RealT
Real norm() const
Returns where .