ROL
step/test_09.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 //
39 // Drew Kouri (dpkouri@sandia.gov) and
40 // Denis Ridzal (dridzal@sandia.gov)
41 //
42 // ************************************************************************
43 // @HEADER
44 
49 #include "Teuchos_GlobalMPISession.hpp"
50 
51 #include "ROL_HS32.hpp"
52 #include "ROL_InteriorPointPrimalDualResidual.hpp"
53 #include "ROL_RandomVector.hpp"
54 #include "ROL_GMRES.hpp"
55 
56 //template<class Real>
57 
58 
59 
60 typedef double RealT;
61 
62 int main(int argc, char *argv[]) {
63 
64  typedef std::vector<RealT> vector;
65  typedef ROL::Vector<RealT> V;
66  typedef ROL::StdVector<RealT> SV;
67 // typedef ROL::PartitionedVector<RealT> PV;
68 
69  typedef typename vector::size_type uint;
70 
71 
72 
73 
74 
75  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
76 
77  int iprint = argc - 1;
78  ROL::Ptr<std::ostream> outStream;
79  ROL::nullstream bhs; // outputs nothing
80  if (iprint > 0)
81  outStream = ROL::makePtrFromRef(std::cout);
82  else
83  outStream = ROL::makePtrFromRef(bhs);
84 
85  int errorFlag = 0;
86 
87  try {
88 
89  uint xo_dim = 3; // Dimension of optimization vectors
90  uint ce_dim = 1; // Dimension of equality constraint
91  uint ci_dim = 4; // Dimension of inequality constraint
92 
93  RealT left = -1.0;
94  RealT right = 1.0;
95 
96  // ----[ Full primal-dual vector ]----------------
97 
98  ROL::Ptr<vector> xo_ptr = ROL::makePtr<vector>(xo_dim,0.0); // opt
99  ROL::Ptr<vector> xs_ptr = ROL::makePtr<vector>(ci_dim,0.0); // slack
100  ROL::Ptr<vector> xe_ptr = ROL::makePtr<vector>(ce_dim,0.0); // equality multipliers
101  ROL::Ptr<vector> xi_ptr = ROL::makePtr<vector>(ci_dim,0.0); // inequality multipliers
102 
103  ROL::Ptr<V> xo = ROL::makePtr<SV>(xo_ptr);
104  ROL::Ptr<V> xs = ROL::makePtr<SV>(xs_ptr);
105  ROL::Ptr<V> xe = ROL::makePtr<SV>(xe_ptr);
106  ROL::Ptr<V> xi = ROL::makePtr<SV>(xi_ptr);
107 
108  ROL::RandomizeVector(*xo,left,right);
109  ROL::RandomizeVector(*xs,left,right);
110  ROL::RandomizeVector(*xe,left,right);
111  ROL::RandomizeVector(*xi,left,right);
112 
113  ROL::Ptr<V> x = ROL::CreatePartitionedVector( xo, xs, xe, xi );
114 
115 
116  // ----[ Full primal-dual direction vector ]------
117 
118  ROL::Ptr<vector> vo_ptr = ROL::makePtr<vector>(xo_dim,0.0); // opt
119  ROL::Ptr<vector> vs_ptr = ROL::makePtr<vector>(ci_dim,0.0); // slack
120  ROL::Ptr<vector> ve_ptr = ROL::makePtr<vector>(ce_dim,0.0); // equality multipliers
121  ROL::Ptr<vector> vi_ptr = ROL::makePtr<vector>(ci_dim,0.0); // inequality multipliers
122 
123  ROL::Ptr<V> vo = ROL::makePtr<SV>(vo_ptr);
124  ROL::Ptr<V> vs = ROL::makePtr<SV>(vs_ptr);
125  ROL::Ptr<V> ve = ROL::makePtr<SV>(ve_ptr);
126  ROL::Ptr<V> vi = ROL::makePtr<SV>(vi_ptr);
127 
128  ROL::RandomizeVector(*vo,left,right);
129  ROL::RandomizeVector(*vs,left,right);
130  ROL::RandomizeVector(*ve,left,right);
131  ROL::RandomizeVector(*vi,left,right);
132 
133  ROL::Ptr<V> v = ROL::CreatePartitionedVector( vo, vs, ve, vi );
134 
135 
136  // ----[ Full primal-dual residual vector ]------
137 
138  ROL::Ptr<vector> ro_ptr = ROL::makePtr<vector>(xo_dim,0.0); // opt
139  ROL::Ptr<vector> rs_ptr = ROL::makePtr<vector>(ci_dim,0.0); // slack
140  ROL::Ptr<vector> re_ptr = ROL::makePtr<vector>(ce_dim,0.0); // equality multipliers
141  ROL::Ptr<vector> ri_ptr = ROL::makePtr<vector>(ci_dim,0.0); // inequality multipliers
142 
143  ROL::Ptr<V> ro = ROL::makePtr<SV>(vo_ptr);
144  ROL::Ptr<V> rs = ROL::makePtr<SV>(vs_ptr);
145  ROL::Ptr<V> re = ROL::makePtr<SV>(ve_ptr);
146  ROL::Ptr<V> ri = ROL::makePtr<SV>(vi_ptr);
147 
148  ROL::RandomizeVector(*ro,left,right);
149  ROL::RandomizeVector(*rs,left,right);
150  ROL::RandomizeVector(*re,left,right);
151  ROL::RandomizeVector(*ri,left,right);
152 
153  ROL::Ptr<V> r = ROL::CreatePartitionedVector( ro, rs, re, ri );
154 
155  // ----[ Primal-dual constraint ]-------
156 
157  ROL::Ptr<ROL::Objective<RealT> > obj_hs32 =
158  ROL::makePtr<ROL::ZOO::Objective_HS32<RealT>>();
159 
160  ROL::Ptr<ROL::EqualityConstraint<RealT> > eqcon_hs32 =
161  ROL::makePtr<ROL::ZOO::EqualityConstraint_HS32<RealT>>();
162 
163  ROL::Ptr<ROL::EqualityConstraint<RealT> > incon_hs32 =
164  ROL::makePtr<ROL::ZOO::InequalityConstraint_HS32<RealT>>();
165 
166 
167  *outStream << "Performing finite difference check on Primal-Dual KKT system"
168  << std::endl;
169 
171 
172  PrimalDualResidual<RealT> con(obj_hs32,eqcon_hs32,incon_hs32, *x);
173 
174  con.checkApplyJacobian(*x,*v,*r,true,*outStream);
175 
176  }
177 
178  catch (std::logic_error& err) {
179  *outStream << err.what() << "\n";
180  errorFlag = -1000;
181  }; // end try
182 
183  if (errorFlag != 0)
184  std::cout << "End Result: TEST FAILED\n";
185  else
186  std::cout << "End Result: TEST PASSED\n";
187 
188 
189  return 0;
190 }
191 
192 
typename PV< Real >::size_type size_type
ROL::Ptr< Vector< Real > > CreatePartitionedVector(const ROL::Ptr< Vector< Real >> &a)
void RandomizeVector(Vector< Real > &x, const Real &lower=0.0, const Real &upper=1.0)
Fill a ROL::Vector with uniformly-distributed random numbers in the interval [lower,upper].
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
Vector< Real > V
Express the Primal-Dual Interior Point gradient as an equality constraint.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Contains definitions for W. Hock and K. Schittkowski 32nd test problem which contains both inequality...
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:72
int main(int argc, char *argv[])