metaheuristic_designer.operators.operator_functions.crossover module#

random_split(population_array, fitness_array, rng)[source]#

Randomly partition the population into two equal-sized groups.

The population rows are randomly permuted and split in half. When the population size is odd, the phantom index (one beyond the original length) is replaced by a random valid index from the opposite group, ensuring both groups have the same shape and no individual is lost.

Return type:

tuple[ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool], ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]]

Parameters:
population_arrayMatrixLike

2D array of shape (N, M) containing the current population.

fitness_arrayVectorLike

Fitness values (unused in this split, kept for interface consistency).

rngRNGLike

Random number generator.

Returns:
tuple[MatrixLike, MatrixLike]

Two arrays (parents1, parents2) of equal shape (ceil(N/2), M) representing the randomly paired groups.

Parameters:
  • population_array (ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool])

  • fitness_array (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])

  • rng (int | Generator)

stable_split(population_array, fitness_array, rng)[source]#

Deterministically split the population into two halves preserving order.

For an even population the first and second halves are returned directly. For an odd population the arrays are made equal by cyclically wrapping indices modulo the original size, i.e. the extra slot is filled with the first individual (index 0). No randomness is used.

Return type:

tuple[ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool], ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]]

Parameters:
population_arrayMatrixLike

2D array of shape (N, M) containing the current population.

fitness_arrayVectorLike

Fitness values (unused).

rngRNGLike

Random number generator (kept for API compatibility; not used).

Returns:
tuple[MatrixLike, MatrixLike]

Two arrays (parents1, parents2) of equal shape (ceil(N/2), M).

Parameters:
  • population_array (ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool])

  • fitness_array (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])

  • rng (int | Generator)

create_pairing_fn(method)[source]#

Retrieve a pairing function by name.

Return type:

Callable

Parameters:
methodstr

Key into the pairing_map dictionary. Supported values are "random" and "stable".

Returns:
Callable

A function with signature (population_array, fitness_array, rng) -> (parents1, parents2).

Raises:
ValueError

If method is not present in pairing_map.

Parameters:

method (str)

k_point_crossover(population_array, fitness_array, k=1, pairing_method='random', crossover_prob=1, rng=None)[source]#

k-point crossover with per-pair probability.

The population is split into paired halves using pairing_method. For each pair, k distinct crossover points are drawn uniformly from \(\{1, \dots, M-1\}\) (sorted). The alternating mask built from these points determines which parent contributes each gene.

With probability crossover_prob the children are formed using the mask; otherwise the parents are copied unchanged. The operator returns exactly N offspring.

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
population_arrayMatrixLike

Population of shape (N, M).

fitness_arrayVectorLike

Fitness values (unused by this operator).

kint

Number of crossover points. Must satisfy 1 <= k < M.

pairing_methodstr, optional

Pairing strategy ("random" or "stable").

crossover_probfloat, optional

Probability of applying the crossover to a given pair.

rngRNGLike, optional

Random number generator.

Returns:
MatrixLike

Offspring population of shape (N, M).

Parameters:
  • population_array (ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool])

  • fitness_array (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])

  • k (int)

  • pairing_method (str)

  • crossover_prob (float)

  • rng (int | Generator | None)

uniform_crossover(population_array, fitness_array, pairing_method='random', crossover_prob=1, rng=None)[source]#

Uniform crossover with per-pair probability.

For each gene of a pair, the contributing parent is chosen independently with probability 0.5. The per-pair decision follows the same pattern as k_point_crossover(): with probability crossover_prob the pair undergoes crossover, otherwise the parents are kept unchanged.

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
population_arrayMatrixLike

Population of shape (N, M).

fitness_arrayVectorLike

Fitness values (unused).

pairing_methodstr, optional

Pairing strategy ("random" or "stable").

crossover_probfloat, optional

Probability of applying crossover to a pair.

rngRNGLike, optional

Random number generator.

