コード例 #1
0
def test_valid_get_bit_positions(number, result):
    ga = BinaryGA(list(range(10)), fitness_test_func)

    bin_repr = ga._get_bit_positions(number)

    assert len(bin_repr) == len(result)

    for bit in result:
        assert bit in bin_repr
コード例 #2
0
def test_valid_get_bit_positions(number, result):
    ga = BinaryGA(list(range(10)), fitness_test_func)

    bin_repr = ga._get_bit_positions(number)

    assert len(bin_repr) == len(result)

    for bit in result:
        assert bit in bin_repr
コード例 #3
0
def test_valid_replace_bits(start, stop, result):
    ga = BinaryGA(list(range(21)), fitness_test_func, cross_prob=1)

    source = [10, 5, 20]
    mutant = ga._replace_bits(source, [], start, stop)

    assert len(mutant) == len(result)

    for bit in result:
        assert bit in mutant
コード例 #4
0
def test_valid_replace_bits(start, stop, result):
    ga = BinaryGA(list(range(21)), fitness_test_func, cross_prob=1)

    source = [10, 5, 20]
    mutant = ga._replace_bits(source, [], start, stop)

    assert len(mutant) == len(result)

    for bit in result:
        assert bit in mutant
コード例 #5
0
def test_generate_random_population(size):
    ga = BinaryGA(list(range(5)), fitness_test_func)
    max_num = 2**5

    population = ga._generate_random_population(max_num, size)

    assert len(population) == size
    assert len(population) == len(set(population))

    for num in population:
        assert max_num > num >= 1
コード例 #6
0
def test_generate_random_population(size):
    ga = BinaryGA(list(range(5)), fitness_test_func)
    max_num = 2**5

    population = ga._generate_random_population(max_num, size)

    assert len(population) == size
    assert len(population) == len(set(population))

    for num in population:
        assert max_num > num >= 1
コード例 #7
0
def test_mutate(mut_type):
    """
    *_mutate()* method is the same (but bit inversion process itself is not)
    for both types of GA: RealGA and Binary GA and thus,
    it is not necessary to test it twice.
    """
    ga = BinaryGA(list(range(10)), fitness_test_func, mut_type=mut_type, mut_prob=1)

    chromosome = []
    mutant = ga._mutate(chromosome)

    assert len(mutant) == mut_type
コード例 #8
0
def test_invert_bit():
    ga = BinaryGA(test_bin_data, fitness_test_func, mut_prob=1)

    chromosome = [10, 5, 20]
    bit_to_invert = [10, 7, 9]

    mutant = ga._invert_bit(chromosome, bit_to_invert)

    assert len(mutant) == 4
    assert 10 not in mutant
    assert 5 in mutant
    assert 20 in mutant
    assert 7 in mutant
    assert 9 in mutant
コード例 #9
0
def test_invert_bit():
    ga = BinaryGA(test_bin_data, fitness_test_func, mut_prob=1)

    chromosome = [10, 5, 20]
    bit_to_invert = [10, 7, 9]

    mutant = ga._invert_bit(chromosome, bit_to_invert)

    assert len(mutant) == 4
    assert 10 not in mutant
    assert 5 in mutant
    assert 20 in mutant
    assert 7 in mutant
    assert 9 in mutant
コード例 #10
0
def test_mutate(mut_type):
    """
    *_mutate()* method is the same (but bit inversion process itself is not)
    for both types of GA: RealGA and Binary GA and thus,
    it is not necessary to test it twice.
    """
    ga = BinaryGA(list(range(10)),
                  fitness_test_func,
                  mut_type=mut_type,
                  mut_prob=1)

    chromosome = []
    mutant = ga._mutate(chromosome)

    assert len(mutant) == mut_type
コード例 #11
0
def test_init_random_population_real(optim, type):
    size, dim, interval = 9, 1, (-5, 5)

    if type == 'real':
        ga = RealGA(fitness_test_linear_func, optim=optim)
        dga = DiffusionGA(ga)

        dga.init_random_population(size, dim, interval)
    else:
        ga = BinaryGA(test_bin_data, fitness_test_func, optim=optim)
        dga = DiffusionGA(ga)
        dga.init_random_population(size)

    assert dga.population[0].size == size
    assert dga.population[1].size == size

    shape = dga.population[0].shape
    if optim == 'max':
        for row in range(shape[0]):
            for column in range(shape[1]):
                assert dga.population[1][row][column] <= dga.best_solution[1]
    else:
        for row in range(shape[0]):
            for column in range(shape[1]):
                assert dga.population[1][row][column] >= dga.best_solution[1]
