-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiment_runner.py
275 lines (222 loc) · 7.54 KB
/
experiment_runner.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
import importlib
import json
import os
from random import *
from sys import *
##############################################
# Parameters
##############################################
import time
HANDS_TO_PLAY = 30000
STEP_LENGTH = 2000
REPEATS = 10
BIG_BLIND = 10
HAND_MAX = 9
STACK = 300
GAME_STATE = []
##############################################
# Helper functions
##############################################
def message(kind, msg):
pass
# print "### POKER( %s ): %s" % (kind, msg)
def winnings(hand, bet):
payment = [0, 0, 0]
[(h1, b1, i1), (h2, b2, i2), (h3, b3, i3)] = \
sorted(zip(hand, bet, range(3)), reverse=True)
if h1 == h2 == h3:
return payment
if h1 == h2:
payment[i1] = b3 / 2
payment[i2] = b3 / 2
payment[i3] = -b3
return payment
payment[i1] = b2 + b3
payment[i2] = -b2
payment[i3] = -b3
return payment
def valid_bet(bet, min, max):
return bet in range(min, max + 1, 1)
def hand_not_finished(player_in_game):
[p1, p2, p3] = player_in_game
return (p1 and p2) or (p1 and p3) or (p2 and p3)
def create_players(names):
players = []
i = 0
for name in names:
players += [importlib.import_module(name).Player()]
try:
players[i].name(i)
except:
pass
i += 1
return players
##############################################
# Game
##############################################
def game(names, filename, reinforce_nn):
scores = [0, 0, 0]
message("START GAME", str(names))
players = create_players(names)
with open(filename, 'a') as results_file:
for i in xrange(1, HANDS_TO_PLAY + 1):
message("HAND START", str(i + 1))
scores = play_hand(players, scores)
if i % STEP_LENGTH == 0:
results_file.write("\n{0},\t".format(i))
results_file.write(",\t".join(map(str, scores)))
print i, scores
scores = [0, 0, 0]
if reinforce_nn:
if os.path.exists("GAME_STATES_RND.out"):
os.remove("GAME_STATES_RND.out")
with open("GAME_STATES_RND.out", "w") as text_file:
text_file.write(json.dumps(GAME_STATE))
for player in players:
if hasattr(player, 'learn'):
player.learn()
s = ""
for i in range(3):
s += names[i] + "(" + str(scores[i]) + ") "
message("GAME RESULT", s)
return scores
def play_hand(players, scores):
# stan gry
hand = [randint(0, HAND_MAX), randint(0, HAND_MAX), randint(0, HAND_MAX)] # wylosowane karty
# hand = [randint(0, HAND_MAX)]*3
in_game = [True, True, True] # czy gracz pozostaje w grze
bet = [0, 0, 0] # wartosci licytacji
result = [0, 0, 0] # bilans rozdania
round_state = [] # zapisuj stan rundy w celu dalszej analizy
# print 'hand', hand
round_state.extend(hand[:])
# rozdaj karty
for i in range(3):
try:
players[i].hand(hand[i])
except IOError:
message("EXCEPTION", "players[%i].hand()" % i)
# pierwsza runda licytacji - obstawianie
for i in range(3):
try:
bet[i] = players[i].bet1(BIG_BLIND)
if not valid_bet(bet[i], BIG_BLIND, STACK):
bet[i] = BIG_BLIND
in_game[i] = False
hand[i] = None
except IOError:
message("EXCEPTION", "players[%i].bet1()" % i)
best_bet1 = max(bet)
# print 'bet1', bet
round_state.extend(bet[:])
# pierwsza runda licytacji - info o obstawianiu
for i in range(3):
try:
players[i].bet1_info(bet)
except IOError:
message("EXCEPTION", "players[%i].bet1_info()" % i)
# pierwsza runda licytacji - sprawdzanie
if hand_not_finished(in_game):
for i in range(3):
if in_game[i] and bet[i] != best_bet1:
try:
calls = players[i].call1(best_bet1)
if calls:
bet[i] = best_bet1
else:
in_game[i] = False
hand[i] = None
except IOError:
message("EXCEPTION", "players[%i].call1()" % i)
# print 'call1', in_game
round_state.extend(in_game[:])
# pierwsza runda licytacji - info o graczach w grze
for i in range(3):
try:
players[i].call1_info(in_game)
except:
message("EXCEPTION", "players[%i].call1_info()" % i)
# druga runda licytacji - obstawianie
if hand_not_finished(in_game):
for i in range(3):
if in_game[i]:
try:
bet[i] = players[i].bet2(best_bet1)
if bet[i] not in range(best_bet1, STACK + 1, 1):
bet[i] = best_bet1
in_game[i] = False
hand[i] = None
except IOError:
message("EXCEPTION", "players[%i].bet2()" % i)
best_bet2 = max(bet)
# print 'bet2', bet
round_state.extend(bet[:])
# druga runda licytacji - info o obstawianiu
for i in range(3):
try:
players[i].bet2_info(bet)
except:
message("EXCEPTION", "players[%i].bet2_info()" % i)
# druga runda licytacji - sprawdzanie
if hand_not_finished(in_game):
for i in range(3):
if in_game[i] and bet[i] != best_bet2:
try:
calls = players[i].call2(best_bet2)
if calls:
bet[i] = best_bet2
else:
in_game[i] = False
hand[i] = None
except IOError:
message("EXCEPTION", "players[%i].call2()" % i)
# print 'call2', in_game
round_state.extend(in_game[:])
# druga runda licytacji - info o o graczach w grze
for i in range(3):
try:
players[i].call2_info(in_game)
except:
message("EXCEPTION", "players[%i].call2_info()" % i)
# okazanie kart
for i in range(3):
try:
if hand_not_finished(in_game):
players[i].showdown(hand)
else:
players[i].showdown([None, None, None])
except IOError:
message("EXCEPTION", "players[%i].showdown()" % i)
# podsumowanie
for i in range(3):
try:
result = winnings(hand, bet)
players[i].result(result)
except IOError:
message("EXCEPTION", "players[%i].result()" % i)
for i in range(3):
scores[i] += result[i]
# print 'result', result
round_state.extend(result[:])
GAME_STATE.append(round_state)
return scores
##############################################
# Main
##############################################
if __name__ == "__main__":
seed()
if len(argv) < 4:
print "Invocation:"
print " game player1 player2 player3"
exit()
names = argv[1:4]
filename = 'results/{0}_{1}.csv'.format(str(time.time()), ('_'.join(names)))
with open(filename, 'w') as results_file:
results_file.write("round,\t")
results_file.write(",\t".join(names))
for i in xrange(REPEATS):
print "REPEAT no.", i
reinforce_nn = len(argv) >= 5 and argv[4].lower() == 'true'
print 'reinforce_nn =', reinforce_nn
scores = game(names, filename, reinforce_nn)
print scores