ROL
ROL_Minimax3.hpp
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 
10 #ifndef ROL_MINIMAX3_HPP
11 #define ROL_MINIMAX3_HPP
12 
13 #include "ROL_TestProblem.hpp"
14 #include "ROL_StdVector.hpp"
15 #include "ROL_Ptr.hpp"
16 
17 namespace ROL {
18 namespace ZOO {
19 
20 template<class Real>
21 class Minimax3 : public Objective<Real> {
22 
23  typedef std::vector<Real> vector;
24  typedef Vector<Real> V;
25  typedef StdVector<Real> SV;
26 
27 private:
28 
29  ROL::Ptr<const vector> getVector( const V& x ) {
30 
31  return dynamic_cast<const SV&>(x).getVector();
32  }
33 
34  ROL::Ptr<vector> getVector( V& x ) {
35 
36  return dynamic_cast<SV&>(x).getVector();
37  }
38 
39 public:
40  Minimax3(void) {}
41 
42  Real value(const Vector<Real> &x, Real &tol) {
43 
44 
45  ROL::Ptr<const vector> xp = getVector(x);
46 
47  Real F = std::pow((*xp)[0],2.0) + std::pow((*xp)[1],2.0) + 2.0*std::pow((*xp)[2],2.0)
48  + std::pow((*xp)[3],2.0) - 5.0*(*xp)[0] - 5.0*(*xp)[1] - 21.0*(*xp)[2] + 7.0*(*xp)[3];
49  Real g2 = -std::pow((*xp)[0],2.0)-std::pow((*xp)[1],2.0)-std::pow((*xp)[2],2.0)-std::pow((*xp)[3],2.0)
50  -(*xp)[0]+(*xp)[1]-(*xp)[2]+(*xp)[3]+8.0;
51  Real g3 = -std::pow((*xp)[0],2.0)-2.0*std::pow((*xp)[1],2.0)-std::pow((*xp)[2],2.0)
52  -2.0*std::pow((*xp)[3],2.0)+(*xp)[0]+(*xp)[3]+10.0;
53  Real g4 = -std::pow((*xp)[0],2.0)-std::pow((*xp)[1],2.0)-std::pow((*xp)[2],2.0)
54  -2.0*(*xp)[0]+(*xp)[1]+(*xp)[3]+5.0;
55  Real a2 = 10.0, a3 = 10.0, a4 = 10.0;
56  return std::max(F,std::max(F-a2*g2,std::max(F-a3*g3,F-a4*g4)));
57  }
58 
59  void gradient(Vector<Real> &g, const Vector<Real> &x, Real &tol) {
60 
61 
62  ROL::Ptr<const vector> xp = getVector(x);
63  ROL::Ptr<vector> gp = getVector(g);
64 
65  Real F = std::pow((*xp)[0],2.0) + std::pow((*xp)[1],2.0) + 2.0*std::pow((*xp)[2],2.0)
66  + std::pow((*xp)[3],2.0) - 5.0*(*xp)[0] - 5.0*(*xp)[1] - 21.0*(*xp)[2] + 7.0*(*xp)[3];
67  Real g2 = -std::pow((*xp)[0],2.0)-std::pow((*xp)[1],2.0)-std::pow((*xp)[2],2.0)-std::pow((*xp)[3],2.0)
68  -(*xp)[0]+(*xp)[1]-(*xp)[2]+(*xp)[3]+8.0;
69  Real g3 = -std::pow((*xp)[0],2.0)-2.0*std::pow((*xp)[1],2.0)-std::pow((*xp)[2],2.0)
70  -2.0*std::pow((*xp)[3],2.0)+(*xp)[0]+(*xp)[3]+10.0;
71  Real g4 = -std::pow((*xp)[0],2.0)-std::pow((*xp)[1],2.0)-std::pow((*xp)[2],2.0)
72  -2.0*(*xp)[0]+(*xp)[1]+(*xp)[3]+5.0;
73  Real a2 = 10.0, a3 = 10.0, a4 = 10.0;
74 
75  (*gp)[0] = 2.0*(*xp)[0] - 5.0;
76  (*gp)[1] = 2.0*(*xp)[1] - 5.0;
77  (*gp)[2] = 4.0*(*xp)[2] - 21.0;
78  (*gp)[3] = 2.0*(*xp)[3] + 7.0;
79  if ( F-a2*g2 >= std::max(F,std::max(F-a3*g3,F-a4*g4)) ) {
80  (*gp)[0] += a2*(2.0*(*xp)[0] + 1.0);
81  (*gp)[1] += a2*(2.0*(*xp)[1] - 1.0);
82  (*gp)[2] += a2*(2.0*(*xp)[2] + 1.0);
83  (*gp)[3] += a2*(2.0*(*xp)[3] - 1.0);
84  }
85  else if ( F-a3*g3 >= std::max(F,std::max(F-a2*g2,F-a4*g4)) ) {
86  (*gp)[0] += a2*(2.0*(*xp)[0] - 1.0);
87  (*gp)[1] += a2*(4.0*(*xp)[1]);
88  (*gp)[2] += a2*(2.0*(*xp)[2]);
89  (*gp)[3] += a2*(4.0*(*xp)[3] - 1.0);
90  }
91  else if ( F-a4*g4 >= std::max(F,std::max(F-a2*g2,F-a3*g3)) ) {
92  (*gp)[0] += a2*(2.0*(*xp)[0] + 2.0);
93  (*gp)[1] += a2*(2.0*(*xp)[1] - 1.0);
94  (*gp)[2] += a2*(2.0*(*xp)[2]);
95  (*gp)[3] += a2*(-1.0);
96  }
97  }
98 }; // class Minimax3
99 
100 template<class Real>
101 class getMinimax3 : public TestProblem<Real> {
102 public:
103  getMinimax3(void) {}
104 
105  Ptr<Objective<Real>> getObjective(void) const {
106  return makePtr<Minimax3<Real>>();
107  }
108 
109  Ptr<Vector<Real>> getInitialGuess(void) const {
110  ROL::Ptr<std::vector<Real> > x_ptr = ROL::makePtr<std::vector<Real>>(4, 0.0);
111  return makePtr<StdVector<Real>>(x_ptr);
112  }
113 
114  Ptr<Vector<Real>> getSolution(const int i = 0) const {
115  ROL::Ptr<std::vector<Real> > z_ptr = ROL::makePtr<std::vector<Real>>(4, 0.0);
116  (*z_ptr)[0] = 0.0; (*z_ptr)[1] = 1.0;
117  (*z_ptr)[2] = 2.0; (*z_ptr)[3] = -1.0;
118  return makePtr<StdVector<Real>>(z_ptr);
119  }
120 };
121 
122 } // namespace ZOO
123 } // namespace ROL
124 
125 #endif
Provides the interface to evaluate objective functions.
Ptr< Vector< Real > > getSolution(const int i=0) const
Ptr< Objective< Real > > getObjective(void) const
StdVector< Real > SV
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
ROL::Ptr< const vector > getVector(const V &x)
Contains definitions of test objective functions.
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Vector< Real > V
ROL::Ptr< vector > getVector(V &x)
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
std::vector< Real > vector
Ptr< Vector< Real > > getInitialGuess(void) const