-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgent.py
More file actions
467 lines (384 loc) · 14.2 KB
/
Agent.py
File metadata and controls
467 lines (384 loc) · 14.2 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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
##########################################################################
# Agent is the class that implements the Agents of the System.
# The class keeps its own state and has direct access to the table.
# Agents with different profiles will process information differently.
# To observe risk calculation in command line, activate the prints at
# lines 250 and 263.
##########################################################################
from enum import Enum
from queue import Queue
from Chips import Chips
from Deck import Deck
from MCTS import MCTS
from Node import StepNode
from GameState import GameState
import utils
import random
import itertools
import time
class Agent:
action = None #TODO: check if this should be here
def __init__(self, identifier, table, chips, profileNumber = None):
self.table = table
self.id = identifier
self.money = Chips(chips)
self.hand = []
self.handVal = 0
self.cardHistory = []
self.deck = Deck()
self.risk = 0
self.roundHistory = []
self.roundAverage = 0
self.gameState = GameState()
if profileNumber == None:
self.profile = self.setStandardProfile()
else:
self.profile = self.setProfile(profileNumber)
self.playRisk = 0
self.opponentPlayRecord = []
def getId(self):
return self.id
#################################################
#### RANDOM DECISION-MAKING #####
#################################################
def randomChoice(self, canCheck, canRaise):
actions = list(Action)
if not canCheck:
del actions[2]
if not canRaise:
del actions[1]
self.action = random.choice(actions)
return self.action
#################################################
#### REACTIVE BEHAVIOUR #####
#################################################
def agentReactiveDecision(self, actions):
if(self.getProfile() == "Risky"):
if(self.risk > 2):
return "FOLD"
elif(self.getProfile() == "Safe"):
if(self.risk > 0.6):
return "FOLD"
elif(self.getProfile() == "Balanced"):
if(self.risk > 1):
return "FOLD"
if(self.getProfile() == "Dummy"):
if(self.risk > 1):
return "FOLD"
else:
return random.choice(actions)
elif(self.getProfile() == "Copycat"):
if(len(self.checkPlayRecords()) == 0):
return "CALL"
return self.checkPlayRecords()[-1]
return
#################################################
#### COMMUNICATION #####
#################################################
def updateGameState(self, other):
self.gameState.updateGameState(other)
def receiveMessage(self):
return [self.makeBet(), self.id]
def receiveWarn(self, flag, msg):
if(flag == "warn"):
self.opponentPlayRecord.append(msg[1])
del self.opponentPlayRecord[ : (-3*len(self.table.agents))]
def sendMessage(self,msg):
return [msg, self.id]
def receiveCards(self, cardList):
for card in cardList:
self.cardHistory.append(card)
self.deck.removeCard(card.getName())
def showHand(self):
self.findHand()
return self.handVal
def receivePot(self, potAmount):
self.money.collect(potAmount)
def fold(self):
self.money.fold()
#################################################
#### UPDATES ########
#################################################
def setProfile(self, profile):
if profile == 1:
return "Risky"
elif profile == 2:
return "Safe"
elif profile == 3:
return "Dummy"
elif profile == 4:
return "Copycat"
elif profile == 5:
return "Balanced"
return
def setStandardProfile(self):
profile = random.randint(1,5)
if profile == 1:
return "Risky"
elif profile == 2:
return "Safe"
elif profile == 3:
return "Dummy"
elif profile == 4:
return "Copycat"
elif profile == 5:
return "Balanced"
return
def calculateRoundAverage(self, counter):
self.roundHistory.append(counter)
self.roundAverage = sum(self.roundHistory)/len(self.roundHistory)
def getRoundBet(self):
return self.money.getRoundBet()
def resetRoundBet(self):
self.money.resetRoundBet()
def getMoney(self):
return self.money.getCurrent()
def payBlind(self, amount):
self.money.bet(amount)
self.resetRoundBet()
def riskCalculation(self):
potF = 0
moneyF = self.checkTheirChips()/(self.checkTheirChips() + self.checkMyChips())
if(self.table.pot < 0.25*self.checkMyChips()):
potF = 0
elif(self.table.pot < 0.5*self.checkMyChips()):
potF = 0.25
elif(self.table.pot < 0.75*self.checkMyChips()):
potF = 0.5
elif(self.table.pot < 1*self.checkMyChips()):
potF = 0.75
else:
potF = 1
self.risk = moneyF*0.6 + potF*0.4/(moneyF + potF)
def opponentModelCalculation(self):
potF = 0
action = 0
raiseF = self.opponentPlayRecord.count("RAISE")
callF = self.opponentPlayRecord.count("CALL")
foldF = self.opponentPlayRecord.count("FOLD")
checkF = self.opponentPlayRecord.count("CHECK")
actionF = 0.1*foldF + 0.1*checkF + 0.3*callF + 0.5*raiseF
if raiseF != 0:
raiseF = raiseF/len(self.opponentPlayRecord)
if callF != 0:
callF = callF/len(self.opponentPlayRecord)
if checkF != 0:
checkF = checkF/len(self.opponentPlayRecord)
moneyF = self.checkTheirChips()/(self.checkTheirChips() + self.checkMyChips())
if(self.table.pot < 0.25*self.checkMyChips()):
potF = 0
elif(self.table.pot < 0.5*self.checkMyChips()):
potF = 0.25
elif(self.table.pot < 0.75*self.checkMyChips()):
potF = 0.5
elif(self.table.pot < 1*self.checkMyChips()):
potF = 0.75
else:
potF = 1
self.risk = moneyF*0.3 + potF*0.3 + actionF*0.4
def makeBet(self):
betAmount = self.gameState.getBetAmount()
raiseAmount = self.gameState.getRaiseAmount()
canCheck = self.gameState.getCanCheck()
canRaise = self.gameState.getCanRaise()
actions = self.gameState.getActions()
state = self.gameState.getSate()
roundAvg = self.roundAverage
if state != "PRE-FLOP":
self.riskCalculation()
level = 0
if state == "TURN": level = 1
if state == "RIVER": level = 2
goReactive = self.agentReactiveDecision(actions)
if(goReactive is not None):
#print("AGENT " + str(self.id) + " IS ACTING REACTIVELY. HAS RISK: " + str(self.risk))
if goReactive == "CALL":
self.money.bet(betAmount)
return "CALL"
elif goReactive == "RAISE":
self.money.bet(betAmount + raiseAmount)
return "RAISE"
elif goReactive == "FOLD":
return "FOLD"
elif goReactive == "CHECK":
return "CHECK"
else:
self.opponentModelCalculation()
#print("AGENT " + str(self.id) + " IS ACTING DELIBERATELY. HAS RISK: " + str(self.risk))
tree = MCTS()
root = StepNode(None, level, None, len(self.table.activeAgents), state, self.deck, self.cardHistory, self.handVal, roundAvg, actions, self.profile, self.risk,
self.table.pot, self.money.getGameBet(), betAmount, raiseAmount)
startingTime = time.time()
for _ in range(20):
tree.rollout(root)
endTime = time.time() - startingTime
action = tree.choose(root, canCheck, canRaise)
if action.creationAction == "CALL":
self.money.bet(betAmount)
return "CALL"
elif action.creationAction == "RAISE":
self.money.bet(betAmount + raiseAmount)
return "RAISE"
elif action.creationAction == "FOLD":
return "FOLD"
elif action.creationAction == "CHECK":
return "CHECK"
else:
self.money.bet(betAmount)
return "CALL"
def reset(self):
self.hand = []
self.cardHistory = []
self.deck = Deck()
#################################################
#### AUXILIARY #########
#################################################
def returnRank(self, card):
return card.getNumericalValue()
def findHand(self, returnRating = False):
possible_hands = itertools.combinations(self.cardHistory, 5)
rating = 0
best = None
for hand in possible_hands:
current = self.rateHand(list(hand))
if current > rating:
rating = current
best = hand
self.hand = best
self.handVal = rating
if returnRating:
return rating
def rateHand(self, hand):
opt1 = self.checkRanks(hand)
opt2 = self.checkSequence(hand)
opt3 = self.checkSuits(hand)
#if its a high card
if opt1 == None and opt2 == None and opt3 == None:
temp = hand
temp.sort(key=self.returnRank)
return temp[-1].getNumericalValue() - 1
#if its a pair/two pairs/three of a kind/four of a kind/full house
if opt1 != None and opt2 == None and opt3 == None:
return opt1
#if its a straight
if opt1 == None and opt2 != None and opt3 == None:
return 52 + (opt2.getNumericalValue()-2)
#if its just a flush
if opt1 == None and opt2 == None and opt3 != None:
return 62 + (opt3.getNumericalValue()-2)
#if its a flush and repeated cards see which one is higher
if opt1 != None and opt2 == None and opt3 != None:
if opt1 > 62 + (opt3.getNumericalValue()-2):
return opt1
else:
return 62 + (opt3.getNumericalValue()-2)
#if its a flush and a sequence
if opt1 == None and opt2 != None and opt3 != None:
#if its a royal flush
if opt2.getNumericalValue() == 10:
return 110
#if its a straight flush
else:
return 101 + (opt2.getNumericalValue()-2)
def checkRanks(self, hand):
repeats = []
times = []
for card in hand:
if card.getNumericalValue() in repeats:
times[repeats.index(card.getNumericalValue())] += 1
else:
repeats.append(card.getNumericalValue())
times.append(1)
if len(repeats) == 5:
return None
#pair
if len(repeats) == 4:
return 14 + (repeats[times.index(2)]-2)
if len(repeats) == 3:
#three of a kind
if 3 in times:
return 40 + (repeats[times.index(3)]-2)
#two pair
else:
firstRank = repeats[times.index(2)]
index = times.index(2)
times.pop(index)
secondRank = repeats[times.index(2)]
if firstRank > secondRank:
return 27 + (firstRank -2)
else:
return 27 + (secondRank -2)
if len(repeats) == 2:
#four of a kind
if 4 in times:
return 88 + (repeats[times.index(4)]-2)
#full house
else:
return 75 + (repeats[times.index(3)]-2)
def checkSuits(self, hand):
#clubs diamonds hearts spades
suits = [0, 0, 0, 0]
for card in hand:
if card.getSuit() == "Clubs":
suits[0] += 1
elif card.getSuit() == "Diamonds":
suits[1] += 1
elif card.getSuit() == "Hearts":
suits[2] += 1
elif card.getSuit() == "Spades":
suits[3] += 1
if 5 not in suits:
return None
else:
temp = hand
temp.sort(key=self.returnRank)
return temp[-1]
def checkSequence(self, hand):
temp = hand
temp.sort(key=self.returnRank)
i = 0
seq = True
while i < len(temp)-1:
if temp[i].getNumericalValue()+1 != temp[i+1].getNumericalValue():
seq = False
break
else:
i += 1
if not seq:
return None
else:
return temp[0]
#################################################
#### SENSORS #####
#################################################
def checkMyCards(self):
return self.hand
def checkMyDeck(self):
return self.deck
def checkTableCards(self):
return table.tableCards
def checkTurn(self):
return table.turn
def checkMyChips(self):
return self.money.getCurrent()
def checkTheirChips(self):
chips = 0
for agent in self.table.agents:
chips += agent.money.getCurrent()
return chips
def checkPot(self):
return table.pot
def checkBlind(self):
return table.betAmount
def checkPlayRecords(self):
return self.opponentPlayRecord
def getProfile(self):
return self.profile
def checkProfiles(self):
opponentsProfiles = []
for a in self.table.agents:
opponentsProfiles.append(a.getProfile())
return opponentsProfiles
def checkEnvironment(self):
return self.table.environment
pass