Ejemplo n.º 1
0
def test_uniform_distribution():
    """check that every gene of the tensor are going to be flipped equally
    Note:
    # ! We need enough iterations and chromosomes to reduce collision
    # ! and ensure numerical stability
    """

    NUM_ITERATIONS = 300
    GENOME_SHAPE = (20, 4, 4)
    population = B.randint(0, 1024, GENOME_SHAPE)
    population_fraction = 1
    crossover_probability = (0.5, 0.5)

    OP = RandomMutations2D(population_fraction, crossover_probability)

    # diff matrix
    previous_population = B.copy(population)
    population = OP(population)
    diff = B.clip(abs(population - previous_population), 0, 1)
    for _ in range(NUM_ITERATIONS - 1):
        previous_population = B.copy(population)
        population = OP(population)

        curr_diff = B.clip(abs(population - previous_population), 0, 1)
        # acumulating diff matrix
        diff += curr_diff

    print(curr_diff)

    for c in diff:
        print(c)
        print('mean', B.mean(c), 'min', B.min(c), 'max', B.max(c))
        assert B.min(c) > NUM_ITERATIONS // 15
        assert B.max(c) < NUM_ITERATIONS // 2
        assert NUM_ITERATIONS // 8 < B.mean(c) < NUM_ITERATIONS // 2
Ejemplo n.º 2
0
def solve_onmax_2d(args):
    """Solve the onemax problem
    """
    population_shape, generations = args
    population = randint_population(population_shape, 1)
    inputs = Input(population_shape)
    x = RandomMutations2D(max_gene_value=1)(inputs)
    outputs = UniformCrossover2D()(x)
    gf = EvoFlow(inputs, outputs, debug=0)
    fitness_function = Sum(max_sum_value=10000000)
    evolution_strategy = SelectFittest()
    gf.compile(evolution_strategy, fitness_function)
    gf.evolve(population, generations=generations, verbose=0)
Ejemplo n.º 3
0
def test_mutation2d_eager():
    pop_shape = (10, 4, 4)
    max_gene_value = 10
    min_gene_value = 0
    population_fraction = 1
    mutations_probability = (0.5, 0.5)
    min_mutation_value = 1
    max_mutation_value = 1

    population = randint_population(pop_shape, max_gene_value)

    # save original
    original_population = B.copy(population)
    cprint('[Initial genepool]', 'blue')
    cprint(original_population, 'blue')

    for _ in range(3):
        RM = RandomMutations2D(population_fraction=population_fraction,
                               mutations_probability=mutations_probability,
                               min_gene_value=min_gene_value,
                               max_gene_value=max_gene_value,
                               min_mutation_value=min_mutation_value,
                               max_mutation_value=max_mutation_value,
                               debug=True)
        population = RM(population)

        cprint('\n[Mutated genepool]', 'yellow')
        cprint(population, 'yellow')

        cprint('\n[Diff]', 'magenta')
        diff = population - original_population
        cprint(diff, 'magenta')

        assert B.is_tensor(population)
        assert population.shape == pop_shape
        assert B.max(diff) <= max_mutation_value
        ok = 0
        for chromosome in diff:
            if B.sum(chromosome) >= 2:
                ok += 1

        if (ok / len(diff)) > 0.5:
            break

    assert (ok / len(diff)) > 0.5
Ejemplo n.º 4
0
def test_mutation2d_graph_mode():
    "make sure the boxing / unboxing works in graph mode"
    pop_shape = (2, 4, 4)
    max_gene_value = 10
    min_gene_value = 0
    population_fraction = 1
    mutations_probability = (0.5, 0.5)
    min_mutation_value = 1
    max_mutation_value = 1

    population = B.randint(0, max_gene_value, pop_shape)

    RM = RandomMutations2D(population_fraction=population_fraction,
                           mutations_probability=mutations_probability,
                           min_gene_value=min_gene_value,
                           max_gene_value=max_gene_value,
                           min_mutation_value=min_mutation_value,
                           max_mutation_value=max_mutation_value,
                           debug=True)

    population = RM._call_from_graph(population)
    assert B.is_tensor(population)
    assert population.shape == pop_shape