forked from firluk/Intro2AI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_table.py
More file actions
128 lines (114 loc) · 3.85 KB
/
Copy pathtrain_table.py
File metadata and controls
128 lines (114 loc) · 3.85 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
import random
import time
import numpy as np
from entities.card import Card
from entities.hand import Hand
from entities.qtabletrainer import QTableTrainer
from gym_poker.envs.poker_env import PokerEnv
def generate_tp(val=15, val2=15, val3=15):
"""Generate two pairs 5 cards hand"""
suits = ["Spade", "Club", "Diamond", "Heart"]
h = Hand()
# First Pair
choice = random.choice(suits)
choice2 = random.choice(suits)
while choice == choice2:
choice2 = random.choice(suits)
if val == 15:
val = random.choice(range(2, 15))
for su in suits:
if not (su.__eq__(choice) or su.__eq__(choice2)):
h.add_card(Card(su, val))
# Second Pair
choice = random.choice(suits)
choice2 = random.choice(suits)
while choice == choice2:
choice2 = random.choice(suits)
while val2 == 15 or val2 == val:
val2 = random.choice(range(2, 15))
for su in suits:
if not (su.__eq__(choice) or su.__eq__(choice2)):
h.add_card(Card(su, val2))
# Last Card
while val3 == 15 or val3 == val or val3 == val2:
val3 = random.choice(range(2, 15))
h.add_card(Card(random.choice(suits), val3))
return h
def generate_tok(val=15, val2=15, val3=15):
"""Generate three of a kind 5 cards hand"""
suits = ["Spade", "Club", "Diamond", "Heart"]
choice = random.choice(suits)
if val == 15:
val = random.choice(range(2, 15))
h = Hand()
for su in suits:
if not su.__eq__(choice):
h.add_card(Card(su, val))
while val2 == 15 or val2 == val:
val2 = random.choice(range(2, 15))
h.add_card(Card(random.choice(suits), val2))
while val3 == 15 or val3 == val or val3 == val2:
val3 = random.choice(range(2, 15))
h.add_card(Card(random.choice(suits), val3))
return h
def generate_fh(val=15, val2=15):
"""Generate Full house 5 cards hand"""
suits = ["Spade", "Club", "Diamond", "Heart"]
choice = random.choice(suits)
if val == 15:
val = random.choice(range(2, 15))
h = Hand()
for su in suits:
if not su.__eq__(choice):
h.add_card(Card(su, val))
choice, choice2 = random.choice(suits), random.choice(suits)
while choice2 == choice:
choice2 = random.choice(suits)
while val2 == 15 or val2 == val:
val2 = random.choice(range(2, 15))
for su in suits:
if not (su.__eq__(choice) or su.__eq__(choice2)):
h.add_card(Card(su, val2))
return h
def train_agent(crds, chips):
trainer = QTableTrainer(chips)
trainer.train_agent(crds)
def print_qt(num, ch):
_c = False
_qt = None
try:
# Load Qtable from file
with np.load('Qtable/qtablenpc.npz') as data:
_qt = data['qtable']
_c = True
except IOError:
print("error loading qtable")
if not _c:
exit(0)
_h = Hand()
_res = np.zeros(8, dtype=int)
for i in range(num):
print("Cards -- big blind -- money ranges -- small blind -- money ranges")
for j in range(i + 1, num):
_h.add_card(Card.decode(i))
_h.add_card(Card.decode(j))
for i1 in range(2):
for j1 in range(4):
_res[i1 * 4 + j1] = PokerEnv.encode(_h, i1, 1 + (j1 * (ch / 2)), ch)
_s = _h.__str__() + " - "
for i2 in range(8):
_s += str.format('{:.2f}', _qt[_res[i2]][0])
_s += "|"
_s += str.format('{:.2f}', _qt[_res[i2]][1])
_s += " -- "
print(_s)
_h.clear_hand()
if __name__ == "__main__":
strt = time.time()
cards = 52
starting_chips = 20
penalty = 25
train_agent(cards, starting_chips)
print_qt(cards, starting_chips)
elapsed = int(time.time() - strt)
print("Time elapsed: {0:d}:{1:02d}".format(elapsed // 60, elapsed % 60))