def _simulate_population(cls, i, seed_offset, population_size, string_size): seed(19520624 + 10 * i + seed_offset) creator = Creator(population_size=population_size, string_size=string_size) population = creator.create_population() evolved_population, evaluations = creator.evolve_population(population) success = False if cls._found_global_optimum(creator, evolved_population, string_size): success = True return success, evaluations
def find_minimum_population_size(cls, string_size, initial_population_size=5, num_runs=5) -> int: population_size = initial_population_size lower_bound = 0 upper_bound = 81920 found_upper_bound = False found_global_optimum_count = 0 while lower_bound != upper_bound or found_global_optimum_count != num_runs: found_global_optimum_count = 0 creator = Creator(population_size=population_size, string_size=string_size) population = creator.create_population() for _ in range(num_runs): evolved_population = creator.evolve_population(population) if cls._found_global_optimum(creator, evolved_population, string_size): found_global_optimum_count += 1 if found_global_optimum_count > 0: # Found global optimum. upper_bound = population_size if not found_upper_bound: lower_bound = int(population_size / 2) population_size = cls._midpoint(lower_bound, upper_bound) found_upper_bound = True else: # Failed to find global optimum 5/5 times if lower_bound == upper_bound: # Lower and upper bound converged # Increment population size until it succeeds 5/5 times population_size += 2 lower_bound = population_size upper_bound = population_size elif found_upper_bound: # Found upper bound and failed. # Increase lower bound and population size. population_size = cls._midpoint(lower_bound, upper_bound) lower_bound = population_size if upper_bound - lower_bound == 2: lower_bound += 2 else: # Failed without finding upper bound. population_size *= 2 # Double population size return upper_bound
def test_create_population(self): seed(1) population_size = 3 creator = Creator(population_size=population_size, string_size=5) population = creator.create_population() expected_population = [ Individual('00101'), Individual('11100'), Individual('10110') ] self.assertEqual(population_size, len(population)) self.assertEqual(expected_population, population)