individual
— Individuals¶
Data structures to store objective values together with the solution.
The classes in this module have attributes for storing the genome (the
solution in the search space) and corresponding objective values. They also
provide methods for generating new individuals (offspring) by mutation and
recombination. Individual
is the base class for all other individuals.
-
class
evoalgos.individual.
Individual
(genome=None, objective_values=None, repair_component=None, num_parents=2, id_number=None)¶ Base class for individuals.
This class does not make any assumptions about the individual’s genome. Implementing the genome and an appropriate mutation and recombination is completely in the user’s responsibility.
Apart from the arguments provided in the constructor, this class possesses the member attributes age, date_of_birth, and date_of_death, which can be manipulated by evolutionary algorithms.
-
__init__
(genome=None, objective_values=None, repair_component=None, num_parents=2, id_number=None)¶ Constructor.
Parameters: - genome (object, optional) – An arbitrary object containing the genome of the individual.
- objective_values (iterable, optional) – The objective values are usually obtained by evaluating the individual’s phenome.
- repair_component (callable, optional) – A function or callable object that will get the phenome as input and shall return a repaired copy of the phenome.
- num_parents (int, optional) – How many individuals are involved in a recombination procedure. Default is 2.
- id_number (int, optional) – A currently unused identification number. If no ID is provided,
one is automatically generated with
itertools.count()
.
-
__str__
()¶ Return string representation of the individual’s objective values.
-
_mutate
()¶ Does the real work for mutation.
This is an abstract method. Override in your own individual. The individual must be changed in-place and not returned.
-
_recombine
(others)¶ Does the real work for recombination.
This is an abstract method. Override in your own individual if you want recombination. This method returns new individuals, so this individual should not be modified.
Returns: children – Newly generated offspring. Return type: list
-
clone
()¶ Clone the individual.
Makes a flat copy of this individual with some exceptions. The clone’s age is set back to 0, a new ID is assigned to the clone, and for objective values and the genome a deep copy is made.
-
invalidate_objective_values
()¶ Set objective values to None.
-
mutate
()¶ Mutate this individual.
This is a template method that calls three other methods in turn. First, the individual is mutated with
_mutate()
, theninvalidate_objective_values()
is called, and finallyrepair()
is carried out.
-
phenome
¶ Accessor to obtain the phenome from the genome.
This mapping from genome to phenome exists to provide the possibility of using a search space that is different to the pre-image of the objective function. Only read-access is provided for this attribute.
-
recombine
(others)¶ Generate offspring from several individuals.
This is a template method. First, the parents are recombined with
_recombine()
, theninvalidate_objective_values()
is called.Returns: children – Newly generated offspring. Return type: list
-
repair
()¶ Repair this individual.
If existing, the repair component is applied to the genome to do the work.
-
-
class
evoalgos.individual.
BinaryIndividual
(bit_flip_prob=None, **kwargs)¶ Individual with a binary genome.
-
__init__
(bit_flip_prob=None, **kwargs)¶ Constructor.
Parameters: - bit_flip_prob (float, optional) – The probability to flip a bit. Default is
1.0 / len(genome)
. - kwargs – Arbitrary keyword arguments, passed to the super class.
- bit_flip_prob (float, optional) – The probability to flip a bit. Default is
-
_mutate
()¶ Carry out standard bit mutation.
-
_recombine
(others)¶ Uniform recombination.
Parameters: others (iterable of Individual) – Other parents to recombine this individual with. Returns: children – A list containing a single child. Return type: list of Individual
-
-
class
evoalgos.individual.
FixedSizeSetIndividual
(avail_elements, swap_prob=None, binary=False, **kwargs)¶ Individual with a set as genome.
Despite the name of this class, the genome must be a sequence, due to implementation issues (
random.choice()
is not supported for sets). However, the set property is enforced for offspring created by recombination.-
__init__
(avail_elements, swap_prob=None, binary=False, **kwargs)¶ Constructor.
Parameters: - avail_elements (sequence) – The possible elements of the set.
- swap_prob (float, optional) – The probability to swap a set element. Default is
1.0 / len(genome)
. - binary (bool, optional) – Indicates if the phenome should be presented as bitstring to the outside world. This is useful for compatibility with binary optimization problems.
- kwargs – Arbitrary keyword arguments, passed to the super class.
-
_mutate
()¶ A set mutation that retains the cardinality.
-
_recombine
(others)¶ Produce one child with a set the same size as this parent.
This recombination guarantees that the intersection of the parents’ sets is present in the child. For the remaining elements, the probability to be chosen is proportional to the frequency in the parents.
Parameters: others (iterable of Individual) – Other parents to recombine this individual with. Returns: children – A list containing a single child. Return type: list of Individual
-
phenome
¶ Either the actual genome or a bitstring representation.
-
-
class
evoalgos.individual.
ESIndividual
(learning_param1=None, learning_param2=None, strategy_params=None, recombination_type='arithmetic', **kwargs)¶ An individual using Gaussian mutation.
This variation was developed in the context of Evolution Strategies (ES) [Beyer2002]. One can choose between discrete, intermediate, and arithmetic recombination. Self-adaptation is used to adjust strategy parameters.
References
[Beyer2002] (1, 2, 3, 4, 5, 6) Hans-Georg Beyer, Hans-Paul Schwefel (2002). Evolution strategies - A comprehensive introduction. Natural Computing, Volume 1, Issue 1, pp. 3-52. https://dx.doi.org/10.1023/A:1015059928466 -
__init__
(learning_param1=None, learning_param2=None, strategy_params=None, recombination_type='arithmetic', **kwargs)¶ Constructor.
If both learning parameters are set to zero, adaptation of strategy parameters is disabled. See [Beyer2002] for more information.
Parameters: - learning_param1 (float, optional) – The first learning parameter for self-adaptation. Default value is chosen according to [Beyer2002].
- learning_param2 (float, optional) – The second learning parameter for self-adaptation. Default value is chosen according to [Beyer2002].
- strategy_params (list of float, optional) – The strategy parameters (mutation strengths) to be adapted. This list must have either length one or the same length as the genome. As a default, [0.25, ..., 0.25] is chosen.
- recombination_type (str, optional) – Must be ‘arithmetic’, ‘intermediate’, ‘discrete’, or ‘none’.
- kwargs – Arbitrary keyword arguments, passed to the super class.
-
_mutate
()¶ Add normally distributed random numbers to each variable.
-
arithmetic
(variables)¶ Return a convex combination of the given variables.
The weights are taken from this object’s
weights
attribute. They need to be positive andsum(weights) == 1
.
-
clone
()¶ Clone this individual.
First, the clone method of the super class is called. Then, a deep copy of attribute strategy_params is additionally made and
are_strategy_params_mutated
is set to False.Returns: child – A clone of this individual. Return type: ESIndividual
-
static
discrete
(variables)¶ Return a randomly chosen variable from the given ones.
-
static
intermediate
(variables)¶ Return the mean of the given variables.
-
mutate_strategy_params
()¶ Mutate the strategy parameters.
By default, the mutation strengths are bounded to the smallest and largest representable positive values of the system,
sys.float_info.min
andsys.float_info.max
. You can change these bounds by settingmutation_strength_bounds = [your_min, your_max]
.
-
recombine
(others)¶ Produce one child from an arbitrary number of parents.
The kind of recombination is determined by the attribute recombination_type.
Parameters: others (iterable of Individual) – Other parents to recombine this individual with. Returns: children – A list containing a single child. Return type: list of Individual
-
-
class
evoalgos.individual.
CMSAIndividual
(num_parents, num_offspring=1, mutation_strength=0.25, learning_param=None, sorting_component=None, cov_matrix=None, **kwargs)¶ An individual with self-adaptive covariance matrix adaptation.
This variation was proposed in [Beyer2008].
References
[Beyer2008] (1, 2) Hans-Georg Beyer, Bernhard Sendhoff (2008). Covariance Matrix Adaptation Revisited - The CMSA Evolution Strategy -. In Parallel Problem Solving from Nature - PPSN X, Springer, pp. 123-132. https://dx.doi.org/10.1007/978-3-540-87700-4_13 -
__init__
(num_parents, num_offspring=1, mutation_strength=0.25, learning_param=None, sorting_component=None, cov_matrix=None, **kwargs)¶ Constructor.
If the learning parameter is set to zero, adaptation of mutation strength is disabled. See [Beyer2002] for more information.
Parameters: - num_parents (int) – The number of parents used for recombination. Normally, it should be chosen equal to the population size.
- num_offspring (int, optional) – The number of offspring to generate from one call to
_recombine()
. - mutation_strength (float, optional) – The strategy parameter to be adapted.
- learning_param (float, optional) – The first learning parameter for self-adaptation. Default value is chosen according to [Beyer2002].
- sorting_component (SortingComponent, optional) – If this sorting component is given, recombination uses the weighted mean of the parents, with weights depending on fitness (according to the CMA-ES weighting rule). Otherwise, it is just the conventional mean, as recommended by [Beyer2008].
- cov_matrix (numpy array, optional) – The covariance matrix used for mutation. The identity matrix is chosen by default.
- kwargs – Arbitrary keyword arguments, passed to the super class.
-
_mutate
()¶ Self-adapt covariance matrix and mutate genome.
-
_recombine
(others)¶ Produce arbitrary number of kids from arbitrary number of parents.
Parameters: others (iterable of Individual) – Other parents to recombine this individual with. Returns: children – A list containing num_offspring children. Return type: list of Individual
-
get_weight_assignments
(individuals)¶ Determine weights from individuals’ fitness.
-
static
weighted_mean
(genomes, weights=None)¶ Calculate weighted mean of vectors.
-
-
class
evoalgos.individual.
DEIndividual
(crossover_constant=0.9, weighting_factor=0.8, num_differences=1, operation_mode='default', **kwargs)¶ An individual that uses variation from Differential Evolution.
The variants DE/something/n and DE/current_to_something/n are supported, where ‘something’ is determined by the reproduction component.
-
__init__
(crossover_constant=0.9, weighting_factor=0.8, num_differences=1, operation_mode='default', **kwargs)¶ Constructor.
Parameters: - crossover_constant (float, optional) – The probability to apply this variation operator per variable.
- weighting_factor (float, optional) – A scalar multiplied with the difference vector.
- num_differences (int, optional) – The number of difference vectors to add.
- operation_mode (str, optional) – Must be ‘default’, ‘rand’, ‘best’, ‘current_to_rand’, or ‘current_to_best’.
- kwargs – Arbitrary keyword arguments, passed to the super class.
-
_mutate
()¶ Does nothing because mutation is integrated in recombination.
-
_recombine
(others)¶ Recombination with vector differences of parent vectors.
Parameters: others (iterable of Individual) – Other parents to recombine this individual with. Returns: child – A list containing exactly one child. Return type: list of Individual
-
-
class
evoalgos.individual.
SBXIndividual
(crossover_dist_index=10, mutation_dist_index=10, crossover_prob=0.7, mutation_prob=0.1, symmetric_variation_prob=0.0, **kwargs)¶ An individual imitating binary variation on a real-valued genome.
Recombination is implemented as simulated binary crossover (SBX). Mutation is polynomial mutation. This kind of individual is often used in genetic algorithms.
-
__init__
(crossover_dist_index=10, mutation_dist_index=10, crossover_prob=0.7, mutation_prob=0.1, symmetric_variation_prob=0.0, **kwargs)¶ Constructor.
Parameters: - crossover_dist_index (float, optional) – Controls the variance of the distribution used for recombination. The higher this value, the lower the variance.
- mutation_dist_index (float, optional) – Controls the variance of the distribution used for mutation. The higher this value, the lower the variance.
- crossover_prob (float, optional) – The probability to recombine a single gene.
- mutation_prob (float, optional) – The probability to mutate a single gene.
- symmetric_variation_prob (float, optional) – The probability for enforcing symmetric mutation and recombination distributions. If symmetry is not enforced and bound-constraints exist, the distributions have the whole search space as domain. See [Wessing2009] for further information on this parameter.
- kwargs – Arbitrary keyword arguments, passed to the super class.
References
[Wessing2009] Simon Wessing. Towards Optimal Parameterizations of the S-Metric Selection Evolutionary Multi-Objective Algorithm. Diploma thesis, Algorithm Engineering Report TR09-2-006, Technische Universität Dortmund, 2009. https://ls11-www.cs.uni-dortmund.de/_media/techreports/tr09-06.pdf
-
_mutate
()¶ Mutate this individual with polynomial mutation.
This mutation follows the same concept as SBX.
-
_recombine
(others)¶ Produce two children from two parents.
This kind of crossover is claimed to have self-adaptive features because the distance of the children to their parents is influenced by the parents’ distance [Deb2001].
Parameters: others (iterable of Individual) – Other parents to recombine this individual with. Returns: children – A list containing two children. Return type: list of Individual References
[Deb2001] Kalyanmoy Deb and Hans-Georg Beyer (2001). Self-Adaptive Genetic Algorithms with Simulated Binary Crossover. Evolutionary Computation, 9:2, pp. 197-221.
-
calc_beta_q
(rand, alpha=2.0)¶ Helper function for crossover.
-
calc_delta_q
(rand, delta_lower, delta_upper)¶ Helper function for mutation.
-