leap_ec.binary_rep package
Submodules
leap_ec.binary_rep.decoders module
Decoders for binary representations.
- class leap_ec.binary_rep.decoders.BinaryToIntDecoder(*descriptors)
Bases:
Decoder
A decoder that converts a Boolean-vector genome into an integer-vector phenome.
- decode(genome, *args, **kwargs)
Converts a Boolean genome to an integer-vector phenome by interpreting each segment of the genome as low-endian binary number.
- Parameters
genome – a list of 0s and 1s representing a Boolean genome
- Returns
a corresponding list of ints representing the integer-vector phenome
For example, a Boolean representation of [1, 12, 5] can be decoded like this:
>>> import numpy as np >>> d = BinaryToIntDecoder(4, 4, 4) >>> b = np.array([0,0,0,1, 1, 1, 0, 0, 0, 1, 1, 0]) >>> d.decode(b) array([ 1, 12, 6])
- class leap_ec.binary_rep.decoders.BinaryToIntGreyDecoder(*descriptors)
Bases:
BinaryToIntDecoder
This performs Gray encoding when converting from binary strings.
See also: https://en.wikipedia.org/wiki/Gray_code#Converting_to_and_from_Gray_code
For example, a grey encoded Boolean representation of [1, 8, 4] can be decoded like this:
>>> import numpy as np >>> d = BinaryToIntGreyDecoder(4, 4, 4) >>> b = np.array([0,0,0,1, 1, 1, 0, 0, 0, 1, 1, 0]) >>> d.decode(b) array([1, 8, 4])
- decode(genome, *args, **kwargs)
Converts a Boolean genome to an integer-vector phenome by interpreting each segment of the genome as low-endian binary number.
- Parameters
genome – a list of 0s and 1s representing a Boolean genome
- Returns
a corresponding list of ints representing the integer-vector phenome
For example, a Boolean representation of [1, 12, 5] can be decoded like this:
>>> import numpy as np >>> d = BinaryToIntDecoder(4, 4, 4) >>> b = np.array([0,0,0,1, 1, 1, 0, 0, 0, 1, 1, 0]) >>> d.decode(b) array([ 1, 12, 6])
- class leap_ec.binary_rep.decoders.BinaryToRealDecoder(*segments)
Bases:
BinaryToRealDecoderCommon
- class leap_ec.binary_rep.decoders.BinaryToRealDecoderCommon(*segments)
Bases:
Decoder
Common implementation for binary to real decoders.
The base classes BinaryToRealDecoder and BinaryToRealGreyDecoder differ by just the underlying binary to integer decoder. Most all the rest of the binary integer to real-value decoding is the same, hence this class.
- decode(genome, *args, **kwargs)
Convert a list of binary values into a real-valued vector.
- class leap_ec.binary_rep.decoders.BinaryToRealGreyDecoder(*segments)
Bases:
BinaryToRealDecoderCommon
leap_ec.binary_rep.initializers module
Used to initialize binary sequences
- leap_ec.binary_rep.initializers.create_binary_sequence(length)
A closure for initializing a binary sequences for binary genomes.
- Parameters
length – how many genes?
- Returns
a function that, when called, generates a binary vector of given length
E.g., can be used for Individual.create_population
>>> from leap_ec.decoder import IdentityDecoder >>> from . problems import MaxOnes >>> population = Individual.create_population(10, create_binary_sequence(length=10), ... decoder=IdentityDecoder(), ... problem=MaxOnes())
leap_ec.binary_rep.ops module
Binary representation specific pipeline operators.
- leap_ec.binary_rep.ops.genome_mutate_bitflip(genome: ndarray = '__no__default__', expected_num_mutations: float = None, probability: float = None) ndarray
Perform bitflip mutation on a particular genome.
This function can be used by more complex operators to mutate a full population (as in mutate_bitflip), to work with genome segments (as in leap_ec.segmented.ops.apply_mutation), etc. This way we don’t have to copy-and-paste the same code for related operators.
- Parameters
genome – of binary digits that we will be mutating
expected_num_mutations – on average how many mutations are we expecting?
- Returns
mutated genome
- leap_ec.binary_rep.ops.mutate_bitflip(next_individual: Iterator = '__no__default__', expected_num_mutations: float = None, probability: float = None) Iterator
Perform bit-flip mutation on each individual in an iterator (population).
This assumes that the genomes have a binary representation.
>>> from leap_ec.individual import Individual >>> from leap_ec.binary_rep.ops import mutate_bitflip >>> import numpy as np
>>> original = Individual(np.array([1, 1])) >>> op = mutate_bitflip(expected_num_mutations=1) >>> pop = iter([original]) >>> mutated = next(op(pop))
- Parameters
next_individual – to be mutated
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)
- Returns
mutated individual
- leap_ec.binary_rep.ops.random() x in the interval [0, 1).
leap_ec.binary_rep.problems module
A set of standard EA problems that rely on a binary-representation
- class leap_ec.binary_rep.problems.DeceptiveTrap(maximize=True)
Bases:
ScalarProblem
A simple bi-modal function whose global optimum is the Boolean vector of all 1’s, but in which fitness decreases as the number of 1’s in the vector increases—giving it a local optimum of [0, …, 0] with a very wide basin of attraction.
- evaluate(phenome)
>>> import numpy as np >>> p = DeceptiveTrap()
The trap function has a global maximum when the number of one’s is maximized:
>>> p.evaluate(np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])) 10
It’s minimized when we have just one zero: >>> p.evaluate(np.array([1, 1, 1, 1, 0, 1, 1, 1, 1, 1])) 0
And has a local optimum when we have no ones at all: >>> p.evaluate(np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])) 9
- class leap_ec.binary_rep.problems.ImageProblem(path, maximize=True, size=(100, 100))
Bases:
ScalarProblem
A variation on max_ones that uses an external image file to define a binary target pattern.
- evaluate(phenome)
Evaluate the given phenome.
Practitioners must over-ride this member function.
Note that by default the individual comparison operators assume a maximization problem; if this is a minimization problem, then just negate the value when returning the fitness.
- Parameters
phenome – the phenome to evaluate (this will not be modified)
- Returns
the fitness value
- class leap_ec.binary_rep.problems.LeadingOnes(target_string=None, maximize=True)
Bases:
ScalarProblem
Implementation of the classic leading-ones problem, where the individuals are represented by a bit vector.
By default, the number of consecutve 1’s starting from the beginning of the phenome are maximized:
>>> p = LeadingOnes()
But an optional target string can also be specified, in which case the number of matches to the target are maximized:
>>> import numpy as np >>> p = LeadingOnes(target_string=np.array([1, 1, 0, 1, 1, 0, 0, 0 ,0]))
- evaluate(phenome)
By default this counts the number of consecutive 1’s at the start of the string:
>>> import numpy as np >>> p = LeadingOnes() >>> p.evaluate(np.array([1, 1, 1, 1, 0, 1, 0, 1, 1])) 4
Or, if a target string was given, we count matches:
>>> p = LeadingOnes(target_string=np.array([1, 1, 0, 1, 1, 0, 0, 0 ,0])) >>> p.evaluate(np.array([1, 1, 1, 1, 0, 1, 0, 1, 1])) 2
- class leap_ec.binary_rep.problems.MaxOnes(target_string=None, maximize=True)
Bases:
ScalarProblem
Implementation of the classic max-ones problem, where the individuals are represented by a bit vector.
By default, the number of 1’s in the phenome are maximized.
>>> p = MaxOnes()
But an optional target string can also be specified, in which case the number of matches to the target are maximized:
>>> import numpy as np >>> p = MaxOnes(target_string=np.array([1, 1, 1, 1, 1, 0, 0, 0 ,0]))
- evaluate(phenome)
By default this counts the number of 1’s:
>>> from leap_ec.individual import Individual >>> import numpy as np >>> p = MaxOnes() >>> p.evaluate(np.array([0, 0, 1, 1, 0, 1, 0, 1, 1])) 5
Or, if a target string was given, we count matches:
>>> from leap_ec.individual import Individual >>> import numpy as np >>> p = MaxOnes(target_string=np.array([1, 1, 1, 1, 1, 0, 0, 0 ,0])) >>> p.evaluate(np.array([0, 0, 1, 1, 0, 1, 0, 1, 1])) 3
- class leap_ec.binary_rep.problems.TwoMax(maximize=True)
Bases:
ScalarProblem
A simple bi-modal function that returns the number of 1’s if there are more 1’s than 0’s, else the number of 0’s.
Also known as the “Twin-Peaks” problem.
- evaluate(phenome)
>>> import numpy as np >>> p = TwoMax()
The TwoMax problems returns the number over 1’s if they are in the majority:
>>> p.evaluate(np.array([1, 1, 1, 1, 1, 1, 1, 0, 0, 0])) 7
Else the number of zeros: >>> p.evaluate(np.array([0, 0, 0, 1, 0, 0, 0, 1, 1, 1])) 6