Ejemplo n.º 1
0
def test_offspring_returns_two_chromosomes_combining_parents():
    parents = [Chromosome.random(), Chromosome.random()]
    subject = Crossover(parents)
    result = subject.offspring()
    assert len(result) == 2
    assert isinstance(result[0], Chromosome)
    assert isinstance(result[1], Chromosome)
Ejemplo n.º 2
0
def test_init_assigns_chromosomes():
    chromosomes = [
        Chromosome(np.array(list("A" * 26))),
        Chromosome(np.array(list("B" * 26))),
    ]
    subject = Population(chromosomes)
    assert subject.chromosomes == chromosomes
Ejemplo n.º 3
0
def test_mutated_generates_genes(random):
    random.side_effect = [0.01, 0.01] + [1] * 24
    subject = Chromosome(np.array(list(string.ascii_uppercase)))
    result = subject.mutated()
    expected = Chromosome(np.array(list("IJCDEFGHIJKLMNOPQRSTUVWXYZ")))
    assert isinstance(result, Chromosome)
    assert result == expected
Ejemplo n.º 4
0
def test_best_chromosomes_sorts_them_in_descending_order():
    chromosomes = [
        Chromosome(np.array(list("ABCXXXXXXXXXXXXXXXXXXXXXXX"))),
        Chromosome(np.array(list("ABXXXXXXXXXXXXXXXXXXXXXXXX"))),
        Chromosome(np.array(list(string.ascii_uppercase))),
    ]
    subject = Population(chromosomes)
    expected = [chromosomes[2], chromosomes[0], chromosomes[1]]
    assert subject.best_chromosomes() == expected
Ejemplo n.º 5
0
def test_parents_probabilities_returns_values_proportional_to_their_fitness():
    chromosomes = [
        Chromosome(np.array(list("ABCXXXXXXXXXXXXXXXXXXXXXXX"))),
        Chromosome(np.array(list("ABXXXXXXXXXXXXXXXXXXXXXXXX"))),
        Chromosome(np.array(list(string.ascii_uppercase))),
    ]
    subject = Population(chromosomes)
    result = subject.parents_probabilities()
    expected = [0.12121212, 0.09090909, 0.78787879]
    np.testing.assert_allclose(result, expected)
Ejemplo n.º 6
0
def test_random_parents_returns_two_random_chromosomes():
    chromosomes = [
        Chromosome(np.array(list("A" * 26))),
        Chromosome(np.array(list("B" * 26))),
        Chromosome(np.array(list("C" * 26))),
    ]
    subject = Population(chromosomes)
    random_parents = subject.random_parents()
    assert len(random_parents) == 2
    assert random_parents[0] == chromosomes[0]
    assert random_parents[1] == chromosomes[0]
Ejemplo n.º 7
0
def test_offspring_returns_chromosomes_combining_parents(monkeypatch):
    parents = [
        Chromosome(np.array(list("XXXXXXXXXXXXXXXXXXXXXXXXXX"))),
        Chromosome(np.array(list("YYYYYYYYYYYYYYYYYYYYYYYYYY"))),
    ]
    subject = Crossover(parents)
    monkeypatch.setattr(subject, "pivot", 3)
    result = subject.offspring()
    assert result[0] == Chromosome(np.array(
        list("XXXYYYYYYYYYYYYYYYYYYYYYYY")))
    assert result[1] == Chromosome(np.array(
        list("YYYXXXXXXXXXXXXXXXXXXXXXXX")))
Ejemplo n.º 8
0
def test_call_returns_parents_if_must_not_crossover(monkeypatch):
    parents = [Chromosome.random(), Chromosome.random()]
    subject = Crossover(parents)
    monkeypatch.setattr(subject, "will_crossover_happen", lambda: False)
    assert subject() == parents
Ejemplo n.º 9
0
def test_will_crossover_happen_returns_false_if_random_is_greater_than_probability(
        random):
    parents = [Chromosome.random(), Chromosome.random()]
    subject = Crossover(parents, probability=0.8)
    random.return_value = 0.9
    assert not subject.will_crossover_happen()
Ejemplo n.º 10
0
def test_call_returns_crossed_over_if_must_crossover(monkeypatch):
    parents = [Chromosome.random(), Chromosome.random()]
    subject = Crossover(parents)
    monkeypatch.setattr(subject, "will_crossover_happen", lambda: True)
    assert subject() == subject.offspring()
Ejemplo n.º 11
0
def test_fitness_returns_number_of_correct_genes():
    subject = Chromosome(np.array(list("ABXXXXXXXXXXXXXXXXXXXXXXXX")))
    assert subject.fitness() == 3
Ejemplo n.º 12
0
def test_fitness_returns_chromosome_length_for_ideal_result():
    subject = Chromosome(np.array(list(string.ascii_uppercase)))
    assert subject.fitness() == 26
Ejemplo n.º 13
0
def test_random_generates_genes():
    subject = Chromosome.random()
    assert isinstance(subject, Chromosome)
    assert np.array_equal(subject.genes, GENES_FIXTURE)
Ejemplo n.º 14
0
def test_init_assigns_genes():
    subject = Chromosome(GENES_FIXTURE)
    assert np.array_equal(subject.genes, GENES_FIXTURE)
Ejemplo n.º 15
0
 def first_generation(self):
     chromosomes = []
     for _ in range(self.SIZE):
         chromosomes.append(Chromosome.random())
     return Population(chromosomes)
Ejemplo n.º 16
0
 def offspring(self):
     a = self.parents[0]
     b = self.parents[1]
     genes_1 = np.concatenate([a.genes[:self.pivot], b.genes[self.pivot:]])
     genes_2 = np.concatenate([b.genes[:self.pivot], a.genes[self.pivot:]])
     return [Chromosome(genes_1), Chromosome(genes_2)]