1.4. Mutation

Simple functions for encapsulating performing mutation operations.

1.4.1. Custom functions

Custom mutation functions can be used in islands. Like most custom functions, the island is a required param, whether used or not. The individual is also required, and some default params can be defined. Below an example of the uniform random mutation.

def mutation_randomize(individual, prob : float = 0.2, island=None):
   for i in range(len(individual.chromosome)):
      if random.random() < prob:
         island._verbose_logging(f"mutate: gene_before {repr(individual.chromosome[i])}")
         individual.chromosome[i].randomise()
         island._verbose_logging(f"mutate: gene_after {repr(individual.chromosome[i])}")

   return individual

1.4.2. Mutation operators

1.4.2.1. Uniform Random

natural_selection.genetic_algorithms.operators.mutation.mutation_randomize(individual, prob: float = 0.2, island=None)

A Classic mutation function, changes a gene of the given individual based on the prob strength of mutation.

Parameters
  • individual (Individual) – Individual object containing a Genome.

  • prob (float) – The probability of randomizing genes (default = 0.2).

  • island (Island) – The Island calling the method (default = None).

Returns

The newly mutated individual.

Return type

Individual

1.4.2.2. Random Point

natural_selection.genetic_algorithms.operators.mutation.mutation_randomize_n_point(individual, n_points: int = 1, prob: float = 0.2, island=None)list

Much like n_point crossover, random slices of the chromosome are selected and then all genes in those selected slices are randomised. This method might be a little more aggressive, as it can mutate longer strings at a time.

Parameters
  • individual (Individual) – Individual object containing a Genome.

  • n_points (int) – The amount of random points to split at (default = 1).

  • prob (float) – The probability of randomizing genes (default = 0.2).

  • island (Island) – The Island calling the method (default = None).

Returns

The newly mutated individual.

Return type

Individual