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