Beispiel #1
0
 def setUp(self):
     self.game = gambit.new_table([2, 2])
     self.game.players[0].label = "joe"
     self.game.players[1].label = "dan"
     self.game.outcomes[0][0] = 1
     self.game.outcomes[0][1] = 2
     self.game.outcomes[1][0] = 3
     self.game.outcomes[1][1] = 4
Beispiel #2
0
 def setUp(self):
     self.game = gambit.new_table([2, 2])
     self.game.players[0].label = "joe"
     self.game.players[1].label = "dan"
     self.game.outcomes[0][0] = 1
     self.game.outcomes[0][1] = 2
     self.game.outcomes[1][0] = 3
     self.game.outcomes[1][1] = 4
Beispiel #3
0
 def to_table(self):
     g = gambit.new_table([len(self.choices)] * self.N)
     for pl in xrange(self.N):
         for (st, label) in enumerate(self.labels):
             g.players[pl].strategies[st].label = label
     for cont in g.contingencies:
         g_outc = g[cont]
         t_outc = self[cont]
         for pl in xrange(self.N):
             g_outc[pl] = t_outc[pl]
     return g
Beispiel #4
0
 def to_table(self):
     g = gambit.new_table([ len(self.choices) ] * self.N)
     for pl in xrange(self.N):
         for (st, label) in enumerate(self.labels):
             g.players[pl].strategies[st].label = label
     for cont in g.contingencies:
         g_outc = g[cont]
         t_outc = self[cont]
         for pl in xrange(self.N):
             g_outc[pl] = t_outc[pl]
     return g
Beispiel #5
0
    def setUp(self):
        self.game = gambit.new_table([2, 2])
        self.game.players[0].label = "joe"
        self.game.players["joe"].strategies[0].label = "cooperate"
        self.game.players[1].label = "dan"
        self.game.players["dan"].strategies[1].label = "defect"

        self.profile_double = self.game.mixed_profile()
        self.profile_rational = self.game.mixed_profile(True)

        self.tree_game = gambit.read_game("test_games/mixed_behavior_game.efg")

        self.tree_profile_double = self.tree_game.mixed_profile()
        self.tree_profile_rational = self.tree_game.mixed_profile(True)
Beispiel #6
0
    def setUp(self):
        self.game = gambit.new_table([2,2])
        self.game.players[0].label = "joe"
        self.game.players["joe"].strategies[0].label = "cooperate"
        self.game.players[1].label = "dan"
        self.game.players["dan"].strategies[1].label = "defect"
        
        self.profile_double = self.game.mixed_profile()
        self.profile_rational = self.game.mixed_profile(True)

        self.tree_game = gambit.read_game("test_games/mixed_behavior_game.efg")

        self.tree_profile_double = self.tree_game.mixed_profile()
        self.tree_profile_rational = self.tree_game.mixed_profile(True)
def main():
    """ Initial execution point """
    strategie_values = [(0.0, 0.0),
                        (0.0, 0.5),
                        (0.0, 1.0),
                        (0.5, 0.0), 
                        (0.5, 0.5),                          
                        (0.5, 1.0),                          
                        (1.0, 0.0),                          
                        (1.0, 0.5),
                        (1.0, 1.0)]
    strategy_list = [game_simulation.StochasticInflationStrategy(value, index)
                     for index, value in enumerate(strategie_values)]
                         
    #The profiles are the cartesian product of the possible strategies           
    profiles = list(itertools.product(strategy_list, repeat=PLAYER_NUMBER))
    
    devprod_dist = stats.beta(ALFA_PARAM, BETA_PARAM, loc=0.0, scale=1.0)
    testprod_dist = stats.poisson(LAMBDA_PARAM)
    probability_map = {game_simulation.DEFAULT_KEY: 60.0,
                       game_simulation.SEVERE_KEY: 25.0,
                       game_simulation.NON_SEVERE_KEY: 15.0}
    scores_per_profile = []
    
    for profile in profiles:
        tester_team = []
        for index, strategy in enumerate(profile):
            tester_team.append(game_simulation.Tester("Tester " + str(index), 
                                                      strategy))
        
        sim_results = game_simulation.simulate(devprod_dist, testprod_dist,
                                          tester_team, probability_map,
                                          TIME_FRAMES, MAX_RUNS)
        pprint(tester_team)     
        print "scores ", sim_results[0]
        scores_per_profile.append(sim_results[0])

    game = gambit.new_table([len(strategie_values) for _ in range(PLAYER_NUMBER)])
    game.title = "Players: " + str(PLAYER_NUMBER) + " Strategies: " + str(len(strategie_values))

    calculate_equilibrium.define_strategies(game, PLAYER_NUMBER, strategy_list)
    calculate_equilibrium.define_payoffs(game, profiles, scores_per_profile)
    calculate_equilibrium.write_to_file(game=game)
Beispiel #8
0
def solveGame(data, outname):
	"""
		data should be an object containing:
		roles = list of roles
		numStrat = number of strategies per role
		strat = {role: {index : strategy}}
		payoff = list of list of payoffs
	"""
	g = gambit.new_table([data.numStrat[0], \
		data.numStrat[1]])
	g.title = outname

	for i,r in enumerate(data.roles):
		g.players[i].label = r
		for j in range(data.numStrat[i]):
			g.players[i].strategies[j].label = data.strat[r][j]

	for item in itertools.product(range(data.numStrat[0]),\
		range(data.numStrat[1])):
		g[item[0],item[1]][0] = Decimal(data.payoff[item[0]][item[1]][0])
		g[item[0],item[1]][1] = Decimal(data.payoff[item[0]][item[1]][1])

	# for profile in g.contingencies:
	# 	print g.players[0].strategies[profile[0]].label, \
	# 		g.players[1].strategies[profile[1]].label, \
	# 		g[profile][0], g[profile][1]

	with open("subGames/Eq-" + outname + ".json", 'w') as rF:
		# rF.write("Mapping of player strategies\n")
		# for player in g.players:
		# 	rF.write(player.label + ":[")
		# 	for strategy in player.strategies:
		# 		rF.write(strategy.label + " ")
		# 	rF.write("]\n")
		# rF.write("\n\n")
		for desc, func in methods.iteritems():
			# rF.write(desc + "\n")
			solver = func();
			res = solver.solve(g)
			writeEquToFileJson(rF, g, res)
