GALib-Parallel
Header-only C++ library for parallel evolutionary algorithms
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
galib::FunctionalFitness< GeneType > Class Template Reference

A wrapper for FitnessFunction that allows using a lambda or std::function. More...

#include <FunctionalFitness.h>

Inheritance diagram for galib::FunctionalFitness< GeneType >:
[legend]

Public Types

using EvalFunc = std::function< double(const std::vector< GeneType > &)>
 

Public Member Functions

 FunctionalFitness (std::size_t dims, GeneType lower, GeneType upper, EvalFunc func)
 Constructor for uniform bounds.
 
 FunctionalFitness (std::size_t dims, std::vector< GeneType > lowers, std::vector< GeneType > uppers, EvalFunc func)
 Constructor for non-uniform bounds.
 
double evaluate (const std::vector< GeneType > &phenotype) const override
 Evaluates the fitness of a candidate solution using the wrapped function.
 
std::size_t size () const override
 Returns the problem dimensionality.
 
GeneType getLowerBound (std::size_t dimension) const override
 Gets the lower bound for a specific dimension.
 
GeneType getUpperBound (std::size_t dimension) const override
 Gets the upper bound for a specific dimension.
 
- Public Member Functions inherited from galib::FitnessFunction< GeneType >
virtual ~FitnessFunction ()=default
 
virtual std::string name () const
 Short identifier used to select a GPU evaluation kernel.
 
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.
 

Detailed Description

template<typename GeneType = double>
class galib::FunctionalFitness< GeneType >

A wrapper for FitnessFunction that allows using a lambda or std::function.

This class eliminates the need for users to create a new class for every custom optimization problem. It supports both uniform bounds (same for all dimensions) and non-uniform bounds.

Template Parameters
GeneTypeThe type of the genes (default is double).

Definition at line 24 of file FunctionalFitness.h.

Member Typedef Documentation

◆ EvalFunc

template<typename GeneType = double>
using galib::FunctionalFitness< GeneType >::EvalFunc = std::function<double(const std::vector<GeneType>&)>

Definition at line 26 of file FunctionalFitness.h.

Constructor & Destructor Documentation

◆ FunctionalFitness() [1/2]

template<typename GeneType = double>
galib::FunctionalFitness< GeneType >::FunctionalFitness ( std::size_t  dims,
GeneType  lower,
GeneType  upper,
EvalFunc  func 
)
inline

Constructor for uniform bounds.

Parameters
dimsNumber of dimensions.
lowerLower bound applied to all dimensions.
upperUpper bound applied to all dimensions.
funcEvaluation callable (lambda, function pointer, std::function).

Definition at line 38 of file FunctionalFitness.h.

◆ FunctionalFitness() [2/2]

template<typename GeneType = double>
galib::FunctionalFitness< GeneType >::FunctionalFitness ( std::size_t  dims,
std::vector< GeneType >  lowers,
std::vector< GeneType >  uppers,
EvalFunc  func 
)
inline

Constructor for non-uniform bounds.

Parameters
dimsNumber of dimensions.
lowersVector of lower bounds per dimension.
uppersVector of upper bounds per dimension.
funcEvaluation callable.
Exceptions
std::invalid_argumentif bounds vector sizes do not match dims.

Definition at line 56 of file FunctionalFitness.h.

Member Function Documentation

◆ evaluate()

template<typename GeneType = double>
double galib::FunctionalFitness< GeneType >::evaluate ( const std::vector< GeneType > &  phenotype) const
inlineoverridevirtual

Evaluates the fitness of a candidate solution using the wrapped function.

Parameters
phenotypeGene values of the individual.
Returns
Fitness score.

Implements galib::FitnessFunction< GeneType >.

Definition at line 75 of file FunctionalFitness.h.

◆ getLowerBound()

template<typename GeneType = double>
GeneType galib::FunctionalFitness< GeneType >::getLowerBound ( std::size_t  dimension) const
inlineoverridevirtual

Gets the lower bound for a specific dimension.

Parameters
dimensionIndex of the dimension.
Returns
Lower bound value.

Implements galib::FitnessFunction< GeneType >.

Definition at line 92 of file FunctionalFitness.h.

◆ getUpperBound()

template<typename GeneType = double>
GeneType galib::FunctionalFitness< GeneType >::getUpperBound ( std::size_t  dimension) const
inlineoverridevirtual

Gets the upper bound for a specific dimension.

Parameters
dimensionIndex of the dimension.
Returns
Upper bound value.

Implements galib::FitnessFunction< GeneType >.

Definition at line 101 of file FunctionalFitness.h.

◆ size()

template<typename GeneType = double>
std::size_t galib::FunctionalFitness< GeneType >::size ( ) const
inlineoverridevirtual

Returns the problem dimensionality.

Returns
Number of dimensions.

Implements galib::FitnessFunction< GeneType >.

Definition at line 83 of file FunctionalFitness.h.


The documentation for this class was generated from the following file: