ROL
example_01b.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
18#include <iostream>
19
20#include "ROL_Algorithm.hpp"
22#include "ROL_StatusTest.hpp"
23#include "ROL_ParameterList.hpp"
24
25#include "ROL_Stream.hpp"
26#include "Teuchos_GlobalMPISession.hpp"
27
28#include "example_01b.hpp"
29
30using namespace ROL;
31
32typedef double RealT;
33
34int main(int argc, char **argv)
35{
36 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
37
38 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
39 int iprint = argc - 1;
40 ROL::Ptr<std::ostream> outStream;
41 ROL::nullstream bhs; // outputs nothing
42 if (iprint > 0)
43 outStream = ROL::makePtrFromRef(std::cout);
44 else
45 outStream = ROL::makePtrFromRef(bhs);
46
47 int errorFlag = 0;
48
49 // *** Example body.
50
51 try {
52
54
55 int dim = 10; // Set problem dimension.
56
57 // Load optimizer parameters form XML file
58 std::string paramfile = "parameters.xml";
59 auto parlist = ROL::getParametersFromXmlFile(paramfile);
60
61 // Define algorithm.
62 ROL::Ptr<ROL::Step<RealT>>
63 step = ROL::makePtr<ROL::LineSearchStep<RealT>>(*parlist);
64 ROL::Ptr<ROL::StatusTest<RealT>>
65 status = ROL::makePtr<ROL::StatusTest<RealT>>(*parlist);
66 ROL::Algorithm<RealT> algo(step,status,false);
67
68 // Iteration Vector
69 ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
70 // Set Initial Guess
71 for (int i=0; i<dim; i++) {
72 (*x_ptr)[i] = 2;
73 }
74
75 StdVector<RealT> x(x_ptr);
76
77 // Run Algorithm
78 algo.run(x, obj, true, *outStream);
79
80 // Get True Solution
81 ROL::Ptr<std::vector<RealT> > xtrue_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
82 StdVector<RealT> xtrue(xtrue_ptr);
83
84
85 // Compute Error
86 x.axpy(-1.0, xtrue);
87 RealT abserr = x.norm();
88 *outStream << std::scientific << "\n Absolute Error: " << abserr;
89 if ( abserr > sqrt(ROL_EPSILON<RealT>()) ) {
90 errorFlag += 1;
91 }
92 }
93 catch (std::logic_error& err) {
94 *outStream << err.what() << "\n";
95 errorFlag = -1000;
96 }; // end try
97
98 if (errorFlag != 0)
99 std::cout << "End Result: TEST FAILED\n";
100 else
101 std::cout << "End Result: TEST PASSED\n";
102
103 return 0;
104
105}
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Provides an interface to run optimization algorithms.
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.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
Real norm() const
Returns where .
int main(int argc, char **argv)
double RealT
constexpr auto dim