-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
147 lines (121 loc) · 4.54 KB
/
agent.py
File metadata and controls
147 lines (121 loc) · 4.54 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
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
import torch
import random
import numpy as np
from collections import deque
from game import Game, Point
from Q_network import QNetwork, QTrainer
import matplotlib.pyplot as plt
from IPython import display
MAX_MEMORY = 100_000
BATCH_SIZE = 1000
LEARNING_RATE = 0.005
class SnakeAgent:
def __init__(self):
self.num_games = 0
self.epsilon = 0
self.discount_rate = 0.9
self.memory = deque(maxlen=MAX_MEMORY)
self.q_network = QNetwork(11, 256, 3)
self.trainer = QTrainer(self.q_network, learning_rate=LEARNING_RATE, gamma=self.discount_rate)
def get_state(self, game):
head = game.snake[0]
point_l = Point(head.x - 20, head.y)
point_r = Point(head.x + 20, head.y)
point_u = Point(head.x, head.y - 20)
point_d = Point(head.x, head.y + 20)
dir_l = game.direction == "LEFT"
dir_r = game.direction == "RIGHT"
dir_u = game.direction == "UP"
dir_d = game.direction == "DOWN"
state = [
(dir_r and game.is_collision(point_r)) or
(dir_l and game.is_collision(point_l)) or
(dir_u and game.is_collision(point_u)) or
(dir_d and game.is_collision(point_d)),
(dir_u and game.is_collision(point_r)) or
(dir_d and game.is_collision(point_l)) or
(dir_l and game.is_collision(point_u)) or
(dir_r and game.is_collision(point_d)),
(dir_d and game.is_collision(point_r)) or
(dir_u and game.is_collision(point_l)) or
(dir_r and game.is_collision(point_u)) or
(dir_l and game.is_collision(point_d)),
dir_l,
dir_r,
dir_u,
dir_d,
game.food.x < game.head.x,
game.food.x > game.head.x,
game.food.y < game.head.y,
game.food.y > game.head.y
]
return np.array(state, dtype=int)
def remember(self, state, action, reward, next_state, done):
self.memory.append((state, action, reward, next_state, done))
def train_long_memory(self):
if len(self.memory) > BATCH_SIZE:
mini_sample = random.sample(self.memory, BATCH_SIZE)
else:
mini_sample = self.memory
states, actions, rewards, next_states, dones = zip(*mini_sample)
self.trainer.train_step(states, actions, rewards, next_states, dones)
def train_short_memory(self, state, action, reward, next_state, done):
self.trainer.train_step(state, action, reward, next_state, done)
def get_action(self, state):
# exploitation vs random
#random move
self.epsilon = 80 - self.num_games
final_move = [0, 0, 0]
if random.randint(0, 200) < self.epsilon:
move = random.randint(0, 2)
final_move[move] = 1
else:
#exploitation
state0 = torch.tensor(state, dtype=torch.float)
prediction = self.q_network(state0)
move = torch.argmax(prediction).item()
final_move[move] = 1
return final_move
def train():
record = 0
agent = SnakeAgent()
game = Game()
plot_arr = []
plot_mean_arr = []
total_score = 0
while True:
state_old = agent.get_state(game)
final_move = agent.get_action(state_old)
reward, done, score = game.play_step(final_move)
state_new = agent.get_state(game)
agent.train_short_memory(state_old, final_move, reward, state_new, done)
agent.remember(state_old, final_move, reward, state_new, done)
if done:
game.reset()
agent.num_games += 1
agent.train_long_memory()
if score > record:
record = score
agent.q_network.save()
print('Game', agent.num_games, 'Score', score, 'Record:', record)
plot_arr.append(score)
total_score += score
mean_score = total_score / agent.num_games
plot_mean_arr.append(mean_score)
plot(plot_arr, plot_mean_arr)
def plot(scores, mean_scores):
display.clear_output(wait=True)
display.display(plt.gcf())
plt.clf()
plt.title('Training...')
plt.xlabel('Number of Games')
plt.ylabel('Score')
plt.plot(scores)
plt.plot(mean_scores)
plt.ylim(ymin=0)
plt.text(len(scores)-1, scores[-1], str(scores[-1]))
plt.text(len(mean_scores)-1, mean_scores[-1], str(mean_scores[-1]))
plt.show(block=False)
plt.pause(.1)
if __name__ == '__main__':
train()