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

template<class E, typename T = typename std::decay_t<E>::value_type>
auto operator()(const xt::xexpression<E> &X)

operator to evaluate the objective function.

Return

auto evaluated array

Template Parameters
  • E: xtensor type

  • value_type: xtensor value type

Parameters
  • X: array to be evaluated

std::pair<std::vector<double>, std::vector<double>> bounder() const

Get the bounder of Branin functions.

Return

std::pair<std::vector<double>, std::vector<double>>

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, typename T = typename std::decay_t<E>::value_type>
auto operator()(const xt::xexpression<E> &X)

operator to evaluate the objective function.

Return

auto evaluated array

Template Parameters
  • E: xtensor type

  • value_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>>

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, typename T = typename std::decay_t<E>::value_type>
auto operator()(const xt::xexpression<E> &X)

operator to evaluate the objective function.

Return

auto evaluated array

Template Parameters
  • E: xtensor type

  • value_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>>

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, typename T = typename std::decay_t<E>::value_type>
auto operator()(const xt::xexpression<E> &X)

operator to evaluate the objective function.

Return

auto evaluated array

Template Parameters
  • E: xtensor type

  • value_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>>

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, typename T = typename std::decay_t<E>::value_type>
auto operator()(const xt::xexpression<E> &X)

operator to evaluate the objective function.

Return

auto evaluated array

Template Parameters
  • E: xtensor type

  • value_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>>

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, typename T = typename std::decay_t<E>::value_type>
auto operator()(const xt::xexpression<E> &X)

operator to evaluate the objective function.

Return

auto evaluated array

Template Parameters
  • E: xtensor type

  • value_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>>

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 rate

  • eta_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 generations

  • index: 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, class POP = Population, typename ...PopArgs, typename T = typename std::decay_t<E>::value_type>
