Utilities

Sorting Algorithms

sort_log_non_dominated(individuals, sel_count, ffo=False)

Sorts individuals in pareto non-dominated fronts using the Generalized Reduced Run-Time Complexity Non-Dominated Sorting Algorithm.

Parameters
  • individuals (list) – A list of individuals to sort.

  • sel_count (int) – The number of individuals to select.

  • ffo (bool) – If True, only the first front is returned, optional.

Returns

A list of Pareto fronts, where the first element is the true Pareto front.

Return type

list

sort_non_dominated(individuals, sel_count, ffo=False)

Sorts the first ‘sel_count’ of ‘individuals’ into different non-domination levels using the “Fast Non-dominated Sorting Approach”.

Parameters
  • individuals (list) – A list of individuals to sort.

  • sel_count (int) – The number of individuals to select.

  • ffo (bool) – If True, only the first front is returned, optional.

Returns

A list of Pareto fronts, where the first element is the true Pareto front.

Return type

list

class SortingNetwork(dimension, connectors=None)

A sorting network is an abstract mathematical model of a network of wires and comparator modules that is used to sort a sequence of numbers. The wires are thought of as running from left to right, carrying values (one per wire) that traverse the network all at the same time. Each comparator connects two wires. When a pair of values, traveling through a pair of wires, encounter a comparator, the comparator swaps the values if and only if the top wire’s value is greater or equal to the bottom wire’s value.

Parameters
  • dimension (int) – The number of wires in the network.

  • connectors (Optional[list]) – A list of pairs of wires that are connected by a comparator, optional.

property depth: int

Returns the depth of the network.

property length: int

Returns the length of the network.

static check_conflict(level, wire1, wire2)

Checks if the given wires are in conflict with each other on the given level.

Parameters
  • level (list) – The level of the network.

  • wire1 (int) – The index of the first wire.

  • wire2 (int) – The index of the second wire.

Returns

True if the wires are in conflict, False otherwise.

Return type

bool

add_connector(wire1, wire2)

Adds a connector to the network.

Parameters
  • wire1 (int) – The index of the first wire.

  • wire2 (int) – The index of the second wire.

Returns

Nothing.

Return type

None

sort(values)

Sorts the given values using the network.

Parameters

values (list) – A list of values to be sorted.

Returns

Nothing.

Return type

None

evaluate(cases=None)

Evaluates the network’s performance on the given cases.

Parameters

cases (Optional[list]) – A list of pairs of values that are to be evaluated.

Returns

The number of cases that were not correctly sorted.

Return type

int

draw()

Creates a visual representation of the network.

Returns

The schemata of the network.

Return type

str



Population Initializers

init_repeat(container, func, size)

Calls the func argument count times and puts the results into an instance of container. This helper function can be used in conjunction with a Toolbox to register a generator of filled containers, such as individuals or a population.

Parameters
  • container (Callable) – A callable which takes an iterable as argument and returns a Collection.

  • func (Callable) – The function to be called count times.

  • size (int) – The number of times to call the func.

Returns

An iterable filled with count results of func.

Return type

Iterable

init_iterate(container, generator)

Calls the generator function and puts the results into an instance of container. The generator function should return an iterable. This helper function can be used in conjunction with a Toolbox to register a generator of filled containers, as individuals or a population.

Parameters
  • container (Callable) – A callable which takes an iterable as argument and returns a Collection.

  • generator (Callable) – A function returning an iterable to fill the container with.

Returns

An iterable filled with the results of the generator.

Return type

Iterable

init_cycle(container, funcs, size=1)

Calls each function in the funcs iterable count times and stores the results from all function calls into the container. This helper function can be used in conjunction with a Toolbox to register a generator of filled containers, as individuals or a population.

Parameters
  • container (Callable) – A callable which takes an iterable as argument and returns a Collection.

  • funcs (Iterable) – A sequence of functions to be called.

  • size (int) – Number of times to iterate through the sequence of functions.

Returns

An iterable filled with the results of all function calls.

Return type

Iterable



Hypervolume Indicators

least_contrib(population, ref_point=None, map_func=map)

Returns the index of the individual with the least hypervolume contribution. Minimization is implicitly assumed.

Parameters
  • population (list) – A list of non-dominated individuals, where each individual has a Fitness attribute.

  • ref_point (Optional[list]) – The reference point for the hypervolume, optional.

  • map_func (Optional[Callable]) – Any map function which maps an iterable to a callable, optional. This can be used to speed up the computation by providing a multiprocess mapping function which is associated to a pool of workers. The default is the regular single-process map function.

Returns

The index of the individual with the least hypervolume contribution.

Return type

Union[int, numpy.ndarray]

hypervolume(population, ref_point=None)

Returns the hypervolume of a population. Minimization is implicitly assumed.

Parameters
  • population (list) – A list of non-dominated individuals, where each individual has a Fitness attribute.

  • ref_point (Optional[list]) – The reference point for the hypervolume, optional. If not provided, the worst value for each objective +1 is used.

Returns

The hypervolume of the given population.

Return type

float

class HyperVolume(ref_point)

Creates a new HyperVolume object with the ref_point.

Parameters

ref_point (numpy.ndarray) – The reference point for the hypervolume calculation.

compute(point_set)

Computes the hypervolume that is dominated by the non-dominated point_set. Minimization is implicitly assumed.

