GALib-Parallel
Header-only C++ library for parallel evolutionary algorithms
Loading...
Searching...
No Matches
FunctionalFitness.h
Go to the documentation of this file.
1#ifndef FUNCTIONAL_FITNESS_H
2#define FUNCTIONAL_FITNESS_H
3
4#pragma once
5
7#include <functional>
8#include <vector>
9#include <cstddef>
10#include <utility>
11
12namespace galib {
13
23 template <typename GeneType = double>
24 class FunctionalFitness : public FitnessFunction<GeneType> {
25 public:
26 using EvalFunc = std::function<double(const std::vector<GeneType>&)>;
27
28 using FitnessFunction<GeneType>::getLowerBound;
29 using FitnessFunction<GeneType>::getUpperBound;
30
39 std::size_t dims,
40 GeneType lower,
41 GeneType upper,
42 EvalFunc func
43 ) : dimensions_m(dims),
44 lower_bounds_m(dims, lower),
45 upper_bounds_m(dims, upper),
46 evaluator_m(std::move(func)) {}
47
57 std::size_t dims,
58 std::vector<GeneType> lowers,
59 std::vector<GeneType> uppers,
60 EvalFunc func
61 ) : dimensions_m(dims),
62 lower_bounds_m(std::move(lowers)),
63 upper_bounds_m(std::move(uppers)),
64 evaluator_m(std::move(func)) {
65 if (lower_bounds_m.size() != dims || upper_bounds_m.size() != dims) {
66 throw std::invalid_argument("Bounds vectors must match dimensions size");
67 }
68 }
69
75 [[nodiscard]] double evaluate(const std::vector<GeneType>& phenotype) const override {
76 return evaluator_m(phenotype);
77 }
78
83 [[nodiscard]] std::size_t size() const override {
84 return dimensions_m;
85 }
86
92 [[nodiscard]] GeneType getLowerBound(std::size_t dimension) const override {
93 return lower_bounds_m.at(dimension);
94 }
95
101 [[nodiscard]] GeneType getUpperBound(std::size_t dimension) const override {
102 return upper_bounds_m.at(dimension);
103 }
104
105 private:
106 std::size_t dimensions_m;
107 std::vector<GeneType> lower_bounds_m;
108 std::vector<GeneType> upper_bounds_m;
109 EvalFunc evaluator_m;
110 };
111
112} // namespace galib
113
114#endif // FUNCTIONAL_FITNESS_H
Abstract base class for objective functions to be minimized.
GeneType getLowerBound() const
Convenience method to get the lower bound of the first dimension.
GeneType getUpperBound() const
Convenience method to get the upper bound of the first dimension.
A wrapper for FitnessFunction that allows using a lambda or std::function.
std::size_t size() const override
Returns the problem dimensionality.
std::function< double(const std::vector< GeneType > &)> EvalFunc
FunctionalFitness(std::size_t dims, GeneType lower, GeneType upper, EvalFunc func)
Constructor for uniform bounds.
GeneType getLowerBound(std::size_t dimension) const override
Gets the lower bound for a specific dimension.
double evaluate(const std::vector< GeneType > &phenotype) const override
Evaluates the fitness of a candidate solution using the wrapped function.
FunctionalFitness(std::size_t dims, std::vector< GeneType > lowers, std::vector< GeneType > uppers, EvalFunc func)
Constructor for non-uniform bounds.
GeneType getUpperBound(std::size_t dimension) const override
Gets the upper bound for a specific dimension.