ROL
zakharov/example_02.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 "ROL_RandomVector.hpp"
19#include "ROL_StatusTest.hpp"
20#include "ROL_StdVector.hpp"
21#include "ROL_Zakharov.hpp"
22
23#include "ROL_Stream.hpp"
24#include "Teuchos_GlobalMPISession.hpp"
25
26typedef double RealT;
27
28int main(int argc, char *argv[]) {
29
30 using namespace Teuchos;
31
32 typedef std::vector<RealT> vector;
33 typedef ROL::Vector<RealT> V; // Abstract vector
34 typedef ROL::StdVector<RealT> SV; // Concrete vector containing std::vector data
35
36 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 auto outStream = ROL::makeStreamPtr( std::cout, argc > 1 );
40
41 int errorFlag = 0;
42
43 // *** Example body.
44
45 try {
46
47 int dim = 10; // Set problem dimension.
48
49 std::string paramfile = "parameters.xml";
50 auto parlist = ROL::getParametersFromXmlFile( paramfile );
51
52 ROL::Ptr<vector> x_ptr = ROL::makePtr<vector>(dim, 1.0);
53 ROL::Ptr<vector> k_ptr = ROL::makePtr<vector>(dim, 0.0);
54
55 ROL::Ptr<V> x = ROL::makePtr<SV>(x_ptr); // Optimization vector
56 ROL::Ptr<V> k = ROL::makePtr<SV>(k_ptr); // Vector appearing in Zakharov objective
57
58 ROL::Ptr<V> s = x->clone(); // Step vector
59
60 for( int i=0; i<dim; ++i ) {
61 (*k_ptr)[i] = i+1.0;
62 }
63
64 ROL::Ptr<ROL::Objective<RealT>> obj = ROL::makePtr<ROL::ZOO::Objective_Zakharov<RealT>>(k);
65 ROL::Ptr<ROL::BoundConstraint<RealT>> bnd = ROL::makePtr<ROL::BoundConstraint<RealT>>();
66 bnd->deactivate();
67
70
71 // Allocate iterate vector in algorithm state
72 state.iterateVec = x->clone();
73 state.iterateVec->set(*x);
74 state.minIterVec = x->clone();
75
76 ROL::LineSearchStep<RealT> ls(*parlist);
77 ROL::TrustRegionStep<RealT> tr(*parlist);
78
80 opt.getSolutionVector()->dual(),
81 *opt.getObjective(),
82 *bnd, state );
84 opt.getSolutionVector()->dual(),
85 *opt.getObjective(),
86 *bnd, state );
87
88 for( int iter = 0; iter<10; ++iter ) {
89 ls.compute( *s,
90 *opt.getSolutionVector(),
91 *opt.getObjective(),
92 *bnd, state );
93 ls.update( *opt.getSolutionVector(),
94 *s,
95 *opt.getObjective(),
96 *bnd, state );
97
98 state.minIterVec->set(*x);
99 state.minIter = state.iter;
100 state.minValue = state.value;
101
102 *outStream << "LS fval = " << state.minValue << std::endl;
103
104 tr.compute( *s,
105 *opt.getSolutionVector(),
106 *opt.getObjective(),
107 *bnd, state );
108 tr.update( *opt.getSolutionVector(),
109 *s,
110 *opt.getObjective(),
111 *bnd, state );
112
113 state.minIterVec->set(*x);
114 state.minIter = state.iter;
115 state.minValue = state.value;
116
117 *outStream << "TR fval = " << state.minValue << std::endl;
118 }
119
120
121
122
123 }
124 catch (std::logic_error& err) {
125 *outStream << err.what() << "\n";
126 errorFlag = -1000;
127 }; // end try
128
129 if (errorFlag != 0)
130 std::cout << "End Result: TEST FAILED\n";
131 else
132 std::cout << "End Result: TEST PASSED\n";
133
134 return 0;
135
136}
137
138
139
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 the interface to compute optimization steps with line search.
void update(Vector< Real > &x, const Vector< Real > &s, Objective< Real > &obj, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Update step, if successful.
void initialize(Vector< Real > &x, const Vector< Real > &s, const Vector< Real > &g, Objective< Real > &obj, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Initialize step with bound constraint.
void compute(Vector< Real > &s, const Vector< Real > &x, Objective< Real > &obj, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Compute step.
virtual Ptr< Vector< Real > > getSolutionVector(void)
virtual Ptr< Objective< Real > > getObjective(void)
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Provides the interface to compute optimization steps with trust regions.
void initialize(Vector< Real > &x, const Vector< Real > &s, const Vector< Real > &g, Objective< Real > &obj, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Initialize step.
void update(Vector< Real > &x, const Vector< Real > &s, Objective< Real > &obj, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Update step, if successful.
void compute(Vector< Real > &s, const Vector< Real > &x, Objective< Real > &obj, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Compute step.
Defines the linear algebra or vector space interface.
State for algorithm class. Will be used for restarts.
ROL::Ptr< Vector< Real > > iterateVec
ROL::Ptr< Vector< Real > > minIterVec
constexpr auto dim
int main(int argc, char *argv[])
double RealT