void initialise(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 population

  • POP: functor for generating the population

  • PopArgs: optional type of arguments for population functor

  • T: value type of xtensor

Parameters
  • X: array of population

  • popargs: optional arguments for population functor

template<class E, class OBJ, class ELIT = Elitism, class SEL = Roulette_selection, class CROSS = Crossover, class MUT = Mutation_polynomial, typename ...ElitArgs, typename ...SelArgs, typename ...CrossArgs, typename ...MutArgs, typename T = typename std::decay_t<E>::value_type>
void evolve(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 function

  • ELIT: functor for elitism

  • SEL: functor for selection

  • CROSS: functor for crossover

  • MUT: functor for mutation

  • ElitArgs: argument types for Elit functor

  • SelArgs: types of arguments for Selection functor

  • CrossArgs: types of arguments for cross over functor

  • MutArgs: types of arguments for mutation functor

  • T: value type of xtensor

Parameters
  • X: array with population at current evolution

  • objective_f: objective function

  • elitargs: function for elitism

  • selargs: function for selection

  • crossargs: function for crossover

  • mutargs: function for mutation

template<class E, class OBJ, class ELIT = Elitism, class SEL = Roulette_selection, class CROSS = Crossover, class MUT = Mutation_polynomial, class TERM = Terminate_gen_max, typename ...ElitArgs, typename ...SelArgs, typename ...CrossArgs, typename ...MutArgs, typename ...TermArgs, typename T = typename std::decay_t<E>::value_type>
auto evolve(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 function

  • ELIT: functor for elitism

  • SEL: functor for selection

  • CROSS: functor for crossover

  • MUT: functor for mutation

  • TERM: functor for ga termination

  • ElitArgs: argument types for Elit functor

  • SelArgs: types of arguments for Selection functor

  • CrossArgs: types of arguments for cross over functor

  • MutArgs: types of arguments for mutation functor

  • TermArgs: types of arguments for terminating functor

  • T: value type of xtensor

Parameters
  • X: array with population at current evolution

  • objective_f: objective function

  • elitargs: arguments for elitism functor

  • selargs: arguments for selection functor

  • crossargs: arguments for crossover functor

  • mutargs: arguments for mutation functor

  • termargs: arguments for terminating functor

Swarm Intelligence algorithms

class xevo::pso

Public Functions

template<class E, class POS = Population, typename ...PosArgs, typename T = typename std::decay_t<E>::value_type>
void initialise(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 vectors

  • POS: functor type for generating the initial positions

  • PosArgs: type of arguments for initialising position functor

  • T: value type of xtensor

Parameters
  • X: array of initial bird positions

  • posargs: (optional) arguments for position functor

template<class E, class POS = Population, class VEL = Velocity_zero, typename ...PosArgs, typename ...VelArgs, typename T = typename std::decay_t<E>::value_type>
void initialise(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 vectors

  • POS: functor type for generating the initial positions

  • VEL: functor type for generating the initial velocity

  • PosArgs: type of arguments for initialising position functor

  • VelArgs: type of arguments for initialising velocity functor

  • T: value type of xtensor

Parameters
  • X: array of initial bird positions

  • V: array of initial bird velocities

  • posargs: (optional) arguments for position functor

  • velargs: (optional) arguments for velocity functor

template<class E, class F, class OBJ, class POS = Position, class VEL = Velocity, class SEL = Selection_best_pso, typename ...PosArgs, typename ...VelArgs, typename ...SelArgs, typename T = typename std::decay_t<E>::value_type>
void evolve(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 vectors

  • F: xtensor type for input and output vectors for evaluation best

  • OBJ: Functor type for objective function evaluation

  • POS: Functor type for position evaluation

  • VEL: Functor type for velocity evaluation

  • PosArgs: type of arguments for position evaluation functor

  • VelArgs: type of arguments for velocity evaluation functor

  • T: value type of xtensor

Parameters
  • X: vector with initial positions of the swarm

  • XB: vector with best positions of the individuals comprising the swarm

  • YB: vector with best evaluations of the individuals comprising the swarm

  • V: vector with initial velocities of the swarm individuals

  • objective_f: functor for objective function evaluation

  • posargs: tuple with arguments for position functor

  • velargs: tuple with arguments for velocity functor

template<class E, class F, class OBJ, class POS = Position, class VEL = Velocity, class SEL = Selection_best_pso, class TERM = Terminate_gen_max, typename ...PosArgs, typename ...VelArgs, typename ...SelArgs, typename ...TermArgs, typename T = typename std::decay_t<E>::value_type>
auto evolve(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 vectors

  • F: xtensor type for input and output vectors for evaluation best

  • OBJ: Functor type for objective function evaluation

  • POS: Functor type for position evaluation

  • VEL: Functor type for velocity evaluation

  • SEL: Functor type for selection best evaluation

  • TERM: Functor type for termination of pso

  • PosArgs: type of arguments for position evaluation functor

  • VelArgs: type of arguments for velocity evaluation functor

  • SelArgs: type of arguments for best selection evaluation functor

  • T: value type of xtensor

Parameters
  • X: vector with initial positions of the swarm

  • XB: vector with best positions of the individuals comprising the swarm

  • YB: vector with best evaluations of the individuals comprising the swarm

  • V: vector with initial velocities of the swarm individuals

  • objective_f: functor for objective function evaluation

  • posargs: tuple with arguments for position functor

  • velargs: tuple with arguments for velocity functor

  • selargs: tuple with arguments for selection functor

  • termargs: tuple with arguments for termination functor

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, class POS = Population, typename ...PosArgs, typename T = typename std::decay_t<E>::value_type>
void initialise(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 vectors

  • POS: functor type for generating the initial positions

  • PosArgs: type of arguments for initialising position functor

  • T: value type of xtensor

Parameters
  • X: array of initial bird positions

  • posargs: (optional) arguments for position functor

template<class E, class F, class OBJ, class POS = Position_pso_ga, class SEL = Selection_best_pso_ga, class MUT = Mutation_polynomial, typename ...PosArgs, typename ...SelArgs, typename ...MutArgs, typename T = typename std::decay_t<E>::value_type>
void evolve(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 vectors

  • F: xtensor type for input and output vectors for evaluation best

  • OBJ: Functor type for objective function evaluation

  • POS: Functor type for position evaluation

  • SEL: Functor type for selection evaluation

  • MUT: Functor type for mutation evaluation

  • PosArgs: type of arguments for position evaluation functor

  • SelArgs: type of arguments for selection evaluation functor

  • MutArgs: type of arguments for mutation evaluation functor

  • T: value type of xtensor

Parameters
  • X: vector with initial positions of the swarm

  • Xm1: vector with previous positions (one generation back) of the individuals comprising the swarm

  • YB: vector with best evaluations of the individuals comprising the swarm

  • A: vector with archived positions of the swarm individuals

  • objective_f: functor for objective function evaluation

  • posargs: tuple with arguments for position functor

  • selargs: tuple with arguments for velocity functor

  • mutargs: tuple with arguments for mutation functor

template<class E, class F, class OBJ, class POS = Position_pso_ga, class SEL = Selection_best_pso_ga, class MUT = Mutation_functor_polynomial, class TERM = Terminate_gen_max, typename ...PosArgs, typename ...SelArgs, typename ...MutArgs, typename ...TermArgs, typename T = typename std::decay_t<E>::value_type>
auto evolve(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 vectors

  • F: xtensor type for input and output vectors for evaluation best

  • OBJ: Functor type for objective function evaluation

  • POS: Functor type for position evaluation

  • SEL: Functor type for selection evaluation

  • MUT: Functor type for mutation evaluation

  • TERM: Functor type for termination of pso

  • PosArgs: type of arguments for position evaluation functor

  • SelArgs: type of arguments for best selection evaluation functor

  • MutArgs: type of arguments for velocity evaluation functor

  • TermArgs: type of arguments for termination evaluation functor

  • T: value type of xtensor

Parameters
  • X: vector with initial positions of the swarm

  • Xm1: vector with previous positions (one generation back) of the individuals comprising the swarm

  • YB: vector with best evaluations of the individuals comprising the swarm

  • A: vector with archived individuals of the swarm individuals

  • objective_f: functor for objective function evaluation

  • posargs: tuple with arguments for position functor

  • selargs: tuple with arguments for selection functor

  • mutargs: tuple with arguments for mutation functor

  • termargs: tuple with arguments for termination functor