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
def test_binary_val_default_params():
    pop_shape = (6, 4)
    population = randint_population(pop_shape, 2)
    population = RandomMutations1D(max_gene_value=1, debug=1)(population)
    print(population)
    assert B.max(population) == 1
    assert not B.min(population)
示例#3
0
def test_randintpop():
    shape = (100, 100, 10)
    pop = randint_population(shape, 42, 1)
    assert pop.shape == shape
    assert B.max(pop) == 42
    assert B.min(pop) == 1
    assert pop.dtype == B.intx()
示例#4
0
def test_uniformpop():
    shape = (100, 100)
    pop = uniform_population(shape)
    assert pop.shape == shape
    assert B.max(pop) == 99
    assert B.min(pop) == 0

    for chrm in pop:
        _, _, count = B.unique_with_counts(chrm)
        assert B.max(count) == 1
def test_clip():
    pop_shape = (100, 100)
    population = randint_population(pop_shape, 100)
    population = RandomMutations1D(max_gene_value=100, debug=1)(population)
    assert B.max(population) <= 100
    assert not B.min(population)