Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
71 lines (54 loc) · 1.62 KB
/
Copy pathmain.py
File metadata and controls
71 lines (54 loc) · 1.62 KB
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
import subprocess
import torch
from game.snake_game import SnakeGame
from agent.agent import Agent
import matplotlib.pyplot as plt
import pandas as pd
import os
def train_model():
scores = []
game = SnakeGame()
agent = Agent()
n_games = 0
record = 0
while True:
old_state = game.get_state()
action = agent.get_action(old_state)
reward, done, score = game.play_action(action)
new_state = game.get_state()
agent.train_short_memory(old_state, action, reward, new_state, done)
if done:
if score > record:
print("New score:", score)
record = score
scores.append(score)
n_games += 1
game.reset_game()
if n_games % 100 == 0:
agent.epsilon /= 2
df = pd.Series(scores)
smoothed = df.rolling(window=20).mean()
plt.plot(smoothed)
plt.show()
if n_games == 550:
agent.model.save()
break
def play():
game = SnakeGame()
game.speed = 20
agent = Agent()
agent.epsilon = 0
agent.model.load_state_dict(torch.load('./model/model.pth', weights_only=True))
while True:
old_state = game.get_state()
action = agent.get_action(old_state)
reward, done, score = game.play_action(action)
if done:
game.reset_game()
def start_server():
subprocess.run(["python", "server/server.py"])
if __name__ == "__main__":
if not os.path.exists('model/model.pth'):
train_model()
# play()
start_server()