ROL
step/test_02.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("Step").set("Type","Trust Region");
49 
50  // Loop Through Test Objectives
51  for ( ROL::ETestOptProblem objFunc = ROL::TESTOPTPROBLEM_ROSENBROCK; objFunc < ROL::TESTOPTPROBLEM_LAST; objFunc++ ) {
53  if ( tr != ROL::TRUSTREGION_LINMORE ) {
54  // Set Up Optimization Problem
55  ROL::Ptr<ROL::Vector<RealT> > x0;
56  std::vector<ROL::Ptr<ROL::Vector<RealT> > > z;
57  ROL::Ptr<ROL::OptimizationProblem<RealT> > problem;
58  ROL::GetTestProblem<RealT>(problem,x0,z,objFunc);
59 
60  if (problem->getProblemType() == ROL::TYPE_U
61  && objFunc != ROL::TESTOPTPROBLEM_MINIMAX1
62  && objFunc != ROL::TESTOPTPROBLEM_MINIMAX2
63  && objFunc != ROL::TESTOPTPROBLEM_MINIMAX3) {
64  *outStream << std::endl << std::endl << ROL::ETestOptProblemToString(objFunc) << std::endl << std::endl;
65 
66  // Get Dimension of Problem
67  int dim = x0->dimension();
68  parlist->sublist("General").sublist("Krylov").set("Iteration Limit", 5*dim);
69 
70  // Error Vector
71  ROL::Ptr<ROL::Vector<RealT> > e = x0->clone();
72  e->zero();
73 
74  *outStream << "\n\n" << ROL::ETrustRegionToString(tr) << "\n\n";
75  parlist->sublist("Step").sublist("Trust Region").set("Subproblem Solver", ETrustRegionToString(tr));
76 
77  // Define Solver
78  ROL::OptimizationSolver<RealT> solver(*problem,*parlist);
79 
80  // Run Solver
81  solver.solve(*outStream);
82 
83  // Compute Error
84  RealT err(0);
85  for (int i = 0; i < static_cast<int>(z.size()); ++i) {
86  e->set(*x0);
87  e->axpy(-1.0,*z[i]);
88  if (i == 0) {
89  err = e->norm();
90  }
91  else {
92  err = std::min(err,e->norm());
93  }
94  }
95  *outStream << std::endl << "Norm of Error: " << err << std::endl;
96  }
97  }
98  }
99  }
100  }
101  catch (std::logic_error& err) {
102  *outStream << err.what() << std::endl;
103  errorFlag = -1000;
104  }; // end try
105 
106  if (errorFlag != 0)
107  std::cout << "End Result: TEST FAILED" << std::endl;
108  else
109  std::cout << "End Result: TEST PASSED" << std::endl;
110 
111  return 0;
112 
113 }
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