GALib-Parallel
Header-only C++ library for parallel evolutionary algorithms
Loading...
Searching...
No Matches
SphereFunction.h
Go to the documentation of this file.
1#ifndef SPHERE_FUNCTION_H
2#define SPHERE_FUNCTION_H
3
4#pragma once
5
6/*
7 * @file SphereFunction.h
8 * - Global Minimum: It has a single global minimum at the origin
9 * x = (0, 0, ..., 0), where f(x) = 0.
10 * - Local Minimum: There are NO local minimum other than the global one.
11 * - Shape: Geometrically, it represents an n-dimensional paraboloid.
12 */
13
15#include <vector>
16
17namespace galib::benchmark {
18
27 template <typename GeneType = double>
28 class SphereFunction : public FitnessFunction<GeneType> {
29 private:
30 std::size_t dimensions;
31 GeneType lower_bound;
32 GeneType upper_bound;
33 public:
40 explicit SphereFunction(const std::size_t dim, GeneType lb, GeneType ub)
41 : dimensions(dim), lower_bound(lb), upper_bound(ub) {}
42
48 double evaluate(const std::vector<GeneType>& phenotype) const override {
49 double sum = 0;
50 for (GeneType gene : phenotype) {
51 sum += static_cast<double>(gene * gene);
52 }
53 return sum;
54 }
55
56 std::size_t size() const override { return dimensions; }
57 std::string name() const override { return "Sphere"; }
58
59 GeneType getLowerBound(std::size_t) const override { return lower_bound; }
60 GeneType getUpperBound(std::size_t) const override { return upper_bound; }
61 };
62
63}
64
65#endif // SPHERE_FUNCTION_H
Abstract base class for objective functions to be minimized.
Implementation of the Sphere function.
std::size_t size() const override
Returns the dimensionality of the optimization problem.
double evaluate(const std::vector< GeneType > &phenotype) const override
Evaluates the N-dimensional Sphere 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.
GeneType getUpperBound(std::size_t) const override
Gets the upper bound for a specific dimension.
SphereFunction(const std::size_t dim, GeneType lb, GeneType ub)
Constructs the Sphere function with given dimensions and bounds.