Пример #1
0
 def test_gene_value_first_bits(self):
     chromosome = 67  # 1000011
     chromosome_size = 7
     start_pos = 1
     end_pos = 3
     individual = Individual(chromosome_size=chromosome_size, chromosome=chromosome)
     self.assertEqual(3, individual.gene_value(start_pos, end_pos))
Пример #2
0
 def test_gene_value_all(self):
     chromosome = 67  # 1000011
     chromosome_size = 7
     start_pos = 1
     end_pos = 7
     individual = Individual(chromosome_size=chromosome_size, chromosome=chromosome)
     self.assertEqual(67, individual.gene_value(start_pos, end_pos))
Пример #3
0
    def __create_next_generation__(self):
        next_generation = []
        while len(next_generation) != self.population_size:

            #  select parents
            parents = self.select_parents_callback(self.current_individuals)
            if len(parents) != 2:
                raise Exception('Parent selection must return two individuals')

            #  create progeny
            chromosomes = self.crossover_callback(*parents)
            current_index = 0

            while current_index < len(chromosomes) and len(
                    next_generation) != self.population_size:
                #  mutation (defaulted to flip mutation)
                chromosome = self.mutate_callback(chromosomes[current_index])
                if not 0 <= chromosome <= utils.max_binary_value(
                        self.chromosome_size):
                    raise Exception(
                        'Chromosomes generated must be between within max chromosome value'
                    )

                #  create individual
                individual = Individual(self.chromosome_size, chromosome)
                individual.set_parents(*parents)
                next_generation.append(individual)
                current_index += 1

        return next_generation
 def setUp(self) -> None:
     self.target = Individual(5, [1, 1, 1, 1, 1])
     self.individual1 = Individual(5, [0, 0, 0, 0, 0])
     self.individual2 = Individual(5, [1, 0, 1, 0, 0])
     self.individual3 = Individual(5, [0, 1, 0, 1, 1])
     self.individual4 = Individual(5, [1, 1, 1, 1, 1])
     self.population = Population(self.target, 4, [self.individual1, self.individual2, self.individual3,
                                                   self.individual4])
     self.population.calc_fitness_scores()
     self.ga_engine = GAEngine(5, 4)
    def setUp(self) -> None:
        self.target = Individual(5, [1, 1, 1, 1, 1])
        self.individual1 = Individual(5, [0, 0, 0, 0, 0])
        self.individual2 = Individual(5, [1, 0, 1, 0, 0])
        self.individual3 = Individual(5, [0, 1, 0, 1, 1])
        self.individual4 = Individual(5, [1, 1, 1, 1, 1])

        self.population = Population(self.target, 4, [
            self.individual1, self.individual2, self.individual3,
            self.individual4
        ])
Пример #6
0
    def test_center_cross_over_uneven(self):
        size = 5
        chromosome_1 = int('10001', 2)
        chromosome_2 = int('11100', 2)
        individual_1 = Individual(chromosome_size=size,
                                  chromosome=chromosome_1)
        individual_2 = Individual(chromosome_size=size,
                                  chromosome=chromosome_2)
        expected_1 = int('10100', 2)
        expected_2 = int('11001', 2)

        cross_1, cross_2 = CenterCross.crossover(individual_1, individual_2)
        self.assertEqual(cross_2, expected_2)
        self.assertEqual(cross_1, expected_1)
Пример #7
0
 def test_gene_value_at_zero(self):
     chromosome = 67  # 1000011
     chromosome_size = 7
     start_pos = 0
     end_pos = 4
     individual = Individual(chromosome_size=chromosome_size, chromosome=chromosome)
     self.assertRaises(ValueError, individual.gene_value, start_pos, end_pos)
Пример #8
0
 def test_gene_value_past_limit(self):
     chromosome = 67  # 1000011
     chromosome_size = 7
     start_pos = 6
     end_pos = 8
     individual = Individual(chromosome_size=chromosome_size, chromosome=chromosome)
     self.assertRaises(ValueError, individual.gene_value, start_pos, end_pos)
Пример #9
0
 def __default_initialize__(algorithm):
     individuals = [
         Individual(algorithm.chromosome_size,
                    generate_random_chromosome(algorithm.chromosome_size))
         for _ in range(algorithm.population_size)
     ]
     return individuals
