ROL
step/test_15.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 #include <iostream>
22 //#include <fenv.h>
23 
24 typedef double RealT;
25 
26 int main(int argc, char *argv[]) {
27  //feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
28 
29  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
30 
31  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
32  int iprint = argc - 1;
33  ROL::Ptr<std::ostream> outStream;
34  ROL::nullstream bhs; // outputs nothing
35  if (iprint > 0)
36  outStream = ROL::makePtrFromRef(std::cout);
37  else
38  outStream = ROL::makePtrFromRef(bhs);
39 
40  int errorFlag = 0;
41 
42  // *** Test body.
43 
44  try {
45 
46  std::string filename = "input.xml";
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","Trust Region");
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  if (problem->getProblemType() == ROL::TYPE_B) {
61  if ( prob == ROL::TESTOPTPROBLEM_HS2 || prob == ROL::TESTOPTPROBLEM_BVP ) {
62  parlist->sublist("Step").sublist("Line Search").set("Initial Step Size",1.e-4);
63  parlist->sublist("Step").sublist("Trust Region").set("Initial Radius",-1.e1);
64  parlist->sublist("Step").sublist("Trust Region").set("Safeguard Size",1.e-4);
65  parlist->sublist("Status Test").set("Gradient Tolerance",1.e-6);
66  }
67  else if ( prob == ROL::TESTOPTPROBLEM_HS25 ) {
68  parlist->sublist("Step").sublist("Line Search").set("Initial Step Size",1.0);
69  parlist->sublist("Step").sublist("Trust Region").set("Initial Radius",1.e3);
70  parlist->sublist("Step").sublist("Trust Region").set("Safeguard Size",1.e4);
71  parlist->sublist("Status Test").set("Gradient Tolerance",1.e-8);
72  }
73  else {
74  parlist->sublist("Step").sublist("Line Search").set("Initial Step Size",1.0);
75  parlist->sublist("Step").sublist("Trust Region").set("Initial Radius",-1.e1);
76  parlist->sublist("Step").sublist("Trust Region").set("Safeguard Size",1.e4);
77  parlist->sublist("Status Test").set("Gradient Tolerance",1.e-6);
78  }
79  parlist->sublist("General").set("Scale for Epsilon Active Sets",1.0);
80  if ( prob == ROL::TESTOPTPROBLEM_HS4 ) {
81  parlist->sublist("General").set("Scale for Epsilon Active Sets",1.e-2);
82  }
83  *outStream << std::endl << std::endl << ROL:: ETestOptProblemToString(prob) << std::endl << std::endl;
84 
85  // Get Dimension of Problem
86  int dim = x0->dimension();
87  parlist->sublist("General").sublist("Krylov").set("Iteration Limit", 2*dim);
88 
89  // Error Vector
90  ROL::Ptr<ROL::Vector<RealT> > e = x0->clone();
91  e->zero();
92 
93  parlist->sublist("Step").sublist("Trust Region").set("Subproblem Solver", "Lin-More");
94  parlist->sublist("Step").sublist("Trust Region").set("Subproblem Model", "Lin-More");
95  *outStream << std::endl << std::endl << "Lin-More" << std::endl << std::endl;
96 
97  // Define Solver
98  ROL::OptimizationSolver<RealT> solver(*problem,*parlist);
99 
100  // Run Solver
101  solver.solve(*outStream);
102 
103  // Compute Error
104  RealT err(0);
105  for (int i = 0; i < static_cast<int>(z.size()); ++i) {
106  e->set(*x0);
107  e->axpy(-1.0,*z[i]);
108  if (i == 0) {
109  err = e->norm();
110  }
111  else {
112  err = std::min(err,e->norm());
113  }
114  }
115  *outStream << std::endl << "Norm of Error: " << err << std::endl;
116  }
117  }
118  }
119  catch (std::logic_error& err) {
120  *outStream << err.what() << std::endl;
121  errorFlag = -1000;
122  }; // end try
123 
124  if (errorFlag != 0)
125  std::cout << "End Result: TEST FAILED" << std::endl;
126  else
127  std::cout << "End Result: TEST PASSED" << std::endl;
128 
129  return 0;
130 
131 }
132 
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.
constexpr auto dim