ROL
step/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 
14 #define USE_HESSVEC 1
15 
16 #include "ROL_GetTestProblems.hpp"
18 #include "ROL_Stream.hpp"
19 #include "Teuchos_GlobalMPISession.hpp"
20 
21 
22 #include <iostream>
23 
24 typedef double RealT;
25 
26 int main(int argc, char *argv[]) {
27 
28  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
29 
30  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
31  int iprint = argc - 1;
32  ROL::Ptr<std::ostream> outStream;
33  ROL::nullstream bhs; // outputs nothing
34  if (iprint > 0)
35  outStream = ROL::makePtrFromRef(std::cout);
36  else
37  outStream = ROL::makePtrFromRef(bhs);
38 
39  int errorFlag = 0;
40 
41  // *** Test body.
42 
43  try {
44 
45  std::string filename = "input.xml";
46 
47  auto parlist = ROL::getParametersFromXmlFile( filename );
48  parlist->sublist("General").set("Inexact Hessian-Times-A-Vector",true);
49 #if USE_HESSVEC
50  parlist->sublist("General").set("Inexact Hessian-Times-A-Vector",false);
51 #endif
52  parlist->sublist("Step").set("Type","Line Search");
53 
55  // Get Objective Function
56  ROL::Ptr<ROL::Vector<RealT> > x0;
57  std::vector<ROL::Ptr<ROL::Vector<RealT> > > z;
58  ROL::Ptr<ROL::OptimizationProblem<RealT> > problem;
59  ROL::GetTestProblem<RealT>(problem,x0,z,prob);
60 
61  if (problem->getProblemType() == ROL::TYPE_B) {
62  *outStream << "\n\n" << ROL:: ETestOptProblemToString(prob) << "\n\n";
63 
64  // Get Dimension of Problem
65  int dim = x0->dimension();
66  parlist->sublist("General").sublist("Krylov").set("Iteration Limit", 2*dim);
67 
68  // Check Derivatives
69  problem->check(*outStream);
70 
71  // Error Vector
72  ROL::Ptr<ROL::Vector<RealT> > e = x0->clone();
73  e->zero();
74 
75  //ROL::EDescent desc = ROL::DESCENT_STEEPEST;
77  parlist->sublist("Step").sublist("Line Search").sublist("Descent Method").set("Type", ROL::EDescentToString(desc));
78  *outStream << std::endl << std::endl << ROL::EDescentToString(desc) << std::endl << std::endl;
79 
80  // Define Solver
81  ROL::OptimizationSolver<RealT> solver(*problem,*parlist);
82 
83  // Run Solver
84  solver.solve(*outStream);
85 
86  // Compute Error
87  RealT err(0);
88  for (int i = 0; i < static_cast<int>(z.size()); ++i) {
89  e->set(*x0);
90  e->axpy(-1.0,*z[i]);
91  if (i == 0) {
92  err = e->norm();
93  }
94  else {
95  err = std::min(err,e->norm());
96  }
97  }
98  *outStream << std::endl << "Norm of Error: " << err << std::endl;
99  }
100  }
101  }
102  catch (std::logic_error& err) {
103  *outStream << err.what() << std::endl;
104  errorFlag = -1000;
105  }; // end try
106 
107  if (errorFlag != 0)
108  std::cout << "End Result: TEST FAILED" << std::endl;
109  else
110  std::cout << "End Result: TEST PASSED" << std::endl;
111 
112  return 0;
113 
114 }
ETestOptProblem
Enumeration of test optimization problems.
Contains definitions of test objective functions.
std::string EDescentToString(EDescent tr)
Definition: ROL_Types.hpp:390
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
basic_nullstream< char, std::char_traits< char >> nullstream
Definition: ROL_Stream.hpp:36
std::string ETestOptProblemToString(ETestOptProblem to)
Provides a simplified interface for solving a wide range of optimization problems.
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.
constexpr auto dim
EDescent
Enumeration of descent direction types.
Definition: ROL_Types.hpp:381