ROL
test_07.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 "ROL_HS32.hpp"
51 #include "ROL_InteriorPoint.hpp"
52 
53 
54 template<class Real>
56  typedef ROL::StdVector<Real> SV;
57  using Teuchos::dyn_cast;
58  const SV &xs = dyn_cast<const SV>(x);
59  Teuchos::RCP<const std::vector<Real> > x_rcp = xs.getVector();
60 
61  for(int i=0;i<xs.dimension();++i) {
62  std::cout << (*x_rcp)[i] << std::endl;
63  }
64 }
65 
66 template<class Real>
67 void print_subvector(const ROL::Vector<Real> &x, const int i){
69  typedef typename PV::size_type size_type;
70  size_type n = static_cast<size_type>(i);
71  using Teuchos::dyn_cast;
72  const PV &xp = dyn_cast<const PV>(x);
73  print_vector(*(xp.get(n)));
74 }
75 
76 typedef double RealT;
77 
78 int main(int argc, char *argv[]) {
79 
80  using Teuchos::RCP;
81  using Teuchos::rcp;
82 
83  typedef std::vector<RealT> vec;
84  typedef ROL::StdVector<RealT> SV;
85  typedef RCP<ROL::Vector<RealT> > RCPV;
86 
87 // typedef ROL::PartitionedVector<RealT> PV;
88 
89 
90  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
91 
92  int iprint = argc - 1;
93  RCP<std::ostream> outStream;
94  Teuchos::oblackholestream bhs; // outputs nothing
95  if (iprint > 0)
96  outStream = rcp(&std::cout, false);
97  else
98  outStream = rcp(&bhs, false);
99 
100  int errorFlag = 0;
101 
102  try {
103 
104  int xopt_dim = 3; // Dimension of optimization vectors
105  int ce_dim = 1; // Dimension of equality constraint
106  int ci_dim = 4; // Dimension of inequality constraint
107 
108  RCP<vec> xopt_rcp = rcp( new vec(xopt_dim,0.0) );
109  RCP<vec> dopt_rcp = rcp( new vec(xopt_dim,0.0) );
110  RCP<vec> vopt_rcp = rcp( new vec(xopt_dim,0.0) );
111 
112  RCP<vec> vec_rcp = rcp( new vec(ce_dim,1.0) );
113  RCP<vec> vel_rcp = rcp( new vec(ce_dim,1.0) );
114 
115  RCP<vec> vic_rcp = rcp( new vec(ci_dim,0.0) );
116  RCP<vec> vil_rcp = rcp( new vec(ci_dim,0.0) );
117 
118  // Slack variables
119  RCP<vec> xs_rcp = rcp( new vec(ci_dim,1.0) );
120  RCP<vec> vs_rcp = rcp( new vec(ci_dim,0.0) );
121  RCP<vec> ds_rcp = rcp( new vec(ci_dim,0.0) );
122 
123  // Feasible initial guess
124  (*xopt_rcp)[0] = 0.1;
125  (*xopt_rcp)[1] = 0.7;
126  (*xopt_rcp)[2] = 0.2;
127 
128  RealT left = -1e0, right = 1e0;
129  for (int i=0; i<xopt_dim; i++) {
130  (*dopt_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
131  (*vopt_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
132  }
133 
134  for (int i=0; i<ci_dim; i++) {
135  (*vic_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
136  (*vil_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
137  (*vs_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
138  (*ds_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
139  }
140 
141  RCPV xopt = rcp( new SV(xopt_rcp) );
142  RCPV dopt = rcp( new SV(dopt_rcp) );
143  RCPV vopt = rcp( new SV(vopt_rcp) );
144  RCPV vec = rcp( new SV(vec_rcp) );
145  RCPV vel = rcp( new SV(vel_rcp) );
146  RCPV vic = rcp( new SV(vic_rcp) );
147  RCPV vil = rcp( new SV(vil_rcp) );
148  RCPV xs = rcp( new SV(xs_rcp) );
149  RCPV vs = rcp( new SV(vs_rcp) );
150  RCPV ds = rcp( new SV(ds_rcp) );
151 
152  // Partitioned vectors of optimization and slack variables
153  RCPV x = CreatePartitionedVector(xopt,xs);
154  RCPV v = CreatePartitionedVector(vopt,vs);
155  RCPV d = CreatePartitionedVector(dopt,ds);
156  RCPV vc = CreatePartitionedVector(vic,vec);
157  RCPV vl = CreatePartitionedVector(vil,vel);
158 
159  // Original obective
160  RCP<ROL::Objective<RealT> > obj_hs32 = rcp( new ROL::ZOO::Objective_HS32<RealT> );
161 
162  // Barrier objective
163  RCP<ROL::Objective<RealT> > barrier = rcp( new ROL::LogBarrierObjective<RealT> );
164 
165  // Interior Point objective
166  RCP<ROL::Objective<RealT> > ipobj =
167  rcp( new ROL::InteriorPointObjective<RealT>(obj_hs32,barrier,1.0) );
168 
169  RCP<ROL::EqualityConstraint<RealT> > eqcon_hs32 =
171 
172  RCP<ROL::EqualityConstraint<RealT> > incon_hs32 =
174 
175  // Interior point constraint
176  RCP<ROL::EqualityConstraint<RealT> > ipcon =
177  rcp( new ROL::InteriorPointEqualityConstraint<RealT>(incon_hs32,eqcon_hs32) );
178 
179  *outStream << "\nChecking individual objectives and constraints separately\n" << std::endl;
180 
181  *outStream << "\nObjective\n" << std::endl;
182  obj_hs32->checkGradient(*xopt,*dopt,true,*outStream);
183  obj_hs32->checkHessVec(*xopt,*vopt,true,*outStream);
184 
185 
186  *outStream << "\nEquality Constraint\n" << std::endl;
187  eqcon_hs32->checkApplyJacobian(*xopt,*vopt,*vec,true,*outStream);
188  eqcon_hs32->checkApplyAdjointJacobian(*xopt,*vel,*vec,*xopt,true,*outStream);
189 
190  *outStream << "\nInequality Constraint\n" << std::endl;
191  incon_hs32->checkApplyJacobian(*xopt,*vopt,*vic,true,*outStream);
192  incon_hs32->checkApplyAdjointJacobian(*xopt,*vil,*vic,*xopt,true,*outStream);
193  incon_hs32->checkApplyAdjointHessian(*xopt,*vil,*dopt,*xopt,true,*outStream);
194 
195 
196  *outStream << "\nCheck Interior Point objective\n" << std::endl;
197  ipobj->checkGradient(*x,*d,true,*outStream);
198  ipobj->checkHessVec(*x,*v,true,*outStream);
199 
200  *outStream << "\nCheck Interior Point constraints\n" << std::endl;
201  ipcon->checkApplyJacobian(*x,*v,*vc,true,*outStream);
202  ipcon->checkApplyAdjointJacobian(*x,*vl,*vc,*x,true,*outStream);
203  ipcon->checkApplyAdjointHessian(*x,*vl,*d,*x,true,*outStream);
204 
205  }
206  catch (std::logic_error err) {
207  *outStream << err.what() << "\n";
208  errorFlag = -1000;
209  }; // end try
210 
211  if (errorFlag != 0)
212  std::cout << "End Result: TEST FAILED\n";
213  else
214  std::cout << "End Result: TEST PASSED\n";
215 
216  return 0;
217 }
218 
Teuchos::RCP< const std::vector< Element > > getVector() const
Has both inequality and equality constraints. Treat inequality constraint as equality with slack vari...
Defines the linear algebra of vector space on a generic partitioned vector.
double RealT
Definition: test_07.cpp:76
Teuchos::RCP< Vector< Real > > CreatePartitionedVector(Teuchos::RCP< Vector< Real > > &a)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Provides the std::vector implementation of the ROL::Vector interface.
void print_vector(const ROL::Vector< Real > &x)
Definition: test_07.cpp:55
int main(int argc, char *argv[])
Definition: test_07.cpp:78
Adds barrier term to generic objective.
Contains definitions for W. Hock and K. Schittkowski 32nd test problem which contains both inequality...
Log barrier objective for interior point methods.
double RealT
void print_subvector(const ROL::Vector< Real > &x, const int i)
Definition: test_07.cpp:67