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 "Teuchos_oblackholestream.hpp"
58 #include "Teuchos_GlobalMPISession.hpp"
59 #include "Teuchos_XMLParameterListHelpers.hpp"
60 
61 typedef double RealT;
62 
63 int main(int argc, char *argv[]) {
64 
65  using namespace Teuchos;
66 
67  typedef std::vector<RealT> vector;
68  typedef ROL::Vector<RealT> V; // Abstract vector
69  typedef ROL::StdVector<RealT> SV; // Concrete vector containing std::vector data
70 
71  GlobalMPISession mpiSession(&argc, &argv);
72 
73  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
74  int iprint = argc - 1;
75  RCP<std::ostream> outStream;
76  oblackholestream bhs; // outputs nothing
77  if (iprint > 0)
78  outStream = rcp(&std::cout, false);
79  else
80  outStream = rcp(&bhs, false);
81 
82  int errorFlag = 0;
83 
84  // *** Example body.
85 
86  try {
87 
88  int dim = 10; // Set problem dimension.
89 
90  RCP<ParameterList> parlist = rcp(new ParameterList());
91  std::string paramfile = "parameters.xml";
92  updateParametersFromXmlFile(paramfile,parlist.ptr());
93 
94  RCP<vector> x_rcp = rcp( new vector(dim, 1.0) );
95  RCP<vector> k_rcp = rcp( new vector(dim, 0.0) );
96 
97  RCP<V> x = rcp( new SV(x_rcp) ); // Optimization vector
98  RCP<V> k = rcp( new SV(k_rcp) ); // Vector appearing in Zakharov objective
99 
100  RCP<V> s = x->clone(); // Step vector
101 
102  for( int i=0; i<dim; ++i ) {
103  (*k_rcp)[i] = i+1.0;
104  }
105 
106  RCP<ROL::Objective<RealT> > obj = rcp(new ROL::ZOO::Objective_Zakharov<RealT>(k) );
107 
110 
111  // Allocate iterate vector in algorithm state
112  state.iterateVec = x->clone();
113  state.iterateVec->set(*x);
114  state.minIterVec = x->clone();
115 
116  ROL::LineSearchStep<RealT> ls(*parlist);
117  ROL::TrustRegionStep<RealT> tr(*parlist);
118 
119  ls.initialize( opt, state );
120  tr.initialize( opt, state );
121 
122  for( int iter = 0; iter<10; ++iter ) {
123  ls.compute( *s, opt, state );
124  ls.update( opt, *s, state );
125 
126  state.minIterVec->set(*x);
127  state.minIter = state.iter;
128  state.minValue = state.value;
129 
130  *outStream << "LS fval = " << state.minValue << std::endl;
131 
132  tr.compute( *s, opt, state );
133  tr.update( opt, *s, state );
134 
135  state.minIterVec->set(*x);
136  state.minIter = state.iter;
137  state.minValue = state.value;
138 
139  *outStream << "TR fval = " << state.minValue << std::endl;
140  }
141 
142 
143 
144 
145  }
146  catch (std::logic_error err) {
147  *outStream << err.what() << "\n";
148  errorFlag = -1000;
149  }; // end try
150 
151  if (errorFlag != 0)
152  std::cout << "End Result: TEST FAILED\n";
153  else
154  std::cout << "End Result: TEST PASSED\n";
155 
156  return 0;
157 
158 }
159 
160 
161 
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:74
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:91
Provides the interface to compute optimization steps with line search.
Provides the std::vector implementation of 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[])
Contains definitions for the Zakharov function as evaluated using only the ROL::Vector interface...
double RealT
Teuchos::RCP< Vector< Real > > minIterVec
Definition: ROL_Types.hpp:107
void compute(Vector< Real > &s, const Vector< Real > &x, Objective< Real > &obj, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Compute step.
void update(Vector< Real > &x, const Vector< Real > &s, Objective< Real > &obj, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Update step, if successful.
Teuchos::RCP< Vector< Real > > iterateVec
Definition: ROL_Types.hpp:105
Provides the interface to compute optimization steps with trust regions.