Skip to content

mutate

MutateDefault

mutate(self, *, population, mutation_rate=0.05)

Mutates individuals in population

Parameters:

Name Type Description Default
population ndarray

the population to mutate. Shape is n_individuals x n_chromosomes.

required
mutation_rate float

decimal value from 0 to 1, default .05

0.05

Returns:

Type Description
ndarray

np.ndarray: same shape as population

Source code in pangadfs/mutate.py
def mutate(self, *, population: np.ndarray, mutation_rate: float = .05) -> np.ndarray:
    """Mutates individuals in population

    Args:
        population (np.ndarray): the population to mutate. Shape is n_individuals x n_chromosomes.
        mutation_rate (float): decimal value from 0 to 1, default .05

    Returns:
        np.ndarray: same shape as population

    """
    # mutate is ndarray of same shape of population of dtype bool
    # if mutation_rate is .05, then 1 out of 20 values should be True
    # where mutate is true, swap randomly-selected player into population
    # ensures swap comes from same lineup slot, but does not prevent duplicates from other slots
    # so lineup positional allocation will stay valid, but duplicates are possible
    mutate = (
        np.random.binomial(n=1, p=mutation_rate, size=population.size)
        .reshape(population.shape)
        .astype(bool)
    )
    swap = population[np.random.choice(len(population), size=len(population), replace=False)]
    return np.where(mutate, swap, population)