Exemplo n.º 1
0
def test_binary_val_default_params():
    pop_shape = (6, 4)
    population = B.randint(0, 2, pop_shape)
    population = RandomMutations1D(max_gene_value=1, debug=1)(population)
    print(population)
    assert B.max(population) == 1
    assert not B.min(population)
Exemplo n.º 2
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 = 1000
    GENOME_SHAPE = (20, 4, 4)
    population = B.randint(0, 1024, GENOME_SHAPE)
    population_fraction = 1
    crossover_probability = (0.5, 0.5)

    # each gene proba of being mutated 0.5*0.5 > 0.25
    # each chromosome proba of being mutated 1
    # => gene average hit rate: 1000 / (1/4)  ~250
    MIN_DIFF_BOUND = 200
    MAX_DIFF_BOUND = 300

    OP = RandomMutations2D(population_fraction, crossover_probability)

    # diff matrix
    previous_population = copy(population)
    population = OP(population)
    diff = B.clip(abs(population - previous_population), 0, 1)
    for _ in range(NUM_ITERATIONS - 1):
        previous_population = 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) > MIN_DIFF_BOUND
        assert B.max(c) < MAX_DIFF_BOUND
        assert MIN_DIFF_BOUND < B.mean(c) < MAX_DIFF_BOUND
Exemplo n.º 3
0
def test_dualcrossover2d_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 = 1000
    GENOME_SHAPE = (100, 4, 4)
    population = B.randint(0, 1024, GENOME_SHAPE)
    population_fraction = 1
    crossover_probability = (0.5, 0.5)

    OP = DualCrossover2D(population_fraction, crossover_probability)

    # diff matrix
    previous_population = copy(population)
    population = OP(population)
    diff = B.clip(abs(population - previous_population), 0, 1)
    print(diff)

    for _ in range(NUM_ITERATIONS - 1):
        previous_population = 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) > 50
        assert B.max(c) < NUM_ITERATIONS / 2
        assert 200 < B.mean(c) < NUM_ITERATIONS / 2
Exemplo n.º 4
0
def test_randintpop():
    shape = (100, 100, 10)
    pop = genRandIntPopulation(shape, 42, 1)
    assert pop.shape == shape
    assert B.max(pop) == 42
    assert B.min(pop) == 1