ROL
ROL_Minimax2.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_MINIMAX2_HPP
11 #define ROL_MINIMAX2_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 Minimax2 : public Objective<Real> {
22 
23  typedef std::vector<Real> vector;
24  typedef Vector<Real> V;
26 
27 private:
28 
29  Ptr<const vector> getVector( const V& x ) {
30  return dynamic_cast<const SV&>(x).getVector();
31  }
32 
33  Ptr<vector> getVector( V& x ) {
34  return dynamic_cast<SV&>(x).getVector();
35  }
36 
37 public:
38  Minimax2(void) {}
39 
40  Real value(const Vector<Real> &x, Real &tol) {
41  Ptr<const vector> xp = getVector(x);
42  Real f1 = std::pow((*xp)[0],4.0) + std::pow((*xp)[1],2.0);
43  Real f2 = std::pow(2.0-(*xp)[0],2.0) + std::pow(2.0-(*xp)[1],2.0);
44  Real f3 = 2.0*std::exp(-(*xp)[0] + (*xp)[1]);
45  return std::max(f1,std::max(f2,f3));
46  }
47 
48  void gradient(Vector<Real> &g, const Vector<Real> &x, Real &tol) {
49  Ptr<const vector> xp = getVector(x);
50  Ptr<vector> gp = getVector(g);
51  Real f1 = std::pow((*xp)[0],4.0) + std::pow((*xp)[1],2.0);
52  Real f2 = std::pow(2.0-(*xp)[0],2.0) + std::pow(2.0-(*xp)[1],2.0);
53  Real f3 = 2.0*std::exp(-(*xp)[0] + (*xp)[1]);
54 
55  if ( f1 >= std::max(f2,f3) ) {
56  (*gp)[0] = 4.0*std::pow((*xp)[0],3.0);
57  (*gp)[1] = 2.0*(*xp)[1];
58  }
59  else if ( f2 >= std::max(f1,f3) ) {
60  (*gp)[0] = 2.0*(*xp)[0]-4.0;
61  (*gp)[1] = 2.0*(*xp)[1]-4.0;
62  }
63  else if ( f3 >= std::max(f1,f2) ) {
64  (*gp)[0] = -2.0*std::exp(-(*xp)[0]+(*xp)[1]);
65  (*gp)[1] = 2.0*std::exp(-(*xp)[0]+(*xp)[1]);
66  }
67  }
68 }; // class Minimax2
69 
70 template<class Real>
71 class getMinimax2 : public TestProblem<Real> {
72 public:
73  getMinimax2(void) {}
74 
75  Ptr<Objective<Real>> getObjective(void) const {
76  return makePtr<Minimax2<Real>>();
77  }
78 
79  Ptr<Vector<Real>> getInitialGuess(void) const {
80  Ptr<std::vector<Real> > x_ptr = makePtr<std::vector<Real>>(2, 0.0);
81  (*x_ptr)[0] = 1.0; (*x_ptr)[1] = -0.1;
82  return makePtr<StdVector<Real>>(x_ptr);
83  }
84 
85  Ptr<Vector<Real>> getSolution(const int i = 0) const {
86  Ptr<std::vector<Real> > z_ptr = makePtr<std::vector<Real>>(2, 0.0);
87  (*z_ptr)[0] = 1.0; (*z_ptr)[1] = 1.0;
88  return makePtr<StdVector<Real>>(z_ptr);
89  }
90 };
91 
92 } // namespace ZOO
93 } // namespace ROL
94 
95 #endif
Provides the interface to evaluate objective functions.
Ptr< Vector< Real > > getSolution(const int i=0) const
Vector< Real > V
StdVector< Real > SV
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
Ptr< vector > getVector(V &x)
Ptr< Objective< Real > > getObjective(void) const
Ptr< Vector< Real > > getInitialGuess(void) const
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Contains definitions of test objective functions.
Real value(const Vector< Real > &x, Real &tol)
Compute value.
std::vector< Real > vector
Ptr< const vector > getVector(const V &x)