ROL
step/test_18.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 #include "ROL_GetTestProblems.hpp"
16 #include "ROL_Stream.hpp"
17 #include "Teuchos_GlobalMPISession.hpp"
18 #include "Teuchos_XMLParameterListHelpers.hpp"
19 
20 #include <iostream>
21 
22 typedef double RealT;
23 
24 int main(int argc, char *argv[]) {
25 
26  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
27 
28  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
29  int iprint = argc - 1;
30  ROL::Ptr<std::ostream> outStream;
31  ROL::nullstream bhs; // outputs nothing
32  if (iprint > 0)
33  outStream = ROL::makePtrFromRef(std::cout);
34  else
35  outStream = ROL::makePtrFromRef(bhs);
36 
37  int errorFlag = 0;
38 
39  // *** Test body.
40 
41  try {
42  std::string filename = "input.xml";
43  auto parlist = ROL::getParametersFromXmlFile( filename );
44  parlist->sublist("Step").sublist("Trust Region").set("Initial Radius",10.0);
45 
46  // Setup optimization problem
47  ROL::Ptr<ROL::Vector<RealT>> x0;
48  std::vector<ROL::Ptr<ROL::Vector<RealT>>> z;
49  ROL::Ptr<ROL::ZOO::getCubic<RealT>> cubic;
50  ROL::Ptr<ROL::OptimizationProblem<RealT>> problem;
51  for (int i = 0; i < 3; ++i) {
52  cubic = ROL::makePtr<ROL::ZOO::getCubic<RealT>>(i);
53  cubic->get(problem,x0,z);
54 
55  // Check Derivatives
56  problem->check(*outStream);
57 
58  // Setup optimization solver
59  ROL::OptimizationSolver<RealT> solver(*problem,*parlist);
60  solver.solve(*outStream);
61 
62  // Compute Error
63  ROL::Ptr<ROL::Vector<RealT>> e = x0->clone();
64  e->zero();
65  RealT err(0);
66  for (int i = 0; i < static_cast<int>(z.size()); ++i) {
67  e->set(*x0);
68  e->axpy(-1.0,*z[i]);
69  if (i == 0) {
70  err = e->norm();
71  }
72  else {
73  err = std::min(err,e->norm());
74  }
75  }
76  *outStream << std::endl << "Norm of Error: " << err << std::endl;
77  RealT tol = static_cast<RealT>(1e-3)*std::max(z[0]->norm(),static_cast<RealT>(1));
78  errorFlag += ((err < tol) ? 0 : 1);
79  }
80  }
81  catch (std::logic_error& err) {
82  *outStream << err.what() << std::endl;
83  errorFlag = -1000;
84  }; // end try
85 
86  if (errorFlag != 0)
87  std::cout << "End Result: TEST FAILED" << std::endl;
88  else
89  std::cout << "End Result: TEST PASSED" << std::endl;
90 
91  return 0;
92 
93 }
94 
Contains definitions of test objective functions.
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
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.