ROL
json/example_01.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
62 #include "ROL_Algorithm.hpp"
63 #include "ROL_StdVector.hpp"
64 #include "ROL_Zakharov.hpp"
65 
66 #include "example_01.hpp"
67 
68 #include "ROL_Stream.hpp"
69 #include "Teuchos_GlobalMPISession.hpp"
70 #include "Teuchos_XMLParameterListHelpers.hpp"
71 
72 typedef double RealT;
73 
74 int main(int argc, char *argv[]) {
75 
76  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
77 
78  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
79  int iprint = argc - 1;
80  ROL::Ptr<std::ostream> outStream;
81  ROL::nullstream bhs; // outputs nothing
82  if (iprint > 0)
83  outStream = ROL::makePtrFromRef(std::cout);
84  else
85  outStream = ROL::makePtrFromRef(bhs);
86 
87  int errorFlag = 0;
88 
89  // *** Example body.
90 
91  try {
92 
93  int dim = 10;
94 
95  ROL::ParameterList parlist;
96 
97  std::string jsonFileName("parameters.json");
98  parlist.setName("Imported from " + jsonFileName);
99 
100  // Load json parameters into a ROL::ParameterList
101  ROL::JSON_Parameters(jsonFileName,parlist);
102  std::string stepname = "Trust Region"; // can we obtain this from parlist? or jsonFile?
103 
105  ROL::Ptr<ROL::Step<RealT> > step = stepFactory.getStep(stepname, parlist);
106 
107  // Define Status Test
108  RealT gtol = parlist.get("Gradient Tolerance",1e-12);
109  RealT stol = parlist.get("Step Tolerance",1e-14);
110  int maxit = parlist.get("Maximum Number of Iterations",100);
111  ROL::Ptr<ROL::StatusTest<RealT> > status = ROL::makePtr<ROL::StatusTest<RealT>>(gtol, stol, maxit);
112 
113  ROL::Algorithm<RealT> algo(step,status,false);
114 
115  ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(dim, 1.0);
116  ROL::Ptr<std::vector<RealT> > k_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
117 
118  ROL::StdVector<RealT> x(x_ptr);
119  ROL::Ptr<ROL::Vector<RealT> > k = ROL::makePtr<ROL::StdVector<RealT>>(k_ptr);
120 
121  for(int i=0;i<dim;++i) {
122  (*k_ptr)[i] = i+1.0;
123  }
124 
126 
127  // Run Algorithm
128  algo.run(x, obj, true, *outStream);
129 
130  // Get True Solution
131  ROL::Ptr<std::vector<RealT> > xtrue_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
132  ROL::StdVector<RealT> xtrue(xtrue_ptr);
133 
134  // Compute Error
135  x.axpy(-1.0, xtrue);
136  RealT abserr = x.norm();
137  *outStream << std::scientific << "\n Absolute Error: " << abserr;
138  if ( abserr > sqrt(ROL::ROL_EPSILON<RealT>())) ) {
139  errorFlag += 1;
140  }
141 
142  // Make an XML file containing the supplied parameters
143  Teuchos::writeParameterListToXmlFile(parlist,"parameters.xml");
144 
145  }
146  catch (std::logic_error& err) {
147  *outStream << err.what() << "\n";
148  errorFlag = -1000;
149  }; // end try
150 
151  if (errorFlag != 0)
152  std::cout << "End Result: TEST FAILED\n";
153  else
154  std::cout << "End Result: TEST PASSED\n";
155 
156  return 0;
157 
158 }
159 
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:72
int main(int argc, char *argv[])
constexpr auto dim