ConstrainedSphere.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Convex quadratic benchmark function.
5  *
6  *
7  * \author T. Voss
8  * \date 2010-2011
9  *
10  *
11  * \par Copyright 1995-2017 Shark Development Team
12  *
13  * <BR><HR>
14  * This file is part of Shark.
15  * <http://shark-ml.org/>
16  *
17  * Shark is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU Lesser General Public License as published
19  * by the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * Shark is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
29  *
30  */
31 #ifndef SHARK_OBJECTIVEFUNCTIONS_BENCHMARK_CONSTRAINEDSPHERE_H
32 #define SHARK_OBJECTIVEFUNCTIONS_BENCHMARK_CONSTRAINEDSPHERE_H
33 
35 #include <shark/Core/Random.h>
36 
37 namespace shark {
38 /**
39  * \brief Constrained Sphere function
40  *
41  * This is a simple sphere function minimizing \f$ f(x) = \sum_i^N x_i^2-m \f$ under the constraints that
42  * \f$ x_i \geq 1\f$ for \f$ i = 1,\dots,m \f$. The minimum is at \f$ x_1=\dots = x_m = 1\f$ and
43  * \f$ x_{m+1}=\dots = x_N = 0 \f$ with function value 0.
44  *
45  * This is a simple benchmark for evolutionary algorithms as, the closer the algorithm is to the optimu
46  */
48 
49  ConstrainedSphere(std::size_t numberOfVariables = 5, std::size_t m = 1)
50  :m_numberOfVariables(numberOfVariables), m_constraints(m) {
54  }
55 
56  /// \brief From INameable: return the class name.
57  std::string name() const
58  { return "ConstrainedSphere"; }
59 
60  std::size_t numberOfVariables()const{
61  return m_numberOfVariables;
62  }
63 
65  return true;
66  }
67 
69  m_numberOfVariables = numberOfVariables;
70  }
71 
73  RealVector x(numberOfVariables());
74 
75  for (std::size_t i = 0; i < m_constraints; i++) {
76  x(i) = std::abs(random::gauss(*mep_rng, 0, 1))+1;
77  }
78  for (std::size_t i = m_constraints; i < x.size(); i++) {
79  x(i) = random::gauss(0, 1);
80  }
81  return x;
82  }
83 
84  bool isFeasible( SearchPointType const& input) const {
85  for (std::size_t i = 0; i < m_constraints; i++) {
86  if(input(i) < 1) return false;
87  }
88  return true;
89  }
90 
91  double eval(const SearchPointType &p) const {
93  return norm_sqr(p)-m_constraints;
94  }
95 private:
96  std::size_t m_numberOfVariables;
97  std::size_t m_constraints;
98 };
99 
100 }
101 
102 #endif