xevo C++ API documentation¶
Analytical functions¶
-
struct
xevo::Branin¶ branin function modified by Forrester et al. 2006
\[\begin{split} f(x) = ( x_2 - \frac{5.1}{4\pi^2}x_2 + \frac{5}{\pi}x_1 - 6 )^2 + 10\left[ (1 - \frac{1}{8\pi})\cos{x_1} + 1 \right] + 5x_1\\ with \quad x_1 \in \left[-5,10\right], x_2 \in \left[0,15\right] \end{split}\]Public Functions
-
struct
xevo::Rosenbrock¶ Rosenbrock’s function.
\[ f(x_1, x_2) = 100*(x_1^2 - x_2)^2 + (1 - x_1)^2 \quad with \quad \mathbf{X} \in \left[-3, 3 \right] \]Public Functions
-
template<class
E, typenameT= typename std::decay_t<E>::value_type>
autooperator()(const xt::xexpression<E> &X)¶ operator to evaluate the objective function.
- Return
auto evaluated array
- Template Parameters
E: xtensor typevalue_type: xtensor value type
- Parameters
X: array to be evaluated
-
std::pair<std::vector<double>, std::vector<double>>
bounder() const¶ get the bounder of Rosenbrock function
- Return
std::pair<std::vector<double>, std::vector<double>>
-
template<class
-
struct
xevo::Rosenbrock_scaled¶ Rosenbrock’s function.
\[ f(x_1, x_2) = 100*(x_1^2 - x_2)^2 + (1 - x_1)^2 \quad with \quad \mathbf{X} \in \left[-3, 3 \right] \]Public Functions
-
template<class
E, typenameT= typename std::decay_t<E>::value_type>
autooperator()(const xt::xexpression<E> &X)¶ operator to evaluate the objective function.
- Return
auto evaluated array
- Template Parameters
E: xtensor typevalue_type: xtensor value type
- Parameters
X: array to be evaluated
-
std::pair<std::vector<double>, std::vector<double>>
bounder() const¶ get the bounder of Rosenbrock function
- Return
std::pair<std::vector<double>, std::vector<double>>
-
template<class
-
struct
xevo::Sphere¶ Sphere function.
\[ f(x_1, x_2) = x_1^2 + x_2^2 + 1 \quad with \quad \mathbf{X} \in \left[-1, 1 \right] \]Public Functions
-
template<class
E, typenameT= typename std::decay_t<E>::value_type>
autooperator()(const xt::xexpression<E> &X)¶ operator to evaluate the objective function.
- Return
auto evaluated array
- Template Parameters
E: xtensor typevalue_type: xtensor value type
- Parameters
X: array to be evaluated
-
std::pair<std::vector<double>, std::vector<double>>
bounder() const¶ get the bounder of Rosenbrock function
- Return
std::pair<std::vector<double>, std::vector<double>>
-
template<class
-
struct
xevo::Rastriginsfcn¶ Rastrigin’s function.
The Rastrigin’s function is expressed as
\[ f(x_1, x_2) = 20 + x_1^2 + x_2^2 - 10(cos(2\pi x_1) + cos(2\pi x_2)) \quad with \quad \mathbf{X} \in \left[-5, 5 \right] \]Public Functions
-
template<class
E, typenameT= typename std::decay_t<E>::value_type>
autooperator()(const xt::xexpression<E> &X)¶ operator to evaluate the objective function.
- Return
auto evaluated array
- Template Parameters
E: xtensor typevalue_type: xtensor value type
- Parameters
X: array to be evaluated
-
std::pair<std::vector<double>, std::vector<double>>
bounder() const¶ get the bounder of Rastrigin function
- Return
std::pair<std::vector<double>, std::vector<double>>
-
template<class
-
struct
xevo::Rastriginsfcn_scaled¶ Rastrigin’s function scaled.
The Rastrigin’s function is expressed as
\[ f(x_1, x_2) = 20 + x_1^2 + x_2^2 - 10(cos(2\pi x_1) + cos(2\pi x_2)) \quad with \quad \mathbf{X} \in \left[-5, 5 \right] \]This function scales the Rastrigin’s function as
\[ f_{scaled}(x_1, x_2) = e^{-\frac{\beta}{max(f(x_1, x_2))} f(x_1, x_2)} \]Public Functions
-
template<class
E, typenameT= typename std::decay_t<E>::value_type>
autooperator()(const xt::xexpression<E> &X)¶ operator to evaluate the objective function.
- Return
auto evaluated array
- Template Parameters
E: xtensor typevalue_type: xtensor value type
- Parameters
X: array to be evaluated
-
std::pair<std::vector<double>, std::vector<double>>
bounder() const¶ get the bounder of Rastrigin function
- Return
std::pair<std::vector<double>, std::vector<double>>
-
template<class
Functors¶
-
struct
Population¶ functor for generating initial population
-
struct
Roulette_selection¶ functor to calculate individual selection with Roulette method.
-
struct
xevo::Crossover¶ Cross over functor.
This functor calculates for single arithmetic crossover For two parents:
\[ X_1: {x_1^1, x_1^2, ... , x_1^k, x_1^{k+1}, ..., x_1^n} \]\[ X_2: {x_2^1, x_2^2, ... , x_2^k, x_2^{k+1}, ..., x_2^n} \]Peak random gene at \({k}\). Then the two children become:
\[ X_1: {x_1^1, x_1^2, ... , \alpha x_2^k + (1 - \alpha)x_1^k, x_1^{k+1}, ..., x_1^n} \]\[ X_2: {x_2^1, x_2^2, ... , \alpha x_1^k + (1 - \alpha)x_2^k, x_2^{k+1}, ..., x_2^n} \]Public Functions
-
Crossover(double crossoverrate)¶ Constructor.
- Parameters
crossoverrate: cross over rate
-
-
struct
xevo::Mutation_polynomial¶ Functor for polynomial mutation.
for a given parent solution \( p \in \left[ a, b \right] \), the mutated solution \( p^{'} \) for a particular variable is created for a random number \( u \in \left[ 0, 1 \right]\)
\[\begin{split} p^{'} = \begin{cases} p + \bar{\delta_L}\left( p - x_i^{(L)} \right), for \quad u \leq 0.5, \\ p + \bar{\delta_R}\left( x_i^{(U)} - p \right), for \quad u > 0.5, \end{cases} \end{split}\]Then \(\bar{\delta_L}\) and \(\bar{\delta_R}\) are calculated as
\[\begin{split} \bar{\delta_L} = (2u)^{1/(1+ \eta_m)} - 1, for \quad u \leq 0.5, \\ \bar{\delta_R} = 1 - (2(1-u))^{1/(1+\eta_m)}, for \quad u > 0.5 \end{split}\]Public Functions
-
Mutation_polynomial(double mr, double eta_m)¶ Construct a new Mutation_functor_polynomial object.
- Parameters
mr: : mutation rateeta_m: index parameter
-
-
struct
xevo::Elitism¶ Functor for elitism.
Public Functions
-
Elitism(double er, bool maximise = true)¶ Constructor.
- Parameters
er: elit rate
-
-
struct
Position¶ Functor for calculating position at t + 1.
\[ X_{i,j}^(t+1) = X_{i,j}^(t) + V_{i,j}^(t+1) \]
-
struct
Velocity¶ Functor to calculate the velocity at the next iteration.
\[ V_{ij}^{t+1} = \omega V_{ij}^t + c_1 r_1^t \left( pbestX_{ij} - X_{ij}^t \right) + c_2 r_2^t \left( gbestx_j - X_{ij}^t \right) \]
-
struct
Velocity_zero¶ Functor for initialising velocity vector to 0.
-
struct
Velocity_ring_topology¶ Functor to calculate the velocity with ring topology at the next iteration.
\[ V_{ij}^{t+1} = \omega V_{ij}^t + c_1 r_1^t \left( pbestX_{ij} - X_{ij}^t \right) + c_2 r_2^t \left( ringbestX_{ij} - X_{ij}^t \right) \]
-
struct
Velocity_cf_ring_topology¶ Functor to calculate the velocity with ring topology at the next iteration.
M. Clerc and J. Kennedy, The particle swarm - explosion, stability, and convergence in a multidimensional complex space, Evolutionary Computation, IEEE Transactions on, vol. 6, no. 1, pp.58-73, Feb 2002.
\[ V_{ij}^{t+1} = \chi \left( \omega V_{ij}^t + c_1 r_1^t \left( pbestX_{ij} - X_{ij}^t \right) + c_2 r_2^t \left( ringbestX_{ij} - X_{ij}^t \right) \right) \]
-
struct
Position_pso_ga¶ Functor to calculate the velocity at the next iteration.
\[ \mathbf{x}_i^{t + 1} = \mathbf{x}_i^{t} + w \left( \mathbf{x}_i^{t} - \mathbf{x}_i^{t-1} \right) + c_1 r_1 \left( \mathbf{p}_{b, i}^t - \mathbf{x}_i^{t} \right) + c_2 r_2 \left( \mathbf{p}_g^t - \mathbf{x}_i^{t} \right) \]
-
struct
Selection_best_pso¶ Functor for selecting the best solutions for pso.
-
struct
Selection_best_pso_ga¶ functor for selecting the best and archive it.
-
struct
xevo::Terminate_gen_max¶ Functor for terminating evolutionary algorithm.
Public Functions
-
Terminate_gen_max(std::size_t generations, std::size_t index)¶ Construct a new Terminate_gen_max object.
- Parameters
generations: maximum number of generationsindex: current generation number
-
-
struct
Terminate_tol¶ Functor for terminating evolutionary algorithm by setting a tolerance (y_best_gen - y_best_gen_n <= tol)
Evolutionary algorithms¶
-
class
xevo::ga¶ class for genetic algorithm
Public Functions
-
template<class
E, classPOP= Population, typename ...PopArgs, typenameT= typename std::decay_t<E>::value_type>
voidinitialise(xt::xexpression<E> &X, std::tuple<PopArgs...> popargs = std::make_tuple())¶ method to initialise population for ga
- Template Parameters
E: xtensor type for generating the initial populationPOP: functor for generating the populationPopArgs: optional type of arguments for population functorT: value type of xtensor
- Parameters
X: array of populationpopargs: optional arguments for population functor
-
template<class
E, classOBJ, classELIT= Elitism, classSEL= Roulette_selection, classCROSS= Crossover, classMUT= Mutation_polynomial, typename ...ElitArgs, typename ...SelArgs, typename ...CrossArgs, typename ...MutArgs, typenameT= typename std::decay_t<E>::value_type>
voidevolve(xt::xexpression<E> &X, OBJ objective_f, std::tuple<ElitArgs...> elitargs, std::tuple<SelArgs...> selargs, std::tuple<CrossArgs...> crossargs, std::tuple<MutArgs...> mutargs)¶ method to evolve the population
- Template Parameters
E: xtensor type for population at the current generation.OBJ: functor for objective functionELIT: functor for elitismSEL: functor for selectionCROSS: functor for crossoverMUT: functor for mutationElitArgs: argument types for Elit functorSelArgs: types of arguments for Selection functorCrossArgs: types of arguments for cross over functorMutArgs: types of arguments for mutation functorT: value type of xtensor
- Parameters
X: array with population at current evolutionobjective_f: objective functionelitargs: function for elitismselargs: function for selectioncrossargs: function for crossovermutargs: function for mutation
-
template<class
E, classOBJ, classELIT= Elitism, classSEL= Roulette_selection, classCROSS= Crossover, classMUT= Mutation_polynomial, classTERM= Terminate_gen_max, typename ...ElitArgs, typename ...SelArgs, typename ...CrossArgs, typename ...MutArgs, typename ...TermArgs, typenameT= typename std::decay_t<E>::value_type>
autoevolve(xt::xexpression<E> &X, OBJ objective_f, std::tuple<ElitArgs...> elitargs, std::tuple<SelArgs...> selargs, std::tuple<CrossArgs...> crossargs, std::tuple<MutArgs...> mutargs, std::tuple<TermArgs...> termargs)¶ method to evolve the population
- Return
auto type from terminating functor (auto TERM::operator<E, F>(E X, F objective_f(Y)))
- Template Parameters
E: xtensor type for population at the current generation.OBJ: functor for objective functionELIT: functor for elitismSEL: functor for selectionCROSS: functor for crossoverMUT: functor for mutationTERM: functor for ga terminationElitArgs: argument types for Elit functorSelArgs: types of arguments for Selection functorCrossArgs: types of arguments for cross over functorMutArgs: types of arguments for mutation functorTermArgs: types of arguments for terminating functorT: value type of xtensor
- Parameters
X: array with population at current evolutionobjective_f: objective functionelitargs: arguments for elitism functorselargs: arguments for selection functorcrossargs: arguments for crossover functormutargs: arguments for mutation functortermargs: arguments for terminating functor
-
template<class
Swarm Intelligence algorithms¶
-
class
xevo::pso¶ Public Functions
-
template<class
E, classPOS= Population, typename ...PosArgs, typenameT= typename std::decay_t<E>::value_type>
voidinitialise(xt::xexpression<E> &X, std::tuple<PosArgs...> posargs = std::make_tuple())¶ method to initialise position or velocity of swarm for pso
- Template Parameters
E: xtensor type for position and velocity initial vectorsPOS: functor type for generating the initial positionsPosArgs: type of arguments for initialising position functorT: value type of xtensor
- Parameters
X: array of initial bird positionsposargs: (optional) arguments for position functor
-
template<class
E, classPOS= Population, classVEL= Velocity_zero, typename ...PosArgs, typename ...VelArgs, typenameT= typename std::decay_t<E>::value_type>
voidinitialise(xt::xexpression<E> &X, xt::xexpression<E> &V, std::tuple<PosArgs...> posargs = std::make_tuple(), std::tuple<VelArgs...> velargs = std::make_tuple())¶ method to initialise position and velocity of swarm for pso
- Template Parameters
E: xtensor type for position and velocity initial vectorsPOS: functor type for generating the initial positionsVEL: functor type for generating the initial velocityPosArgs: type of arguments for initialising position functorVelArgs: type of arguments for initialising velocity functorT: value type of xtensor
- Parameters
X: array of initial bird positionsV: array of initial bird velocitiesposargs: (optional) arguments for position functorvelargs: (optional) arguments for velocity functor
-
template<class
E, classF, classOBJ, classPOS= Position, classVEL= Velocity, classSEL= Selection_best_pso, typename ...PosArgs, typename ...VelArgs, typename ...SelArgs, typenameT= typename std::decay_t<E>::value_type>
voidevolve(xt::xexpression<E> &X, xt::xexpression<E> &XB, xt::xexpression<F> &YB, xt::xexpression<E> &V, OBJ objective_f, std::tuple<PosArgs...> posargs, std::tuple<VelArgs...> velargs, std::tuple<SelArgs...> selargs)¶ method to evolve bird positions of the swarm
- Template Parameters
E: xtensor type for input and output vectorsF: xtensor type for input and output vectors for evaluation bestOBJ: Functor type for objective function evaluationPOS: Functor type for position evaluationVEL: Functor type for velocity evaluationPosArgs: type of arguments for position evaluation functorVelArgs: type of arguments for velocity evaluation functorT: value type of xtensor
- Parameters
X: vector with initial positions of the swarmXB: vector with best positions of the individuals comprising the swarmYB: vector with best evaluations of the individuals comprising the swarmV: vector with initial velocities of the swarm individualsobjective_f: functor for objective function evaluationposargs: tuple with arguments for position functorvelargs: tuple with arguments for velocity functor
-
template<class
E, classF, classOBJ, classPOS= Position, classVEL= Velocity, classSEL= Selection_best_pso, classTERM= Terminate_gen_max, typename ...PosArgs, typename ...VelArgs, typename ...SelArgs, typename ...TermArgs, typenameT= typename std::decay_t<E>::value_type>
autoevolve(xt::xexpression<E> &X, xt::xexpression<E> &XB, xt::xexpression<F> &YB, xt::xexpression<E> &V, OBJ objective_f, std::tuple<PosArgs...> posargs, std::tuple<VelArgs...> velargs, std::tuple<SelArgs...> selargs, std::tuple<TermArgs...> termargs)¶ method to evolve bird positions of the swarm
- Template Parameters
E: xtensor type for input and output vectorsF: xtensor type for input and output vectors for evaluation bestOBJ: Functor type for objective function evaluationPOS: Functor type for position evaluationVEL: Functor type for velocity evaluationSEL: Functor type for selection best evaluationTERM: Functor type for termination of psoPosArgs: type of arguments for position evaluation functorVelArgs: type of arguments for velocity evaluation functorSelArgs: type of arguments for best selection evaluation functorT: value type of xtensor
- Parameters
X: vector with initial positions of the swarmXB: vector with best positions of the individuals comprising the swarmYB: vector with best evaluations of the individuals comprising the swarmV: vector with initial velocities of the swarm individualsobjective_f: functor for objective function evaluationposargs: tuple with arguments for position functorvelargs: tuple with arguments for velocity functorselargs: tuple with arguments for selection functortermargs: tuple with arguments for termination functor
-
template<class
Hybrid algorithms¶
-
class
xevo::pso_ga¶ class for hybrid pso (pso ea) as presented by Deb et al. 2010 (https://dl.acm.org/doi/10.1145/1830483.1830492)
The position is updated as
\[ \mathbf{x}_i^{t + 1} = \mathbf{x}_i^{t} + w \left( \mathbf{x}_i^{t} - \mathbf{x}_i^{t-1} \right) + c_1 r_1 \left( \mathbf{p}_{b, i}^t - \mathbf{x}_i^{t} \right) + c_2 r_2 \left( \mathbf{p}_g^t - \mathbf{x}_i^{t} \right) \]At the end the user can mutate the population by passing a mutation functor.
Public Functions
-
template<class
E, classPOS= Population, typename ...PosArgs, typenameT= typename std::decay_t<E>::value_type>
voidinitialise(xt::xexpression<E> &X, std::tuple<PosArgs...> posargs = std::make_tuple())¶ method to initialise position and velocity of swarm for pso
- Template Parameters
E: xtensor type for position and velocity initial vectorsPOS: functor type for generating the initial positionsPosArgs: type of arguments for initialising position functorT: value type of xtensor
- Parameters
X: array of initial bird positionsposargs: (optional) arguments for position functor
-
template<class
E, classF, classOBJ, classPOS= Position_pso_ga, classSEL= Selection_best_pso_ga, classMUT= Mutation_polynomial, typename ...PosArgs, typename ...SelArgs, typename ...MutArgs, typenameT= typename std::decay_t<E>::value_type>
voidevolve(xt::xexpression<E> &X, xt::xexpression<E> &Xm1, xt::xexpression<F> &YB, xt::xexpression<E> &A, OBJ objective_f, std::tuple<PosArgs...> posargs, std::tuple<SelArgs...> selargs, std::tuple<MutArgs...> mutargs)¶ method to evolve bird positions of the swarm
- Template Parameters
E: xtensor type for input and output vectorsF: xtensor type for input and output vectors for evaluation bestOBJ: Functor type for objective function evaluationPOS: Functor type for position evaluationSEL: Functor type for selection evaluationMUT: Functor type for mutation evaluationPosArgs: type of arguments for position evaluation functorSelArgs: type of arguments for selection evaluation functorMutArgs: type of arguments for mutation evaluation functorT: value type of xtensor
- Parameters
X: vector with initial positions of the swarmXm1: vector with previous positions (one generation back) of the individuals comprising the swarmYB: vector with best evaluations of the individuals comprising the swarmA: vector with archived positions of the swarm individualsobjective_f: functor for objective function evaluationposargs: tuple with arguments for position functorselargs: tuple with arguments for velocity functormutargs: tuple with arguments for mutation functor
-
template<class
E, classF, classOBJ, classPOS= Position_pso_ga, classSEL= Selection_best_pso_ga, classMUT= Mutation_functor_polynomial, classTERM= Terminate_gen_max, typename ...PosArgs, typename ...SelArgs, typename ...MutArgs, typename ...TermArgs, typenameT= typename std::decay_t<E>::value_type>
autoevolve(xt::xexpression<E> &X, xt::xexpression<E> &Xm1, xt::xexpression<F> &YB, xt::xexpression<E> &A, OBJ objective_f, std::tuple<PosArgs...> posargs, std::tuple<SelArgs...> selargs, std::tuple<MutArgs...> mutargs, std::tuple<TermArgs...> termargs)¶ method to evolve bird positions of the swarm
- Template Parameters
E: xtensor type for input and output vectorsF: xtensor type for input and output vectors for evaluation bestOBJ: Functor type for objective function evaluationPOS: Functor type for position evaluationSEL: Functor type for selection evaluationMUT: Functor type for mutation evaluationTERM: Functor type for termination of psoPosArgs: type of arguments for position evaluation functorSelArgs: type of arguments for best selection evaluation functorMutArgs: type of arguments for velocity evaluation functorTermArgs: type of arguments for termination evaluation functorT: value type of xtensor
- Parameters
X: vector with initial positions of the swarmXm1: vector with previous positions (one generation back) of the individuals comprising the swarmYB: vector with best evaluations of the individuals comprising the swarmA: vector with archived individuals of the swarm individualsobjective_f: functor for objective function evaluationposargs: tuple with arguments for position functorselargs: tuple with arguments for selection functormutargs: tuple with arguments for mutation functortermargs: tuple with arguments for termination functor
-
template<class