leap_ec.segmented_rep package

Submodules

leap_ec.segmented_rep.decoders module

Used to decode segments

class leap_ec.segmented_rep.decoders.SegmentedDecoder(segment_decoder)

Bases: Decoder

For decoding LEAP segmented representations

>>> from leap_ec.binary_rep.decoders import BinaryToIntDecoder

This example presumes that each segment has five bits, the first to map to an integer and the remaining three to a different integer.

>>> import numpy as np
>>> decoder = SegmentedDecoder(BinaryToIntDecoder(2,3))
>>> genome = np.array([[1, 0, 1, 0, 1],
...                    [0, 0, 1, 1, 1],
...                    [1, 0, 0, 0, 1]])
>>> vals = decoder.decode(genome)
>>> assert np.all(vals == np.array([[2, 5], [0, 7], [2, 1]]))
decode(genome, *args, **kwargs)

For decoding genome which is a list of lists, or a segmented representation.

Parameters
  • genome (will be a list of segments (or lists)) – for a given individual

  • args (list) – optional args

  • kwargs (dict) – optional keyword args

Returns

a list of list of values decoded from genome

Return type

list

leap_ec.segmented_rep.initializers module

Used to initialize segments

leap_ec.segmented_rep.initializers.create_segmented_sequence(length, seq_initializer)

Create a segmented test_sequence

A segment is a list of lists. seq_initializer is used to create length individual segments, which allows for the using any of the pre-supplied initializers for a regular genomic test_sequence, or for making your own.

length denotes how many segments to generate. If it’s an integer, then we will create length segments. However, if it’s a function that draws from a random distribution that returns an int, we will, instead, use that to calculate the number of segments to generate.

>>> from leap_ec.binary_rep.initializers import create_binary_sequence
>>> segmented_initializer = create_segmented_sequence(3, create_binary_sequence(3))
>>> segments = segmented_initializer()
>>> assert len(segments) == 3
Parameters
  • length (int or Callable) – How many segments?

  • seq_initializer (Callable) – initializer for creating individual sequences

Returns

function that returns a list of segmented

Return type

Callable

leap_ec.segmented_rep.ops module

Segmented representation specific pipeline operators.

leap_ec.segmented_rep.ops.add_segment(next_individual: Iterator = '__no__default__', seq_initializer: Callable = '__no__default__', probability: float = '__no__default__', append: bool = False) Iterator

Possibly add a segment to the given individual

New segments can be always appended, or randomly inserted within the individual’s genome.

TODO add a parameter for accepting a function that will yield a distribution for the number of segments to be randomly inserted.

>>> from leap_ec.individual import Individual
>>> from leap_ec.binary_rep.initializers import create_binary_sequence
>>> import numpy as np
>>> original = Individual([np.array([0, 0]), np.array([1, 1])])
>>> mutated = next(add_segment(iter([original]),
...                seq_initializer=create_binary_sequence(2),
...                probability=1.0))
Parameters
  • next_individual – to possibly add a segment

  • seq_initializer – callable for initializing any new segments

  • probability – likelihood of adding a segment

  • append – if True, always append any new segments

Returns

yielded individual with a possible new segment

leap_ec.segmented_rep.ops.apply_mutation(next_individual: Iterator = '__no__default__', mutator: Callable[[list, float], list] = '__no__default__', expected_num_mutations: float = 1.0) Iterator

This expects next_individual to have a segmented representation; i.e., a test_sequence of sequences. mutator will be applied to each sub-test_sequence with the expected probability. The expected probability applies to all the sequences, and defaults to a single mutation among all components, on average.

>>> from leap_ec.binary_rep.ops import genome_mutate_bitflip
>>> mutation_op = apply_mutation(mutator=genome_mutate_bitflip)
>>> import numpy as np
>>> from leap_ec.individual import Individual
>>> original = Individual(np.array([[0, 0], [1, 1]]))
>>> mutated = next(mutation_op(iter([original])))
Parameters
  • next_individual – to possibly mutate

  • mutator – function to be applied to each segment in the individual’s genome; first argument is a segment, the second the expected probability of mutating each segment element.

  • expected – expected mutations on average in [0.0,1.0]

Returns

yielded mutated individual

leap_ec.segmented_rep.ops.copy_segment(next_individual: Iterator = '__no__default__', probability: float = '__no__default__', append: bool = False) Iterator

with a given probability, randomly select and copy a segment

>>> from leap_ec.individual import Individual
>>> import numpy as np
>>> original = Individual([np.array([0, 0])])
>>> mutated = next(copy_segment(iter([original]), probability=1.0))
>>> assert np.all(mutated.genome[0] == [0, 0])            and np.all(mutated.genome[1] == [0, 0])
param next_individual

to have a segment possibly removed

param probability

likelihood of doing this

param append

if True, always append any new segments

returns

the next individual

leap_ec.segmented_rep.ops.remove_segment(next_individual: Iterator = '__no__default__', probability: float = '__no__default__') Iterator

for some chance, remove a segment

Nothing happens if the individual has a single segment; i.e., there is no chance for an empty individual to be returned.

>>> from leap_ec.individual import Individual
>>> import numpy as np
>>> original = Individual([np.array([0, 0]), np.array([1, 1])])
>>> mutated = next(remove_segment(iter([original]), probability=1.0))
>>> assert np.all(mutated.genome[0] == [0, 0])             or np.all(mutated.genome[0] == [1, 1])
param next_individual

to have a segment possibly removed

param probability

likelihood of removing a segment

returns

the next individual

leap_ec.segmented_rep.ops.segmented_mutate(next_individual: Iterator = '__no__default__', mutator_functions: list = '__no__default__')

A mutation operator that applies a different mutation operator to each segment of a segmented genome.

Module contents