Parameters

point_set (numpy.ndarray) – The set of points that are to be evaluated.

Returns

The hypervolume of the given point set.

Return type

float



Constraint Decorators

class DeltaPenalty(feasibility, delta, distance=None)

This decorator returns penalized fitness for invalid individuals and the original fitness value for valid individuals. The penalized fitness is made of a constant factor delta added with an optional distance penalty. The distance function, if provided, returns a value, which is growing as the individual moves away from the valid zone.

Parameters
  • feasibility (Callable) – A function returning the validity status of an individual.

  • delta (NumOrSeq) – Constant or a sequence of constants returned for an invalid individual.

  • distance (Callable) – A function returning the distance between the individual and a given valid point.

Returns

A decorator for the fitness function.

class ClosestValidPenalty(validity, feasible, alpha, distance=None)

This decorator returns penalized fitness for invalid individuals and the original fitness value for valid individuals. The penalized fitness is made of the fitness of the closest valid individual added with an optional weighted distance penalty. The distance function, if provided, returns a value, which is growing as the individual moves away from the valid zone.

Parameters
  • validity (Callable) – A function returning the validity status of any individual.

  • feasible (Callable) – A function returning the closest feasible individual from the current invalid individual.

  • alpha (float) – Multiplication factor on the distance between the valid and invalid individuals.

  • distance (Callable) – A function returning the distance between the individual and a given valid point.

Returns

A decorator for the fitness function.



Benchmark Decorators

class Translate(vector)

A decorator for evaluation functions. When the decorated function is called, the input Individual is translated by the preset list of vector values, the result of which is then passed into the evaluation function as a plain list of values. This decorator adds the translate() method to the decorated function, which can be used to update the translation vector values.

Parameters

vector (list) – The translation vector values. Must have the same length as the individual.

translate(vector)

Updates the translation vector values. After decorating the evaluation function, this method is directly available from the function object, which can be used to update the translation vector values.

Parameters

vector (list) – The translation vector.

class Rotate(matrix)

A decorator for evaluation functions. When the decorated function is called, the input Individual is rotated by the preset ndarray of matrix values, the result of which is then passed into the evaluation function as a plain ndarray of values. This decorator adds a rotate() method to the decorated function, which can be used to update the rotation matrix values.

Parameters

matrix – The rotation matrix values. Must be a valid orthogonal N * N rotation matrix, where N is the length of the individual.

rotate(matrix)

Updates the rotation matrix values. After decorating the evaluation function, this method is directly available from the function object, which can be used to update the rotation matrix values.

Parameters

matrix – The rotation matrix.

class Scale(factor)

A decorator for evaluation functions. When the decorated function is called, the input Individual is scaled by the preset list of factor values, the result of which is then passed into the evaluation function as a plain list of values. This decorator adds the scale() method to the decorated function, which can be used to update the scale factor values.

Parameters

factor – The scale factor values. Must have the same length as the individual.

scale(factor)

Updates the scale factor values. After decorating the evaluation function, this method is directly available from the function object, which can be used to update the scale factor values.

Parameters

factor (list) – The scale factor.

class Noise(funcs)

A decorator for evaluation functions. When the decorated function is called, noise is added to the result(s) of the decorated function by the preset callable(s), which are called without arguments. This decorator adds the noise() method to the decorated functions, which can be used to update the noise generator callables.

Parameters

funcs – The noise generator callables. Can either be a single callable, which is applied to all values in the results object, or a list of callables, which must have the same length as the input individual. The values in the funcs argument can also be of type None, which prevents noise from being added to the corresponding value(s).

noise(funcs)

Updates the noise generator funcs. After decorating the evaluation function, this method is directly available from the function object.

Parameters

funcs (Union[Callable, list[Callable]]) – The noise function(s).

bin2float(min_, max_, n_bits)

Returns a decorator, which converts a binary array into an array of floats where each float is composed of n_bits and has a value between min and max and returns the result of the decorated function.

Parameters
  • min – Minimum value of the value range.

  • max – Maximum value of the value range.

  • n_bits (int) – Number of bits used to represent the float.

Returns

Decorated function.

Return type

Callable



Various Metrics

nsga_diversity(population, first, last)

Given a Pareto front population and the two extreme points first and last of the optimal Pareto front, this function returns the diversity metric of the population as explained in the original NSGA-II article by K. Deb. Smaller values indicate better solutions.

Parameters
  • population (list) – The Pareto front to be evaluated.

  • first (Individual) – The first extreme point of the optimal Pareto front.

  • last (Individual) – The last extreme point of the optimal Pareto front.

Returns

The diversity metric of the front.

Return type

float

nsga_convergence(population, optimal)

Given a Pareto front and the optimal Pareto front, this function returns the convergence metric of the front as explained in the original NSGA-II article by K. Deb. Smaller values indicate more optimal solutions.

Parameters
  • population (list) – The Pareto front to be evaluated.

  • optimal (list) – The optimal Pareto front.

Returns

The convergence metric of the front.

Return type

float

inv_gen_dist(ind1, ind2)

Computes the Inverted Generational Distance (IGD) between the two individuals. The IGD is a metric for assessing the quality of approximations to the Pareto front obtained by multi-objective optimization algorithms.

Parameters
Returns

The IGD between the two individuals.

Return type

tuple[Any, Optional[Any]]