leap_ec.int_rep package
Submodules
leap_ec.int_rep.initializers module
Initializers for integer-valued genomes.
- leap_ec.int_rep.initializers.create_int_vector(bounds)
A closure for initializing lists of integers for int-vector genomes, sampled from a uniform distribution.
Having a closure allows us to just call the returned function N times in Individual.create_population().
TODO Allow either a single tuple or a sequence of tuples for bounds. —Siggy
- Parameters
bounds – a list of (min, max) values bounding the uniform sampline of each element
- Returns
A function that, when called, generates a random genome.
>>> from leap_ec.decoder import IdentityDecoder >>> from leap_ec.real_rep.problems import SpheroidProblem >>> bounds = [(0, 1), (-5, 5), (-1, 100)] >>> population = Individual.create_population(10, create_int_vector(bounds), ... decoder=IdentityDecoder(), ... problem=SpheroidProblem())
leap_ec.int_rep.ops module
Evolutionary operators for maniuplating integer-vector genomes.
- leap_ec.int_rep.ops.genome_mutate_binomial(std='__no__default__', bounds: list = '__no__default__', expected_num_mutations: float = None, probability: float = None, n: int = 10000)
Perform additive binomial mutation of a particular genome.
>>> import numpy as np >>> genome = np.array([42, 12]) >>> bounds = [(0,50), (-10,20)] >>> genome_op = genome_mutate_binomial(std=0.5, bounds=bounds, ... expected_num_mutations=1) >>> new_genome = genome_op(genome)
- leap_ec.int_rep.ops.individual_mutate_randint(genome='__no__default__', bounds: list = '__no__default__', expected_num_mutations=None, probability=None)
Perform random-integer mutation on a particular genome.
>>> import numpy as np >>> genome = np.array([42, 12]) >>> bounds = [(0,50), (-10,20)] >>> new_genome = individual_mutate_randint(genome, bounds, expected_num_mutations=1)
- Parameters
genome – test_sequence of integers to be mutated
bounds – test_sequence of bounds tuples; e.g., [(1,2),(3,4)]
expected_num_mutations – on average how many mutations done (specificy either this or probability, but not both)
probability – the probability of mutating any given gene (specificy either this or expected_num_mutations, but not both)
- leap_ec.int_rep.ops.mutate_binomial(next_individual: Iterator = '__no__default__', std: float = '__no__default__', bounds: list = '__no__default__', expected_num_mutations: float = None, probability: float = None, n: int = 10000) Iterator
Mutate genes by adding an integer offset sampled from a binomial distribution centered on the current gene value.
This is very similar to applying additive Gaussian mutation and then rounding to the nearest integer, but does so in a way that is more natural for integer-valued genes.
- Parameters
std (float) – standard deviation of the binomial distribution
bounds – list of pairs of hard bounds to clip each gene by (to prevent mutation from carrying a gene value outside an allowed range)
expected_num_mutations – on average how many mutations done (specificy either this or probability, but not both)
probability – the probability of mutating any given gene (specificy either this or expected_num_mutations, but not both)
n (int) – the number of “coin flips” to use in the binomial process (defaults to 10000)
Usage example:
>>> from leap_ec.individual import Individual >>> from leap_ec.int_rep.ops import mutate_binomial >>> import numpy as np >>> population = iter([Individual(np.array([1, 1]))]) >>> operator = mutate_binomial(std=2.5, ... bounds=[(0, 10), (0, 10)], ... expected_num_mutations=1) >>> mutated = next(operator(population))
The std parameter can also be given as a list with a value to use for each gene locus:
>>> population = iter([Individual(np.array([1, 1]))]) >>> operator = mutate_binomial(std=[2.5, 3.0], ... bounds=[(0, 10), (0, 10)], ... expected_num_mutations=1) >>> mutated = next(operator(population))
Note
The binomial distribution is defined by two parameters, n and p. Here we simplify the interface by asking instead for an std parameter, and fixing a high value of n by default. The value of p needed to obtain the given std is computed for you internally.
As the plots below illustrate, the binomial distribution is approximated by a Gaussian. For high n and large standard deviations, the two are effectively equivalent. But when the standard deviation (and thus binomial p parameter) is relatively small, the approximation becomes less accurate, and the binomial differs somewhat from a Gaussian.
- leap_ec.int_rep.ops.mutate_randint(next_individual: Iterator = '__no__default__', bounds='__no__default__', expected_num_mutations=None, probability=None) Iterator
Perform randint mutation on each individual in an iterator (population).
This operator replaces randomly selected genes with an integer samples from a uniform distribution.
- Parameters
bounds – test_sequence of bounds tuples; e.g., [(1,2),(3,4)]
expected_num_mutations – on average how many mutations done (specificy either this or probability, but not both)
probability – the probability of mutating any given gene (specificy either this or expected_num_mutations, but not both)
>>> from leap_ec.individual import Individual >>> from leap_ec.int_rep.ops import mutate_randint >>> import numpy as np
>>> population = iter([Individual(np.array([1, 1]))]) >>> operator = mutate_randint(expected_num_mutations=1, bounds=[(0, 10), (0, 10)]) >>> mutated = next(operator(population))