-
Notifications
You must be signed in to change notification settings - Fork 4
/
qmix.py
278 lines (230 loc) · 10.2 KB
/
qmix.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import tensorflow as tf
import numpy as np
import copy
class MixingNet(tf.keras.Model):
def __init__(self, agent_nets, embed_shape):
super(MixingNet, self).__init__()
self.agent_nets = agent_nets
self.agent_num = len(agent_nets)
self.embed_shape = embed_shape
self.timesteps = agent_nets[0].input_shape[1]
self.agent_output_dim = agent_nets[0].output_shape[-1]
# self.hyper_w1 = tf.keras.layers.Dense(embed_shape*self.agent_num*self.agent_output_dim, activation='linear',use_bias=True)
self.hyper_w1_1 = tf.keras.layers.Dense(
embed_shape, activation='relu', use_bias=True)
self.hyper_w1_2 = tf.keras.layers.Dense(
embed_shape *
self.agent_num *
self.agent_output_dim,
activation='linear',
use_bias=True)
self.hyper_b1 = tf.keras.layers.Dense(self.embed_shape)
# self.hyper_w2 = tf.keras.layers.Dense(self.embed_shape, activation='linear', use_bias=True)
self.hyper_w2_1 = tf.keras.layers.Dense(
self.embed_shape, activation='relu', use_bias=True)
self.hyper_w2_2 = tf.keras.layers.Dense(
self.embed_shape, activation='linear', use_bias=True)
self.hyper_b2 = tf.keras.layers.Dense(1, activation="relu")
def call(self, inputs):
agents_inputs = inputs[0]
states = inputs[1]
masks = inputs[2]
batch_size = states.shape[0]
agents_outputs = []
for agent_net, agent_input, mask in zip(
self.agent_nets, agents_inputs, masks):
agent_out = agent_net(agent_input)
agent_out = tf.multiply(agent_out, mask)
agents_outputs.append(agent_out)
# w1 = tf.abs(self.hyper_w1(states))
w1 = tf.abs(self.hyper_w1_2(self.hyper_w1_1(states)))
agents_outputs = tf.concat(agents_outputs, 1)
agents_outputs = tf.expand_dims(agents_outputs, 1)
w1 = tf.reshape(w1, [
batch_size, self.agent_output_dim * self.agent_num, -1])
b1 = self.hyper_b1(states)
b1 = tf.reshape(b1, [batch_size, 1, -1])
hidden = tf.keras.activations.elu(tf.matmul(agents_outputs, w1) + b1)
# w2 = tf.abs(self.hyper_w2(states))
w2 = tf.abs(self.hyper_w2_2(self.hyper_w2_1(states)))
w2 = tf.reshape(w2, [batch_size, self.embed_shape, 1])
b2 = self.hyper_b2(states)
b2 = tf.reshape(b2, [batch_size, 1, 1])
y = tf.matmul(hidden, w2) + b2
q_tot = tf.reshape(y, [-1, 1])
return q_tot
class QMix:
def __init__(
self,
agents=None,
memory=None,
gamma=0.99,
batch_size=32,
loss_fn=tf.keras.losses.MeanSquaredError(),
optimizer=tf.keras.optimizers.RMSprop(),
is_ddqn=False,
update_interval=200,
embed_shape=60,
lr=0.0005,
agent_action_num=2):
self.agents = agents
self.memory = memory
self.gamma = gamma
self.batch_size = batch_size
self.is_ddqn = is_ddqn
self.update_interval = update_interval
self.step = 0
self.train_interval = 1
self.warmup_steps = 60
self.prev_state = None
self.prev_observations = None
self.agent_action_num = agent_action_num
self.last_q_values = [0] # @todo
self.last_targets = [0] # @todo
models = []
target_models = []
self.trainable_variables = None
self.target_trainable_variables = None
for agent in agents:
models.append(agent.model)
target_models.append(agent.target_model)
if self.trainable_variables is None:
self.trainable_variables = agent.model.trainable_variables
self.target_trainable_variables = agent.target_model.trainable_variables
else:
self.trainable_variables += agent.model.trainable_variables
self.target_trainable_variables += agent.target_model.trainable_variables
self.model = MixingNet(models, embed_shape)
self.target_model = MixingNet(target_models, embed_shape)
self.trainable_variables += self.model.trainable_variables
self.target_trainable_variables += self.target_model.trainable_variables
self.loss_fn = loss_fn
self.optimizer = optimizer
def save(self, state, observations, actions, reward, is_terminal):
if self.prev_state is None:
self.prev_state = copy.deepcopy(state)
self.prev_observations = observations
self.memory.append(self.prev_state,
self.prev_observations,
actions,
reward,
state,
observations,
terminal=is_terminal)
self.prev_state = copy.deepcopy(state)
self.prev_observations = copy.deepcopy(observations)
self.step += 1
def train(self):
loss = self._experience_replay()
return loss
def _experience_replay(self):
loss = 0
if self.step > self.warmup_steps \
and self.step % self.train_interval == 0:
states, observations, actions, rewards, next_states, next_observations, terminals = self.memory.sample(
self.batch_size)
rewards = np.array(rewards).reshape(-1, 1)
terminals = np.array(terminals).reshape(-1, 1)
next_observations = np.array(next_observations)
next_states = np.array(next_states)
masks, target_masks = [], []
for idx, (agent, next_observation) in enumerate(
zip(self.agents, next_observations)):
agent_out = agent.target_model(next_observation)
argmax_actions = tf.keras.backend.argmax(agent_out)
target_mask = tf.one_hot(
argmax_actions, depth=self.agent_action_num)
target_masks.append(target_mask)
masks.append(actions[:, idx, :])
masks = tf.convert_to_tensor(masks)
target_masks = tf.convert_to_tensor(target_masks)
target_q_values = self._predict_on_batch(
next_states, next_observations, target_masks, self.target_model)
discounted_reward_batch = self.gamma * target_q_values * terminals
targets = rewards + discounted_reward_batch
# Set up logging.
# from datetime import datetime
# stamp = datetime.now().strftime("%Y%m%d-%H%M%S")
# logdir = 'logs/func/%s' % stamp
# writer = tf.summary.create_file_writer(logdir)
# tf.summary.trace_on(graph=True, profiler=True)
observations = np.array(observations)
states = np.array(states)
states = tf.convert_to_tensor(states, dtype=tf.float32)
observations = tf.convert_to_tensor(
observations, dtype=tf.float32)
loss = self._train_on_batch(
states, observations, masks, targets)
# with writer.as_default():
# tf.summary.trace_export(
# name="my_func_trace",
# step=0,
# profiler_outdir=logdir)
if self.update_interval > 1:
# hard update
self._hard_update_target_model()
else:
# soft update
self._soft_update_target_model()
self.step += 1
return loss
def _predict_on_batch(
self,
states,
observations,
masks,
model):
q_values = model([observations, states, masks])
return q_values
def _compute_q_values(self, state):
q_values = self.target_model.predict(np.array([state]))
return q_values[0]
# @tf.function
def _train_on_batch(self, states, observations, masks, targets):
with tf.GradientTape() as tape:
tape.watch(observations)
tape.watch(states)
y_preds = self.model([observations, states, masks])
loss_value = self.loss_fn(targets, y_preds)
self.last_q_values = y_preds # @todo
self.last_targets = targets # @todo
grads = tape.gradient(loss_value, self.trainable_variables)
self.optimizer.apply_gradients(
zip(grads, self.trainable_variables))
return loss_value.numpy()
def _hard_update_target_model(self):
""" for hard update """
if self.step % self.update_interval == 0:
self.target_model.set_weights(self.model.get_weights())
for agent in self.agents:
agent._hard_update_target_model()
def _soft_update_target_model(self):
target_model_weights = np.array(self.target_model.get_weights())
model_weights = np.array(self.model.get_weights())
new_weight = (1. - self.update_interval) * target_model_weights \
+ self.update_interval * model_weights
self.target_model.set_weights(new_weight)
for agent in self.agents:
agent._soft_update_target_model()
def get_qmix_output(self):
"""
for debug
"""
obs = np.array([[[[0., 0., 1.]]], [[[0., 0., 1.]]]])
st = np.array([[0., 0., 1.]])
mk = np.array([[[1., 0.]], [[1., 0.]]])
obs = tf.convert_to_tensor(obs, dtype=np.float32)
st = tf.convert_to_tensor(st, dtype=np.float32)
mk = tf.convert_to_tensor(mk, dtype=np.float32)
result = {}
result[(0, 0)] = round(self.model([obs, st, mk]).numpy()[0][0], 2)
mk = np.array([[[1., 0.]], [[0., 1.]]])
mk = tf.convert_to_tensor(mk, dtype=np.float32)
result[(0, 1)] = round(self.model([obs, st, mk]).numpy()[0][0], 2)
mk = np.array([[[0., 1.]], [[1., 0.]]])
mk = tf.convert_to_tensor(mk, dtype=np.float32)
result[(1, 0)] = round(self.model([obs, st, mk]).numpy()[0][0], 2)
mk = np.array([[[0., 1.]], [[0., 1.]]])
mk = tf.convert_to_tensor(mk, dtype=np.float32)
result[(1, 1)] = round(self.model([obs, st, mk]).numpy()[0][0], 2)
return result