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)
예제 #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 )
예제 #3
0
	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] ) 
예제 #6
0
	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 test_next_gen_returns_sensible_children(self):
		"""
		Call the next gen method for a population with three pairs.
		Assert that the returned frequencies are as expected.
		"""

		##All the individuals want maximum assortment
		I1 = Individual( 1,1, 0, 0 )
		I2 = Individual( 1,1, 0, 1 )
		I3 = Individual( 1,1, 1, 1 )

		##A popwith two of each kind should pair up exactly...
		pop = Population( [I1,I1,I2,I2,I3,I3] )
		##...so long as we set this bit by hand
		pop.males = [I1,I2,I3]
		pop.females = [I1,I2,I3]
		##Make the fitnesses exactly addative, for ease
		M = np.array( [ [ 0, 0.5 ],[ 0.5, 1. ] ] )
		delta = 0
		new_pops = [ pop.new_generation(M,delta = delta, mu_strat = 0, mu_assort = 0) for _ in range(5000) ]
		##We shhould have ratios of roughly 2:1:0 for phenotypic values of 1:0.5:0
		##Making the mean 2.5/3 = 
		mean_fairness = np.mean( [ p.fairness for p in new_pops ]  )
		self.assertAlmostEqual( mean_fairness, 2.5/3, places = 2 )
예제 #9
0
	def setUp(self):
		self.random_pop = Population.from_random(128)