1#ifndef FUNCTIONAL_FITNESS_H
2#define FUNCTIONAL_FITNESS_H
23 template <
typename GeneType =
double>
26 using EvalFunc = std::function<double(
const std::vector<GeneType>&)>;
43 ) : dimensions_m(dims),
44 lower_bounds_m(dims, lower),
45 upper_bounds_m(dims, upper),
46 evaluator_m(std::move(func)) {}
58 std::vector<GeneType> lowers,
59 std::vector<GeneType> uppers,
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");
75 [[nodiscard]]
double evaluate(
const std::vector<GeneType>& phenotype)
const override {
76 return evaluator_m(phenotype);
83 [[nodiscard]] std::size_t
size()
const override {
92 [[nodiscard]] GeneType
getLowerBound(std::size_t dimension)
const override {
93 return lower_bounds_m.at(dimension);
101 [[nodiscard]] GeneType
getUpperBound(std::size_t dimension)
const override {
102 return upper_bounds_m.at(dimension);
106 std::size_t dimensions_m;
107 std::vector<GeneType> lower_bounds_m;
108 std::vector<GeneType> upper_bounds_m;
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.