Returns:
MatrixLike

Offspring population of shape (N, M).

Parameters:
  • population_array (ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool])

  • fitness_array (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])

  • pairing_method (str)

  • crossover_prob (float)

  • rng (int | Generator | None)

averaged_crossover(population_array, fitness_array, pairing_method='random', alpha=0.5, crossover_prob=1, rng=None)[source]#

Arithmetic (averaged) crossover with per-pair probability.

For a pair of parents \(p_1, p_2\) the two children are defined as :rtype: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

\[\begin{split}c_1 &= (1-\alpha)\,p_1 + \alpha\,p_2,\\ c_2 &= (1-\alpha)\,p_2 + \alpha\,p_1,\end{split}\]

where \(\alpha \in [0,1]\) controls the blend. With probability crossover_prob the pair is recombined; otherwise the parents are copied unchanged.

Parameters:
population_arrayMatrixLike

Population of shape (N, M).

fitness_arrayVectorLike

Fitness values (unused).

pairing_methodstr, optional

Pairing strategy.

alphafloat, optional

Blend factor. alpha=0 gives pure parent 1; alpha=1 gives pure parent 2; alpha=0.5 gives the midpoint.

crossover_probfloat, optional

Probability of applying crossover to a pair.

rngRNGLike, optional

Random number generator.

Returns:
MatrixLike

Offspring population of shape (N, M).

Parameters:
  • population_array (ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool])

  • fitness_array (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])

  • pairing_method (str)

  • alpha (float)

  • crossover_prob (float)

  • rng (int | Generator | None)

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

blend_crossover(population_array, fitness_array, pairing_method='random', alpha=0.5, crossover_prob=1, rng=None)[source]#

Blend crossover (BLX-\(\alpha\)) with per-pair probability.

For a pair of parents the smaller/larger values per gene are taken as \(x_{\min}\) and \(x_{\max}\). Each child gene is then sampled uniformly from the expanded interval :rtype: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

\[[x_{\min} - \alpha\,(x_{\max}-x_{\min}),\; x_{\max} + \alpha\,(x_{\max}-x_{\min})].\]

The pair undergoes crossover with probability crossover_prob; otherwise the parents are kept intact.

Parameters:
population_arrayMatrixLike

Population of shape (N, M).

fitness_arrayVectorLike

Fitness values (unused).

pairing_methodstr, optional

Pairing strategy.

alphafloat, optional

Expansion factor (>=0).

crossover_probfloat, optional

Probability of applying crossover to a pair.

rngRNGLike, optional

Random number generator.

Returns:
MatrixLike

Offspring population of shape (N, M).

Parameters:
  • population_array (ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool])

  • fitness_array (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])

  • pairing_method (str)

  • alpha (float)

  • crossover_prob (float)

  • rng (int | Generator | None)

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

sbx_crossover(population_array, fitness_array, pairing_method='random', eta=0.5, crossover_prob=1, rng=None)[source]#

Simulated Binary Crossover (SBX) with per-pair probability.

For a pair of parents \(p_1, p_2\) the children are computed as :rtype: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

\[\begin{split}c_1 &= 0.5(p_1+p_2) - 0.5\,\beta\,|p_1-p_2|,\\ c_2 &= 0.5(p_1+p_2) + 0.5\,\beta\,|p_1-p_2|,\end{split}\]

where the spread factor \(\beta\) is drawn from a polynomial distribution with index \(\eta\):

\[\begin{split}\beta = \begin{cases} (2u)^{1/(\eta+1)}, & u \le 0.5,\\ \bigl(\frac{1}{2(1-u)}\bigr)^{1/(\eta+1)}, & u > 0.5, \end{cases}\end{split}\]

with \(u \sim \mathcal{U}(0,1)\). Larger eta keeps children closer to the parents.

Parameters:
population_arrayMatrixLike

Population of shape (N, M).

fitness_arrayVectorLike

