-
Notifications
You must be signed in to change notification settings - Fork 1
/
KuhnPoker.py
294 lines (222 loc) · 9.73 KB
/
KuhnPoker.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
from enum import Enum
from queue import *
import numpy as np
import itertools
class Players(Enum):
one = 1
two = 2
class Moves(Enum):
uplayed = 0
pas = 1
bet = 11
#list(map(int, Color))
MovesToOneHot = {0: 0, 1: 1, 2: 11}
CardsToOneHot = {1: 0, 2: 1, 3: 11}
NUM_ACTIONS = 2
class Results(Enum):
toHighCard = 0
toPlayer1 = 1
toPlayer2 = 2
CARDS = [1, 2, 3]
def NextPlayer(currentPlayer):
if(currentPlayer == Players.one):
return Players.two
return Players.one
class KuhnPoker:
MaxDif = 0
@staticmethod
def JoinMoves(moves):
res = ";".join(move.name for move in moves)
return res
def AddToPayoffTable(self, result, moves):
movesStr = KuhnPoker.JoinMoves(moves)
self.payoffTable[movesStr] = result
def InitPayoffTable(self):
self.AddToPayoffTable([Results.toHighCard, 1], [Moves.pas, Moves.pas, Moves.uplayed])
self.AddToPayoffTable([Results.toPlayer2, 1], [Moves.pas, Moves.bet, Moves.pas])
self.AddToPayoffTable([Results.toHighCard, 2], [Moves.pas, Moves.bet, Moves.bet])
self.AddToPayoffTable([Results.toPlayer1, 1], [Moves.bet, Moves.pas, Moves.uplayed])
self.AddToPayoffTable([Results.toHighCard, 2], [Moves.bet, Moves.bet, Moves.uplayed])
def InitMovesSet(self):
self.moves = np.array([Moves.uplayed.value, Moves.uplayed.value, Moves.uplayed.value])
self.currentMoveId = 0
def __init__(self):
self.PayoffCount = 0
self.payoffTable = {}
self.InitPayoffTable()
self.InitMovesSet()
self.cardsPermutations = []
for deck in itertools.permutations(CARDS):
self.cardsPermutations.append(deck)
self.permutationIndex = 0
self.cards = None
# def GetFullInfoSet(self, player):
# card = self.GetPlayerCard(player)
# for i in range(self.C)
#
# def GetPrettyInfoset(self):
# target = self.infoSet
# for action in Moves:
# target = target.replace(action.value, action)
#
# return target
def MovesToStr(self, movesValuesArray):
return KuhnPoker.JoinMoves([Moves(move) for move in movesValuesArray])
def _getInfoset(self, moves, curPlayer):
card = self.GetPlayerCard(curPlayer)
infosetString = str(card) + " | " + self.MovesToStr(moves)
return infosetString
def GetInfoset(self, curPlayer):
return self._getInfoset(self.moves, curPlayer)
def GetLastSubState(self, curPlayer):
if (curPlayer == Players.one):
if(self.currentMoveId == 0):
return str(self.GetPlayerCard(curPlayer))
elif(self.currentMoveId == 2):
lastMove = self.moves[self.currentMoveId - 1]
return str(Moves(lastMove).name)
else:
raise ValueError("Call in incorrect currentMoveId")
else:
if (self.currentMoveId == 1):
lastMove = self.moves[self.currentMoveId - 1]
lastMoveStr = self._getInfoset([lastMove], curPlayer)
return lastMoveStr
else:
raise ValueError("Call in incorrect currentMoveId")
def GetPrevInfoset(self, curPlayer):
prevInfoset = self.moves.copy()
if (curPlayer == Players.one):
if(self.currentMoveId == 0):
return "GameTree"
if(self.currentMoveId == 2):
prevInfoset[self.currentMoveId - 1] = Moves.uplayed.value
prevInfoset[self.currentMoveId - 2] = Moves.uplayed.value
else:
raise ValueError("Call in incorrect currentMoveId")
else:
if (self.currentMoveId == 1):
return "GameTree"
else:
raise ValueError("Call in incorrect currentMoveId")
return self._getInfoset(prevInfoset, curPlayer)
def GetPlayerCard(self, player):
if(not self.cards):
raise ValueError("Round not started")
if (player == Players.one):
return self.cards[0]
else:
return self.cards[1]
def SaveInfoSet(self):
infosetBackup = self.moves.copy()
pokerEngineMoveId = self.currentMoveId
return infosetBackup, pokerEngineMoveId
def RestoreInfoSet(self, backupTuple):
self.moves = backupTuple[0].copy()
self.currentMoveId = backupTuple[1]
def GetPlayerOneCard(self):
return self.cards[0]
def GetCurrentPlayer(self):
playerId = (self.currentMoveId % 2) + 1
return Players(playerId)
def GetPlayerTwoCard(self):
return self.cards[1]
def IsTerminateState(self):
moves = self.MovesToStr(self.moves)
return moves in self.payoffTable
def GetPayoff(self, player):
self.PayoffCount += 1
cardOne = self.GetPlayerOneCard()
cardTwo = self.GetPlayerTwoCard()
if(cardOne == cardTwo):
raise ValueError("The same cards")
movesStr = self.MovesToStr(self.moves)
result = self.payoffTable[movesStr]
winner = result[0]
reward = result[1]
if(player == Players.two):
reward = reward * (-1);
if(winner == Results.toPlayer1):
return reward
elif(winner == Results.toPlayer2):
return -reward
else:
if(cardOne > cardTwo):
return reward
else:
return -reward
def _nexDeck(self):
for deck in itertools.permutations(CARDS):
yield deck
def NewRound(self):
retValue = 0
self.cards = self.cardsPermutations[self.permutationIndex]
self.permutationIndex += 1
if(self.permutationIndex >= len(self.cardsPermutations)):
self.permutationIndex = 0
retValue = 1
#self.cards = self.cardsPermutations[len(self.cardsPermutations) - 1] #ToDO: REMOVE!!!!!! Temp!!!!!!
self.InitMovesSet()
return retValue
# Make move from action 0 - pas; 1 - bet
def MakeAction(self, action):
curMove = Moves(MovesToOneHot[action + 1])
self.MakeMove(curMove)
def MakeMove(self, move):
self.MakeOneHotMove(move.value)
def MakeOneHotMove(self, adHocMove):
if (not self.cards):
raise ValueError("Round not started")
if(self.currentMoveId >= len(self.moves)):
ValueError("To much moves!")
self.moves[self.currentMoveId] = adHocMove
self.currentMoveId += 1
@staticmethod
def IsClose(real, target, tolerance):
dif = abs(real - target)
if(KuhnPoker.MaxDif < dif):
KuhnPoker.MaxDif = dif
return dif <= tolerance
@staticmethod
def IsPlayerTwoCloseToNash(playerTwoTree, tolerance=0.07):
playerTwoStrategy = playerTwoTree['1 | bet;uplayed;uplayed'].data.GetAverageStrategy()
if (not KuhnPoker.IsClose(playerTwoStrategy[1], 0.0, tolerance)): # when having a Jack, never calling
return False
playerTwoStrategy = playerTwoTree['1 | pas;uplayed;uplayed'].data.GetAverageStrategy()
if (not KuhnPoker.IsClose(playerTwoStrategy[1], 1 / 3, tolerance)): # when having a Jack, betting with the probability of 1/3
return False
playerTwoStrategy = playerTwoTree['2 | pas;uplayed;uplayed'].data.GetAverageStrategy()
if (not KuhnPoker.IsClose(playerTwoStrategy[0], 1.0, tolerance)): # when having a Queen, checking if possible
return False
playerTwoStrategy = playerTwoTree['2 | bet;uplayed;uplayed'].data.GetAverageStrategy()
if (not KuhnPoker.IsClose(playerTwoStrategy[1], 1 / 3, tolerance)): # otherwise calling with the probability of 1/3
return False
playerTwoStrategy = playerTwoTree['3 | pas;uplayed;uplayed'].data.GetAverageStrategy()
if (not KuhnPoker.IsClose(playerTwoStrategy[0], 0.0, tolerance)): # Always betting or calling when having a King
return False
playerTwoStrategy = playerTwoTree['3 | bet;uplayed;uplayed'].data.GetAverageStrategy()
if (not KuhnPoker.IsClose(playerTwoStrategy[1], 1.0, tolerance)): # Always betting or calling when having a King
return False
return True
@staticmethod
def IsPlayerOneCloseToNash(playerOneTree, tolerance=0.07):
playerStrategy = playerOneTree['1 | uplayed;uplayed;uplayed'].data.GetAverageStrategy()
alpha = playerStrategy[1]
if (alpha > (1/3 + tolerance) or alpha < 0): # freely chooses the probability alpha with which he will bet when having a Jack [0; 1/3]
return False
playerStrategy = playerOneTree['1 | pas;bet;uplayed'].data.GetAverageStrategy()
if (not KuhnPoker.IsClose(playerStrategy[1], 0.0, tolerance)):
return False
playerStrategy = playerOneTree['2 | uplayed;uplayed;uplayed'].data.GetAverageStrategy()
if (not KuhnPoker.IsClose(playerStrategy[0], 1.0, tolerance)): # he should always check when having a Queen
return False
playerStrategy = playerOneTree['2 | pas;bet;uplayed'].data.GetAverageStrategy()
if (not KuhnPoker.IsClose(playerStrategy[1], (1/3 + alpha), tolerance)): # if the other player bets after this check, he should call with the probability of 1/3 + alpha
return False
playerStrategy = playerOneTree['3 | uplayed;uplayed;uplayed'].data.GetAverageStrategy()
if (not KuhnPoker.IsClose(playerStrategy[1], 3 * alpha, tolerance)): # When having a King, he should bet with the probability of 3 * alpha
return False
playerStrategy = playerOneTree['3 | pas;bet;uplayed'].data.GetAverageStrategy()
if (not KuhnPoker.IsClose(playerStrategy[1], 1.0, tolerance)):
return False
return True