def test_can_be_initialised_from_random(self):
		"""
		Test that the population can be initialised from random individuals.
		
		"""

		myPop = Population.from_random( size = 128 )
		self.assertIsInstance(myPop, Population)
Example #2
0
	def from_random_pop(cls, size, *args, **kwargs):
		"""
		Create the model from a random initial condition.
		Inputs
		======
		size : int
			size of the population
		*args, **kwargs :
			passed to default constructor of model
		"""
		pop = Population.from_random(size)
		return Model( pop, *args, **kwargs )
	def test_records_assortment(self):
		"""
		Check that the model records assortment.
		"""

		gens = 26
		pop = Population.from_random(32)
		assort0 = pop.average_assortment
		myModel = Model( pop, h = 0.4, s = 0.6, delta = 0.3, generations = gens)
		myModel.go()
		self.assertEqual( len( myModel.desired_assortment ), gens )
		self.assertEqual( assort0, myModel.desired_assortment[0] )
	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] ) 
	def test_records_fairness(self):
		"""
		Test that the model record fairness, simply by asserting that the model
		has a list of the length of the number of generations.

		"""

		gens = 26
		pop = Population.from_random(32)
		fairness0 = pop.fairness
		myModel = Model( pop, h = 0.4, s = 0.6, delta = 0.3, generations = gens)
		myModel.go()
		self.assertEqual( len( myModel.fairness ), gens )
		self.assertEqual( fairness0, myModel.fairness[0] )
	def test_pairing_is_sensible(self):
		"""
		Test that the pairing of individuals is sensible. The corelation of phentypes
		should be roughly equal to the average value of desired assortment.
		"""
		##Start fro scratch, incase this messes with something else
		pop = Population.from_random(10000)
		pop.pair_population()
		x = [ p[0].phenotypic_value for p in pop.pairs ]
		y = [ p[1].phenotypic_value for p in pop.pairs ]
		corr = np.corrcoef( x, y )[0,1]
		average_assortment = np.mean( [ I.desired_assortment for I in pop.individuals ] )
		##This seems to be a very approximate law, so let's be quite generous in what we allow
		self.assertAlmostEqual( corr, average_assortment, places = 1 )
	def setUp(self):
		self.random_pop = Population.from_random(128)