Fitness values (unused).

pairing_methodstr, optional

Pairing strategy.

etafloat, optional

Distribution index for the spread factor (>=0).

crossover_probfloat, optional

Probability of applying crossover to a pair.

rngRNGLike, optional

Random number generator.

Returns:
MatrixLike

Offspring population of shape (N, M).

Parameters:
  • population_array (ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool])

  • fitness_array (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])

  • pairing_method (str)

  • eta (float)

  • crossover_prob (float)

  • rng (int | Generator | None)

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

bitwise_xor_crossover(population_array, fitness_array, pairing_method='random', crossover_prob=1, rng=None)[source]#

Bitwise XOR crossover for binary-valued populations.

For a pair of parents \(p_1, p_2\) the two children are :rtype: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

\[\begin{split}c_1 &= p_1 \oplus p_2,\\ c_2 &= p_1 \oplus \neg p_2,\end{split}\]

where \(\oplus\) denotes bitwise XOR and \(\neg\) is bitwise NOT. This operator is intended for Boolean arrays (0/1). With probability crossover_prob the pair is crossed; otherwise the parents are kept unchanged.

Parameters:
population_arrayMatrixLike

Population of shape (N, M). Should be of a Boolean or integer type where bitwise operations are meaningful.

fitness_arrayVectorLike

Fitness values (unused).

pairing_methodstr, optional

Pairing strategy.

crossover_probfloat, optional

Probability of applying crossover to a pair.

rngRNGLike, optional

Random number generator.

Returns:
MatrixLike

Offspring population of shape (N, M).

Parameters:
  • population_array (ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool])

  • fitness_array (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])

  • pairing_method (str)

  • crossover_prob (float)

  • rng (int | Generator | None)

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

multiparent_discrete_crossover(population_array, fitness_array=None, k=3, crossover_prob=1.0, replace=False, rng=None)[source]#

Multi-parent discrete crossover (uniform scanning).

For each of the N offspring, k parents are drawn (with or without replacement) from the whole population. Every gene of the offspring is then taken uniformly at random from one of those k parents.

With probability crossover_prob an offspring is produced by recombination; otherwise it is a direct copy of the original individual at the same index.

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
population_arrayMatrixLike

Population of shape (N, M).

fitness_arrayVectorLike, optional

Fitness values (unused).

kint, optional

Number of parents per offspring.

crossover_probfloat, optional

Probability of applying crossover to an individual.

replacebool, optional

If False (default), the k parents are distinct (no replacement). If True, parents are sampled independently (with replacement).

rngRNGLike, optional

Random number generator.

Returns:
MatrixLike

Offspring population of shape (N, M).

Parameters:
  • population_array (ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool])

  • fitness_array (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool] | None)

  • k (int)

  • crossover_prob (float)

  • replace (bool)

  • rng (int | Generator | None)

multiparent_intermediate_crossover(population_array, fitness_array=None, k=3, crossover_prob=1.0, replace=False, rng=None)[source]#

Multi-parent intermediate crossover (averaging recombination).

For each offspring, k parents are drawn (with or without replacement). The offspring is the arithmetic mean of those k parents.

With probability crossover_prob the offspring is the averaged vector; otherwise it is the original individual at the same index.

Return type:

ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]

Parameters:
population_arrayMatrixLike

Population of shape (N, M).

fitness_arrayVectorLike, optional

Fitness values (unused).

kint, optional

Number of parents per offspring.

crossover_probfloat, optional

Probability of applying crossover to an individual.

replacebool, optional

If False (default), the k parents are distinct. If True, parents are sampled with replacement.

rngRNGLike, optional

Random number generator.

Returns:
MatrixLike

Offspring population of shape (N, M).

Parameters:
  • population_array (ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool])

  • fitness_array (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool] | None)

  • k (int)

  • crossover_prob (float)

  • replace (bool)

  • rng (int | Generator | None)