GALib-Parallel
Header-only C++ library for parallel evolutionary algorithms
Loading...
Searching...
No Matches
HimmelblauFunction.h
Go to the documentation of this file.
1#ifndef HIMMELBLAU_FUNCTION_H
2#define HIMMELBLAU_FUNCTION_H
3
4#pragma once
5
7#include <vector>
8#include <stdexcept>
9#include <cstddef>
10#include <string>
11
12namespace galib::benchmark {
13
22 template <typename GeneType = double>
23 class HimmelblauFitness : public FitnessFunction<GeneType> {
24 private:
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;
28
29 public:
30 HimmelblauFitness() = default;
31
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())
43 );
44 }
45
46 const auto x = static_cast<double>(phenotype[0]);
47 const auto y = static_cast<double>(phenotype[1]);
48
49 const double term1 = (x * x) + y - 11.0;
50 const double term2 = x + (y * y) - 7.0;
51
52 return (term1 * term1) + (term2 * term2);
53 }
54
55 [[nodiscard]] std::size_t size() const{
56 return DIMENSIONS_M;
57 }
58
59 GeneType getLowerBound(const std::size_t dimension) const override {
60 return static_cast<GeneType>(LOWER_BOUND_M);
61 }
62
63 GeneType getUpperBound(const std::size_t dimension) const override {
64 return static_cast<GeneType>(UPPER_BOUND_M);
65 }
66 };
67
68} // namespace galib
69
70#endif // HIMMELBLAU_FUNCTION_H
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.
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.