forked from lossv/yueyue_2048_DQN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.py
172 lines (139 loc) · 5.29 KB
/
agent.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
import torch
import sys
import numpy as np
import torch.optim as optim
import torch.autograd as autograd
import torch.nn.functional as F
from model import DQN_MLP, DDQN_MLP
from memory import Memory
class Agent:
def __init__(
self,
size_board,
hidden_dim,
output_dim,
decay_rate,
capacity,
batch_size,
gamma,
learning_rate,
dueling,
):
# Use cuda
self.__use_cuda = torch.cuda.is_available()
# Models
if dueling:
# Usage of dueling models
self.__policy_net = DDQN_MLP(
size_board * size_board, hidden_dim, output_dim
)
self.__policy_net.train()
self.__target_net = DDQN_MLP(
size_board * size_board, hidden_dim, output_dim
)
print("Creating a dueling DQN...")
else:
self.__policy_net = DQN_MLP(size_board * size_board, hidden_dim, output_dim)
self.__policy_net.train()
self.__target_net = DQN_MLP(size_board * size_board, hidden_dim, output_dim)
print("Creating a no-dueling DQN")
# Target model is the model that will calculate the q_next_state_values
self.update_target_net()
for param in self.__target_net.parameters():
param.requires_grad = False
# Epsilon and decay rate
self.__epsilon_start = 1.
self.__epsilon_stop = 0.01
self.__decay_rate = decay_rate
self.__decay_step = 0
self.__epsilon_threshold = 0
# threshold 阀值
# Memory
self.__memory = Memory(capacity, size_board)
# capacity 容量
# Batch size
self.__batch_size = batch_size
# Gamma
self.__gamma = gamma
# Learning rate
self.__learning_rate = learning_rate
# Optimizer
self.__optimizer = optim.Adam(self.__policy_net.parameters(), lr=learning_rate)
if self.__use_cuda:
self.__policy_net = self.__policy_net.cuda()
self.__target_net = self.__target_net.cuda()
self.__variable = (
lambda *args, **kwargs: autograd.Variable(*args, **kwargs).cuda()
if self.__use_cuda
else autograd.Variable(*args, **kwargs)
)
# Copy parameters of policy net to target net
def update_target_net(self):
self.__target_net.load_state_dict(self.__policy_net.state_dict())
# Update threshold that divide the agent to choose a random action or a guided action
def __update_epsilon(self):
self.__epsilon_threshold = self.__epsilon_stop + (
self.__epsilon_start - self.__epsilon_stop
) * np.exp(-self.__decay_rate * self.__decay_step)
def __sample_batch(self):
state, next_state, action, reward, done = self.__memory.sample(
self.__batch_size
)
if self.__use_cuda:
to_float_tensor, to_long_tensor = (
torch.cuda.FloatTensor,
torch.cuda.LongTensor,
)
else:
to_float_tensor, to_long_tensor = torch.FloatTensor, torch.LongTensor
return (
to_float_tensor(state),
to_float_tensor(next_state),
to_long_tensor(action),
to_float_tensor(reward),
to_float_tensor(done),
)
def store_memory(self, state, next_state, action, reward, done):
self.__memory.store(state, next_state, action, reward, done)
def selection_action(self, valid_movements, state):
sample = np.random.rand(1)
# Increase decay step to decrease epsilon threshold exponentially
self.__decay_step += 1
# Update epsilon threshold
self.__update_epsilon()
if sample > self.__epsilon_threshold:
with torch.no_grad():
output = self.__policy_net(
self.__variable(torch.from_numpy(state).float())
)
output_np = output.cpu().detach().numpy()
ordered = np.flip(
np.argsort(np.expand_dims(output_np, axis=0), axis=1)
)[0]
for action in ordered:
if valid_movements[action] != 0:
return action
else:
return np.random.choice(np.nonzero(valid_movements)[0])
def train_model(self):
if len(self.__memory) < self.__batch_size:
return -1
state, next_state, action, reward, done = self.__sample_batch()
action = action.unsqueeze(0)
action = action.view(self.__batch_size, 1)
# print(action)
q_values = self.__policy_net(state).gather(1, action)
with torch.no_grad():
next_q_values = self.__target_net(next_state).max(1)[0]
target_q_values = reward + self.__gamma * (1 - done) * next_q_values
# Loss
# loss = (q_values - target_q_values).pow(2).mean()
loss = F.smooth_l1_loss(q_values, target_q_values.view(self.__batch_size, 1))
self.__optimizer.zero_grad()
loss.backward()
self.__optimizer.step()
return loss.item()
def get_threshold(self):
return self.__epsilon_threshold
def save_model(self, path):
torch.save(self.__policy_net.state_dict(), path)