Selection

The selection of parents for the next generation is a vital part of the genetic algorithm this library provides the following selection methods to do such:

  • Roulette Wheel Selection

  • Tournament Selection

  • Rank Selection

  • Boltzmann Selection

The methods are implemented by functions generally structured as the following

def selection_method(fx, population, probabiity, fitness, **kwargs):
    # Calcuate the fitness value by calling the fitness function
    y = fx(population)
    f = fitness(y)

    # Determine the probability of selection for each individual
    p = probability(f, **kwargs)

    # Select the individuals
    selected = np.random.choice(population, size=len(population), p=p)

    return selected, f, p, y

Note

The selection method needs to return 4 values:
  • The selected individuals

  • The fitness values of the selected individuals

  • The probability of selection of the selected individuals

  • The fitness values of the population

for which the first one is important to the functioning of the genetic algorithm, the other three are for the user to use for debugging/logging purposes.

Roulette Wheel Selection

The roulette wheel selection method is the most common selection method used in genetic algorithms. It is based on the idea that the probability of selection of an individual is proportional to its fitness value. The fitness values are normalized to a range of [0, 1] and the individuals are selected based on the probability of selection of each individual.

So that the mathmatical formula for the probability of selection of an individual is

Tournament Selection

Rank Selection

Boltzmann Selection

Fitness evaluation

Selection methods

dfmcontrol.Utility.selection.boltzmann_selection(*args, **kwargs)
Parameters

args – args, kwargs passed onto the calc_fx function, these can be ignored by the user

The following kwargs are used by the function and can be set by the user through the selargs dict:

Parameters
  • k (list) – list of paramters for the flattening function (IE fitness_func), default is [1.5]

  • T (float) – the temperature parameter for the boltzmann selection, default is 10

  • mode (str) – the mode of the optimisation (IE minimisation or optimisation)

  • fitness_func (function) – the fitness function to use

Returns

selected parents based on their probability, the fitness values, probablity values and the calculated values

Return type

tuple

dfmcontrol.Utility.selection.calc_fx(pop, fx, bitsize, nbit2num=<function Ndbit2floatIEEE754>, **kwargs)

Calculate the fitness of a population.

Parameters
  • pop – Population of individuals

  • fx – Function to calculate the fitness of an individual

  • bitsize – Bitsize of an individual

  • nbit2num – Function to convert a ndarray of bits to a number

  • kwargs – Additional arguments for nbit2num

Returns

Fitness of the population

dfmcontrol.Utility.selection.exp_fitness(y, k)

Scale fitness exponentially :param y: np.ndarray with values f(x1, x2, x3, x…) in order of pop :param k: parameter k :return: np.ndarray with exponential values f(x1, x2, x3, x…) in order of pop

dfmcontrol.Utility.selection.lin_fitness(y, a, b)

Scale fitness linearly

Parameters
  • y – np.ndarray with values f(x1, x2, x3, x…) in order of pop

  • a – parameter a

  • b – parameter b

Returns

np.ndarray with linearised values f(x1, x2, x3, x…) in order of pop

dfmcontrol.Utility.selection.no_fitness(y, *args)
dfmcontrol.Utility.selection.probability_sort_list(y, p, allow_duplicates=False, **kwargs)

Select parents out the pool pop represented by their (in same order as pop in ga) from their corresponding :param y: np.ndarray with values f(x1, x2, x3, x…) in order of pop :param p: np.ndarray with probablity values correlating to y values

so that p[1] has contains the probablity of y[1]

Returns

selected parents based on their probability

dfmcontrol.Utility.selection.rank_selection(*args, **kwargs)

Select parents out the pool pop based on their rank in the fitness function

calc_fx params: pop, fx, bitsize, nbit2num = Ndbit2float, **kwargs

Parameters

args – args, kwargs passed onto the calc_fx function, these can be ignored by the user

The following kwargs are used by the function and can be set by the user through the selargs dict:

Parameters
  • k (list) – list of paramters for the flattening function (IE fitness_func), default is [1.5]

  • p (float) – the probability parameter for the rank selection, default is 1.9

  • mode (str) – the mode of the optimisation (IE minimisation or optimisation)

  • fitness_func (function) – the fitness function to use

Returns

selected parents based on their probability, the fitness values, probablity values and the calculated values

Return type

tuple

dfmcontrol.Utility.selection.rank_space_selection(*args, **kwargs)

Select parents out the pool pop according to rank space selection

calc_fx params: pop, fx, bitsize, nbit2num = Ndbit2float, **kwargs :param args: args, kwargs passed onto the calc_fx function, these can be ignored by the user

The following kwargs are used by the function and can be set by the user through the selargs dict:

Parameters
  • k (list) – list of paramters for the flattening function (IE fitness_func), default is [1.5]

  • p (float) – the probability parameter for the rank selection, default is 1.9

  • d (float) – the diversity parameter for the rank selection, default is 1

  • mode (str) – the mode of the optimisation (IE minimisation or optimisation)

  • fitness_func (function) – the fitness function to use

Returns

selected parents based on their probability, the fitness values, probablity values and the calculated values

Return type

tuple

dfmcontrol.Utility.selection.rank_tournament_selection(*args, **kwargs)

pop, fx, bitsize, nbit2num = Ndbit2float, **kwargs

Parameters

args – args, kwargs passed onto the calc_fx function, these can be ignored by the user

The following kwargs are used by the function and can be set by the user through the selargs dict:

Parameters
  • k (list) – list of paramters for the flattening function (IE fitness_func), default is [1.5]

  • p (float) – the probability of selecting the best individual, default is 1.9

  • tournament_size (int) – the size of the tournament, default is 4

  • mode (str) – the mode of the optimisation (IE minimisation or optimisation)

  • fitness_func (function) – the fitness function to use

Returns

selected parents based on their probability, the fitness values, probablity values and the calculated values

Return type

tuple

dfmcontrol.Utility.selection.roulette_selection(*args, **kwargs)

pop, fx, bitsize, nbit2num = Ndbit2float, **kwargs

Parameters

args – args, kwargs passed onto the calc_fx function, these can be ignored by the user

The following kwargs are used by the function and can be set by the user through the selargs dict:

Parameters
  • k (list) – list of paramters for the flattening function (IE fitness_func), default is [1.5]

  • mode (str) – the mode of the optimisation (IE minimisation or optimisation)

  • fitness_func (function) – the fitness function to use

Returns

selected parents based on their probability, the fitness values, probablity values and the calculated values

Return type

tuple

dfmcontrol.Utility.selection.sigmoid_fitness(x, k, x0=0)

Sigmoid function :param x: np.ndarray with values f(x1, x2, x3, x…) in order of pop :param k: steepness parameter k :param x0: start pos parameter x0 :return: np.ndarray with sigmoid values f(x1, x2, x3, x…) in order of pop

dfmcontrol.Utility.selection.simple_fitness(y, *args)

Normalise fitness

Parameters
  • y – np.ndarray with fitness values f(x1, x2, x3, x…) in order of pop

  • args – Pipeline for additional arguments that are included by default (IE a, b or k)

Returns

np.ndarray with normalised values f(x1, x2, x3, x…) in order of pop

dfmcontrol.Utility.selection.sort_list(y, p, **kwargs)

Select parents out the pool pop :param y: np.ndarray with values f(x1, x2, x3, x…) in order of pop :param p: np.ndarray with probablity values correlating to y values :param kwargs: allow_duplicates => allow duplicates in the selection :return: selected parents based on their probability