def getMixedNashEquilibria(A, cost=True):
    # Returns all mixed NE of cost game A (2 players only)
    if cost:
        mat = reversePayoff(A)
    else:
        mat = A
    (shape, num_players, pure_moves) = parseGame(A)
    g = gambit.new_table(list(shape[1:]))
    for move in pure_moves:
        for player in range(0, num_players):
            g[tuple(move)][player] = Decimal(mat[(player,)+tuple(move)])
    solver = gambit.nash.ExternalEnumMixedSolver()
    a = solver.solve(g)
    ne = []
    for eq in a:
        new_eq = []
        for player in range(0, num_players):
            eq_pl = []
            for strat in range(0, shape[player+1]):
                eq_pl.append(eq[g.players[player].strategies[strat]])
            new_eq.append(eq_pl)
        ne.append(new_eq)
    return ne
Beispiel #10
0
def getMixedNashEquilibria(A, cost=True):
    # Returns all mixed NE of cost game A (2 players only)
    if cost:
        mat = reversePayoff(A)
    else:
        mat = A
    (shape, num_players, pure_moves) = parseGame(A)
    g = gambit.new_table(list(shape[1:]))
    for move in pure_moves:
        for player in range(0, num_players):
            g[tuple(move)][player] = Decimal(mat[(player, ) + tuple(move)])
    solver = gambit.nash.ExternalEnumMixedSolver()
    a = solver.solve(g)
    ne = []
    for eq in a:
        new_eq = []
        for player in range(0, num_players):
            eq_pl = []
            for strat in range(0, shape[player + 1]):
                eq_pl.append(eq[g.players[player].strategies[strat]])
            new_eq.append(eq_pl)
        ne.append(new_eq)
    return ne
Beispiel #11
0
#import all pre-requisites
import gambit
from gambit import *
from gambit import nash
from graphics import *

#Create new, trivial strategic form game with [n,n] strategies
g = gambit.new_table([2,2])

#Label game, players and strategies

g.title = "A fight for survival"
g.players[0].label = "Refugee"
g.players[1].label = "Army Officer"

g.players[0].strategies[0].label = "Kill"
g.players[0].strategies[0].label = "Leave"

g.players[1].strategies[1].label = "Kill"
g.players[1].strategies[1].label = "Leave"

#Assign strategies in the form g[strategy of p1 , strategy of p2][payoff for player #]
g[0,0][0] = 8
g[0,0][1] = 4
g[0,1][0] = 2
g[0,1][1] = 10
g[1,0][0] = 3
g[1,0][1] = 2
g[1,1][0] = 2
g[1,1][1] = 5
Beispiel #12
0
 def setUp(self):
     self.strategic_game = gambit.new_table([2,2])
     self.strategic_game.players[0].label = "Alphonse"
     self.strategic_game.players[1].label = "Gaston"
     self.extensive_game = gambit.new_tree()
Beispiel #13
0
 def test_game_players_index_exception_player(self):
     "Test to verify when a player object is not in the players"
     self.strategic_game_2 = gambit.new_table([2,2])
     assert_raises(IndexError, self.strategic_game.players.__getitem__, self.strategic_game_2.players[0])
Beispiel #14
0
 def setUp(self):
     self.game = gambit.new_table([2, 2])
     self.game.players[0].label = "Alphonse"
     self.game.players[1].label = "Gaston"
Beispiel #15
0
 def setUp(self):
     self.game = gambit.new_table([2,2])
     self.extensive_game = gambit.read_game("test_games/basic_extensive_game.efg")
Beispiel #16
0
 def test_game_players_index_exception_player(self):
     "Test to verify when a player object is not in the players"
     self.strategic_game_2 = gambit.new_table([2, 2])
     assert_raises(IndexError, self.strategic_game.players.__getitem__,
                   self.strategic_game_2.players[0])
Beispiel #17
0
 def test_game_strategies_index_exception_player(self):
     "Test to verify when a strategy object is not in a player's list of strategies"
     self.game_2 = gambit.new_table([2,2])
     assert_raises(IndexError, self.game.players[0].strategies.__getitem__, self.game_2.players[0].strategies[0])
Beispiel #18
0
 def test_game_strategies_index_exception_player(self):
     "Test to verify when a strategy object is not in a player's list of strategies"
     self.game_2 = gambit.new_table([2, 2])
     assert_raises(IndexError, self.game.players[0].strategies.__getitem__,
                   self.game_2.players[0].strategies[0])
Beispiel #19
0
 def setUp(self):
     self.game = gambit.new_table([2,2])
     self.game.players[0].label = "Alphonse"
     self.game.players[1].label = "Gaston"
Beispiel #20
0
 def setUp(self):
     self.game = gambit.new_table([2,2])
Beispiel #21
0
 def setUp(self):
     self.strategic_game = gambit.new_table([2, 2])
     self.strategic_game.players[0].label = "Alphonse"
     self.strategic_game.players[1].label = "Gaston"
     self.extensive_game = gambit.new_tree()
Beispiel #22
0
 def setUp(self):
     self.game = gambit.new_table([2, 2])