-
Notifications
You must be signed in to change notification settings - Fork 0
/
testi5.py
321 lines (257 loc) · 9.49 KB
/
testi5.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
from pyautogui import getWindowsWithTitle, screenshot, press, keyDown, keyUp
import pyscreeze
import cv2
import numpy as np
from collections import deque, namedtuple
import math
import random
import time
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from Enviroment2 import Enviroment
moves_list_p1 = [
'up',
'down',
'left',
'right',
'enter',
'shiftright'
]
moves_list_p2 = [
'w',
'x',
'a',
'd',
'tab',
'ctrlright'
]
human_readable_action_list = [
'up',
'down',
'left',
'right',
'punch',
'kick'
]
Transition = namedtuple('Transition',
('state', 'action', 'next_state', 'reward'))
class Replay_memory:
def __init__(self, mem_len):
self.mem_len = mem_len
self.memory = []
self.position = 0
def push(self, *args):
"""Saves a transition."""
if len(self.memory) < self.mem_len:
self.memory.append(None)
self.memory[self.position] = Transition(*args)
self.position = (self.position + 1) % self.mem_len
def sample(self, batch_size):
return random.sample(self.memory, batch_size)
def __len__(self):
return len(self.memory)
class Fighter(nn.Module):
def __init__(self):
super(Fighter, self).__init__()
self.steps_done = 0
hidden = 128
self.conv_layer = nn.Sequential(
nn.Conv2d(4, 16, kernel_size=7, stride=2),
nn.BatchNorm2d(16),
nn.Conv2d(16, 32, kernel_size=7, stride=2),
nn.BatchNorm2d(32),
nn.Conv2d(32, 32, kernel_size=7, stride=2),
nn.BatchNorm2d(32)
)
self.conv_layer2 = nn.Sequential(
nn.Conv2d(32, 32, kernel_size=3, stride=2),
nn.BatchNorm2d(32),
nn.Conv2d(32, 32, kernel_size=3, stride=2),
nn.BatchNorm2d(32),
nn.Conv2d(32, 32, kernel_size=3, stride=2),
nn.BatchNorm2d(32)
)
self.conv_layer3 = nn.Sequential(
nn.Conv2d(32, 32, kernel_size=2, stride=2),
nn.BatchNorm2d(16),
nn.Conv2d(16, 32, kernel_size=2, stride=2),
nn.BatchNorm2d(32),
nn.Conv2d(32, 32, kernel_size=2, stride=2),
nn.BatchNorm2d(32)
)
self.output_layer = nn.Sequential(
nn.Linear(192, hidden),
nn.Sigmoid(),
nn.Linear(hidden, hidden),
nn.Sigmoid(),
nn.Linear(hidden, 6)
)
def forward(self, x):
x = self.conv_layer(x)
x = self.conv_layer2(x)
# # x = self.conv_layer3(x)
# x = torch.flatten(x)
x = self.output_layer(x.view(x.size(0), -1))
return x
def save_model(model):
timestr = time.strftime("%Y%m%d-%H%M%S")
torch.save(model.state_dict(),
fr'G:\Kdaus\pyyttoni\Omf_projekti2\fighter_models\{timestr}Fighter.tar')
def optimize(agent, target, optim, memory):
if len(memory) < BATCH_SIZE:
return
transitions = memory.sample(BATCH_SIZE)
batch = Transition(*zip(*transitions))
non_final_mask = torch.tensor(tuple(map(lambda s: s is not None,
batch.next_state)),
device=device,
dtype=torch.uint8)
non_final_next_states = torch.cat([s for s in batch.next_state
if s is not None])
state_batch = torch.cat(batch.state).to(device)
action_batch = torch.cat(batch.action).to(device)
reward_batch = torch.cat(batch.reward).to(device)
state_action_values = agent(state_batch).gather(1, action_batch)
next_state_values = torch.zeros(BATCH_SIZE, device=device)
next_state_values[non_final_mask] = target(
non_final_next_states.to(device)).max(1)[0].detach()
expected_state_action_values = (next_state_values * GAMMA) + reward_batch
loss = F.smooth_l1_loss(state_action_values,
expected_state_action_values.unsqueeze(1))
optim.zero_grad()
loss.backward()
optim.step()
def stack_frames():
for _ in range(4):
cur_screen = capture_screen()
img_stack.append(screen_preprocess(cur_screen))
env = Enviroment()
player1_memory = Replay_memory(1_000_000)
player2_memory = Replay_memory(1_000_000)
LEARNING_RATE = 0.009
BATCH_SIZE = 128
GAMMA = 0.999
EPS_START = 0.9
EPS_END = 0.05
EPS_DECAY = 200
steps_done = 0
device = 'cuda'
player1 = Fighter().to(device)
player1.load_state_dict(torch.load(r'G:\Kdaus\pyyttoni\Omf_projekti2\fighter_models\20190930-193442Fighter.tar'))
player1_target = Fighter().to(device)
player1_target.load_state_dict(player1.state_dict())
player1_optim = optim.Adagrad(player1.parameters(), lr=LEARNING_RATE)
player2 = Fighter().to(device)
player2.load_state_dict(torch.load(r'G:\Kdaus\pyyttoni\Omf_projekti2\fighter_models\20190930-193442Fighter.tar'))
player2_target = Fighter().to(device)
player2_target.load_state_dict(player2.state_dict())
player2_optim = optim.Adagrad(player2.parameters(), lr=LEARNING_RATE)
img_stack = deque([env.screen_preprocess(env.capture_screen()) for
_ in range(4)],
maxlen=4)
last_screen = torch.tensor(img_stack).float().unsqueeze(0).to(device)
current_screen = torch.tensor(img_stack).float().unsqueeze(0).to(device)
state = current_screen - last_screen
p1_last_scores = deque([1], maxlen=100)
p2_last_scores = deque([1], maxlen=100)
def select_action(player, state):
global steps_done
sample = random.random()
eps_threshold = EPS_END + (EPS_START - EPS_END) * \
math.exp(-1. * player.steps_done / EPS_DECAY)
player.steps_done += 1
if sample > eps_threshold:
with torch.no_grad():
return player(state).max(1)[1].view(1, 1)
else:
return torch.tensor([[random.randrange(6)]], device=device, dtype=torch.long)
training = True
while training:
if env.match() == 'fight':
torch.cuda.empty_cache()
in_fight = True
gameover = False
p1_last = 196
p2_last = 196
p1_total_score = []
p2_total_score = []
player1_target.load_state_dict(player1.state_dict())
player2_target.load_state_dict(player2.state_dict())
while in_fight:
if env.match() != 'fight':
in_fight = False
break
if gameover:
print('!GAME OVER!')
p1_total_score.clear()
p2_total_score.clear()
break
try:
p1_action = select_action(player1, state)
p2_action = select_action(player2, state)
press((moves_list_p1[p1_action], moves_list_p2[p2_action]))
except:
pass
p1, p2 = env.get_fight_status()
if p1 >= 200:
p2_score = 100.
p1 = p1_last
save_model(player2)
gameover = True
else:
p2_score = float(p1_last - p1)
p1_last = p1
if p2 >= 200:
p1_score = 100.
p2 = p2_last
save_model(player1)
gameover = True
else:
p1_score = float(p2_last - p2)
p2_last = p2
p1_last_scores.append(p1_score)
p2_last_scores.append(p2_score)
if sum(p1_last_scores) == 0:
p1_score = -1.
if sum(p2_last_scores) == 0:
p2_score = -1.
print(f'P1:{p1} score:{p1_score}\
move:{human_readable_action_list[p1_action]}\
p2:{p2} score:{p2_score}\
move:{human_readable_action_list[p2_action]}')
last_screen = current_screen
img_stack.append(env.screen_preprocess(env.capture_screen()))
current_screen = torch.tensor(
img_stack).float().unsqueeze(0).to(device)
if gameover:
print('!GAME OVER!')
p1_total_score.clear()
p2_total_score.clear()
next_state = None
else:
next_state = current_screen - last_screen
try:
player1_memory.push(state.to('cpu'),
p1_action.to('cpu'),
next_state.to('cpu'),
torch.tensor([p1_score], device='cpu'))
player2_memory.push(state.to('cpu'),
p2_action.to('cpu'),
next_state.to('cpu'),
torch.tensor([p2_score], device='cpu'))
except:
pass
state = next_state
try:
optimize(player1, player1_target, player1_optim, player1_memory)
optimize(player2, player2_target, player2_optim, player2_memory)
except:
pass
if gameover:
break
p1_total_score.append(p1_score)
p2_total_score.append(p2_score)
else:
press(['enter', 'ctrlright'])