ROL
json/example_01.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 
28 #include "ROL_Algorithm.hpp"
29 #include "ROL_StdVector.hpp"
30 #include "ROL_Zakharov.hpp"
31 
32 #include "example_01.hpp"
33 
34 #include "ROL_Stream.hpp"
35 #include "Teuchos_GlobalMPISession.hpp"
36 #include "Teuchos_XMLParameterListHelpers.hpp"
37 
38 typedef double RealT;
39 
40 int main(int argc, char *argv[]) {
41 
42  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
43 
44  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
45  int iprint = argc - 1;
46  ROL::Ptr<std::ostream> outStream;
47  ROL::nullstream bhs; // outputs nothing
48  if (iprint > 0)
49  outStream = ROL::makePtrFromRef(std::cout);
50  else
51  outStream = ROL::makePtrFromRef(bhs);
52 
53  int errorFlag = 0;
54 
55  // *** Example body.
56 
57  try {
58 
59  int dim = 10;
60 
61  ROL::ParameterList parlist;
62 
63  std::string jsonFileName("parameters.json");
64  parlist.setName("Imported from " + jsonFileName);
65 
66  // Load json parameters into a ROL::ParameterList
67  ROL::JSON_Parameters(jsonFileName,parlist);
68  std::string stepname = "Trust Region"; // can we obtain this from parlist? or jsonFile?
69 
71  ROL::Ptr<ROL::Step<RealT> > step = stepFactory.getStep(stepname, parlist);
72 
73  // Define Status Test
74  RealT gtol = parlist.get("Gradient Tolerance",1e-12);
75  RealT stol = parlist.get("Step Tolerance",1e-14);
76  int maxit = parlist.get("Maximum Number of Iterations",100);
77  ROL::Ptr<ROL::StatusTest<RealT> > status = ROL::makePtr<ROL::StatusTest<RealT>>(gtol, stol, maxit);
78 
79  ROL::Algorithm<RealT> algo(step,status,false);
80 
81  ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(dim, 1.0);
82  ROL::Ptr<std::vector<RealT> > k_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
83 
84  ROL::StdVector<RealT> x(x_ptr);
85  ROL::Ptr<ROL::Vector<RealT> > k = ROL::makePtr<ROL::StdVector<RealT>>(k_ptr);
86 
87  for(int i=0;i<dim;++i) {
88  (*k_ptr)[i] = i+1.0;
89  }
90 
92 
93  // Run Algorithm
94  algo.run(x, obj, true, *outStream);
95 
96  // Get True Solution
97  ROL::Ptr<std::vector<RealT> > xtrue_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
98  ROL::StdVector<RealT> xtrue(xtrue_ptr);
99 
100  // Compute Error
101  x.axpy(-1.0, xtrue);
102  RealT abserr = x.norm();
103  *outStream << std::scientific << "\n Absolute Error: " << abserr;
104  if ( abserr > sqrt(ROL::ROL_EPSILON<RealT>())) ) {
105  errorFlag += 1;
106  }
107 
108  // Make an XML file containing the supplied parameters
109  Teuchos::writeParameterListToXmlFile(parlist,"parameters.xml");
110 
111  }
112  catch (std::logic_error& err) {
113  *outStream << err.what() << "\n";
114  errorFlag = -1000;
115  }; // end try
116 
117  if (errorFlag != 0)
118  std::cout << "End Result: TEST FAILED\n";
119  else
120  std::cout << "End Result: TEST PASSED\n";
121 
122  return 0;
123 
124 }
125 
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
void stepFactory(ROL::ParameterList &parlist, ROL::Ptr< ROL::Step< Real > > &step)
A minimalist step factory which specializes the Step Type depending on whether a Trust-Region or Line...
ROL::Ptr< Step< Real > > getStep(const std::string &type, ROL::ParameterList &parlist) const
void JSON_Parameters(const std::string &jsonFileName, ROL::ParameterList &parlist)
Read a JSON file and store all parameters in a ROL::ParameterList. Checks for a key called &quot;Algorithm...
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Real norm() const
Returns where .
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Provides an interface to run optimization algorithms.
Contains definitions for the Zakharov function as evaluated using only the ROL::Vector interface...
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:38
int main(int argc, char *argv[])
constexpr auto dim