ROL
step/fletcher/test_03.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
13 #define USE_HESSVEC 1
14 
15 #include "ROL_GetTestProblems.hpp"
17 #include "ROL_Stream.hpp"
18 #include "Teuchos_GlobalMPISession.hpp"
19 
20 
21 #include <iostream>
22 
23 typedef double RealT;
24 
25 int main(int argc, char *argv[]) {
26 
27  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
28 
29  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
30  int iprint = argc - 1;
31  ROL::Ptr<std::ostream> outStream;
32  ROL::nullstream bhs; // outputs nothing
33  if (iprint > 0)
34  outStream = ROL::makePtrFromRef(std::cout);
35  else
36  outStream = ROL::makePtrFromRef(bhs);
37 
38  int errorFlag = 0;
39 
40  // *** Test body.
41 
42  try {
43 
44  std::string filename = "input_ex03.xml";
45 
46  auto parlist = ROL::getParametersFromXmlFile( filename );
47  parlist->sublist("General").set("Inexact Hessian-Times-A-Vector",true);
48 #if USE_HESSVEC
49  parlist->sublist("General").set("Inexact Hessian-Times-A-Vector",false);
50 #endif
51 
52  // Krylov parameters.
53  parlist->sublist("Step").set("Type","Fletcher");
54 
56  if ((prob != ROL::TESTOPTPROBLEM_HS55) && (prob != ROL::TESTOPTPROBLEM_HS14) && (prob != ROL::TESTOPTPROBLEM_HS63) && (prob != ROL::TESTOPTPROBLEM_HS41)) {
57  // Get Objective Function
58  ROL::Ptr<ROL::Vector<RealT> > x0;
59  std::vector<ROL::Ptr<ROL::Vector<RealT> > > z;
60  ROL::Ptr<ROL::OptimizationProblem<RealT> > problem;
61  ROL::GetTestProblem<RealT>(problem,x0,z,prob);
62 
63  if ((problem->getProblemType() == ROL::TYPE_E || problem->getProblemType() == ROL::TYPE_EB)
65  *outStream << std::endl << std::endl << ROL:: ETestOptProblemToString(prob) << std::endl << std::endl;
66 
67  // Get Dimension of Problem
68  // int dim = x0->dimension();
69  // parlist->sublist("General").sublist("Krylov").set("Iteration Limit", 2*dim);
70 
71  // Error Vector
72  ROL::Ptr<ROL::Vector<RealT> > e = x0->clone();
73  e->zero();
74 
75  // Define Solver
76  ROL::OptimizationSolver<RealT> solver(*problem,*parlist);
77 
78  // Run Solver
79  solver.solve(*outStream);
80 
81  // Compute Error
82  RealT err(0);
83  for (int i = 0; i < static_cast<int>(z.size()); ++i) {
84  e->set(*x0);
85 //std::cout << "\n\n e dim =" << e->dimension() << " z[i] dim =" << z[i]->dimension() << "\n\n";
86  e->axpy(-1.0,*z[i]);
87  if (i == 0) {
88  err = e->norm();
89  }
90  else {
91  err = std::min(err,e->norm());
92  }
93  }
94  *outStream << std::endl << "Norm of Error: " << err << std::endl;
95 
96  // Update error flag
97  ROL::Ptr<const ROL::AlgorithmState<RealT> > state = solver.getAlgorithmState();
98  errorFlag += ((err < std::max(1.e-6*z[0]->norm(),1.e-8) || (state->gnorm < 1.e-6)) ? 0 : 1);
99  }
100  }
101  }
102  }
103  catch (std::logic_error& err) {
104  *outStream << err.what() << std::endl;
105  errorFlag = -1000;
106  }; // end try
107 
108  if (errorFlag != 0)
109  std::cout << "End Result: TEST FAILED" << std::endl;
110  else
111  std::cout << "End Result: TEST PASSED" << std::endl;
112 
113  return 0;
114 
115 }
ETestOptProblem
Enumeration of test optimization problems.
Contains definitions of test objective functions.
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
std::string ETestOptProblemToString(ETestOptProblem to)
Provides a simplified interface for solving a wide range of optimization problems.
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:38
int main(int argc, char *argv[])
int solve(const ROL::Ptr< StatusTest< Real > > &status=ROL::nullPtr, const bool combineStatus=true)
Solve optimization problem with no iteration output.
ROL::Ptr< const AlgorithmState< Real > > getAlgorithmState(void) const
Return the AlgorithmState.