-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
221 lines (155 loc) · 6.91 KB
/
train.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
from agent import Agent
from rules import Board
from time import sleep
from datetime import datetime
import os
class Simulation:
'''
Class constructor - returns empty object (None)
if wrong agent strategies parameters have been
passed.
Else objects is correctly instantiated.
'''
def __new__(cls, agent_strategies):
if agent_strategies not in ["RQ", "QQ"]:
print("Wrong agent behaviour passed, must be: 'RQ' or 'QQ'.")
return None
else:
return object.__new__(cls)
def __init__(self, agent_strategies):
self.board = Board()
self.agent_strategies = agent_strategies
self.agentX = Agent(self.board.setting, "X")
self.agentO = Agent(self.board.setting, "O")
'''
Implementation od simulateGame method is
dynamically set based on passed agent_strategies
parameter.
This way we can always call the same 'simulateGame'
method in other functions without needing to worry
about what game strategy agents use.
'''
if agent_strategies == "QQ":
self.agentX.setStrategy("q")
self.agentO.setStrategy("q")
self.simulateGame = self.__simulateQQGame
elif agent_strategies == "RQ":
self.simulateGame = self.__simulateRQGame
# Agents play agains the agent with same strategy.
def __simulateQQGame(self):
self.board.resetBoard()
while self.board.isGameOver() is False:
self.agentX.updateStates(self.board.setting)
self.agentX.updateAvailablePos()
posX = self.agentX.makeMove()
self.board.setSignToPos(posX, self.agentX.sign)
if self.board.checkWinner() == self.agentX.sign:
self.agentX.beRewarded(1)
self.agentO.beRewarded(-1)
elif self.board.checkWinner() == self.agentO.sign:
self.agentX.beRewarded(-1)
self.agentO.beRewarded(1)
else:
self.agentX.beRewarded(0.1)
self.agentO.beRewarded(0.1)
if self.board.isGameOver() is True:
break
self.agentO.updateStates(self.board.setting)
self.agentO.updateAvailablePos()
posO = self.agentO.makeMove()
self.board.setSignToPos(posO, self.agentO.sign)
if self.board.checkWinner() == self.agentO.sign:
self.agentO.beRewarded(1)
self.agentX.beRewarded(-1)
elif self.board.checkWinner() == self.agentX.sign:
self.agentO.beRewarded(-1)
self.agentX.beRewarded(1)
else:
self.agentO.beRewarded(0.1)
self.agentX.beRewarded(0.1)
self.agentO.strategy.resetHistoricStates()
self.agentX.strategy.resetHistoricStates()
# Agents play againt agent with different strategy.
def __simulateRQGame(self):
self.board.resetBoard()
self.agentX.setStrategy("q")
self.agentO.setStrategy("q")
# In this game agent takes turn playing against the random agent.
agent_rand = Agent(self.board.setting, "O", "random")
while self.board.isGameOver() is False:
self.agentX.updateStates(self.board.setting)
self.agentX.updateAvailablePos()
posX = self.agentX.makeMove()
self.board.setSignToPos(posX, self.agentX.sign)
if self.board.checkWinner() == self.agentX.sign:
self.agentX.beRewarded(1)
elif self.board.checkWinner() == agent_rand.sign:
self.agentX.beRewarded(-1)
else:
self.agentX.beRewarded(0.1)
if self.board.isGameOver() is True:
break
agent_rand.updateStates(self.board.setting)
agent_rand.updateAvailablePos()
posRand = agent_rand.makeMove()
self.board.setSignToPos(posRand, agent_rand.sign)
if self.board.checkWinner() == agent_rand.sign:
self.agentX.beRewarded(-1)
elif self.board.checkWinner() == self.agentX.sign:
self.agentX.beRewarded(1)
else:
self.agentX.beRewarded(0.1)
agent_rand.strategy.resetHistoricStates()
self.agentX.strategy.resetHistoricStates()
self.board.resetBoard()
agent_rand.switchSign()
while self.board.isGameOver() is False:
agent_rand.updateStates(self.board.setting)
agent_rand.updateAvailablePos()
posRand = agent_rand.makeMove()
self.board.setSignToPos(posRand, agent_rand.sign)
if self.board.checkWinner() == agent_rand.sign:
self.agentO.beRewarded(-1)
elif self.board.checkWinner() == self.agentO.sign:
self.agentO.beRewarded(1)
else:
self.agentO.beRewarded(0.1)
if self.board.isGameOver() is True:
break
self.agentO.updateStates(self.board.setting)
self.agentO.updateAvailablePos()
posO = self.agentO.makeMove()
self.board.setSignToPos(posO, self.agentO.sign)
if self.board.checkWinner() == self.agentO.sign:
self.agentO.beRewarded(1)
elif self.board.checkWinner() == agent_rand.sign:
self.agentO.beRewarded(-1)
else:
self.agentO.beRewarded(0.1)
self.agentO.strategy.resetHistoricStates()
agent_rand.strategy.resetHistoricStates()
def saveAgents(self, filepath: str):
filepath = filepath.replace("/", "\\")
# Give file default name if user passes empty filename.
if filepath == "":
now = datetime.now()
datetime_str = now.strftime("%m_%d_%Y_%H_%M_%S")
O_filename = f"trained_O_{str(self.agent_strategies)}_{datetime_str}.pkl"
X_filename = f"trained_X_{str(self.agent_strategies)}_{datetime_str}.pkl"
dirname = f"save_{str(self.agent_strategies)}_{datetime_str}"
O_full_path = f"saves\\{dirname}\\{O_filename}"
X_full_path = f"saves\\{dirname}\\{X_filename}"
os.makedirs(f"saves\\{dirname}")
# If filepath is provided, save there.
else:
full_filename = os.path.basename(filepath)
filename = os.path.splitext(full_filename)
dirname = filename[0]
root_dir = filepath.replace(full_filename, '')[:-1]
O_filename = f"trained_O_{''.join(filename)}"
X_filename = f"trained_X_{''.join(filename)}"
O_full_path = f"{root_dir}\\{dirname}\\{O_filename}"
X_full_path = f"{root_dir}\\{dirname}\\{X_filename}"
os.makedirs(f"{root_dir}\\{dirname}")
self.agentO.saveQStates(O_full_path)
self.agentX.saveQStates(X_full_path)