ROL
vector/test_06.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
15#include "ROL_StdVector.hpp"
16#include "ROL_Zakharov.hpp"
17#include "ROL_Algorithm.hpp"
19#include "ROL_StatusTest.hpp"
20#include "ROL_Stream.hpp"
21
22#include "Teuchos_GlobalMPISession.hpp"
23
24typedef int OrdinalT;
25typedef double RealT;
26
27template<>
30
31int main(int argc, char *argv[]) {
32
33
34 using ROL::ParameterList;
35
36 typedef std::vector<RealT> vector;
37
38 typedef ROL::Vector<RealT> V;
39 typedef ROL::StdVector<RealT> SV;
40
41 Teuchos::GlobalMPISession mpiSession(&argc, &argv,0);
42
43 int iprint = argc - 1;
44 ROL::Ptr<std::ostream> outStream;
45 ROL::nullstream bhs; // outputs nothing
46 if (iprint > 0)
47 outStream = ROL::makePtrFromRef(std::cout);
48 else
49 outStream = ROL::makePtrFromRef(bhs);
50
51 int errorFlag = 0;
52
53 try {
54 // Dimension of the optimization vector
55 int dim = 10;
56
57 std::string paramfile = "parameters.xml";
58 auto parlist = ROL::getParametersFromXmlFile(paramfile);
59
60 // Define algorithm.
61 ROL::Ptr<ROL::Step<RealT>>
62 step = ROL::makePtr<ROL::TrustRegionStep<RealT>>(*parlist);
63 ROL::Ptr<ROL::StatusTest<RealT>>
64 status = ROL::makePtr<ROL::StatusTest<RealT>>(*parlist);
65 ROL::Algorithm<RealT> algo(step,status,false);
66
67 ROL::Ptr<vector> x_ptr = ROL::makePtr<vector>(dim,1.0);
68 ROL::Ptr<vector> k_ptr = ROL::makePtr<vector>(dim);
69
70 for(int i=0;i<dim;++i) {
71 (*k_ptr)[i] = 1.0 + i;
72 }
73
74 ROL::Ptr<V> xs = ROL::makePtr<SV>(x_ptr);
75 ROL::Ptr<V> ks = ROL::makePtr<SV>(k_ptr);
76
77 // Create ProfiledVector objects
79 ROL::Ptr<V> kpf = ROL::makePtr<ROL::ProfiledVector<int,RealT>>(ks);
80
82
83 // Run algorithm.
84 algo.run(xpf, obj, true, *outStream);
85
86 // Display number of function calls
87 ROL::printVectorFunctionCalls(xpf, *outStream);
88 }
89
90 catch (std::logic_error& err) {
91 *outStream << err.what() << "\n";
92 errorFlag = -1000;
93 }; // end try
94
95 if (errorFlag != 0)
96 std::cout << "End Result: TEST FAILED\n";
97 else
98 std::cout << "End Result: TEST PASSED\n";
99
100 return 0;
101}
Vector< Real > V
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Contains definitions for the Zakharov function as evaluated using only the ROL::Vector interface.
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.
By keeping a pointer to this in a derived Vector class, a tally of all methods is kept for profiling ...
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Defines the linear algebra or vector space interface.
void printVectorFunctionCalls(const ProfiledVector< Ordinal, Real > &x, std::ostream &outStream=std::cout)
int main(int argc, char *argv[])
int OrdinalT
double RealT
constexpr auto dim