1#ifndef HIMMELBLAU_FUNCTION_H
2#define HIMMELBLAU_FUNCTION_H
22 template <
typename GeneType =
double>
25 static constexpr std::size_t DIMENSIONS_M = 2;
26 static constexpr double LOWER_BOUND_M = -5.0;
27 static constexpr double UPPER_BOUND_M = 5.0;
38 double evaluate(
const std::vector<GeneType>& phenotype)
const override {
39 if (phenotype.size() != DIMENSIONS_M) {
40 throw std::invalid_argument(
41 "Himmelblau's function requires exactly 2 dimensions, but received " +
42 std::to_string(phenotype.size())
46 const auto x =
static_cast<double>(phenotype[0]);
47 const auto y =
static_cast<double>(phenotype[1]);
49 const double term1 = (x * x) + y - 11.0;
50 const double term2 = x + (y * y) - 7.0;
52 return (term1 * term1) + (term2 * term2);
55 [[nodiscard]] std::size_t
size()
const{
60 return static_cast<GeneType
>(LOWER_BOUND_M);
64 return static_cast<GeneType
>(UPPER_BOUND_M);
Abstract base class for objective functions to be minimized.
Implementation of Himmelblau's function.
std::size_t size() const
Returns the dimensionality of the optimization problem.
GeneType getLowerBound(const std::size_t dimension) const override
Gets the lower bound for a specific dimension.
HimmelblauFitness()=default
double evaluate(const std::vector< GeneType > &phenotype) const override
Evaluates the 2D Himmelblau function.
GeneType getUpperBound(const std::size_t dimension) const override
Gets the upper bound for a specific dimension.