GALib-Parallel
Header-only C++ library for parallel evolutionary algorithms
Loading...
Searching...
No Matches
HeavyTrigFunction.h
Go to the documentation of this file.
1#ifndef HEAVY_TRIG_FUNCTION_H
2#define HEAVY_TRIG_FUNCTION_H
3
4#pragma once
5
7
8#include <cstddef>
9#include <cmath>
10#include <stdexcept>
11#include <string>
12#include <vector>
13
14namespace galib::benchmark {
15
24template <typename GeneType = double>
25class HeavyTrigFunction : public FitnessFunction<GeneType> {
26private:
27 std::size_t dimensions_m;
28 GeneType lower_bound_m;
29 GeneType upper_bound_m;
30 std::size_t repeat_count_m;
31
32public:
40 explicit HeavyTrigFunction(std::size_t dimensions,
41 GeneType lower_bound,
42 GeneType upper_bound,
43 std::size_t repeat_count = 64)
44 : dimensions_m(dimensions),
45 lower_bound_m(lower_bound),
46 upper_bound_m(upper_bound),
47 repeat_count_m(repeat_count) {}
48
54 double evaluate(const std::vector<GeneType>& phenotype) const override {
55 if (phenotype.size() != dimensions_m) {
56 throw std::invalid_argument(
57 "HeavyTrig requires exactly " + std::to_string(dimensions_m) +
58 " dimensions, but received " + std::to_string(phenotype.size())
59 );
60 }
61
62 double sum = 0.0;
63 for (std::size_t i = 0; i < phenotype.size(); ++i) {
64 const double x = static_cast<double>(phenotype[i]);
65 double state = x;
66 for (std::size_t r = 1; r <= repeat_count_m; ++r) {
67 const double factor = static_cast<double>(r);
68 const double angle = factor * 0.017 + x * 0.031;
69 state = std::sin(state + angle)
70 + std::cos(state - angle * 0.5)
71 + std::exp(-0.001 * state * state)
72 + 0.0005 * state * state;
73 }
74 sum += state * state + 0.1 * x * x;
75 }
76 return sum;
77 }
78
79 [[nodiscard]] std::size_t size() const override { return dimensions_m; }
80 [[nodiscard]] std::string name() const override { return "HeavyTrig"; }
81
82 GeneType getLowerBound(std::size_t) const override {
83 return lower_bound_m;
84 }
85
86 GeneType getUpperBound(std::size_t) const override {
87 return upper_bound_m;
88 }
89};
90
91} // namespace galib::benchmark
92
93#endif // HEAVY_TRIG_FUNCTION_H
Abstract base class for objective functions to be minimized.
A computationally intensive trigonometric benchmark function.
GeneType getUpperBound(std::size_t) const override
Gets the upper bound for a specific dimension.
HeavyTrigFunction(std::size_t dimensions, GeneType lower_bound, GeneType upper_bound, std::size_t repeat_count=64)
Constructs the benchmark.
std::string name() const override
Short identifier used to select a GPU evaluation kernel.
GeneType getLowerBound(std::size_t) const override
Gets the lower bound for a specific dimension.
std::size_t size() const override
Returns the dimensionality of the optimization problem.
double evaluate(const std::vector< GeneType > &phenotype) const override
Evaluates the heavy trigonometric sum.