コード例 #12
0
def test_init_random_population(optim):
    ga = BinaryGA(test_bin_data, fitness_test_func, optim=optim)

    pop_size = 5
    ga.init_random_population(pop_size)

    assert len(ga.population) == pop_size
    assert ga.population[pop_size - 1].fitness_val == ga.best_solution[1]
    assert ga.population[pop_size - 1].chromosome == ga.best_solution[0]

    if optim == 'max':
        for i in range(pop_size - 1):
            assert ga.population[i].fitness_val <= ga.best_solution[1]
            assert ga.population[0].fitness_val <= ga.population[i].fitness_val
    else:
        for i in range(pop_size - 1):
            assert ga.population[i].fitness_val >= ga.best_solution[1]
            assert ga.population[0].fitness_val >= ga.population[i].fitness_val
コード例 #13
0
def test_init_random_population(optim):
    ga = BinaryGA(test_bin_data, fitness_test_func, optim=optim)

    pop_size = 5
    ga.init_random_population(pop_size)

    assert len(ga.population) == pop_size
    assert ga.population[pop_size - 1].fitness_val == ga.best_solution[1]
    assert ga.population[pop_size - 1].chromosome == ga.best_solution[0]

    if optim == 'max':
        for i in range(pop_size - 1):
            assert ga.population[i].fitness_val <= ga.best_solution[1]
            assert ga.population[0].fitness_val <= ga.population[i].fitness_val
    else:
        for i in range(pop_size - 1):
            assert ga.population[i].fitness_val >= ga.best_solution[1]
            assert ga.population[0].fitness_val >= ga.population[i].fitness_val
コード例 #14
0
def test_valid_init_population(optim):
    chromosomes = list(test_bin_population)
    expected_population = []
    for chromosome in chromosomes:
        expected_population.append(IndividualGA(chromosome, fitness_test_func(chromosome, test_bin_data)))

    expected_population = sort_population(optim, expected_population)

    ga = BinaryGA(test_bin_data, fitness_test_func, optim=optim)
    ga.init_population(test_bin_population)

    best_solution = ga.best_solution
    if optim == 'min':
        assert test_bin_population_best_min[0] == best_solution[0]
    else:
        assert test_bin_population_best_max[0] == best_solution[0]

    for actual, expected in zip(ga.population, expected_population):
        assert actual.chromosome == expected.chromosome
コード例 #15
0
def test_cross(cross_type):
    """
    *_cross()* method is the same (but bit replacement process itself is not)
    for both types of GA: RealGA and Binary GA and thus,
    it is not necessary to test it twice.
    """
    ga = BinaryGA(test_bin_data, fitness_test_func, cross_type=cross_type, cross_prob=1)

    source_chrom = list(range(len(test_bin_data)))
    child = ga._cross([], source_chrom)

    print(child)

    if cross_type > 2:
        assert len(child) == cross_type
    else:
        # single- and two-point crossover differs from other types
        # we don't know the exact replacement interval as its end points are random numbers
        # but this interval must be at least 1
        assert len(child) >= 1
コード例 #16
0
def test_valid_init_population(optim):
    chromosomes = list(test_bin_population)
    expected_population = []
    for chromosome in chromosomes:
        expected_population.append(
            IndividualGA(chromosome,
                         fitness_test_func(chromosome, test_bin_data)))

    expected_population = sort_population(optim, expected_population)

    ga = BinaryGA(test_bin_data, fitness_test_func, optim=optim)
    ga.init_population(test_bin_population)

    best_solution = ga.best_solution
    if optim == 'min':
        assert test_bin_population_best_min[0] == best_solution[0]
    else:
        assert test_bin_population_best_max[0] == best_solution[0]

    for actual, expected in zip(ga.population, expected_population):
        assert actual.chromosome == expected.chromosome
