Exemple #1
0
def test_truncation_parents_selection():
    """ Test (mu + lambda), i.e., parents competing with offspring

    Create parent and offspring populations such that each has an "best" individual that will be selected by
    truncation selection.
    """
    parents = [
        Individual([0, 0, 0], decoder=IdentityDecoder(), problem=MaxOnes()),
        Individual([1, 1, 0], decoder=IdentityDecoder(), problem=MaxOnes())
    ]

    parents = Individual.evaluate_population(parents)

    offspring = [
        Individual([0, 0, 1], decoder=IdentityDecoder(), problem=MaxOnes()),
        Individual([1, 1, 1], decoder=IdentityDecoder(), problem=MaxOnes())
    ]
    offspring = Individual.evaluate_population(offspring)

    truncated = ops.truncation_selection(offspring, 2, parents=parents)

    assert len(truncated) == 2

    assert parents[1] in truncated
    assert offspring[1] in truncated
Exemple #2
0
def test_tournament_selection():
    """ This simple binary tournament_selection selection """
    # Make a population where binary tournament_selection has an obvious reproducible choice
    pop = [
        Individual([0, 0, 0], decoder=IdentityDecoder(), problem=MaxOnes()),
        Individual([1, 1, 1], decoder=IdentityDecoder(), problem=MaxOnes())
    ]

    # We first need to evaluate all the individuals so that truncation selection has fitnesses to compare
    pop = Individual.evaluate_population(pop)

    best = next(ops.tournament_selection(pop))
    pass
Exemple #3
0
def test_simple_evaluate():
    # Let's try evaluating a single individual
    pop = [Individual([1, 1], decoder=IdentityDecoder(), problem=MaxOnes())]

    evaluated_individual = next(ops.evaluate(iter(pop)))

    assert evaluated_individual.fitness == 2
Exemple #4
0
 def __init__(self,
              initialize,
              decoder=IdentityDecoder(),
              individual_cls=Individual):
     self.decoder = decoder
     self.initialize = initialize
     self.individual_cls = individual_cls
Exemple #5
0
def test_multiple_evaluations():
    # Let's try evaluating a single individual
    pop = [
        Individual([0, 0], decoder=IdentityDecoder(), problem=MaxOnes()),
        Individual([0, 1], decoder=IdentityDecoder(), problem=MaxOnes()),
        Individual([1, 0], decoder=IdentityDecoder(), problem=MaxOnes()),
        Individual([1, 1], decoder=IdentityDecoder(), problem=MaxOnes())
    ]

    evaluated_individuals = Individual.evaluate_population(pop)

    # Since this is the MAX ONES problem, we just count the ones ahead of
    # time, and ensure that the actual fitnesses match up.
    expected_fitnesses = [0, 1, 1, 2]

    for individual, fitness in zip(evaluated_individuals, expected_fitnesses):
        assert individual.fitness == fitness
Exemple #6
0
def test_truncation_selection():
    """ Basic truncation selection test"""
    pop = [
        Individual([0, 0, 0], decoder=IdentityDecoder(), problem=MaxOnes()),
        Individual([0, 0, 1], decoder=IdentityDecoder(), problem=MaxOnes()),
        Individual([1, 1, 0], decoder=IdentityDecoder(), problem=MaxOnes()),
        Individual([1, 1, 1], decoder=IdentityDecoder(), problem=MaxOnes())
    ]

    # We first need to evaluate all the individuals so that truncation selection has fitnesses to compare
    pop = Individual.evaluate_population(pop)

    truncated = ops.truncation_selection(pop, 2)

    assert len(truncated) == 2

    # Just to make sure, check that the two best individuals from the original population are in the selected population
    assert pop[2] in truncated
    assert pop[3] in truncated
Exemple #7
0
def test_naive_cyclic_selection():
    """ Test of the naive deterministic cyclic selection """
    pop = [
        Individual([0, 0], decoder=IdentityDecoder(), problem=MaxOnes()),
        Individual([0, 1], decoder=IdentityDecoder(), problem=MaxOnes())
    ]

    # This selection operator will deterministically cycle through the
    # given population
    selector = ops.naive_cyclic_selection(pop)

    selected = next(selector)
    assert selected.genome == [0, 0]

    selected = next(selector)
    assert selected.genome == [0, 1]

    # And now we cycle back to the first individual
    selected = next(selector)
    assert selected.genome == [0, 0]
Exemple #8
0
def test_broken_evaluate():
    # Test evaluations that throw exception
    pop = [
        RobustIndividual([1, 1],
                         decoder=IdentityDecoder(),
                         problem=BrokenProblem(True))
    ]

    evaluated_individual = next(ops.evaluate(iter(pop)))

    assert evaluated_individual.fitness is nan
    assert evaluated_individual.is_viable is False
    assert isinstance(evaluated_individual.exception, RuntimeError)
Exemple #9
0
def test_mutate_bitflip():
    # Create a very simple individual with two binary genes of all ones.
    ind = [Individual([1, 1], decoder=IdentityDecoder(),
                           problem=MaxOnes())]

    # Now mutate the individual such that we *expect both bits to bitflip*
    mutated_ind = next(ops.mutate_bitflip(iter(ind), expected_num_mutations=2))

    assert mutated_ind.genome == [0, 0]

    # Of course, since we didn't clone the original, well, that actually got
    # zapped, too.

    assert ind[0].genome == [0, 0]