예제 #1
0
    def test_point_cross_over_max(self):
        size = 4
        cross_point = 2
        chromosome_1 = utils.max_binary_value(size)
        chromosome_2 = utils.max_binary_value(size)
        expected = utils.max_binary_value(size)

        individual_1, individual_2 = point_cross_over(chromosome_1,
                                                      chromosome_2, size,
                                                      cross_point)
        self.assertEqual(individual_1, expected)
        self.assertEqual(individual_2, expected)
예제 #2
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
예제 #3
0
def chromosome_subset_value(chromosome, start, length):
    if start <= 0:
        raise ValueError('Start position has to be greater than or equal to 1')
    shifted_chromosome = utils.shift_bytes(chromosome, start - 1)
    mask = utils.max_binary_value(length)
    return utils.apply_mask(shifted_chromosome, mask)
예제 #4
0
def junction_cross_over(chromosome_1: int, chromosome_2: int, size: int, point_1: int, point_2: int) -> Tuple[int, int]:
    max_val = utils.max_binary_value(size)
    mask_1 = utils.shift_bytes(max_val, point_1)
    mask_2 = utils.shift_bytes(max_val, point_2)
    mask = mask_1 | ~mask_2
    return create_offspring_from_mask(chromosome_1, chromosome_2, mask)
예제 #5
0
def point_cross_over(chromosome_1: int, chromosome_2: int, size: int, point: int) -> Tuple[int, int]:
    max_val = utils.max_binary_value(size)
    mask = utils.shift_bytes(max_val, point)
    return create_offspring_from_mask(chromosome_1, chromosome_2, mask)
예제 #6
0
 def test_max_binary_value_one(self):
     size = 1
     self.assertEqual(1, utils.max_binary_value(size))
예제 #7
0
 def test_max_binary_value_zero(self):
     size = 0
     self.assertEqual(0, utils.max_binary_value(size))
예제 #8
0
 def test_max_binary_value_normal(self):
     size = 4
     self.assertEqual(15, utils.max_binary_value(size))