def from_random(cls): """ Creates a pair from two random individuals. Not usually called directly, exists mostly for testing perposes. """ return cls( Individual.from_random(), Individual.from_random() )
def from_random(cls, size): """ Initialise a population with a given size with random individuals. """ individuals = [ Individual.from_random() for _ in range(size) ] return cls( individuals )
def test_initialiser_raises_value_error_with_odd_individuals(self): """ Population must be created with an even number of individuals. Test that valueerror is raised if this is not the case. """ list_of_individuals = [ Individual.from_random() for _ in range(127) ] with self.assertRaises(ValueError): myPop = Population( list_of_individuals ) with self.assertRaises(ValueError): myPop = Population.from_random(127)
def setUp(self): """ Make some dummy lists of individuals and popualtions, that can be tested repeastedly. """ self.random_individuals = [ Individual.from_random() for _ in range(128) ] self.random_pop = Population.from_random(128) I1 = Individual( 1,1 , 0,0 ) I2 = Individual( 1,1 , 0,0 ) I3 = Individual( 1,0 , 0,1 ) I4 = Individual( 0,0 , 1,0 ) self.small_pop = Population( [I1,I2,I3,I4] )