コード例 #17
0
def test_cross(cross_type):
    """
    *_cross()* method is the same (but bit replacement process itself is not)
    for both types of GA: RealGA and Binary GA and thus,
    it is not necessary to test it twice.
    """
    ga = BinaryGA(test_bin_data,
                  fitness_test_func,
                  cross_type=cross_type,
                  cross_prob=1)

    source_chrom = list(range(len(test_bin_data)))
    child = ga._cross([], source_chrom)

    print(child)

    if cross_type > 2:
        assert len(child) == cross_type
    else:
        # single- and two-point crossover differs from other types
        # we don't know the exact replacement interval as its end points are random numbers
        # but this interval must be at least 1
        assert len(child) >= 1
コード例 #18
0
def test_invalid_get_bit_positions():
    ga = BinaryGA(list(range(10)), fitness_test_func)

    with pytest.raises(ValueError):
        ga._get_bit_positions(-1)
コード例 #19
0
def test_valid_check_init_random_population():
    ga = BinaryGA(list(range(5)), fitness_test_func)

    max_num = ga._check_init_random_population(15)
    assert max_num == 2**5
コード例 #20
0
def test_invalid_init_data(data):
    with pytest.raises(ValueError):
        BinaryGA(data, fitness_test_func)
コード例 #21
0
def test_invalid_replace_bits(start, stop):
    ga = BinaryGA(list(range(10)), fitness_test_func)

    with pytest.raises(ValueError):
        ga._replace_bits([], [], start, stop)
コード例 #22
0
def test_invalid_replace_bits(start, stop):
    ga = BinaryGA(list(range(10)), fitness_test_func)

    with pytest.raises(ValueError):
        ga._replace_bits([], [], start, stop)
コード例 #23
0
def test_compute_fitness(chromosome, expected):
    ga = BinaryGA(test_bin_data, fitness_test_func)

    assert ga._compute_fitness(chromosome) == expected
コード例 #24
0
def test_compute_fitness(chromosome, expected):
    ga = BinaryGA(test_bin_data, fitness_test_func)

    assert ga._compute_fitness(chromosome) == expected
コード例 #25
0
def test_invalid_get_bit_positions():
    ga = BinaryGA(list(range(10)), fitness_test_func)

    with pytest.raises(ValueError):
        ga._get_bit_positions(-1)
コード例 #26
0
def test_invalid_cross_type():
    with pytest.raises(ValueError):
        BinaryGA(list(range(5)), fitness_test_func, cross_type=10)
コード例 #27
0
        assert elem in dga._chrom_arr
        assert fit in dga._fitness_arr

    if optim == 'min':
        assert dga.best_solution[0] == min(population)
        assert dga.best_solution[1] == fitness_test_linear_func(min(population))
    else:
        assert dga.best_solution[0] == max(population)
        assert dga.best_solution[1] == fitness_test_linear_func(max(population))


@pytest.mark.parametrize(['size', 'dim', 'interval', 'ga_inst'],
                         [(10, None, None, RealGA(fitness_test_linear_func)),
                         (None, None, None, RealGA(fitness_test_linear_func)),
                         (10, 3, None, RealGA(fitness_test_linear_func)),
                         (None, None, None, BinaryGA([1,2,3,4], fitness_test_linear_func))
                          ])
def test_invalid_init_random_population(size, dim, interval, ga_inst):
    with pytest.raises(ValueError):
        DiffusionGA(ga_inst).init_random_population(size, dim, interval)


@pytest.mark.parametrize('optim', ('min', 'max'))
@pytest.mark.parametrize('type', ('real', 'binary'))
def test_init_random_population_real(optim, type):
    size, dim, interval = 9, 1, (-5, 5)

    if type == 'real':
        ga = RealGA(fitness_test_linear_func, optim=optim)
        dga = DiffusionGA(ga)
コード例 #28
0
def test_invalid_check_init_random_population(size):
    ga = BinaryGA(list(range(5)), fitness_test_func)

    with pytest.raises(ValueError):
        ga._check_init_random_population(size)
コード例 #29
0
def test_invalid_check_init_random_population(size):
    ga = BinaryGA(list(range(5)), fitness_test_func)

    with pytest.raises(ValueError):
        ga._check_init_random_population(size)
コード例 #30
0
def test_valid_check_init_random_population():
    ga = BinaryGA(list(range(5)), fitness_test_func)

    max_num = ga._check_init_random_population(15)
    assert max_num == 2**5