GALib-Parallel
Header-only C++ library for parallel evolutionary algorithms
Loading...
Searching...
No Matches
RastriginFunction.h
Go to the documentation of this file.
1#ifndef RASTRIGIN_FUNCTION_H
2#define RASTRIGIN_FUNCTION_H
3
4#pragma once
5
6/*
7 * @file RastriginFunction.h
8 * Global Minimum: The function has a unique global minimum at the origin
9 * x = (0, 0, ..., 0), where f(x) = 0.
10 * Local Minimum: It is highly multi-modal. Due to the cosine component, local
11 * minima are located roughly at all points where each coordinate is an integer
12 * (e.g., x_i ≈ ±1, ±2, ±3, ...).
13 * Search Space: Usually evaluated on the hypercube x_i ∈ [-5.12, 5.12].
14 */
15
17#include <vector>
18#include <cmath>
19#include <cstddef>
20#include <numbers>
21
22namespace galib::benchmark {
23
32 template <typename GeneType = double>
33 class RastriginFunction : public FitnessFunction<GeneType> {
34 private:
35 std::size_t dimensions_m;
36 GeneType lower_bound_m;
37 GeneType upper_bound_m;
38 static constexpr double A = 10.0;
39
40 public:
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) {}
49
55 double evaluate(const std::vector<GeneType>& genotype) const override {
56 double sum = A * dimensions_m;
57
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));
61 }
62
63 return sum;
64 }
65
66 std::size_t size() const override { return dimensions_m; }
67 std::string name() const override { return "Rastrigin"; }
68
69 GeneType getLowerBound(std::size_t) const override {
70 return lower_bound_m;
71 }
72
73 GeneType getUpperBound(std::size_t) const override {
74 return upper_bound_m;
75 }
76 };
77
78}
79
80#endif // RASTRIGIN_FUNCTION_H
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.