ROL
poisson-inversion/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 #define USE_HESSVEC 1
50 
51 #include "ROL_Types.hpp"
52 #include "ROL_PoissonInversion.hpp"
53 #include "ROL_Algorithm.hpp"
54 #include "ROL_LineSearchStep.hpp"
55 #include "ROL_TrustRegionStep.hpp"
56 #include "ROL_StatusTest.hpp"
57 #include "ROL_Stream.hpp"
58 #include "Teuchos_GlobalMPISession.hpp"
59 
60 #include <iostream>
61 #include <algorithm>
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 
84  int dim = 128; // Set problem dimension.
86 
87  // Define algorithm.
88  ROL::ParameterList parlist;
89  std::string stepname = "Trust Region";
90  parlist.sublist("Step").sublist(stepname).set("Subproblem Solver", "Truncated CG");
91  parlist.sublist("General").sublist("Krylov").set("Iteration Limit",50);
92  parlist.sublist("General").sublist("Krylov").set("Relative Tolerance",1e-2);
93  parlist.sublist("General").sublist("Krylov").set("Absolute Tolerance",1e-4);
94  parlist.sublist("Status Test").set("Gradient Tolerance",1.e-12);
95  parlist.sublist("Status Test").set("Step Tolerance",1.e-14);
96  parlist.sublist("Status Test").set("Iteration Limit",100);
97  ROL::Algorithm<RealT> algo(stepname,parlist);
98 
99  // Iteration vector.
100  ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
101  // Set initial guess.
102  for (int i=0; i<dim; i++) {
103  (*x_ptr)[i] = 0.1;
104  }
105  ROL::StdVector<RealT> x(x_ptr);
106 
107  // Run algorithm.
108  algo.run(x, obj, true, *outStream);
109 
110  // Compute dense Hessian matrix.
111  Teuchos::SerialDenseMatrix<int, RealT> H(x.dimension(), x.dimension());
112  H = ROL::computeDenseHessian<RealT>(obj, x);
113  //H.print(*outStream);
114 
115  // Compute and print eigenvalues.
116  std::vector<std::vector<RealT> > eigenvals = ROL::computeEigenvalues<RealT>(H);
117 
118  *outStream << "\nEigenvalues:\n";
119  for (unsigned i=0; i<(eigenvals[0]).size(); i++) {
120  if (i==0) {
121  *outStream << std::right
122  << std::setw(28) << "Real"
123  << std::setw(28) << "Imag"
124  << "\n";
125  }
126  *outStream << std::scientific << std::setprecision(16) << std::right
127  << std::setw(28) << (eigenvals[0])[i]
128  << std::setw(28) << (eigenvals[1])[i]
129  << "\n";
130  }
131 
132  // Compute and print generalized eigenvalues.
133  Teuchos::SerialDenseMatrix<int, RealT> M = computeDotMatrix(x);
134  //M.print(*outStream);
135  std::vector<std::vector<RealT> > genEigenvals = ROL::computeGenEigenvalues<RealT>(H, M);
136 
137  *outStream << "\nGeneralized eigenvalues:\n";
138  for (unsigned i=0; i<(genEigenvals[0]).size(); i++) {
139  if (i==0) {
140  *outStream << std::right
141  << std::setw(28) << "Real"
142  << std::setw(28) << "Imag"
143  << "\n";
144  }
145  *outStream << std::scientific << std::setprecision(16) << std::right
146  << std::setw(28) << (genEigenvals[0])[i]
147  << std::setw(28) << (genEigenvals[1])[i]
148  << "\n";
149  }
150 
151  // Sort and compare eigenvalues and generalized eigenvalues - should be close.
152  std::sort((eigenvals[0]).begin(), (eigenvals[0]).end());
153  std::sort((eigenvals[1]).begin(), (eigenvals[1]).end());
154  std::sort((genEigenvals[0]).begin(), (genEigenvals[0]).end());
155  std::sort((genEigenvals[1]).begin(), (genEigenvals[1]).end());
156 
157  RealT errtol = std::sqrt(ROL::ROL_EPSILON<RealT>());
158  for (unsigned i=0; i<(eigenvals[0]).size(); i++) {
159  if ( std::abs( (genEigenvals[0])[i] - (eigenvals[0])[i] ) > errtol*((eigenvals[0])[i]+ROL::ROL_THRESHOLD<RealT>()) ) {
160  errorFlag++;
161  *outStream << std::scientific << std::setprecision(20) << "Real genEigenvals - eigenvals (" << i << ") = " << std::abs( (genEigenvals[0])[i] - (eigenvals[0])[i] ) << " > " << errtol*((eigenvals[0])[i]+1e4*ROL::ROL_THRESHOLD<RealT>()) << "\n";
162  }
163  if ( std::abs( (genEigenvals[1])[i] - (eigenvals[1])[i] ) > errtol*((eigenvals[1])[i]+ROL::ROL_THRESHOLD<RealT>()) ) {
164  errorFlag++;
165  *outStream << std::scientific << std::setprecision(20) << "Imag genEigenvals - eigenvals (" << i << ") = " << std::abs( (genEigenvals[1])[i] - (eigenvals[1])[i] ) << " > " << errtol*((eigenvals[1])[i]+ROL::ROL_THRESHOLD<RealT>()) << "\n";
166  }
167  }
168 
169  // Compute inverse of Hessian.
170  Teuchos::SerialDenseMatrix<int, RealT> invH = ROL::computeInverse<RealT>(H);
171  Teuchos::SerialDenseMatrix<int, RealT> HinvH(H);
172 
173  // Multiply with Hessian and verify that it gives the identity (l2 dot matrix M from above).
174  HinvH.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, H, invH, 0.0);
175  //*outStream << std::scientific << std::setprecision(6); HinvH.print(*outStream);
176  HinvH -= M;
177  if (HinvH.normOne() > errtol) {
178  errorFlag++;
179  *outStream << std::scientific << std::setprecision(20) << "1-norm of H*inv(H) - I = " << HinvH.normOne() << " > " << errtol << "\n";
180  }
181 
182  // Use Newton algorithm with line search.
183  stepname = "Line Search";
184  parlist.sublist("Step").sublist(stepname).sublist("Descent Method").set("Type", "Newton-Krylov");
185  ROL::Algorithm<RealT> newton_algo(stepname,parlist);
186 
187  // Reset initial guess.
188  for (int i=0; i<dim; i++) {
189  (*x_ptr)[i] = 0.1;
190  }
191 
192  // Run Newton algorithm.
193  newton_algo.run(x, obj, true, *outStream);
194 
195  ROL::Ptr<const ROL::AlgorithmState<RealT> > new_state = newton_algo.getState();
196  ROL::Ptr<const ROL::AlgorithmState<RealT> > old_state = algo.getState();
197  *outStream << "old_optimal_value = " << old_state->value << std::endl;
198  *outStream << "new_optimal_value = " << new_state->value << std::endl;
199  if ( std::abs(new_state->value - old_state->value) / std::abs(old_state->value) > errtol ) {
200  errorFlag++;
201  *outStream << std::scientific << std::setprecision(20) << "\nabs(new_optimal_value - old_optimal_value) / abs(old_optimal_value) = " << std::abs(new_state->value - old_state->value) / std::abs(old_state->value) << " > " << errtol << "\n";
202  }
203 
204  }
205  catch (std::logic_error err) {
206  *outStream << err.what() << "\n";
207  errorFlag = -1000;
208  }; // end try
209 
210  if (errorFlag != 0)
211  std::cout << "End Result: TEST FAILED\n";
212  else
213  std::cout << "End Result: TEST PASSED\n";
214 
215  return 0;
216 
217 }
218 
Contains definitions of custom data types in ROL.
virtual std::vector< std::string > run(Vector< Real > &x, Objective< Real > &obj, bool print=false, std::ostream &outStream=std::cout, bool printVectors=false, std::ostream &vectorStream=std::cout)
Run algorithm on unconstrained problems (Type-U). This is the primary Type-U interface.
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Contains definitions for Poisson material inversion.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Provides an interface to run optimization algorithms.
ROL::Ptr< const AlgorithmState< Real > > getState(void) const
int dimension() const
Return dimension of the vector space.
Teuchos::SerialDenseMatrix< int, Real > computeDotMatrix(const Vector< Real > &x)
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:72
int main(int argc, char *argv[])