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 
102 
103  // Allocate iterate vector in algorithm state
104  state.iterateVec = x->clone();
105  state.iterateVec->set(*x);
106  state.minIterVec = x->clone();
107 
108  ROL::LineSearchStep<RealT> ls(*parlist);
109  ROL::TrustRegionStep<RealT> tr(*parlist);
110 
111  ls.initialize( opt, state );
112  tr.initialize( opt, state );
113 
114  for( int iter = 0; iter<10; ++iter ) {
115  ls.compute( *s, opt, state );
116  ls.update( opt, *s, state );
117 
118  state.minIterVec->set(*x);
119  state.minIter = state.iter;
120  state.minValue = state.value;
121 
122  *outStream << "LS fval = " << state.minValue << std::endl;
123 
124  tr.compute( *s, opt, state );
125  tr.update( opt, *s, state );
126 
127  state.minIterVec->set(*x);
128  state.minIter = state.iter;
129  state.minValue = state.value;
130 
131  *outStream << "TR fval = " << state.minValue << std::endl;
132  }
133 
134 
135 
136 
137  }
138  catch (std::logic_error err) {
139  *outStream << err.what() << "\n";
140  errorFlag = -1000;
141  }; // end try
142 
143  if (errorFlag != 0)
144  std::cout << "End Result: TEST FAILED\n";
145  else
146  std::cout << "End Result: TEST PASSED\n";
147 
148  return 0;
149 
150 }
151 
152 
153 
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.
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.
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
Provides the interface to compute optimization steps with trust regions.