Пример #10
0
 def random_population(self, city2travel, population_size):
     """Generate a random population"""
     self.size = population_size
     for i in range(self.size):
         self.list.append(Individual(city_list=city2travel))
     self.compute_stats()
     self.compute_breed_probability()
    def test_add_individual(self):
        temp_individual = Individual(5, [1, 0, 0, 0, 0])
        self.population.add_individual(temp_individual)
        self.assertEqual(self.population.individuals, [
            temp_individual, self.individual2, self.individual3,
            self.individual4
        ], "post addition scores wrong")
        self.assertEqual(self.population.fitness_scores, [1, 2, 3, 5],
                         "post addition scores wrong")

        temp_individual = Individual(5, [1, 0, 1, 1, 1])
        self.population.add_individual(temp_individual)
        self.assertEqual(self.population.individuals, [
            temp_individual, self.individual2, self.individual3,
            self.individual4
        ], "post addition scores wrong")
        self.assertEqual(self.population.fitness_scores, [4, 2, 3, 5],
                         "post addition scores wrong")
 def test_select_parents_two_elements(self):
     individual_1 = Individual(4, 1)
     individual_1.fitness = 1
     individual_2 = Individual(4, 1)
     individual_2.fitness = 2
     child_1, child_2 = RouletteWheel.select_parents(
         [individual_1, individual_2])
     self.assertFalse(child_1 == child_2)
Пример #13
0
    def next_generation(self):
        """Generate the next generation based on the current population

        The next generation's individuals are sons of the current population.
        The function return a new instance of a Population class.

        """
        next_gen = Population()
        i = 0
        while i < self.size:
            father, mother = self.pick_parents()
            next_gen = next_gen + Individual(father=father, mother=mother)
            i += 1
        next_gen.compute_stats()
        next_gen.compute_breed_probability()
        return next_gen
Пример #14
0
 def test_add_individual(self):
     print("scores_b4_adding", self.population.get_fitness_scores())
     new_individual = Individual(choose_from_search_space(
         self.search_space))
     new_individual.set_fitness_score(0.01978)
     worst_index = np.argmin(self.population.get_fitness_scores())
     print(worst_index)
     print("new_params", new_individual.get_nn_params())
     print("worst_param",
           self.population.individuals[worst_index].get_nn_params())
     self.population.add_individual(new_individual,
                                    new_individual.get_fitness_score())
     print("scores_after_adding", self.population.get_fitness_scores())
     self.assertEqual(self.population.individuals[worst_index],
                      new_individual)
Пример #15
0
 def setUp(self):
     self.individual = Individual(chromosome_size=10, chromosome=0)
Пример #16
0
 def generate_individual(self, number: int) -> Individual:
     return Individual(
         uid=self.generate_uid(number),
         yob=self.year,
         iq=self.get_random_iq_from_current_distribution()
     )
Пример #17
0
class TestIndividual(unittest.TestCase):

    def setUp(self):
        self.individual = Individual(chromosome_size=10, chromosome=0)

    def test_add_metric_value(self):
        key = 'metric'
        val = 5
        self.individual.add_metric(key, val)
        self.assertEqual(1, len(self.individual.metrics))
        self.assertEqual(5, self.individual.get_metric(key))

    def test_add_metric_overrides(self):
        key = 'metric'
        val_1 = 5
        val_2 = 3
        self.individual.add_metric(key, val_1)
        self.individual.add_metric(key, val_2)
        self.assertEqual(1, len(self.individual.metrics))
        self.assertEqual(3, self.individual.get_metric(key))

    def test_add_metric_averaging(self):
        key = 'metric'
        val_1 = 5
        val_2 = 3
        self.individual.add_metric(key, val_1, average=True)
        self.individual.add_metric(key, val_2, average=True)
        self.assertEqual(1, len(self.individual.metrics))
        self.assertEqual(4, self.individual.get_metric(key))

    def test_gene_value_first_bits(self):
        chromosome = 67  # 1000011
        chromosome_size = 7
        start_pos = 1
        end_pos = 3
        individual = Individual(chromosome_size=chromosome_size, chromosome=chromosome)
        self.assertEqual(3, individual.gene_value(start_pos, end_pos))

    def test_gene_value_end_bits(self):
        chromosome = 67  # 1000011
        chromosome_size = 7
        start_pos = 6
        end_pos = 7
        individual = Individual(chromosome_size=chromosome_size, chromosome=chromosome)
        self.assertEqual(2, individual.gene_value(start_pos, end_pos))

    def test_gene_value_past_limit(self):
        chromosome = 67  # 1000011
        chromosome_size = 7
        start_pos = 6
        end_pos = 8
        individual = Individual(chromosome_size=chromosome_size, chromosome=chromosome)
        self.assertRaises(ValueError, individual.gene_value, start_pos, end_pos)

    def test_gene_value_at_zero(self):
        chromosome = 67  # 1000011
        chromosome_size = 7
        start_pos = 0
        end_pos = 4
        individual = Individual(chromosome_size=chromosome_size, chromosome=chromosome)
        self.assertRaises(ValueError, individual.gene_value, start_pos, end_pos)

    def test_gene_value_all(self):
        chromosome = 67  # 1000011
        chromosome_size = 7
        start_pos = 1
        end_pos = 7
        individual = Individual(chromosome_size=chromosome_size, chromosome=chromosome)
        self.assertEqual(67, individual.gene_value(start_pos, end_pos))

    def tearDown(self):
        self.individual.clear_metrics()