forked from claudia-maria-dudau/RL-Poker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
371 lines (268 loc) · 13.3 KB
/
main.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
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
import sys
import pandas as pd
import numpy as np
from gym_env.env import HoldemTable
from gym_env.env import PlayerShell
from tools.helper import get_config
from agents.agent_random import Player as RandomPlayer
from poker_agent_dqn import DQN_PokerAgent, MethodToUse
from poker_agent_expected_sarsa import ExpectedSarsa_PokerAgent
from poker_agent_q_learning import QLearning_PokerAgent
from poker_agent_sarsa import Sarsa_PokerAgent
"""
Usage:
main.py [option]
option:
sarsa -- 5 random players + 1 SARSA player
expected_sarsa -- 5 random players + 1 Expected SARSA player
q_learning -- 5 random players + 1 Q Learning player
dqn -- 5 random players + 1 DQN_BASE player
dqn_target -- 5 random players + 1 DQN_TARGET_NETWORK player
dqn_experience_replay -- 5 random players + 1 DQN_TARGET_NETWORK_AND_EXPERIENCE_REPLAY player
dqn_all -- 5 random players + 1 DQN_AND_ALL player
all_basic -- 1 SARSA player + 1 Expected SARSA player + 1 Q Learning player
all_dqn -- 2 random players + all 4 dqn players
all -- 1 Expected SARSA player + 1 Q Learning player + all 4 dqn players
"""
class SelfPlay:
"""Orchestration of playing against itself"""
def __init__(self, render, num_episodes, use_cpp_montecarlo, funds_plot, stack=500):
self.winner_in_episodes = []
self.use_cpp_montecarlo = use_cpp_montecarlo
self.funds_plot = funds_plot
self.render = render
self.num_episodes = num_episodes
self.stack = stack
self.env = HoldemTable(initial_stacks=self.stack, render=self.render)
def sarsa_agent(self):
"""Create an environment with 5 random players and a sarsa player"""
self.env = HoldemTable(initial_stacks=self.stack, render=self.render)
for _ in range(5):
player = RandomPlayer()
self.env.add_player(player)
self.env.add_player(PlayerShell(name='SARSA', stack_size=self.stack))
self.env.reset()
sarsaAgent = Sarsa_PokerAgent(self.env, gamma=0.8, alpha=1e-1,
start_epsilon=1, end_epsilon=1e-2, epsilon_decay=0.999)
scores = []
actions_per_ep = []
for _ in range(self.num_episodes):
self.env.reset()
score, actions = sarsaAgent.play(no_episodes=1)
scores.append(score)
actions_per_ep.append(actions)
self.winner_in_episodes.append(self.env.winner_ix)
average_score = np.mean(scores)
average_actions = np.mean(actions_per_ep)
league_table = pd.Series(self.winner_in_episodes).value_counts()
best_player = league_table.index[0]
print("League Table")
print("============")
print("Player - No episodes won")
print(league_table)
print(f"Best Player: {best_player}")
print('------------')
print(f'Average score: {average_score}, Average actions per ep: {average_actions}')
def expected_sarsa_agent(self):
"""Create an environment with 5 random players and a expected sarsa player"""
self.env = HoldemTable(initial_stacks=self.stack, render=self.render)
for _ in range(5):
player = RandomPlayer()
self.env.add_player(player)
self.env.add_player(PlayerShell(name='Expected_SARSA', stack_size=self.stack))
self.env.reset()
expectedSarsaAgent = ExpectedSarsa_PokerAgent(self.env, gamma=0.8, alpha=1e-1,
start_epsilon=1, end_epsilon=1e-2, epsilon_decay=0.999)
scores = []
actions_per_ep = []
for _ in range(self.num_episodes):
self.env.reset()
score, actions = expectedSarsaAgent.play(no_episodes=1)
scores.append(score)
actions_per_ep.append(actions)
self.winner_in_episodes.append(self.env.winner_ix)
average_score = np.mean(scores)
average_actions = np.mean(actions_per_ep)
league_table = pd.Series(self.winner_in_episodes).value_counts()
best_player = league_table.index[0]
print("League Table")
print("============")
print("Player - No episodes won")
print(league_table)
print(f"Best Player: {best_player}")
print('------------')
print(f'Average score: {average_score}, Average actions per ep: {average_actions}')
def q_learning_agent(self):
"""Create an environment with 5 random players and a q learning player"""
self.env = HoldemTable(initial_stacks=self.stack, render=self.render)
for _ in range(5):
player = RandomPlayer()
self.env.add_player(player)
self.env.add_player(PlayerShell(name='Q_Learning', stack_size=self.stack))
self.env.reset()
QLearningAgent = QLearning_PokerAgent(self.env, gamma=0.8, alpha=1e-1,
start_epsilon=1, end_epsilon=1e-2, epsilon_decay=0.999)
scores = []
actions_per_ep = []
for _ in range(self.num_episodes):
self.env.reset()
score, actions = QLearningAgent.play(no_episodes=1)
scores.append(score)
actions_per_ep.append(actions)
self.winner_in_episodes.append(self.env.winner_ix)
average_score = np.mean(scores)
average_actions = np.mean(actions_per_ep)
league_table = pd.Series(self.winner_in_episodes).value_counts()
best_player = league_table.index[0]
print("League Table")
print("============")
print("Player - No episodes won")
print(league_table)
print(f"Best Player: {best_player}")
print('------------')
print(f'Average score: {average_score}, Average actions per ep: {average_actions}')
def get_dqn_agent(self, method):
return DQN_PokerAgent(self.env, seed=42, gamma=0.99, batch_size=64, lr=0.0007,
steps_until_sync=200, replay_buffer_size=32000, pre_train_steps=0,
start_epsilon = 1, end_epsilon = 0.1, final_epsilon_step = 10000,
method=method, load_prev=True)
def dqn_agent(self, method):
"""Create an environment with 5 random players and a dqn player"""
self.env = HoldemTable(initial_stacks=self.stack, render=self.render)
for _ in range(5):
self.env.add_player(RandomPlayer())
self.env.add_player(PlayerShell(name=method.name, stack_size=self.stack))
self.env.reset()
DQNAgent = self.get_dqn_agent(method=method)
scores = []
actions_per_ep = []
for _ in range(self.num_episodes):
self.env.reset()
score, actions = DQNAgent.play(no_episodes=1)
scores.append(score)
actions_per_ep.append(actions)
self.winner_in_episodes.append(self.env.winner_ix)
average_score = np.mean(scores)
average_actions = np.mean(actions_per_ep)
league_table = pd.Series(self.winner_in_episodes).value_counts()
best_player = league_table.index[0]
print("League Table")
print("============")
print("Player - No episodes won")
print(league_table)
print(f"Best Player: {best_player}")
print('------------')
print(f'Average score: {average_score}, Average actions per ep: {average_actions}')
def all_basic_agents(self):
"""Create an environment with all 3 basic players"""
self.env = HoldemTable(initial_stacks=self.stack, render=self.render)
self.env.add_player(PlayerShell(name='SARSA', stack_size=self.stack))
self.env.add_player(PlayerShell(name='Expected_SARSA', stack_size=self.stack))
self.env.add_player(PlayerShell(name='Q_Learning', stack_size=self.stack))
self.env.reset()
sarsaAgent = Sarsa_PokerAgent(self.env, gamma=0.8, alpha=1e-1,
start_epsilon=1, end_epsilon=1e-2, epsilon_decay=0.999)
sarsaAgent.load_q_table()
expectedSarsaAgent = ExpectedSarsa_PokerAgent(self.env, gamma=0.8, alpha=1e-1,
start_epsilon=1, end_epsilon=1e-2, epsilon_decay=0.999)
expectedSarsaAgent.load_q_table()
QLearningAgent = QLearning_PokerAgent(self.env, gamma=0.8, alpha=1e-1,
start_epsilon=1, end_epsilon=1e-2, epsilon_decay=0.999)
QLearningAgent.load_q_table()
for _ in range(self.num_episodes):
self.env.reset()
sarsaAgent.play(no_episodes=1)
self.winner_in_episodes.append(self.env.winner_ix)
league_table = pd.Series(self.winner_in_episodes).value_counts()
best_player = league_table.index[0]
print("League Table")
print("============")
print("Player - No episodes won")
print(league_table)
print(f"Best Player: {best_player}")
def all_dqn_agents(self):
"""Create an environment with 2 random players and all 4 dqn players"""
self.env = HoldemTable(initial_stacks=self.stack, render=self.render)
self.env.add_player(RandomPlayer())
self.env.add_player(RandomPlayer())
self.env.add_player(PlayerShell(name='DQN_BASE', stack_size=self.stack))
self.env.add_player(PlayerShell(name='DQN_TARGET_NETWORK', stack_size=self.stack))
self.env.add_player(PlayerShell(name='DDQN_AND_ALL', stack_size=self.stack))
self.env.add_player(PlayerShell(name='DQN_TARGET_NETWORK_AND_EXPERIENCE_REPLAY', stack_size=self.stack))
self.env.reset()
DQNAgent1 = self.get_dqn_agent(method=MethodToUse.DQN_BASE)
DQNAgent2 = self.get_dqn_agent(method=MethodToUse.DQN_TARGET_NETWORK)
DQNAgent3 = self.get_dqn_agent(method=MethodToUse.DQN_TARGET_NETWORK_AND_EXPERIENCE_REPLAY)
DQNAgent4 = self.get_dqn_agent(method=MethodToUse.DDQN_AND_ALL)
for _ in range(self.num_episodes):
self.env.reset()
DQNAgent1.play(no_episodes=1)
self.winner_in_episodes.append(self.env.winner_ix)
league_table = pd.Series(self.winner_in_episodes).value_counts()
best_player = league_table.index[0]
print("League Table")
print("============")
print("Player - No episodes won")
print(league_table)
print(f"Best Player: {best_player}")
def all_agents(self):
"""Create an environment with all 6 players"""
self.env = HoldemTable(initial_stacks=self.stack, render=self.render)
self.env.add_player(PlayerShell(name='Expected_SARSA', stack_size=self.stack))
self.env.add_player(PlayerShell(name='Q_Learning', stack_size=self.stack))
self.env.add_player(PlayerShell(name='DQN_BASE', stack_size=self.stack))
self.env.add_player(PlayerShell(name='DQN_TARGET_NETWORK', stack_size=self.stack))
self.env.add_player(PlayerShell(name='DDQN_AND_ALL', stack_size=self.stack))
self.env.add_player(PlayerShell(name='DQN_TARGET_NETWORK_AND_EXPERIENCE_REPLAY', stack_size=self.stack))
self.env.reset()
expectedSarsaAgent = ExpectedSarsa_PokerAgent(self.env, gamma=0.8, alpha=1e-1,
start_epsilon=1, end_epsilon=1e-2, epsilon_decay=0.999)
expectedSarsaAgent.load_q_table()
QLearningAgent = QLearning_PokerAgent(self.env, gamma=0.8, alpha=1e-1,
start_epsilon=1, end_epsilon=1e-2, epsilon_decay=0.999)
QLearningAgent.load_q_table()
DQNAgent1 = self.get_dqn_agent(method=MethodToUse.DQN_BASE)
DQNAgent2 = self.get_dqn_agent(method=MethodToUse.DQN_TARGET_NETWORK)
DQNAgent3 = self.get_dqn_agent(method=MethodToUse.DQN_TARGET_NETWORK_AND_EXPERIENCE_REPLAY)
DQNAgent4 = self.get_dqn_agent(method=MethodToUse.DDQN_AND_ALL)
for _ in range(self.num_episodes):
self.env.reset()
expectedSarsaAgent.play(no_episodes=1)
self.winner_in_episodes.append(self.env.winner_ix)
league_table = pd.Series(self.winner_in_episodes).value_counts()
best_player = league_table.index[0]
print("League Table")
print("============")
print("Player - No episodes won")
print(league_table)
print(f"Best Player: {best_player}")
def command_line_parser():
args = sys.argv[1]
_ = get_config()
num_episodes = 3
runner = SelfPlay(render=True, num_episodes=num_episodes, use_cpp_montecarlo=False,
funds_plot=True, stack=20)
if args == 'sarsa':
runner.sarsa_agent()
elif args == 'expected_sarsa':
runner.expected_sarsa_agent()
elif args == 'q_learning':
runner.q_learning_agent()
elif args == 'dqn':
runner.dqn_agent(method=MethodToUse.DQN_BASE)
elif args == 'dqn_target':
runner.dqn_agent(method=MethodToUse.DQN_TARGET_NETWORK)
elif args == 'dqn_experience_replay':
runner.dqn_agent(method=MethodToUse.DQN_TARGET_NETWORK_AND_EXPERIENCE_REPLAY)
elif args == 'dqn_all':
runner.dqn_agent(method=MethodToUse.DDQN_AND_ALL)
elif args == 'all_basic':
runner.all_basic_agents()
elif args == 'all_dqn':
runner.all_dqn_agents()
elif args == 'all':
runner.all_agents()
else:
raise RuntimeError("Argument not yet implemented")
if __name__ == '__main__':
command_line_parser()