def test_simulate_discrite_distribution(self): self.assertEqual(utils.simulate_discrete_distribution([1.0, 0.0]), 0, "Simulating singleton distribution fails") self.assertEqual(utils.simulate_discrete_distribution([0.0, 1.0]), 1, "Simulating singleton distribution fails") coin = [0.5, 0.5] coin_tossing = np.array([utils.simulate_discrete_distribution(coin) for _ in xrange(0,500000)]) sample = np.mean(coin_tossing) self.assertAlmostEqual(0.5, sample, delta=0.01, msg="Coin tossing test fails " + str(sample) +" is not 0.5" )
def _detached_step(population_array, population_size, payoff_function, intensity_of_selection, number_of_strategies, mutation_kernel, fitness_mapping='exp', mutation_step=True): """ One generation step. This is a copy of the previous but it is detached so that it can be called more efficiently Parameters: ---------- population_array = mutation_step = """ current_distribution = (1.0 / population_size) * np.array(population_array, dtype=float) # COMPUTE PAYOFF payoff = payoff_function(population_array) # compute fitness distribution fitness_sum = 0.0 fitness = np.empty_like(payoff) if fitness_mapping == 'lin': for i in range(0, number_of_strategies): val = current_distribution[i] * ( 1.0 - intensity_of_selection + intensity_of_selection * payoff[i]) fitness[i] = val fitness_sum += val fitness /= fitness_sum else: for i in range(0, number_of_strategies): val = current_distribution[i] * (the_number_e**( intensity_of_selection * payoff[i])) fitness[i] = val fitness_sum += val fitness /= fitness_sum # choose one random guy in proportion to fitness chosen_one = utils.simulate_discrete_distribution(fitness) # mutate this guy if mutation_step: chosen_one = utils.simulate_discrete_distribution( mutation_kernel[chosen_one]) # a random guy dies dies = utils.simulate_discrete_distribution(current_distribution) population_array[dies] -= 1 # we add a copy of the fit guy population_array[chosen_one] += 1 # the payoff is returned but almost never used. return StepResult(population_array, payoff)
def test_simulate_discrite_distribution(self): self.assertEqual(utils.simulate_discrete_distribution([1.0, 0.0]), 0, "Simulating singleton distribution fails") self.assertEqual(utils.simulate_discrete_distribution([0.0, 1.0]), 1, "Simulating singleton distribution fails") coin = [0.5, 0.5] coin_tossing = np.array([ utils.simulate_discrete_distribution(coin) for _ in range(0, 500000) ]) sample = np.mean(coin_tossing) self.assertAlmostEqual(0.5, sample, delta=0.01, msg="Coin tossing test fails " + str(sample) + " is not 0.5")
def _detached_step(population_array, population_size, payoff_function, intensity_of_selection, number_of_strategies, mutation_kernel, fitness_mapping='exp', mutation_step=True): """ One generation step. This is a copy of the previous but it is detached so that it can be called more efficiently Parameters: ---------- population_array = mutation_step = """ current_distribution = (1.0 / population_size) * np.array(population_array, dtype=float) # COMPUTE PAYOFF payoff = payoff_function(population_array) # compute fitness distribution fitness_sum = 0.0 fitness = np.empty_like(payoff) if fitness_mapping == 'lin': for i in range(0, number_of_strategies): val = current_distribution[i] * (1.0 - intensity_of_selection + intensity_of_selection * payoff[i]) fitness[i] = val fitness_sum += val fitness /= fitness_sum else: for i in range(0, number_of_strategies): val = current_distribution[i] * (the_number_e ** (intensity_of_selection * payoff[i])) fitness[i] = val fitness_sum += val fitness /= fitness_sum # choose one random guy in proportion to fitness chosen_one = utils.simulate_discrete_distribution(fitness) # mutate this guy if mutation_step: chosen_one = utils.simulate_discrete_distribution(mutation_kernel[chosen_one]) # a random guy dies dies = utils.simulate_discrete_distribution(current_distribution) population_array[dies] -= 1 # we add a copy of the fit guy population_array[chosen_one] += 1 # the payoff is returned but almost never used. return StepResult(population_array, payoff)
def test_simulate_discrete_distribution_longer(self): dist = [0.75, 0.2, 1.0-0.2-0.75] zero = 0 one =0 two =0 for _ in xrange(0,5000000): sample = utils.simulate_discrete_distribution(dist) if sample ==0: zero+=1 if sample ==1: one+=1 if sample ==2: two+=1 result = [zero, one, two] result /= np.sum(result, dtype=float) np.testing.assert_allclose(dist, result, rtol=0.001, atol=0.0001, err_msg="Bad simulate discrete dist", verbose=True)
def test_simulate_discrete_distribution_longer(self): dist = [0.75, 0.2, 1.0 - 0.2 - 0.75] zero = 0 one = 0 two = 0 for _ in range(0, 5000000): sample = utils.simulate_discrete_distribution(dist) if sample == 0: zero += 1 if sample == 1: one += 1 if sample == 2: two += 1 result = [zero, one, two] result /= np.sum(result, dtype=float) np.testing.assert_allclose(dist, result, rtol=0.001, atol=0.0001, err_msg="Bad simulate discrete dist", verbose=True)