ROL
function/test_18.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
31 #include "ROL_RandomVector.hpp"
32 #include "ROL_StdVector.hpp"
34 
35 #include "ROL_Stream.hpp"
36 #include "Teuchos_GlobalMPISession.hpp"
37 
39 #include "ROL_Zakharov.hpp"
40 
41 
42 int main(int argc, char *argv[]) {
43 
44  using RealT = double;
46  using ObjectiveT = ROL::ZOO::Objective_Zakharov<RealT>;
48 
49  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
50 
51  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
52  int iprint = argc - 1;
53  ROL::Ptr<std::ostream> outStream;
54  ROL::nullstream bhs; // outputs nothing
55  if (iprint > 0)
56  outStream = ROL::makePtrFromRef(std::cout);
57  else
58  outStream = ROL::makePtrFromRef(bhs);
59 
60  // Save the format state of the original std::cout.
61  ROL::nullstream oldFormatState;
62  oldFormatState.copyfmt(std::cout);
63 
64 // RealT errtol = std::sqrt(ROL::ROL_THRESHOLD<RealT>());
65 
66  int errorFlag = 0;
67 
68  // *** Test body.
69 
70  try {
71 
72  uint x_dim = 5; // Constraint domain space dimension
73  uint y_dim = 3; // Constraint range space dimension and objective domain space dimension
74 
75 
76  // Make a Zakharov objective function f(y)
77  auto k_ptr = ROL::makePtr<VectorT>(y_dim);
78  auto& k = *k_ptr;
79  k[0] = 1;
80  k[1] = 2;
81  k[2] = 3;
82 
83  auto x = VectorT(x_dim);
84  auto l = VectorT(y_dim);
85 
86  auto obj_ptr = ROL::makePtr<ObjectiveT>(k_ptr);
87  auto con_ptr = ROL::makePtr<ConstraintT>();
88 
89  VectorT v(x_dim), g(x_dim), hv(x_dim), u(x_dim);
90 
91  auto obj = ROL::ChainRuleObjective<RealT>(obj_ptr,con_ptr,x,l);
92 
96 
97  RealT tol = std::sqrt(ROL::ROL_EPSILON<RealT>());
98 
99  auto result_1 = obj.checkGradient(x,v,true,*outStream,7,4);
100 
101  bool gradient_passed = false;
102 
103  for( auto& row : result_1 ) {
104  if(row[3] < tol) {
105  gradient_passed = true;
106  break;
107  }
108  }
109 
110  errorFlag += (!gradient_passed);
111 
112  auto result_2 = obj.checkHessVec(x,hv,v,true,*outStream,7,4);
113 
114  bool hessVec_passed = false;
115 
116  for( auto& row : result_2 ) {
117  if(row[3] < tol) {
118  hessVec_passed = true;
119  break;
120  }
121  }
122 
123  errorFlag += (!hessVec_passed) << 1;
124 
125  auto result_3 = obj.checkHessSym(x,hv,v,u,true,*outStream);
126  auto hessSym_passed = (result_3[2] < tol);
127 
128  errorFlag += (!hessSym_passed) << 2;
129 
130  }
131  catch (std::logic_error& err) {
132  *outStream << err.what() << "\n";
133  errorFlag = -1000;
134  }; // end try
135 
136  if (errorFlag != 0)
137  std::cout << "End Result: TEST FAILED\n";
138  else
139  std::cout << "End Result: TEST PASSED\n";
140 
141  return 0;
142 
143 
144 }
145 
Equality constraints c_i(x) = 0, where: c1(x) = x1^2+x2^2+x3^2+x4^2+x5^2 - 10 c2(x) = x2*x3-5*x4*x5 c...
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 a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
basic_nullstream< char, std::char_traits< char >> nullstream
Definition: ROL_Stream.hpp:36
std::vector< RealT > VectorT
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Contains definitions for the Zakharov function as evaluated using only the ROL::Vector interface...
Contains definitions for the equality constrained NLP from Nocedal/Wright, 2nd edition, page 574, example 18.2; note the typo in reversing the initial guess and the solution.
int main(int argc, char *argv[])
Defines an objective of the form f(g(x)) where.