|
| 1 | +import pygambit as gbt |
| 2 | + |
| 3 | + |
| 4 | +def check_subtensor(game, payoffs, seq_dict, player_index, sequence_form): |
| 5 | + if player_index == len(game.players): |
| 6 | + for player in game.players: |
| 7 | + assert sequence_form.get_payoff(seq_dict, player) == payoffs[player.label] |
| 8 | + else: |
| 9 | + player = game.players[player_index] |
| 10 | + seq_dict[player] = None |
| 11 | + sub_payoffs = payoffs[None] |
| 12 | + check_subtensor(game, sub_payoffs, seq_dict, player_index + 1, sequence_form) |
| 13 | + for action in player.actions: |
| 14 | + seq_dict[player] = action |
| 15 | + sub_payoffs = payoffs[action.label] |
| 16 | + check_subtensor(game, sub_payoffs, seq_dict, player_index + 1, sequence_form) |
| 17 | + |
| 18 | +def test_sequence_form(game, payoffs): |
| 19 | + sequence_form = gbt.GameSequenceForm(game) |
| 20 | + seq_dict = {} |
| 21 | + check_subtensor(game, payoffs, seq_dict, 0, sequence_form) |
| 22 | + |
| 23 | + |
| 24 | +g = gbt.Game.new_tree( |
| 25 | + players=["Buyer", "Seller"], |
| 26 | + title="One-shot trust game, after Kreps (1990)" |
| 27 | +) |
| 28 | + |
| 29 | +g.append_move( |
| 30 | + g.root, # This is the node to append the move to |
| 31 | + player="Buyer", |
| 32 | + actions=["A", "B"] |
| 33 | +) |
| 34 | + |
| 35 | +g.set_outcome( |
| 36 | + g.root.children[0], |
| 37 | + outcome=g.add_outcome( |
| 38 | + payoffs=[4, 1], |
| 39 | + label="10" |
| 40 | + ) |
| 41 | +) |
| 42 | + |
| 43 | +g.set_outcome( |
| 44 | + g.root.children[1], |
| 45 | + outcome=g.add_outcome( |
| 46 | + payoffs=[4, 1], |
| 47 | + label="1" |
| 48 | + ) |
| 49 | +) |
| 50 | + |
| 51 | +p1 = g.players[0] |
| 52 | +p2 = g.players[1] |
| 53 | + |
| 54 | + |
| 55 | +names = {p1 : ["A", "B"], p2 : []} |
| 56 | +pffs = {None: {None : {"Buyer" : 0, "Seller" : 0}}, "A" : {None : {"Buyer" : 4, "Seller" : 1}}, "B" : {None : {"Buyer" : 4, "Seller" : 1}}} |
| 57 | + |
| 58 | +test_sequence_form(g, pffs) |
0 commit comments