-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDQN_BoardVec.py
177 lines (143 loc) · 5.37 KB
/
DQN_BoardVec.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
"""
Deep Q Network
modified from Q-table where the table is replaced by a deep NN.
Using:
PyTorch: 1.9.1+cpu
gym: 0.8.0
"""
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.autograd import Variable
from torch.distributions import Categorical
from torch.distributions import Normal
import random
import numpy as np
np.random.seed(7)
torch.manual_seed(7)
device = torch.device("cpu")
class ReplayBuffer:
def __init__(self, capacity):
self.capacity = capacity
self.buffer = []
self.position = 0
def push(self, state, action, reward, next_state, done):
if len(self.buffer) < self.capacity:
self.buffer.append(None)
self.buffer[self.position] = (state, action, reward, next_state, done)
self.position = (self.position + 1) % self.capacity
def last_reward(self):
return self.buffer[self.position-1][2]
def sample(self, batch_size):
batch = random.sample(self.buffer, batch_size)
state, action, reward, next_state, done = \
map(np.stack, zip(*batch)) # stack for each element
'''
the * serves as unpack: sum(a,b) <=> batch=(a,b), sum(*batch) ;
zip: a=[1,2], b=[2,3], zip(a,b) => [(1, 2), (2, 3)] ;
the map serves as mapping the function on each list element: map(square, [2,3]) => [4,9] ;
np.stack((1,2)) => array([1, 2])
'''
# print("sampled state=", state)
# print("sampled action=", action)
return state, action, reward, next_state, done
def __len__(self):
return len(self.buffer)
class QNetwork(nn.Module):
def __init__(self, input_dim, action_dim, hidden_size, activation=F.relu, init_w=3e-3):
super(QNetwork, self).__init__()
self.linear1 = nn.Linear(input_dim, hidden_size)
self.linear2 = nn.Linear(hidden_size, hidden_size)
self.linear3 = nn.Linear(hidden_size, hidden_size)
self.linear4 = nn.Linear(hidden_size, hidden_size)
self.logits_linear = nn.Linear(hidden_size, action_dim)
self.logits_linear.weight.data.uniform_(-init_w, init_w)
self.logits_linear.bias.data.uniform_(-init_w, init_w)
self.activation = F.relu
def forward(self, state):
x = self.activation(self.linear1(state))
x = self.activation(self.linear2(x))
x = self.activation(self.linear3(x))
x = self.activation(self.linear4(x))
logits = self.logits_linear(x)
# logits = F.leaky_relu(self.logits_linear(x))
return logits
class DQN():
def __init__(
self,
action_dim,
state_dim,
learning_rate = 3e-4,
gamma = 0.9 ):
super(DQN, self).__init__()
self.action_dim = action_dim
self.state_dim = state_dim
self.lr = learning_rate
self.gamma = gamma
self.replay_buffer = ReplayBuffer(int(1e6))
hidden_dim = 32
self.q_net = QNetwork(state_dim, action_dim, hidden_dim, activation=F.relu).to(device)
self.q_criterion = nn.MSELoss()
self.q_optimizer = optim.Adam(self.q_net.parameters(), lr=self.lr)
def choose_action(self, state, deterministic=True):
state = torch.FloatTensor(state).unsqueeze(0).to(device)
logits = self.q_net(state)
probs = torch.softmax(logits, dim=1)
dist = Categorical(probs)
action = dist.sample().numpy()[0]
# print("chosen action=", action)
return action
def update(self, batch_size, reward_scale, gamma=0.99):
alpha = 1.0 # trade-off between exploration (max entropy) and exploitation (max Q)
state, action, reward, next_state, done = self.replay_buffer.sample(batch_size)
# print('sample (state, action, reward, next state, done):', state, action, reward, next_state, done)
state = torch.FloatTensor(state).to(device)
next_state = torch.FloatTensor(next_state).to(device)
action = torch.LongTensor(action).to(device)
reward = torch.FloatTensor(reward).to(device) # .to(device) # reward is single value, unsqueeze() to add one dim to be [reward] at the sample dim;
done = torch.BoolTensor(done).to(device)
logits = self.q_net(state)
next_logits = self.q_net(next_state)
# **** Train deep Q function, this is just Bellman equation:
# DQN(st,at) += η [ R + γ max_a DQN(s_t+1,a) - DQN(st,at) ]
# DQN[s, action] += self.lr *( reward + self.gamma * np.max(DQN[next_state, :]) - DQN[s, action] )
# max 是做不到的,但似乎也可以做到。 DQN 输出的是 probs.
# probs 和 Q 有什么关系? Q 的 Boltzmann 是 probs (SAC 的做法).
# This implies that Q = logits.
# logits[at] += self.lr *( reward + self.gamma * np.max(logits[next_state, next_a]) - logits[at] )
q = logits[range(logits.shape[0]), action]
m = torch.max(next_logits, 1, keepdim=False).values
# print("m:", m.shape)
# q = q + self.lr *( reward + self.gamma * m - q )
target_q = torch.where(done, reward, reward + self.gamma * m)
# print("q, target_q:", q.shape, target_q.shape)
q_loss = self.q_criterion(q, target_q.detach())
self.q_optimizer.zero_grad()
q_loss.backward()
self.q_optimizer.step()
return
def net_info(self):
config = "(9)-32-32-32-32-(9)"
neurons = config.split('-')
last_n = 9
total = 0
for n in neurons[1:-1]:
n = int(n)
total += last_n * n
last_n = n
total += last_n * 9
return (config, total)
def play_random(self, state, action_space):
# Select an action (0-9) randomly
# NOTE: random player never chooses occupied squares
while True:
action = action_space.sample()
occupied = state[action]
if occupied > -0.1 and occupied < 0.1:
break
return action
def save_net(self, fname):
print("Model not saved.")
def load_net(self, fname):
print("Model not loaded.")