ROL
zakharov/example_02.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 
50 #include "ROL_LineSearchStep.hpp"
51 #include "ROL_TrustRegionStep.hpp"
52 #include "ROL_RandomVector.hpp"
53 #include "ROL_StatusTest.hpp"
54 #include "ROL_StdVector.hpp"
55 #include "ROL_Zakharov.hpp"
56 
57 #include "ROL_Stream.hpp"
58 #include "Teuchos_GlobalMPISession.hpp"
59 
60 typedef double RealT;
61 
62 int main(int argc, char *argv[]) {
63 
64  using namespace Teuchos;
65 
66  typedef std::vector<RealT> vector;
67  typedef ROL::Vector<RealT> V; // Abstract vector
68  typedef ROL::StdVector<RealT> SV; // Concrete vector containing std::vector data
69 
70  GlobalMPISession mpiSession(&argc, &argv);
71 
72  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
73  auto outStream = ROL::makeStreamPtr( std::cout, argc > 1 );
74 
75  int errorFlag = 0;
76 
77  // *** Example body.
78 
79  try {
80 
81  int dim = 10; // Set problem dimension.
82 
83  std::string paramfile = "parameters.xml";
84  auto parlist = ROL::getParametersFromXmlFile( paramfile );
85 
86  ROL::Ptr<vector> x_ptr = ROL::makePtr<vector>(dim, 1.0);
87  ROL::Ptr<vector> k_ptr = ROL::makePtr<vector>(dim, 0.0);
88 
89  ROL::Ptr<V> x = ROL::makePtr<SV>(x_ptr); // Optimization vector
90  ROL::Ptr<V> k = ROL::makePtr<SV>(k_ptr); // Vector appearing in Zakharov objective
91 
92  ROL::Ptr<V> s = x->clone(); // Step vector
93 
94  for( int i=0; i<dim; ++i ) {
95  (*k_ptr)[i] = i+1.0;
96  }
97 
98  ROL::Ptr<ROL::Objective<RealT>> obj = ROL::makePtr<ROL::ZOO::Objective_Zakharov<RealT>>(k);
99  ROL::Ptr<ROL::BoundConstraint<RealT>> bnd = ROL::makePtr<ROL::BoundConstraint<RealT>>();
100  bnd->deactivate();
101 
104 
105  // Allocate iterate vector in algorithm state
106  state.iterateVec = x->clone();
107  state.iterateVec->set(*x);
108  state.minIterVec = x->clone();
109 
110  ROL::LineSearchStep<RealT> ls(*parlist);
111  ROL::TrustRegionStep<RealT> tr(*parlist);
112 
113  ls.initialize( *opt.getSolutionVector(),
114  opt.getSolutionVector()->dual(),
115  *opt.getObjective(),
116  *bnd, state );
117  tr.initialize( *opt.getSolutionVector(),
118  opt.getSolutionVector()->dual(),
119  *opt.getObjective(),
120  *bnd, state );
121 
122  for( int iter = 0; iter<10; ++iter ) {
123  ls.compute( *s,
124  *opt.getSolutionVector(),
125  *opt.getObjective(),
126  *bnd, state );
127  ls.update( *opt.getSolutionVector(),
128  *s,
129  *opt.getObjective(),
130  *bnd, state );
131 
132  state.minIterVec->set(*x);
133  state.minIter = state.iter;
134  state.minValue = state.value;
135 
136  *outStream << "LS fval = " << state.minValue << std::endl;
137 
138  tr.compute( *s,
139  *opt.getSolutionVector(),
140  *opt.getObjective(),
141  *bnd, state );
142  tr.update( *opt.getSolutionVector(),
143  *s,
144  *opt.getObjective(),
145  *bnd, state );
146 
147  state.minIterVec->set(*x);
148  state.minIter = state.iter;
149  state.minValue = state.value;
150 
151  *outStream << "TR fval = " << state.minValue << std::endl;
152  }
153 
154 
155 
156 
157  }
158  catch (std::logic_error& err) {
159  *outStream << err.what() << "\n";
160  errorFlag = -1000;
161  }; // end try
162 
163  if (errorFlag != 0)
164  std::cout << "End Result: TEST FAILED\n";
165  else
166  std::cout << "End Result: TEST PASSED\n";
167 
168  return 0;
169 
170 }
171 
172 
173 
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.
virtual Ptr< Objective< Real > > getObjective(void)
void update(Vector< Real > &x, const Vector< Real > &s, Objective< Real > &obj, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Update step, if successful.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Ptr< ostream > makeStreamPtr(ostream &os, bool noSuppressOutput=true)
Definition: ROL_Stream.hpp:75
Vector< Real > V
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.
State for algorithm class. Will be used for restarts.
Definition: ROL_Types.hpp:143
Provides the interface to compute optimization steps with line search.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar 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)
ROL::Ptr< Vector< Real > > iterateVec
Definition: ROL_Types.hpp:157
Contains definitions for the Zakharov function as evaluated using only the ROL::Vector interface...
void compute(Vector< Real > &s, const Vector< Real > &x, Objective< Real > &obj, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Compute step.
int main(int argc, char *argv[])
void update(Vector< Real > &x, const Vector< Real > &s, Objective< Real > &obj, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Update step, if successful.
ROL::Ptr< Vector< Real > > minIterVec
Definition: ROL_Types.hpp:159
constexpr auto dim
Provides the interface to compute optimization steps with trust regions.