GALib-Parallel
Header-only C++ library for parallel evolutionary algorithms
Loading...
Searching...
No Matches
DeJongF5Function.h
Go to the documentation of this file.
1#ifndef DEJONG_F5_FUNCTION_H
2#define DEJONG_F5_FUNCTION_H
3
4#pragma once
5
7#include <vector>
8#include <stdexcept>
9#include <cstddef>
10#include <string>
11#include <cmath>
12
14
23 template <typename GeneType = double>
24 class DeJongF5Function : public FitnessFunction<GeneType> {
25 private:
26 static constexpr std::size_t DIMENSIONS_M = 2;
27 static constexpr double LOWER_BOUND_M = -50;
28 static constexpr double UPPER_BOUND_M = 50;
29
30 public:
31 DeJongF5Function() = default;
32 ~DeJongF5Function() override = default;
33
40 double evaluate(const std::vector<GeneType>& phenotype) const override {
41 if (phenotype.size() != 2) {
42 throw std::invalid_argument(
43 "De Jong F5 requires exactly 2 dimensions, but received " +
44 std::to_string(phenotype.size())
45 );
46 }
47
48 static constexpr double a[2][25] = {
49 {-32, -16, 0, 16, 32, -32, -16, 0, 16, 32, -32, -16, 0, 16, 32, -32, -16, 0, 16, 32, -32, -16, 0, 16, 32},
50 {-32, -32, -32, -32, -32, -16, -16, -16, -16, -16, 0, 0, 0, 0, 0, 16, 16, 16, 16, 16, 32, 32, 32, 32, 32}
51 };
52
53 const auto x = static_cast<double>(phenotype[0]);
54 const auto y = static_cast<double>(phenotype[1]);
55
56 double sum = 0.0;
57 for (int i = 0; i < 25; ++i) {
58 const double term1 = std::pow(x - a[0][i], 6);
59 const double term2 = std::pow(y - a[1][i], 6);
60 sum += 1.0 / (static_cast<double>(i + 1) + term1 + term2);
61 }
62
63 return 1.0 / (0.002 + sum);
64 }
65
66 [[nodiscard]] std::size_t size() const override {
67 return DIMENSIONS_M;
68 }
69
70 GeneType getLowerBound(std::size_t dimension) const override {
71 return static_cast<GeneType>(LOWER_BOUND_M);
72 }
73
74 GeneType getUpperBound(std::size_t dimension) const override {
75 return static_cast<GeneType>(UPPER_BOUND_M);
76 }
77 };
78
79} // namespace galib
80
81#endif // DEJONG_F5_FUNCTION_H
Abstract base class for objective functions to be minimized.
Implementation of De Jong's Function F5 (Shekel's Foxholes).
GeneType getLowerBound(std::size_t dimension) const override
Gets the lower bound for a specific dimension.
GeneType getUpperBound(std::size_t dimension) const override
Gets the upper bound for a specific dimension.
double evaluate(const std::vector< GeneType > &phenotype) const override
Evaluates the foxholes function.
~DeJongF5Function() override=default
std::size_t size() const override
Returns the dimensionality of the optimization problem.