Utils

Submodules

pygenalgo.utils.auxiliary module

class pygenalgo.utils.auxiliary.SubPopulation(pop_id: int, population: list = <factory>)[source]

Bases: object

Auxiliary class container used in the IslandModelGA to hold all the subpopulations (one on each island).

property id: int

Accessor (getter) of the id parameter.

Returns:

the id value.

pop_id: int
population: list
pygenalgo.utils.auxiliary.apply_corrections(input_population: list[Chromosome], fit_func: Callable | None = None) tuple[int, int][source]

Check the population for invalid genes and correct them by applying directly the random method. It is assumed that the random method of the Gene is always returning a ‘valid’ value for the Gene. After that we need to re-evaluate the chromosome to update its fitness.

Parameters:
  • input_population – List(Chromosome) the population we want to apply corrections (if applicable).

  • fit_func – callable fitness function.

Returns:

the number of corrected genes and the additional function evaluations.

pygenalgo.utils.auxiliary.average_hamming_distance(population: list[Chromosome], normal: bool = True) float[source]

Computes the average Hamming distance of a population. We use this to measure the similarity in the whole population of chromosomes.

Parameters:
  • population – List(Chromosome) the population we want to compute the average Hamming distance.

  • normal – (bool) flag that requires the return of the normalized average distance.

Returns:

(float) the total number of differences, in the genes, divided by the total number of genes compared.

pygenalgo.utils.auxiliary.unique_pairs(n_size: int) int[source]

Computes the number of unique pairs among ‘n_size’ distinct numbers by using the combination formula for choosing 2 items from a set.

Parameters:

n_size – the number of ‘distinct’ numbers.

Returns:

the ‘n choose 2’, or C(n, 2).

pygenalgo.utils.utilities module

pygenalgo.utils.utilities.clamp(x: int | float, x_lower: int | float, x_upper: int | float) int | float[source]

Clamps a value within a specified range.

Parameters:
  • x – value to clamp.

  • x_lower – lower bound.

  • x_upper – upper bound.

Returns:

clamped value.

pygenalgo.utils.utilities.cost_function(func: Callable | None = None, minimize: bool = False)[source]

Decorator for the function that we want to optimize. The default setting is maximization.

Parameters:
  • func – the function to be optimized.

  • minimize – if ‘True’ it will return the negative function value to allow for the minimization. Default is set to ‘False’.

Returns:

the ‘function_wrapper’ method.

pygenalgo.utils.utilities.np_cdist(x_pos: ndarray[Any, dtype[ScalarType]], scaled: bool = False) ndarray[Any, dtype[ScalarType]][source]

This is equivalent to the scipy.spatial.distance.cdist method with Euclidean distance metric. It is a tailored version for the purposes of the multimodal operation mode.

Parameters:
  • x_pos – a ndarray of positions. The dimensions of the input array should be [n_rows, n_cols], where n_rows is the number of particles and n_cols are the number of positions. In special 3D cases the input can have a shape of [n_sample, n_rows, n_cols], in these cases the input will be first reshaped to [n_sample, (n_rows*n_cols)] before we continue.

  • scaled – boolean flag that allows the input array to be scaled, using the MaxAbsScaler, before computing the distances.

Returns:

a square [n_rows, n_rows] ndarray of distances.

pygenalgo.utils.utilities.np_pareto_front(points: ndarray[Any, dtype[ScalarType]]) ndarray[Any, dtype[ScalarType]][source]

Simple function that calculates the Pareto (optimal) front points from a given input points numpy array.

Parameters:

points – array of points [(fx1, fx2, …, fxn), (fy1, fy2, …, fyn), ……………….., (fk1, fk2, …, fkn)]

Returns:

array of points that lie on the Pareto front.

pygenalgo.utils.utilities.pareto_dominance(point_a: tuple | list, point_b: tuple | list) bool[source]

Implements a shortcut version of the Pareto dominance condition:

all(p <= q for p, q in zip(point_i, point_j)) && any(p < q for p, q in zip(point_i, point_j))

NOTE: It is assumed that both points have the same size (length).

Parameters:
  • point_a – the first point (as tuple or list).

  • point_b – the second point (as tuple or list).

Returns:

if the condition is satisfied.

pygenalgo.utils.utilities.pareto_front(points: list) list[source]

Simple function that calculates the Pareto (optimal) front points from a given input points list.

NOTE: The function is working directly with lists, even though it can be optimized using numpy arrays.

Parameters:

points – list of points [(fx1, fx2, …, fxn), (fy1, fy2, …, fyn), ……………….., (fk1, fk2, …, fkn)]

Returns:

List of points that lie on the Pareto front.

Module contents