-
Notifications
You must be signed in to change notification settings - Fork 1
/
KuhnTrainer.py
220 lines (172 loc) · 6.97 KB
/
KuhnTrainer.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
import tensorflow as tf
import numpy
import GameProcessor
import random
rng = numpy.random
import numpy as np
import math
import copy
from KuhnPoker import KuhnPoker, Players, Moves, Results, NextPlayer, MovesToOneHot, CardsToOneHot
from Utils import *
#numpy.random.seed(12)
# Parameters
learning_rate = 1e-4
training_epochs = 2000
SAMPLE_SIZE = 80
DISPLAY_STATS_STEP = 10
INPUT_SIZE = 4 # Max - three possible moves
OUTPUT_SIZE = 2 # Two actions: pass, bet
alpha = 0.1
beta = 0.005
# Network Parameters
n_hidden_1 = 54 # 1st layer number of features
n_hidden_2 = 54 # 2nd layer number of features
# # Store layers weight & bias
# weights = {
# 'h1': tf.Variable(tf.random_normal([INPUT_SIZE, n_hidden_1])),
# 'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
# 'out': tf.Variable(tf.random_normal([n_hidden_2, OUTPUT_SIZE]))
# }
# biases = {
# 'b1': tf.Variable(tf.random_normal([n_hidden_1])),
# 'b2': tf.Variable(tf.random_normal([n_hidden_2])),
# 'out': tf.Variable(tf.random_normal([OUTPUT_SIZE]))
# }
# Store layers weight & bias
weights = {
'h1': tf.Variable(tf.ones([INPUT_SIZE, n_hidden_1])),
'h2': tf.Variable(tf.ones([n_hidden_1, n_hidden_2])),
'out': tf.Variable(tf.ones([n_hidden_2, OUTPUT_SIZE]))
}
biases = {
'b1': tf.Variable(tf.ones([n_hidden_1])),
'b2': tf.Variable(tf.ones([n_hidden_2])),
'out': tf.Variable(tf.ones([OUTPUT_SIZE]))
}
infoState = tf.placeholder("float", [1, INPUT_SIZE], name= "infoState")
# Create model
def multilayer_perceptron(x, weights, biases):
# Hidden layer with RELU activation
layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
layer_1 = tf.nn.relu(layer_1)
#dropOp1 = tf.nn.dropout(layer_1, 0.5)
# Hidden layer with RELU activation
layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
layer_2 = tf.nn.relu(layer_2)
#dropOp2 = tf.nn.dropout(layer_2, 0.5)
# Output layer with linear activation
out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
return out_layer
def TrainModel():
# Construct model
pred = tf.nn.softmax(multilayer_perceptron(infoState, weights, biases))
Y = tf.placeholder(tf.float32, [1, OUTPUT_SIZE], name= "Y_output")
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels = Y, logits = pred)
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy)
init = tf.global_variables_initializer()
pokerEngine = KuhnPoker()
permProb = 1.0 / len(pokerEngine.cardsPermutations)
with tf.Session() as sess:
sess.run(init)
displayCyclePayoff = 0
globalPayoff = 0
roundsCount = 0
lastPayoffCount = pokerEngine.PayoffCount
for epoch in range(training_epochs):
currentPayoff = UpdateSubGraphAndGetValue(sess, pred, optimizer, Y, pokerEngine, Players.one, permProb)
displayCyclePayoff += currentPayoff
if(pokerEngine.NewRound() == 1):
roundsCount += 1
if(roundsCount == DISPLAY_STATS_STEP):
roundsCount = 0
globalPayoff += displayCyclePayoff
gamesCount = pokerEngine.PayoffCount - lastPayoffCount
p1Payoff = displayCyclePayoff
p2Payoff = -p1Payoff
strToPrint = "Cycle payoff. p1: {0:+.3f}, p2: {1:+.3f}" .format(p1Payoff / gamesCount, p2Payoff / gamesCount)
print(strToPrint)
lastPayoffCount = pokerEngine.PayoffCount
displayCyclePayoff = 0
print("Total global payoff: ", globalPayoff)
def GetStateArray(pokerEngine, player):
card = pokerEngine.GetPlayerCard(player)
carOneHot = CardsToOneHot[card]
mixedAr = np.append(pokerEngine.infoSet, carOneHot) #rotate
mixedAr = mixedAr.reshape((-1, len(mixedAr)))
return mixedAr
def UpdateSubGraphAndGetValue(sess, pred, optimizer, Y, pokerEngine, player, graphProb):
infosetBackup = pokerEngine.infoSet.copy()
pokerEngineMoveId = pokerEngine.currentMoveId
if (random.random() < beta):
movesProbabileties = np.array([0.33, 0.33])
else:
mixedAr = GetStateArray(pokerEngine, player)
movesProbabileties = sess.run(pred, feed_dict={infoState: mixedAr})[0]
sampleSize = SAMPLE_SIZE * graphProb
if(sampleSize < 1):
if(random.random() < sampleSize):
moveIds = MakeNormChoise(movesProbabileties, 1)
else:
moveIds = np.array([])
else:
sampleSize = round(sampleSize)
moveIds = MakeNormChoise(movesProbabileties, sampleSize)
totalPayoff = 0
for moveId in moveIds:
oneHotMove = MovesToOneHot[moveId + 1]
pokerEngine.MakeOneHotMove(oneHotMove)
if(pokerEngine.IsTerminateState()):
currentPayoff = pokerEngine.GetPayoff(player)
else:
subGraphProb = graphProb * movesProbabileties[moveId]
currentPayoff = -UpdateSubGraphAndGetValue(sess, pred, optimizer, Y, pokerEngine, NextPlayer(player), subGraphProb)
totalPayoff += currentPayoff
newMoveProb = movesProbabileties[moveId] + movesProbabileties[moveId] * currentPayoff * alpha
movesProbabileties[moveId] = newMoveProb
movesProbabileties = Normalise(movesProbabileties)
pokerEngine.infoSet = infosetBackup.copy()
pokerEngine.currentMoveId = pokerEngineMoveId
Yar = movesProbabileties.reshape((-1, len(movesProbabileties)))
mixedAr = GetStateArray(pokerEngine, player)
sess.run(optimizer, feed_dict={Y: Yar, infoState: mixedAr})
return totalPayoff
def GetSubSamplingPayOff(pokerEngine, player):
infosetBackup = pokerEngine.SaveInfoSet()
movesProbabileties = [0.33, 0.33]
moveIds = MakeNormChoise(movesProbabileties, SAMPLE_SIZE)
totalPayoff = 0
for moveId in moveIds:
oneHotMove = MovesToOneHot[moveId + 1]
pokerEngine.MakeOneHotMove(oneHotMove)
if(pokerEngine.IsTerminateState()):
totalPayoff += pokerEngine.GetPayoff(player)
else:
totalPayoff -= GetSubSamplingPayOff(pokerEngine, NextPlayer(player))
pokerEngine.RestoreInfoSet(infosetBackup)
return totalPayoff
def GetSingeRandomWalkPayoff(pokerEngine):
if (bool(random.getrandbits(1))):
pokerEngine.MakeMove(Moves.bet)
else:
pokerEngine.MakeMove(Moves.pas)
if (pokerEngine.IsTerminateState()):
return pokerEngine.GetPayoff(Players.one)
else:
return GetSingeRandomWalkPayoff(pokerEngine)
if __name__ == '__main__':
TrainModel()
# pokerEngine = KuhnPoker()
# # totalPayoff = 0
# #
# # for _ in range(2000):
# # totalPayoff += RndGame(pokerEngine)
# # pokerEngine.NewRound()
#
# totalPayoff = 0
# for _ in range(1000):
# currentPayoff = GetSubSamplingPayOff(pokerEngine, Players.one)
# totalPayoff += currentPayoff
# #print(pokerEngine.cards, currentPayoff)
# pokerEngine.NewRound()
#
# print("Total: ", totalPayoff)