-
Notifications
You must be signed in to change notification settings - Fork 1k
/
DQN.py
175 lines (161 loc) · 7.61 KB
/
DQN.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
import random
import numpy as np
import pandas as pd
from operator import add
import collections
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import copy
DEVICE = 'cpu' # 'cuda' if torch.cuda.is_available() else 'cpu'
class DQNAgent(torch.nn.Module):
def __init__(self, params):
super().__init__()
self.reward = 0
self.gamma = 0.9
self.dataframe = pd.DataFrame()
self.short_memory = np.array([])
self.agent_target = 1
self.agent_predict = 0
self.learning_rate = params['learning_rate']
self.epsilon = 1
self.actual = []
self.first_layer = params['first_layer_size']
self.second_layer = params['second_layer_size']
self.third_layer = params['third_layer_size']
self.memory = collections.deque(maxlen=params['memory_size'])
self.weights = params['weights_path']
self.load_weights = params['load_weights']
self.optimizer = None
self.network()
def network(self):
# Layers
self.f1 = nn.Linear(11, self.first_layer)
self.f2 = nn.Linear(self.first_layer, self.second_layer)
self.f3 = nn.Linear(self.second_layer, self.third_layer)
self.f4 = nn.Linear(self.third_layer, 3)
# weights
if self.load_weights:
self.model = self.load_state_dict(torch.load(self.weights))
print("weights loaded")
def forward(self, x):
x = F.relu(self.f1(x))
x = F.relu(self.f2(x))
x = F.relu(self.f3(x))
x = F.softmax(self.f4(x), dim=-1)
return x
def get_state(self, game, player, food):
"""
Return the state.
The state is a numpy array of 11 values, representing:
- Danger 1 OR 2 steps ahead
- Danger 1 OR 2 steps on the right
- Danger 1 OR 2 steps on the left
- Snake is moving left
- Snake is moving right
- Snake is moving up
- Snake is moving down
- The food is on the left
- The food is on the right
- The food is on the upper side
- The food is on the lower side
"""
state = [
(player.x_change == 20 and player.y_change == 0 and ((list(map(add, player.position[-1], [20, 0])) in player.position) or
player.position[-1][0] + 20 >= (game.game_width - 20))) or (player.x_change == -20 and player.y_change == 0 and ((list(map(add, player.position[-1], [-20, 0])) in player.position) or
player.position[-1][0] - 20 < 20)) or (player.x_change == 0 and player.y_change == -20 and ((list(map(add, player.position[-1], [0, -20])) in player.position) or
player.position[-1][-1] - 20 < 20)) or (player.x_change == 0 and player.y_change == 20 and ((list(map(add, player.position[-1], [0, 20])) in player.position) or
player.position[-1][-1] + 20 >= (game.game_height-20))), # danger straight
(player.x_change == 0 and player.y_change == -20 and ((list(map(add,player.position[-1],[20, 0])) in player.position) or
player.position[ -1][0] + 20 > (game.game_width-20))) or (player.x_change == 0 and player.y_change == 20 and ((list(map(add,player.position[-1],
[-20,0])) in player.position) or player.position[-1][0] - 20 < 20)) or (player.x_change == -20 and player.y_change == 0 and ((list(map(
add,player.position[-1],[0,-20])) in player.position) or player.position[-1][-1] - 20 < 20)) or (player.x_change == 20 and player.y_change == 0 and (
(list(map(add,player.position[-1],[0,20])) in player.position) or player.position[-1][
-1] + 20 >= (game.game_height-20))), # danger right
(player.x_change == 0 and player.y_change == 20 and ((list(map(add,player.position[-1],[20,0])) in player.position) or
player.position[-1][0] + 20 > (game.game_width-20))) or (player.x_change == 0 and player.y_change == -20 and ((list(map(
add, player.position[-1],[-20,0])) in player.position) or player.position[-1][0] - 20 < 20)) or (player.x_change == 20 and player.y_change == 0 and (
(list(map(add,player.position[-1],[0,-20])) in player.position) or player.position[-1][-1] - 20 < 20)) or (
player.x_change == -20 and player.y_change == 0 and ((list(map(add,player.position[-1],[0,20])) in player.position) or
player.position[-1][-1] + 20 >= (game.game_height-20))), #danger left
player.x_change == -20, # move left
player.x_change == 20, # move right
player.y_change == -20, # move up
player.y_change == 20, # move down
food.x_food < player.x, # food left
food.x_food > player.x, # food right
food.y_food < player.y, # food up
food.y_food > player.y # food down
]
for i in range(len(state)):
if state[i]:
state[i]=1
else:
state[i]=0
return np.asarray(state)
def set_reward(self, player, crash):
"""
Return the reward.
The reward is:
-10 when Snake crashes.
+10 when Snake eats food
0 otherwise
"""
self.reward = 0
if crash:
self.reward = -10
return self.reward
if player.eaten:
self.reward = 10
return self.reward
def remember(self, state, action, reward, next_state, done):
"""
Store the <state, action, reward, next_state, is_done> tuple in a
memory buffer for replay memory.
"""
self.memory.append((state, action, reward, next_state, done))
def replay_new(self, memory, batch_size):
"""
Replay memory.
"""
if len(memory) > batch_size:
minibatch = random.sample(memory, batch_size)
else:
minibatch = memory
for state, action, reward, next_state, done in minibatch:
self.train()
torch.set_grad_enabled(True)
target = reward
next_state_tensor = torch.tensor(np.expand_dims(next_state, 0), dtype=torch.float32).to(DEVICE)
state_tensor = torch.tensor(np.expand_dims(state, 0), dtype=torch.float32, requires_grad=True).to(DEVICE)
if not done:
target = reward + self.gamma * torch.max(self.forward(next_state_tensor)[0])
output = self.forward(state_tensor)
target_f = output.clone()
target_f[0][np.argmax(action)] = target
target_f.detach()
self.optimizer.zero_grad()
loss = F.mse_loss(output, target_f)
loss.backward()
self.optimizer.step()
def train_short_memory(self, state, action, reward, next_state, done):
"""
Train the DQN agent on the <state, action, reward, next_state, is_done>
tuple at the current timestep.
"""
self.train()
torch.set_grad_enabled(True)
target = reward
next_state_tensor = torch.tensor(next_state.reshape((1, 11)), dtype=torch.float32).to(DEVICE)
state_tensor = torch.tensor(state.reshape((1, 11)), dtype=torch.float32, requires_grad=True).to(DEVICE)
if not done:
target = reward + self.gamma * torch.max(self.forward(next_state_tensor[0]))
output = self.forward(state_tensor)
target_f = output.clone()
target_f[0][np.argmax(action)] = target
target_f.detach()
self.optimizer.zero_grad()
loss = F.mse_loss(output, target_f)
loss.backward()
self.optimizer.step()