metaheuristic_designer.operators.operator_functions.permutation module#

Permutation-specific genetic operators (mutations and crossover).

permute_mutation(population_array, fitness_array, rng=None, N=None)[source]#

Randomly permute N components of each individual.

When N is not given, all components are shuffled (a full permutation). The same subset of positions is used for every row, but a different random permutation is applied to each individual.

Return type:

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

Parameters:
population_arrayMatrixLike

Population of shape (pop_size, num_components).

fitness_arrayVectorLike

Fitness values (unused; kept for interface consistency).

rngRNGLike, optional

Random number generator.

Nint, optional

Number of components to permute. Clipped between 2 and the number of components. Defaults to the population size when None.

Returns:
MatrixLike

The mutated population with permuted components.

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 | None)

  • N (int | None)

roll_mutation(population_array, fitness_array, rng=None, N=1)[source]#

Cyclically shift (roll) a random segment of each individual.

For each solution, a contiguous interval [start, end) is chosen uniformly. That segment is then rolled by N positions. N defaults to 1, which effectively moves the first element of the segment to the end.

Return type:

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

Parameters:
population_arrayMatrixLike

Population of shape (pop_size, num_components).

fitness_arrayVectorLike

Fitness values (unused).

rngRNGLike, optional

Random number generator.

Nint, optional

Number of positions to roll inside the segment. Default is 1.

Returns:
MatrixLike

The mutated population.

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 | None)

  • N (int)

invert_mutation(population_array, fitness_array, rng=None)[source]#

Reverse the order of a random contiguous segment in each individual.

A segment [start, end) is selected uniformly for every row, and its elements are reversed in place.

Return type:

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

Parameters:
population_arrayMatrixLike

Population of shape (pop_size, num_components).

fitness_arrayVectorLike

Fitness values (unused).

rngRNGLike, optional

Random number generator.

Returns:
MatrixLike

The mutated population.

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 | None)

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

Partially Mapped Crossover (PMX) for permutation chromosomes.

Parents are paired using the given pairing_method. For each pair, two children are created by the standard PMX procedure, which preserves a randomly chosen segment from one parent and maps the remaining positions from the other parent. With probability crossover_prob the children are replaced by exact copies of the parents.

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), where each row is a permutation of integers 0 M-1.

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)

pmx_single(vector1, vector2, rng=None)[source]#

Core PMX operation for a single pair of parents.

Original implementation found in cosminmarina/A1_ComputacionEvolutiva

Return type:

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

Parameters:
vector1, vector2VectorLike

Two parent permutations (1-D arrays).

rngRNGLike, optional

Random number generator.

Returns:
VectorLike

One offspring permutation.

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

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

  • rng (int | Generator | None)

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

Order Crossover (OX) for permutation chromosomes.

Builds offspring by preserving a randomly chosen segment from one parent and filling the remaining positions with the order of the other parent. The pairing and probability logic is identical to pmx().

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.

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)

order_cross_single(vector1, vector2, rng=None)[source]#

Core OX operation for a single pair of parents.

Return type:

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

Parameters:
vector1, vector2VectorLike

Two parent permutations.

rngRNGLike, optional

Random number generator.

Returns:
VectorLike

One offspring permutation.

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

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

  • rng (int | Generator | None)