ROL
test_08.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_getConst.hpp"
50 #include "ROL_HS29.hpp"
52 #include "ROL_InteriorPoint.hpp"
53 
54 typedef double RealT;
55 
56 int main(int argc, char *argv[]) {
57 
58  using Teuchos::RCP;
59  using Teuchos::rcp;
60 
61  typedef std::vector<RealT> vec;
62  typedef ROL::StdVector<RealT> SV;
63  typedef RCP<ROL::Vector<RealT> > RCPV;
64 
65  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
66 
67  int iprint = argc - 1;
68  RCP<std::ostream> outStream;
69  Teuchos::oblackholestream bhs; // outputs nothing
70  if (iprint > 0)
71  outStream = rcp(&std::cout, false);
72  else
73  outStream = rcp(&bhs, false);
74 
75  int errorFlag = 0;
76 
77  try {
78 
79  int xopt_dim = 3; // Dimension of optimization vectors
80  int ci_dim = 1; // Dimension of inequality constraint
81 
82  RCP<vec> xopt_rcp = rcp( new vec(xopt_dim,1.0) ); // Feasible initial guess
83  RCP<vec> dopt_rcp = rcp( new vec(xopt_dim,0.0) );
84  RCP<vec> vopt_rcp = rcp( new vec(xopt_dim,0.0) );
85 
86  RCP<vec> vic_rcp = rcp( new vec(ci_dim,0.0) );
87  RCP<vec> vil_rcp = rcp( new vec(ci_dim,0.0) );
88 
89  // Slack variables
90  RCP<vec> xs_rcp = rcp( new vec(ci_dim,1.0) );
91  RCP<vec> vs_rcp = rcp( new vec(ci_dim,0.0) );
92  RCP<vec> ds_rcp = rcp( new vec(ci_dim,0.0) );
93 
94  RealT left = -1e0, right = 1e0;
95  for (int i=0; i<xopt_dim; i++) {
96  (*dopt_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
97  (*vopt_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
98  }
99 
100  for (int i=0; i<ci_dim; i++) {
101  (*vic_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
102  (*vil_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
103  (*vs_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
104  (*ds_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
105  }
106 
107  RCPV xopt = rcp( new SV(xopt_rcp) );
108  RCPV dopt = rcp( new SV(dopt_rcp) );
109  RCPV vopt = rcp( new SV(vopt_rcp) );
110  RCPV vic = rcp( new SV(vic_rcp) );
111  RCPV vil = rcp( new SV(vil_rcp) );
112  RCPV xs = rcp( new SV(xs_rcp) );
113  RCPV vs = rcp( new SV(vs_rcp) );
114  RCPV ds = rcp( new SV(ds_rcp) );
115 
116  // Partitioned vectors of optimization and slack variables
117  RCPV x = CreatePartitionedVector(xopt,xs);
118  RCPV v = CreatePartitionedVector(vopt,vs);
119  RCPV d = CreatePartitionedVector(dopt,ds);
120  RCPV vc = CreatePartitionedVector(vic);
121  RCPV vl = CreatePartitionedVector(vil);
122 
123  // Original obective
124  RCP<ROL::Objective<RealT> > obj_hs29 = rcp( new ROL::ZOO::Objective_HS29<RealT> );
125 
126  // Barrier objective
127  RCP<ROL::Objective<RealT> > barrier = rcp( new ROL::LogBarrierObjective<RealT> );
128 
129  // Interior Point objective
130  RCP<ROL::Objective<RealT> > ipobj =
131  rcp( new ROL::InteriorPointObjective<RealT>(obj_hs29,barrier,1.0) );
132 
133  RCP<ROL::EqualityConstraint<RealT> > incon_hs29 =
135 
136  // Interior point constraint
137  RCP<ROL::EqualityConstraint<RealT> > ipcon =
138  rcp( new ROL::InteriorPointEqualityConstraint<RealT>(incon_hs29) );
139 
140  *outStream << "\nChecking individual objectives and constraints separately\n" << std::endl;
141 
142  *outStream << "\nObjective\n" << std::endl;
143  obj_hs29->checkGradient(*xopt,*dopt,true,*outStream);
144  obj_hs29->checkHessVec(*xopt,*vopt,true,*outStream);
145 
146  *outStream << "\nInequality Constraint\n" << std::endl;
147  incon_hs29->checkApplyJacobian(*xopt,*vopt,*vic,true,*outStream);
148  incon_hs29->checkApplyAdjointJacobian(*xopt,*vil,*vic,*xopt,true,*outStream);
149  incon_hs29->checkApplyAdjointHessian(*xopt,*vil,*dopt,*xopt,true,*outStream);
150 
151  *outStream << "\nCheck Interior Point objective\n" << std::endl;
152  ipobj->checkGradient(*x,*d,true,*outStream);
153  ipobj->checkHessVec(*x,*v,true,*outStream);
154 
155  *outStream << "\nCheck Interior Point constraints\n" << std::endl;
156  ipcon->checkApplyJacobian(*x,*v,*vc,true,*outStream);
157  ipcon->checkApplyAdjointJacobian(*x,*vl,*vc,*x,true,*outStream);
158  ipcon->checkApplyAdjointHessian(*x,*vl,*d,*x,true,*outStream);
159 
160  }
161  catch (std::logic_error err) {
162  *outStream << err.what() << "\n";
163  errorFlag = -1000;
164  }; // end try
165 
166  if (errorFlag != 0)
167  std::cout << "End Result: TEST FAILED\n";
168  else
169  std::cout << "End Result: TEST PASSED\n";
170 
171  return 0;
172 
173 
174 
175 }
Contains definitions for W. Hock and K. Schittkowski 32nd test problem which contains only inequality...
Has both inequality and equality constraints. Treat inequality constraint as equality with slack vari...
Teuchos::RCP< Vector< Real > > CreatePartitionedVector(Teuchos::RCP< Vector< Real > > &a)
double RealT
Definition: test_08.cpp:54
Provides the std::vector implementation of the ROL::Vector interface.
int main(int argc, char *argv[])
Definition: test_08.cpp:56
Adds barrier term to generic objective.
Log barrier objective for interior point methods.
double RealT