def test_fitness(self): """ Fitness must always be the same length as individuals """ pop = Population(individuals=[1, 2, 3, 4, 5]) # should raise error when trying to set fitness with length not # equal to the length of indivuals with self.assertRaises(PopulationFittnessDimensionError): pop.fitness = [1, 2, 3] # should allow setting fitness to an iterable with the same length # as individuals self.fitness = np.zeros(pop.npop) self.assertEqual(len(self.fitness), pop.npop) # setting fitness should force a sort of individuals and fitness ind0 = np.random.rand(100, 5) fit0 = np.arange(100) pop = Population(individuals=ind0, fitness=fit0) for v0, v1 in zip(ind0[0], pop.individuals[-1]): self.assertEqual(v0, v1) self.assertEqual(fit0.max(), pop.fitness[0]) # ... but should not change the shape of individuals self.assertEqual(ind0.shape, pop.individuals.shape) self.assertEqual(pop.fitness.shape, (100,)) # should not allow fitness to be greater than 1d with self.assertRaises(PopulationFittnessDimensionError): pop.fitness = np.zeros((pop.npop, 10))