class DNA_Test(unittest.TestCase):
    nucleotides = Nucleotides(set(string.printable))

    def test_dna_with_same_structure_are_equal(self):
        dna1 = DNA("abcd")
        dna2 = DNA("abcd")
        self.assertEquals(dna1, dna2)
        self.assertEquals(dna1.__hash__(), dna2.__hash__())

    def test_dna_with_different_structure_are_not_equal(self):
        dna2 = DNA("abcde")
        self.assertNotEqual(DNA("abcd"), dna2)
        self.assertNotEqual(DNA("abcd").__hash__(), dna2.__hash__())

    def test_fitness_of_dna(self):
        dna1 = DNA("abcd")
        dna2 = DNA("abef")
        self.assertEqual(dna1.measure_fitness(dna2), 2)

    def test_dna_mutation(self):
        dna = DNA("asdfghjkzxcvbnm")
        mutated_dna = dna.mutate(20, self.nucleotides)
        self.assertNotEqual(dna, mutated_dna)
        self.assertEquals(len(dna.structure), len(mutated_dna.structure))

    def test_dna_crossover(self):
        dna1 = DNA("asdfghjkl")
        dna2 = DNA("qwertyuio")
        crossover_points = 2
        child1 = dna1.crossover(dna2, crossover_points)
        child2 = dna2.crossover(dna1, crossover_points)
        self.assertNotEqual(child1, child2)
        self.assertEqual(len(child1.structure), len(child2.structure))
Beispiel #2
0
class DNA(object):
    nucleotides = Nucleotides(string.printable)

    def __init__(self, structure):
        self.structure = structure

    def __eq__(self, other):
        return self.structure == other.structure

    def __hash__(self):
        return self.structure.__hash__()

    def measure_fitness(self, target):
        fitness = 0
        for this, other in zip(self.structure, target.structure):
            if this == other:
                fitness += 1
        return fitness

    def mutate(self, percentage=10, space_of_possibilities=nucleotides):
        original_structure = list(self.structure)
        mutate_indices = self.__generate_mutable_indices(percentage)
        mutation_values = self.__generate_random_mutation(
            space_of_possibilities, percentage)
        mutated_structure = generate_mutated_structure(original_structure,
                                                       mutate_indices,
                                                       mutation_values)
        return DNA(mutated_structure)

    def crossover(self, other, crossover_points=2):
        crossover_points = self.__generate_crossover_points(crossover_points)
        child_structure = self.__generate_breed(crossover_points, other)
        return DNA(child_structure)

    def __generate_random_mutation(self, nucleotides, percentage):
        mutate_count = self.__calculate_mutate_count(percentage)
        nucleotides_values = nucleotides.create(mutate_count)
        return nucleotides_values

    def __generate_mutable_indices(self, percentage):
        mutate_count = self.__calculate_mutate_count(percentage)
        mutate_indices = random.sample(xrange(len(self.structure)),
                                       mutate_count)
        return mutate_indices

    def __calculate_mutate_count(self, percentage):
        mutate_count = len(self.structure) * percentage / 100
        return mutate_count

    def __generate_crossover_points(self, crossover_points):
        crossover_indices = random.sample(xrange(len(self.structure)),
                                          crossover_points)
        crossover_indices.sort()
        return crossover_indices

    def __generate_breed(self, indices, other):
        return self.structure[:indices[0]] + \
               other.structure[indices[0]:indices[1]] + \
               self.structure[indices[1]:]
 def _initialise(self, target_string):
     target_length = len(target_string)
     nucleotides = Nucleotides(string.printable)
     DNA.nucleotides = nucleotides
     population_initialise = PopulationInitialise(nucleotides)
     first_population = population_initialise.create(
         population_size=self.population_size, structure_size=target_length)
     first_generation = Generation(1, first_population)
     return [first_generation]
Beispiel #4
0
 def test_nucleotides_with_same_space_are_equal(self):
     space1 = set("asdfghjk")
     space2 = set("qwertyui")
     nucleotides1 = Nucleotides(space1)
     nucleotides2 = Nucleotides(space2)
     self.assertNotEqual(nucleotides1, nucleotides2)
     self.assertNotEqual(nucleotides1.__hash__(), nucleotides2.__hash__())
 def test_initialise_population(self):
     nucleotides = Nucleotides(set(string.printable))
     population_initialise = PopulationInitialise(nucleotides)
     population = population_initialise.create(population_size=10,
                                               structure_size=500)
     self.assertEquals(population.size, 10)
Beispiel #6
0
 def test_nucleotides_with_same_space_are_equal(self):
     space = set("asdfghjk")
     nucleotides1 = Nucleotides(space)
     nucleotides2 = Nucleotides(space)
     self.assertEquals(nucleotides1, nucleotides2)
     self.assertEquals(nucleotides1.__hash__(), nucleotides2.__hash__())
Beispiel #7
0
 def test_nucleotides_generate_structure_greater_than_available_space(self):
     space = set("asdfghj")
     nucleotides = Nucleotides(space)
     structure = nucleotides.create(size=10)
     self.assertEquals(len(structure), 10)
Beispiel #8
0
 def test_nucleotides_generate_structure(self):
     space = set("asdfghjkiytfvygjmv")
     nucleotides = Nucleotides(space)
     structure = nucleotides.create(size=10)
     self.assertEquals(len(structure), 10)