forked from xylary/deepstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_strategy.py
More file actions
92 lines (75 loc) · 3.8 KB
/
example_strategy.py
File metadata and controls
92 lines (75 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# coding: utf-8
import numpy as np
def random_distribution(n_items):
""" Returns a random probability distribution over n items. Formally, we
choose a point uniformly at random from the n-1 simplex.
"""
return np.random.dirichlet([1.0 for i in range(n_items)])
def uniformly_random_strategy(game, player):
""" Returns a dictionary from information set identifiers to probabilities
over actions for the given player. The distribution is always uniform over
actions.
- game is an ExtensiveGame instance.
"""
info_sets = game.info_set_ids
# Restrict to information sets where the given player has to take an action.
player_info_sets = {k: v for k, v in info_sets.items() if k.player == player}
# Now randomly generate player 1's strategy and player 2's strategy. Define
# a strategy as being a dictionary from information set identifiers to
# probabilities over actions available in that information set.
strategy = {}
for node, identifier in player_info_sets.items():
actions = node.children.keys()
# Only need to add to the strategy for nodes whose information set has
# not been included already.
if identifier not in strategy:
# Sample actions uniformly at random. Can change this later.
strategy[identifier] = {a: 1.0 / float(len(actions)) for a in actions}
return strategy
def random_strategy(game, player):
""" We return a dictionary from information set identifiers to probabilities
over actions for the given player.
- game is an ExtensiveGame instance.
"""
info_sets = game.build_information_sets(player)
# Restrict to information sets where the given player has to take an action.
player_info_sets = {k: v for k, v in info_sets.items() if k.player == player}
# Now randomly generate player 1's strategy and player 2's strategy. Define
# a strategy as being a dictionary from information set identifiers to
# probabilities over actions available in that information set.
strategy = {}
for node, identifier in player_info_sets.items():
actions = node.children.keys()
# Only need to add to the strategy for nodes whose information set has
# not been included already.
if identifier not in strategy:
# Sample actions uniformly at random. Can change this later.
probs = random_distribution(len(actions))
strategy[identifier] = {a: p for a, p in zip(actions, probs)}
return strategy
def constant_action(game, player, action):
""" This strategy always plays action 'action'. We return a dictionary from
information set identifiers to probabilities over actions for the given
player.
- game is an ExtensiveGame instance.
"""
info_sets = game.build_information_sets(player)
# Restrict to information sets where the given player has to take an action.
player_info_sets = {k: v for k, v in info_sets.items() if k.player == player}
# Now randomly generate player 1's strategy and player 2's strategy. Define
# a strategy as being a dictionary from information set identifiers to
# probabilities over actions available in that information set.
strategy = {}
for node, identifier in player_info_sets.items():
actions = node.children.keys()
# Only need to add to the strategy for nodes whose information set has
# not been included already.
if identifier not in strategy:
# Play the action specified. If it's not available, play uniformly
# over all actions.
strategy[identifier] = {a: 0.0 for a in actions}
if action in actions:
strategy[identifier][action] = 1.0
else:
strategy[identifier] = {a: 1.0 / len(actions) for a in actions}
return strategy