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 "Teuchos_oblackholestream.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  Teuchos::RCP<std::ostream> outStream;
81  Teuchos::oblackholestream bhs; // outputs nothing
82  if (iprint > 0)
83  outStream = Teuchos::rcp(&std::cout, false);
84  else
85  outStream = Teuchos::rcp(&bhs, false);
86 
87  int errorFlag = 0;
88 
89  // *** Example body.
90 
91  try {
92 
93  int dim = 10;
94 
95  Teuchos::ParameterList parlist;
96 
97  std::string jsonFileName("parameters.json");
98  parlist.setName("Imported from " + jsonFileName);
99 
100  // Load json parameters into a Teuchos::ParameterList
101  ROL::JSON_Parameters(jsonFileName,parlist);
102  std::string stepname = "Trust Region"; // can we obtain this from parlist? or jsonFile?
103 
105  Teuchos::RCP<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  Teuchos::RCP<ROL::StatusTest<RealT> > status = Teuchos::rcp(new ROL::StatusTest<RealT>(gtol, stol, maxit));
112 
113  ROL::Algorithm<RealT> algo(step,status,false);
114 
115  Teuchos::RCP<std::vector<RealT> > x_rcp = Teuchos::rcp(new std::vector<RealT> (dim, 1.0) );
116  Teuchos::RCP<std::vector<RealT> > k_rcp = Teuchos::rcp(new std::vector<RealT> (dim, 0.0) );
117 
118  ROL::StdVector<RealT> x(x_rcp);
119  Teuchos::RCP<ROL::Vector<RealT> > k = Teuchos::rcp(new ROL::StdVector<RealT>(k_rcp) );
120 
121  for(int i=0;i<dim;++i) {
122  (*k_rcp)[i] = i+1.0;
123  }
124 
126 
127  // Run Algorithm
128  algo.run(x, obj, true, *outStream);
129 
130  // Get True Solution
131  Teuchos::RCP<std::vector<RealT> > xtrue_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
132  ROL::StdVector<RealT> xtrue(xtrue_rcp);
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) ) {
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 .
double RealT
void stepFactory(Teuchos::ParameterList &parlist, Teuchos::RCP< ROL::Step< Real > > &step)
A minimalist step factory which specializes the Step Type depending on whether a Trust-Region or Line...
int main(int argc, char *argv[])
Real norm() const
Returns where .
Provides the std::vector implementation of the ROL::Vector interface.
Provides an interface to run optimization algorithms.
Provides an interface to check status of optimization algorithms.
Contains definitions for the Zakharov function as evaluated using only the ROL::Vector interface...
virtual std::vector< std::string > run(Vector< Real > &x, Objective< Real > &obj, bool print=false, std::ostream &outStream=std::cout)
Run algorithm on unconstrained problems (Type-U). This is the primary Type-U interface.
double RealT
void JSON_Parameters(const std::string &jsonFileName, Teuchos::ParameterList &parlist)
Read a JSON file and store all parameters in a Teuchos::ParameterList. Checks for a key called "Algor...
Teuchos::RCP< Step< Real > > getStep(const std::string &type, Teuchos::ParameterList &parlist) const
static const double ROL_EPSILON
Platform-dependent machine epsilon.
Definition: ROL_Types.hpp:118