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_PoissonInversion.hpp"
52 #include "ROL_LineSearchStep.hpp"
53 #include "ROL_TrustRegionStep.hpp"
54 #include "ROL_Algorithm.hpp"
55 #include "ROL_Types.hpp"
56 #include "Teuchos_oblackholestream.hpp"
57 #include "Teuchos_GlobalMPISession.hpp"
58 
59 #include <iostream>
60 #include <algorithm>
61 
62 typedef double RealT;
63 
64 int main(int argc, char *argv[]) {
65 
66  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
67 
68  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
69  int iprint = argc - 1;
70  Teuchos::RCP<std::ostream> outStream;
71  Teuchos::oblackholestream bhs; // outputs nothing
72  if (iprint > 0)
73  outStream = Teuchos::rcp(&std::cout, false);
74  else
75  outStream = Teuchos::rcp(&bhs, false);
76 
77  int errorFlag = 0;
78 
79  // *** Example body.
80 
81  try {
82 
83  int dim = 128; // Set problem dimension.
85 
86  Teuchos::ParameterList parlist;
87  // Basic algorithm.
88  parlist.set("Trust-Region Subproblem Solver Type", "Truncated CG");
89  // Krylov parameters.
90  parlist.set("Absolute Krylov Tolerance", 1.e-4);
91  parlist.set("Relative Krylov Tolerance", 1.e-2);
92  parlist.set("Maximum Number of Krylov Iterations", 50);
93  // Define step.
94  ROL::TrustRegionStep<RealT> step(parlist);
95 
96  // Define status test.
97  RealT gtol = 1e-12; // norm of gradient tolerance
98  RealT stol = 1e-14; // norm of step tolerance
99  int maxit = 100; // maximum number of iterations
100  ROL::StatusTest<RealT> status(gtol, stol, maxit);
101 
102  // Define algorithm.
103  ROL::DefaultAlgorithm<RealT> algo(step,status,false);
104 
105  // Iteration vector.
106  Teuchos::RCP<std::vector<RealT> > x_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
107  // Set initial guess.
108  for (int i=0; i<dim; i++) {
109  (*x_rcp)[i] = 0.1;
110  }
111  ROL::StdVector<RealT> x(x_rcp);
112 
113  // Run algorithm.
114  std::vector<std::string> output = algo.run(x, obj, false);
115  for ( unsigned i = 0; i < output.size(); i++ ) {
116  std::cout << output[i];
117  }
118 
119  // Compute dense Hessian matrix.
120  Teuchos::SerialDenseMatrix<int, RealT> H(x.dimension(), x.dimension());
121  H = ROL::computeDenseHessian<RealT>(obj, x);
122  //H.print(*outStream);
123 
124  // Compute and print eigenvalues.
125  std::vector<std::vector<RealT> > eigenvals = ROL::computeEigenvalues<RealT>(H);
126 
127  *outStream << "\nEigenvalues:\n";
128  for (unsigned i=0; i<(eigenvals[0]).size(); i++) {
129  if (i==0) {
130  *outStream << std::right
131  << std::setw(28) << "Real"
132  << std::setw(28) << "Imag"
133  << "\n";
134  }
135  *outStream << std::scientific << std::setprecision(16) << std::right
136  << std::setw(28) << (eigenvals[0])[i]
137  << std::setw(28) << (eigenvals[1])[i]
138  << "\n";
139  }
140 
141  // Compute and print generalized eigenvalues.
142  Teuchos::SerialDenseMatrix<int, RealT> M = computeDotMatrix(x);
143  //M.print(*outStream);
144  std::vector<std::vector<RealT> > genEigenvals = ROL::computeGenEigenvalues<RealT>(H, M);
145 
146  *outStream << "\nGeneralized eigenvalues:\n";
147  for (unsigned i=0; i<(genEigenvals[0]).size(); i++) {
148  if (i==0) {
149  *outStream << std::right
150  << std::setw(28) << "Real"
151  << std::setw(28) << "Imag"
152  << "\n";
153  }
154  *outStream << std::scientific << std::setprecision(16) << std::right
155  << std::setw(28) << (genEigenvals[0])[i]
156  << std::setw(28) << (genEigenvals[1])[i]
157  << "\n";
158  }
159 
160  // Sort and compare eigenvalues and generalized eigenvalues - should be close.
161  std::sort((eigenvals[0]).begin(), (eigenvals[0]).end());
162  std::sort((eigenvals[1]).begin(), (eigenvals[1]).end());
163  std::sort((genEigenvals[0]).begin(), (genEigenvals[0]).end());
164  std::sort((genEigenvals[1]).begin(), (genEigenvals[1]).end());
165 
166  RealT errtol = std::sqrt(ROL::ROL_EPSILON);
167  for (unsigned i=0; i<(eigenvals[0]).size(); i++) {
168  if ( std::abs( (genEigenvals[0])[i] - (eigenvals[0])[i] ) > errtol*((eigenvals[0])[i]+ROL::ROL_THRESHOLD) ) {
169  errorFlag++;
170  *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) << "\n";
171  }
172  if ( std::abs( (genEigenvals[1])[i] - (eigenvals[1])[i] ) > errtol*((eigenvals[1])[i]+ROL::ROL_THRESHOLD) ) {
173  errorFlag++;
174  *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) << "\n";
175  }
176  }
177 
178  // Compute inverse of Hessian.
179  Teuchos::SerialDenseMatrix<int, RealT> invH = ROL::computeInverse<RealT>(H);
180  Teuchos::SerialDenseMatrix<int, RealT> HinvH(H);
181 
182  // Multiply with Hessian and verify that it gives the identity (l2 dot matrix M from above).
183  HinvH.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, H, invH, 0.0);
184  //*outStream << std::scientific << std::setprecision(6); HinvH.print(*outStream);
185  HinvH -= M;
186  if (HinvH.normOne() > errtol) {
187  errorFlag++;
188  *outStream << std::scientific << std::setprecision(20) << "1-norm of H*inv(H) - I = " << HinvH.normOne() << " > " << errtol << "\n";
189  }
190 
191  // Use Newton algorithm.
192  parlist.set("Descent Type", "Newton's Method");
193  // Define step.
194  ROL::LineSearchStep<RealT> newton_step(parlist);
195  // Define algorithm.
196  ROL::DefaultAlgorithm<RealT> newton_algo(newton_step,status,false);
197 
198  // Reset initial guess.
199  for (int i=0; i<dim; i++) {
200  (*x_rcp)[i] = 0.1;
201  }
202 
203  // Run Newton algorithm.
204  output = newton_algo.run(x, obj, false);
205  for ( unsigned i = 0; i < output.size(); i++ ) {
206  std::cout << output[i];
207  }
208 
209  Teuchos::RCP<const ROL::AlgorithmState<RealT> > new_state = newton_algo.getState();
210  Teuchos::RCP<const ROL::AlgorithmState<RealT> > old_state = algo.getState();
211  if ( std::abs(new_state->value - old_state->value) > errtol ) {
212  errorFlag++;
213  *outStream << std::scientific << std::setprecision(20) << "\nabs(new_optimal_value - old_optimal_value) = " << std::abs(new_state->value - old_state->value) << " > " << errtol << "\n";
214  }
215 
216  }
217  catch (std::logic_error err) {
218  *outStream << err.what() << "\n";
219  errorFlag = -1000;
220  }; // end try
221 
222  if (errorFlag != 0)
223  std::cout << "End Result: TEST FAILED\n";
224  else
225  std::cout << "End Result: TEST PASSED\n";
226 
227  return 0;
228 
229 }
230 
Contains definitions of custom data types in ROL.
Teuchos::RCP< const AlgorithmState< Real > > getState(void) const
static const double ROL_THRESHOLD
Tolerance for various equality tests.
Definition: ROL_Types.hpp:119
Provides the interface to compute optimization steps with line search.
Contains definitions for Poisson material inversion.
Provides the std::vector implementation of the ROL::Vector interface.
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.
int main(int argc, char *argv[])
Provides an interface to check status of optimization algorithms.
int dimension() const
Return dimension of the vector space.
Teuchos::SerialDenseMatrix< int, Real > computeDotMatrix(const Vector< Real > &x)
double RealT
Provides the interface to compute optimization steps with trust regions.
static const double ROL_EPSILON
Platform-dependent machine epsilon.
Definition: ROL_Types.hpp:115