1#ifndef RASTRIGIN_FUNCTION_H
2#define RASTRIGIN_FUNCTION_H
32 template <
typename GeneType =
double>
35 std::size_t dimensions_m;
36 GeneType lower_bound_m;
37 GeneType upper_bound_m;
38 static constexpr double A = 10.0;
47 explicit RastriginFunction(std::size_t dimensions, GeneType lower_bound, GeneType upper_bound)
48 : dimensions_m(dimensions), lower_bound_m(lower_bound), upper_bound_m(upper_bound) {}
55 double evaluate(
const std::vector<GeneType>& genotype)
const override {
56 double sum = A * dimensions_m;
58 for (GeneType gene : genotype) {
59 double x =
static_cast<double>(gene);
60 sum += (x * x - A * std::cos(2.0 * std::numbers::pi * x));
66 std::size_t
size()
const override {
return dimensions_m; }
67 std::string
name()
const override {
return "Rastrigin"; }
Abstract base class for objective functions to be minimized.
Implementation of the Rastrigin function.
double evaluate(const std::vector< GeneType > &genotype) const override
Evaluates the N-dimensional Rastrigin function.
GeneType getLowerBound(std::size_t) const override
Gets the lower bound for a specific dimension.
std::string name() const override
Short identifier used to select a GPU evaluation kernel.
std::size_t size() const override
Returns the dimensionality of the optimization problem.
GeneType getUpperBound(std::size_t) const override
Gets the upper bound for a specific dimension.
RastriginFunction(std::size_t dimensions, GeneType lower_bound, GeneType upper_bound)
Constructs the Rastrigin function with given dimensions and bounds.