ROL
example_01a.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 
23 #include <iostream>
24 
25 #include "ROL_Sacado_Objective.hpp"
26 
27 #include "ROL_Algorithm.hpp"
28 #include "ROL_LineSearchStep.hpp"
29 #include "ROL_StatusTest.hpp"
30 #include "ROL_ParameterList.hpp"
31 
32 #include "ROL_Stream.hpp"
33 #include "Teuchos_GlobalMPISession.hpp"
34 
35 #include "example_01a.hpp"
36 
37 using namespace ROL;
38 
39 typedef double RealT;
40 
41 int main(int argc, char **argv)
42 {
43  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
44 
45  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
46  int iprint = argc - 1;
47  ROL::Ptr<std::ostream> outStream;
48  ROL::nullstream bhs; // outputs nothing
49  if (iprint > 0)
50  outStream = ROL::makePtrFromRef(std::cout);
51  else
52  outStream = ROL::makePtrFromRef(bhs);
53 
54  int errorFlag = 0;
55 
56  // *** Example body.
57 
58  try {
59 
60  Sacado_Objective<RealT,Zakharov> obj;
61 
62  int dim = 10; // Set problem dimension.
63 
64  // Load optimizer parameters form XML file
65  std::string paramfile = "parameters.xml";
66  auto parlist = ROL::getParametersFromXmlFile(paramfile);
67 
68  // Define algorithm.
69  ROL::Ptr<ROL::Step<RealT>>
70  step = ROL::makePtr<ROL::LineSearchStep<RealT>>(*parlist);
71  ROL::Ptr<ROL::StatusTest<RealT>>
72  status = ROL::makePtr<ROL::StatusTest<RealT>>(*parlist);
73  ROL::Algorithm<RealT> algo(step,status,false);
74 
75  // Iteration vector.
76  ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
77  // Set Initial Guess
78  for (int i=0; i<dim; i++) {
79  (*x_ptr)[i] = 2;
80  }
81 
82  StdVector<RealT> x(x_ptr);
83 
84  // Run algorithm.
85  algo.run(x, obj, true, *outStream);
86 
87  // Get true solution.
88  ROL::Ptr<std::vector<RealT> > xtrue_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
89  StdVector<RealT> xtrue(xtrue_ptr);
90 
91  // Compute error.
92  x.axpy(-1.0, xtrue);
93  RealT abserr = x.norm();
94  *outStream << std::scientific << "\n Absolute Error: " << abserr << std::endl;
95  if ( abserr > sqrt(ROL_EPSILON<RealT>()) ) {
96  errorFlag += 1;
97  }
98  }
99  catch (std::logic_error& err) {
100  *outStream << err.what() << "\n";
101  errorFlag = -1000;
102  }; // end try
103 
104  if (errorFlag != 0)
105  std::cout << "End Result: TEST FAILED\n";
106  else
107  std::cout << "End Result: TEST PASSED\n";
108 
109  return 0;
110 
111 }
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
virtual std::vector< std::string > run(Vector< Real > &x, Objective< Real > &obj, bool print=false, std::ostream &outStream=std::cout, bool printVectors=false, std::ostream &vectorStream=std::cout)
Run algorithm on unconstrained problems (Type-U). This is the primary Type-U interface.
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.
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:38
int main(int argc, char *argv[])
constexpr auto dim