Beispiel #1
0
 def test_recombine_dna_ocx1_preserves_order(self):
     dna1 = ga.UniqueIntDNA(10)  # 0, 1, 2, 3, ...
     dna2 = ga.UniqueIntDNA(10)
     dna2._data.reverse()  # 9, 8, 7, 6, ...
     ga.recombine_dna_ocx1(dna1, dna2, 0, 3)
     assert list(dna1) == [9, 8, 7, 0, 1, 2, 3, 4, 5, 6]
     assert list(dna2) == [0, 1, 2, 9, 8, 7, 6, 5, 4, 3]
Beispiel #2
0
 def test_reset_data(self):
     dna = ga.UniqueIntDNA(10)
     dna.reset(range(9, -1, -1))
     assert len(dna) == 10
     assert dna.is_valid is True
     assert dna[0] == 9
     assert dna[9] == 0
Beispiel #3
0
def test_tournament_selection():
    candidates = [ga.UniqueIntDNA(10) for _ in range(10)]
    for index, dna in enumerate(candidates):
        dna.fitness = index
    selection = ga.TournamentSelection(2)
    selection.reset(candidates)

    result = list(selection.pick(2))
    assert len(result) == 2

    result = list(selection.pick(3))
    assert len(result) == 3
Beispiel #4
0
 def test_init_invalid_values(self):
     with pytest.raises(TypeError):
         ga.UniqueIntDNA([0, 1, 2, 2])
Beispiel #5
0
 def test_init_values(self):
     dna = ga.UniqueIntDNA([0, 1, 2, 3])
     assert dna.is_valid is True
     assert list(dna) == [0, 1, 2, 3]
Beispiel #6
0
 def test_init_value(self):
     dna = ga.UniqueIntDNA(10)
     assert dna.is_valid is True
     assert list(dna) == list(range(10))
Beispiel #7
0
def test_scramble_mutate():
    dna = ga.UniqueIntDNA(10)
    mutate = ga.ScrambleMutate(5)
    mutate.mutate(dna, 1.0)
    assert list(dna) != list(ga.UniqueIntDNA(10))
    assert len(set(dna)) == 10
Beispiel #8
0
def test_reverse_mutate():
    dna = ga.UniqueIntDNA(10)
    mutate = ga.ReverseMutate(3)
    mutate.mutate(dna, 1.0)
    assert list(dna) != list(ga.UniqueIntDNA(10))
    assert len(set(dna)) == 10
Beispiel #9
0
 def test_subscription_setter(self):
     dna = ga.UniqueIntDNA(10)
     dna[-3:] = [1, 2, 3]
     assert len(dna) == 10
     assert dna[-4:] == [6, 1, 2, 3]
     assert dna.is_valid is False