def test_when_population_size_is_lower_than_selection_size(self): """ Cannot select more individuals than population size. """ p_size = 10 population = utils.DummyPopulation(size=p_size) with self.assertRaises(PyneticsError): Tournament(sample_size=int(p_size / 2))(population, p_size * 2)
def test_when_population_size_is_equals_to_selection_size(self): """ All the population is returned. """ p_size = 10 individuals = utils.individuals(p_size) population = utils.DummyPopulation(size=p_size, individuals=individuals) selected_individuals = BestIndividual()(population, p_size) self.assertEquals(len(selected_individuals), len(population))
def test_when_population_size_is_bigger_than_selection_size(self): """ The best individuals are returned. """ p_size = 10 selection_size = int(p_size / 2) population = utils.DummyPopulation(size=p_size) individuals = BestIndividual()(population, selection_size) self.assertEquals(len(individuals), selection_size) for i in range(len(individuals)): self.assertEquals(individuals[i], population[i])
def test_individuals_are_correctly_added_to_the_population(self): size = 10 individuals = utils.individuals(size + 1) population = utils.DummyPopulation(size=size, individuals=individuals[:10]) self.assertEquals(size, len(population)) population.append(individuals[10]) self.assertEquals(size + 1, len(population)) self.assertIn(individuals[10], population)
def test_population_maintain_size_at_initialization_time(self): """ Size is kept when initializing. That means that, if a population is initializated with size n, it will have n individuals, regardless the number of individuals are passed as parameter. """ size = 10 individuals_list = [[], utils.individuals(10), utils.individuals(100)] for individuals in individuals_list: self.assertEquals( size, len(utils.DummyPopulation(size=size, individuals=individuals)))
def test_best_individuals_are_returned(self): size = 10 individuals = utils.individuals(size) population = utils.DummyPopulation(size=size, individuals=individuals) self.assertEquals(population.best().fitness(), individuals[-1].fitness())
def test_population_shrinks_when_individual_is_removed(self): size = 10 population = utils.DummyPopulation(size=size) for i in range(10): self.assertEquals(size - i, len(population)) del population[0]
def test_population_length_is_computed_correctly(self): sizes = (1, 10, 100, 1000) for size in sizes: self.assertEquals(size, len(utils.DummyPopulation(size=size)))
def test_cannot_initialize_a_population_of_size_0_or_less(self): for size in (-100, -10, -1, 0): with self.assertRaises(InvalidSize): utils.DummyPopulation(size=size)
def test_class_is_pickeable(self): """ Checks is pickeable by writing it into a temporary file. """ with TemporaryFile() as f: pickle.dump(utils.DummyPopulation(size=10), f)
def test_error_if_more_selections_than_size_when_not_repetition(self): """ When no repetable and requested more individuals than available. """ population = utils.DummyPopulation(10) for i in range(1, 10): with self.assertRaises(PyneticsError): utils.DummySelection()(population, len(population) + 1)