-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_data.py
369 lines (318 loc) · 12.7 KB
/
gen_data.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import json
import sys
import csv
import keras
from treys import Card, Deck, Evaluator
import numpy as np
from preprocessor import PreProcessor
pair_strengths = json.load(open('new_hole_card_rankings.json'))
evaluator = Evaluator()
model = keras.models.load_model('best_model')
pre = PreProcessor()
def calc_hand_potential(my_cards): #calculates hand potentials for hole stage
hole = sorted(my_cards,key=lambda c:c[0], reverse=True)
s = str(hole[0][0]) + str(hole[1][0])
if hole[0][1] == hole[1][1]:
s += "s"
if s not in pair_strengths and "s" not in s:
s = s[::-1]
elif s not in pair_strengths and "s" in s:
s = ''.join([ s[x:x+2][::-1] for x in range(0, len(s), 2) ])
return pair_strengths[s]
def trans_cards(cards): #just for printing out cards
trans = []
for card in cards:
trans.append([Card.int_to_str(card)[0], Card.int_to_str(card)[1]])
return trans
# Estimate the ratio of winning games given the current state of the game
def estimate_win_rate(num_players,hole_cards,community_cards,deck):
items = deck
opponents_hole = [[items[i],items[j]] for i in range(len(items)) for j in range(i+1, len(items))]
opponent_scores = []
for h in opponents_hole:
hand_potential = 1 - evaluator.get_five_card_rank_percentage(evaluator.evaluate(community_cards, h))
opponent_scores.append(hand_potential)
hand_potential = 1 - evaluator.get_five_card_rank_percentage(evaluator.evaluate(community_cards, hole_cards))
my_score = hand_potential
win_prob = [1 for s in opponent_scores if my_score >= s]
return len(win_prob) / len(opponent_scores)
#NOTE: all player classes are very similar, only difference are the quality of hands played
class AgressivePlayer:
def __init__(self,name,money):
self.name = name
self.money = money
self.hand_cards = []
self.string_cards = []
self.hand_strength = 9
self.hand_potential = 0
self.action = ''
self.rest_cards = Deck().cards
self.win_prob = 0.0
self.comm_cards = []
def set_cards(self,cards):
self.hand_cards = cards
self.string_cards = trans_cards(self.hand_cards)
def set_comm_cards(self, cards):
self.comm_cards = cards
def take_action(self,stage,actions):
action = ''
if self.win_prob < 0.1:
action = actions[0]
elif self.win_prob >= 0.1 and self.win_prob < 0.4:
action = actions[1]
else:
action = actions[2]
self.action = action
return action
def __str__(self):
return self.name + ": " + str(self.string_cards[0]) + ", " + str(self.string_cards[1]) + ", " + str(self.win_prob)
class AveragePlayer:
def __init__(self,name,money):
self.name = name
self.money = money
self.hand_cards = []
self.string_cards = []
self.hand_strength = 9
self.hand_potential = 0
self.action = ''
self.rest_cards = Deck().cards
self.win_prob = 0.0
self.comm_cards = []
def set_cards(self,cards):
self.hand_cards = cards
self.string_cards = trans_cards(self.hand_cards)
def set_comm_cards(self, cards):
self.comm_cards = cards
def take_action(self,stage,actions):
action = ''
if self.win_prob < 0.2:
action = actions[0]
elif self.win_prob >= 0.2 and self.win_prob < 0.5:
action = actions[1]
else:
action = actions[2]
self.action = action
return action
def __str__(self):
return self.name + ": " + str(self.string_cards[0]) + ", " + str(self.string_cards[1]) + ", " + str(self.win_prob)
class SafePlayer:
def __init__(self,name,money):
self.name = name
self.money = money
self.hand_cards = []
self.string_cards = []
self.hand_strength = 9
self.hand_potential = 0
self.action = ''
self.win_prob = 0.0
self.rest_cards = Deck().cards
self.comm_cards = []
def set_cards(self,cards):
self.hand_cards = cards
self.string_cards = trans_cards(self.hand_cards)
def set_comm_cards(self, cards):
self.comm_cards = cards
def take_action(self,stage,actions):
action = ''
if self.win_prob < 0.3:
action = actions[0]
elif self.win_prob >= 0.3 and self.win_prob < 0.75:
action = actions[1]
else:
action = actions[2]
self.action = action
return action
def __str__(self):
return self.name + ": " + str(self.string_cards[0]) + ", " + str(self.string_cards[1]) + ", " + str(self.win_prob)
class MyPlayer:
def __init__(self,name,money):
self.name = name
self.money = money
self.hand_cards = []
self.string_cards = []
self.hand_strength = 9
self.hand_potential = 0
self.action = ''
self.win_prob = 0.0
self.rest_cards = Deck().cards
self.comm_cards = []
def set_cards(self,cards):
self.hand_cards = cards
self.string_cards = trans_cards(self.hand_cards)
def set_comm_cards(self, cards):
self.comm_cards = cards
print("PLAYER", trans_cards(self.comm_cards))
def take_action(self,stage,actions):
enc_data = pre.encode_datapoint(stage, self.string_cards, trans_cards(self.comm_cards), self.hand_potential, self.hand_strength)
a = model.predict(enc_data.reshape(1, 110).astype(float))
a_idx = np.argmax(a)
print("MYPlayer ACTIONS: ", actions)
prediction = ''
if a_idx == 0:
prediction = 'bet'
if a_idx == 1:
prediction = 'check'
if a_idx == 2:
prediction = 'fold'
if prediction == 'bet' and 'bet' in actions:
return 'bet'
elif prediction == 'bet' and 'raise' in actions:
return 'call'
elif prediction == 'bet' and 're-raise' in actions:
return 'raise'
elif prediction == 'bet' and 'call' in actions:
return 'raise'
elif prediction == 'check' and 'check' in actions:
return 'check'
else:
return 'fold'
'''
action = ''
if self.win_prob < 0.2:
action = actions[0]
elif self.win_prob >= 0.2 and self.win_prob < 0.5:
action = actions[1]
else:
action = actions[2]
self.action = action
return action
'''
def __str__(self):
return self.name + ": " + str(self.string_cards[0]) + ", " + str(self.string_cards[1]) + ", " + str(self.win_prob)
#Class for tracking game data
class Poker:
def __init__(self):
self.deck = Deck()
self.num_players = 0
self.players = []
self.board = []
self.state = 'hole'
self.big_blind = 'myPlayer'
self.my_player_wins = False
def add_players(self,players):
for player in players:
self.num_players += 1
self.players.append(player)
def deal(self): #Deal cards
print()
print(self.state.upper())
if self.state == 'hole':
for player in self.players:
player.set_cards(self.deck.draw(2))
player.hand_potential = calc_hand_potential(player.string_cards)
player.win_prob = 10 * ((1.0-0.0)/(2.32--0.15)*(float(player.hand_potential)-2.32)+1.0)
print(player)
player.rest_cards.remove(player.hand_cards[0])
player.rest_cards.remove(player.hand_cards[1])
if player.name == self.big_blind:
#player.metric = get_metric(self.state,float(player.hand_potential),int(player.hand_strength))
player.action = 'call'
self.act()
if len(self.players) > 1: #get player action
self.state = 'flop'
elif self.state == 'flop':
self.board = self.deck.draw(3)
print(trans_cards(self.board))
for player in self.players:
player.set_comm_cards(self.board)
for k in self.board:
player.rest_cards.remove(k)
player.win_prob = estimate_win_rate(self.num_players,player.hand_cards,self.board,player.rest_cards)
self.act()
if len(self.players) > 1:
self.state = 'turn'
elif self.state == 'turn':
self.board.append(self.deck.draw(1))
print(trans_cards(self.board))
for player in self.players:
player.set_comm_cards(self.board)
player.rest_cards.remove(self.board[3])
player.win_prob = estimate_win_rate(self.num_players,player.hand_cards,self.board,player.rest_cards)
self.act()
if len(self.players) > 1:
self.state = 'river'
elif self.state == 'river':
self.board.append(self.deck.draw(1))
print(trans_cards(self.board))
for player in self.players:
player.set_comm_cards(self.board)
player.rest_cards.remove(self.board[4])
player.win_prob = estimate_win_rate(self.num_players,player.hand_cards,self.board,player.rest_cards)
self.act()
self.state = 'showdown'
if self.state == 'showdown':
best_hand = 0.0
winner = self.players[0]
for k in self.players:
if (k.win_prob) > best_hand:
best_hand = k.win_prob
winner = k
self.players = [winner]
self.get_result()
def get_result(self):
print()
if self.players[0].name == "myPlayer":
self.my_player_wins = True
print("WINNER:", self.players[0])
def handle_bet(self,current_players,ordered_players,actions):
prev_action = 'bet'
curr_action = ''
t = 0
for player in ordered_players:
curr_action = player.take_action(self.state,actions)
print(player.name,player.win_prob,curr_action)
if curr_action == 'fold': #remove folded
current_players.remove(player)
elif curr_action == actions[2]: #if bet,raise,reraise
idx = current_players.index(player)
new_iter = current_players[idx+1:] + current_players[:idx]
if actions[2] == 'raise':
self.handle_bet(current_players,new_iter,['fold','call','re-raise'])
break
elif actions[2] == 're-raise':
self.handle_bet(current_players,new_iter,['fold','call','call'])
break
self.players = current_players
def act(self):
prev_action = ''
curr_action = ''
t = 0
current_players = self.players.copy()
for player in self.players:
if player.name == self.big_blind and self.state == 'hole':
player.action = 'call'
curr_action = 'call'
elif ((player.name == self.big_blind and self.state != 'hole') or (prev_action == 'check') or (self.state != 'hole' and t==0)):
curr_action = player.take_action(self.state,['check', 'check','bet'])
elif prev_action == 'call' or prev_action == 'fold':
curr_action = player.take_action(self.state,['fold', 'call','bet'])
print(player.name,player.win_prob,curr_action)
if curr_action == 'fold':
current_players.remove(player)
elif curr_action == 'bet':
new_iter = current_players[t+1:] + current_players[:t]
self.handle_bet(current_players,new_iter,['fold','call','raise'])
break
t += 1
prev_action = curr_action
self.players = current_players
if len(self.players) == 1:
self.state = 'showdown'
for i in range(100):
poker = Poker()
w = MyPlayer("myPlayer",100)
e = AgressivePlayer("agressivePlayer",50)
a = AveragePlayer("averagePlayer",25)
p = SafePlayer("safePlayer",50)
#poker.add_players([w,e,a,p])
#poker.add_players([w, e])
#poker.add_players([w, a])
poker.add_players([w, p])
my_wins = 0
for state in ['hole','flop','flop+turn','flop+turn+river']: #iterate through stages of hand
poker.deal()
if poker.state == 'showdown':
if poker.my_player_wins == True:
my_wins += 1
break
print("Model won ", my_wins, "out of 100 games")