-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathenvironment.py
166 lines (140 loc) · 5.43 KB
/
environment.py
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import sys
import os
sys.path.insert(0,os.path.realpath('../cfr/'))
from pokertrees import *
from pokerstrategy import *
import random
class Agent(object):
"""
An abstract base class that poker agents must extend.
"""
def __init__(self, rules, seat):
self.infoset = None
self.possible_states = None
self.seat = seat
self.rules = rules
def episode_starting(self):
pass
def set_holecards(self, hc):
self.holecards = hc
def episode_over(self, state):
"""
For now, we assume all imperfect information is revealed at the end of the episode.
"""
pass
def set_infoset(self, infoset):
self.infoset = infoset
def get_action(self):
pass
def observe_action(self, player, board, bet_history, action):
pass
def observe_reward(self, r):
pass
class HumanAgent(Agent):
def __init__(self, rules, seat):
Agent.__init__(self, rules, seat)
def set_holecards(self, hc):
Agent.set_holecards(self, hc)
print 'Player {0}: {1}'.format(self.seat, self.holecards)
def get_action(self):
s = raw_input('Your action (f, c, r) >> ')
while s is not 'f' and s is not 'c' and s is not 'r':
s = raw_input('Your action (f, c, r) >> ')
if s is 'f':
return FOLD
if s is 'c':
return CALL
return RAISE
class SavedAgent(Agent):
def __init__(self, rules, seat, filename):
Agent.__init__(self, rules, seat)
self.strategy = Strategy(seat)
self.strategy.load_from_file(filename)
def set_infoset(self, infoset):
self.infoset = infoset
def get_action(self):
return self.strategy.sample_action(self.infoset)
class StationaryRandomAgent(Agent):
def __init__(self, rules, seat, gametree=None):
Agent.__init__(self, rules, seat)
self.strategy = Strategy(seat)
if gametree is None:
gametree = GameTree(rules)
if gametree.root is None:
gametree.build()
self.strategy.build_random(gametree)
def set_infoset(self, infoset):
self.infoset = infoset
def get_action(self):
return self.strategy.sample_action(self.infoset)
class GameSimulator(object):
def __init__(self, rules, agents, verbose=False, showhands=True):
self.agents = agents
self.verbose = verbose
self.showhands = showhands
self.rules = rules
self.tree = PublicTree(rules)
self.tree.build()
def play(self):
for agent in self.agents:
agent.episode_starting()
self.play_helper(self.tree.root, [() for agent in self.agents])
def play_helper(self, node, holecards):
if type(node) is TerminalNode:
return self.terminal_node(node)
if type(node) is HolecardChanceNode or type(node) is BoardcardChanceNode:
return self.chance_node(node)
return self.action_node(node)
def terminal_node(self, node, holecards):
for i,agent in enumerate(self.agents):
agent.observe_reward(node.payoffs[i])
agent.episode_over(node)
if self.verbose:
self.print_payoffs(node)
return node.payoffs
def chance_node(self, node):
next = random.choice(node.children)
if self.verbose:
if type(node) is HolecardChanceNode:
print 'Dealing holecards'
if type(node) is BoardcardChanceNode:
print 'Dealing board: {0}'.format(next.board)
if type(node) is HolecardChanceNode:
for i,agent in enumerate(self.agents):
agent.set_holecards(next.holecards[i])
return self.play_helper(next)
def action_node(self, node):
agent = self.agents[node.player]
agent.set_infoset(node.player_view)
action = agent.get_action()
next = node.valid(action)
for agent in self.agents:
agent.observe_action(node.player, node.board, node.bet_history, action)
if self.verbose:
self.print_action(action, node, next)
return self.play_helper(next)
def print_action(self, action, curnode, nextnode):
if action is FOLD:
print 'Player {0} folds.'.format(curnode.player)
else:
commit = nextnode.committed[curnode.player] - curnode.committed[curnode.player]
if action is CALL:
if commit == 0:
print 'Player {0} checks.'.format(curnode.player)
else:
print 'Player {0} calls ${1}'.format(curnode.player, commit)
else:
cur_round = curnode.bet_history.count('/') - 1
bets = curnode.bet_history[curnode.bet_history.rfind('/'):].count('r')
if bets > 0 or (cur_round is 0 and self.rules.blinds is not None and len(self.rules.blinds) > 0):
print 'Player {0} raises ${1}'.format(curnode.player, commit)
else:
print 'Player {0} bets ${1}'.format(curnode.player, commit)
def print_payoffs(self, node):
if self.showhands:
for i,agent in enumerate(self.agents):
print 'Player {0} shows {1}'.format(i, node.holecards[i])
for i,agent in enumerate(self.agents):
potshare = node.payoffs[i] + node.committed[i]
if potshare > 0:
print 'Player {0} wins ${1}'.format(i, potshare)