def replicator_equation(x, t, number_of_players): x /= np.sum(x) G = network.create_braess_network() R = ReplicatorDynamics(G, number_of_players) fitness_vector = R.compute_payoff(x) average_fitness = np.dot(x, fitness_vector) return x * (fitness_vector - average_fitness)
def test_invariable_population_size(self): braess_graph = network.create_braess_network() population_array=[2, 3, 5, 0] pop_size = sum(population_array) mp = MoranProcess(graph=braess_graph, number_of_players=2, w=5, mutation_probability=0.001, population_array=[2, 3, 5, 0], seed=123) for i in range(10000): mp.step() self.assertTrue(pop_size == np.sum(mp.population)) self.assertTrue(np.all(mp.population >=0))
def replicator_mutator_equation(x, t, number_of_players, mutation_matrix): x /= np.sum(x) G = network.create_braess_network() R = ReplicatorDynamics(G, number_of_players) fitness_vector = R.compute_payoff(x) average_fitness = np.dot(x, fitness_vector) temp = np.zeros_like(R.network_game.strategy_set) for i in range(len(R.network_game.strategy_set)): for j in range(len(R.network_game.strategy_set)): temp[i] += x[j] * fitness_vector[j] * mutation_matrix[j, i] # - (x[i] * average_fitness) b = x * average_fitness c = np.array(temp) - b return np.array(c, dtype=float)
def test_braess_network(self): my_graph = network.create_braess_network() my_network_game = network.Network(my_graph, 2) # print(my_network_game.payoff) # print(my_network_game.strategy_set) profile = (0, 0, 0, 2) payoff_expected = np.array([0, 0, 0, 4]) computed_payoff = my_network_game.payoff[profile] np.testing.assert_almost_equal( payoff_expected, computed_payoff, decimal=3, err_msg="Payoff for profile {} should be {}, but it is {}".format( profile, payoff_expected, computed_payoff ), ) profile = (0, 0, 1, 1) payoff_expected = np.array([0, 0, 3, 3]) computed_payoff = my_network_game.payoff[profile] np.testing.assert_almost_equal( payoff_expected, computed_payoff, decimal=3, err_msg="Payoff for profile {} should be {}, but it is {}".format( profile, payoff_expected, computed_payoff ), ) profile = (0, 0, 2, 0) payoff_expected = np.array([0, 0, 4, 0]) computed_payoff = my_network_game.payoff[profile] np.testing.assert_almost_equal( payoff_expected, computed_payoff, decimal=3, err_msg="Payoff for profile {} should be {}, but it is {}".format( profile, payoff_expected, computed_payoff ), ) profile = (0, 1, 0, 1) payoff_expected = np.array([0, 2, 0, 2]) computed_payoff = my_network_game.payoff[profile] np.testing.assert_almost_equal( payoff_expected, computed_payoff, decimal=3, err_msg="Payoff for profile {} should be {}, but it is {}".format( profile, payoff_expected, computed_payoff ), ) profile = (0, 1, 1, 0) payoff_expected = np.array([0, 3, 3, 0]) computed_payoff = my_network_game.payoff[profile] np.testing.assert_almost_equal( payoff_expected, computed_payoff, decimal=3, err_msg="Payoff for profile {} should be {}, but it is {}".format( profile, payoff_expected, computed_payoff ), ) profile = (0, 2, 0, 0) payoff_expected = np.array([0, 4, 0, 0]) computed_payoff = my_network_game.payoff[profile] np.testing.assert_almost_equal( payoff_expected, computed_payoff, decimal=3, err_msg="Payoff for profile {} should be {}, but it is {}".format( profile, payoff_expected, computed_payoff ), ) profile = (1, 0, 0, 1) payoff_expected = np.array([3, 0, 0, 3]) computed_payoff = my_network_game.payoff[profile] np.testing.assert_almost_equal( payoff_expected, computed_payoff, decimal=3, err_msg="Payoff for profile {} should be {}, but it is {}".format( profile, payoff_expected, computed_payoff ), ) profile = (1, 0, 1, 0) payoff_expected = np.array([2, 0, 2, 0]) computed_payoff = my_network_game.payoff[profile] np.testing.assert_almost_equal( payoff_expected, computed_payoff, decimal=3, err_msg="Payoff for profile {} should be {}, but it is {}".format( profile, payoff_expected, computed_payoff ), ) profile = (1, 1, 0, 0) payoff_expected = np.array([3, 3, 0, 0]) computed_payoff = my_network_game.payoff[profile] np.testing.assert_almost_equal( payoff_expected, computed_payoff, decimal=3, err_msg="Payoff for profile {} should be {}, but it is {}".format( profile, payoff_expected, computed_payoff ), ) profile = (2, 0, 0, 0) payoff_expected = np.array([4, 0, 0, 0]) computed_payoff = my_network_game.payoff[profile] np.testing.assert_almost_equal( payoff_expected, computed_payoff, decimal=3, err_msg="Payoff for profile {} should be {}, but it is {}".format( profile, payoff_expected, computed_payoff ), )