-
Notifications
You must be signed in to change notification settings - Fork 0
/
independentPursuit.py
293 lines (245 loc) · 8.7 KB
/
independentPursuit.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
279
280
281
282
283
284
285
286
287
288
289
290
291
import sys
import gym
import random
import numpy as np
import ray
from ray import tune
from ray.rllib.models import Model, ModelCatalog
from ray.tune.registry import register_env
from ray.rllib.utils import try_import_tf
from sisl_games.pursuit import pursuit
from ray.rllib.models.tf.tf_modelv2 import TFModelV2
tf = try_import_tf()
# RDQN - Rainbow DQN
# ADQN - Apex DQN
methods = ["A2C", "ADQN", "DQN", "IMPALA", "PPO", "RDQN"]
assert len(sys.argv) == 2, "Input the learning method as the second argument"
method = sys.argv[1]
assert method in methods, "Method should be one of {}".format(methods)
def env_creator(args):
return pursuit.env()
env = env_creator(1)
register_env("pursuit", env_creator)
obs_space = gym.spaces.Box(low=0, high=1, shape=(148,), dtype=np.float32)
act_space = gym.spaces.Discrete(5)
class MLPModel(Model):
def _build_layers_v2(self, input_dict, num_outputs, options):
last_layer = tf.layers.dense(
input_dict["obs"], 400, activation=tf.nn.relu, name="fc1")
last_layer = tf.layers.dense(
last_layer, 300, activation=tf.nn.relu, name="fc2")
output = tf.layers.dense(
last_layer, num_outputs, activation=None, name="fc_out")
return output, last_layer
class MLPModelV2(TFModelV2):
def __init__(self, obs_space, action_space, num_outputs, model_config,
name="my_model"):
super().__init__(obs_space, action_space, num_outputs, model_config,
name)
# Simplified to one layer.
input = tf.keras.layers.Input(obs_space.shape, dtype=obs_space.dtype)
output = tf.keras.layers.Dense(num_outputs, activation=None)
self.base_model = tf.keras.models.Sequential([input, output])
self.register_variables(self.base_model.variables)
def forward(self, input_dict, state, seq_lens):
return self.base_model(input_dict["obs"]), []
num_agents = env.num_agents
if method == "ADQN":
ModelCatalog.register_custom_model("MLPModelV2", MLPModelV2)
def gen_policyV2(i):
config = {
"model": {
"custom_model": "MLPModelV2",
},
"gamma": 0.99,
}
return (None, obs_space, act_space, config)
policies = {"policy_{}".format(i): gen_policyV2(i) for i in range(num_agents)}
else:
ModelCatalog.register_custom_model("MLPModel", MLPModel)
def gen_policy(i):
config = {
"model": {
"custom_model": "MLPModel",
},
"gamma": 0.99,
}
return (None, obs_space, act_space, config)
policies = {"policy_{}".format(i): gen_policy(i) for i in range(num_agents)}
# for all methods
policy_ids = list(policies.keys())
if __name__ == "__main__":
if method == "A2C":
tune.run(
"A2C",
stop={"episodes_total": 60000},
checkpoint_freq=10,
config={
# Enviroment specific
"env": "pursuit",
# General
"log_level": "ERROR",
"num_gpus": 1,
"num_workers": 8,
"num_envs_per_worker": 8,
"compress_observations": False,
"sample_batch_size": 20,
"train_batch_size": 512,
"gamma": .99,
"lr_schedule": [[0, 0.0007],[20000000, 0.000000000001]],
# Method specific
"multiagent": {
"policies": policies,
"policy_mapping_fn": (
lambda agent_id: policy_ids[agent_id]),
},
},
)
elif method == "ADQN":
# APEX-DQN
tune.run(
"APEX",
stop={"episodes_total": 60000},
checkpoint_freq=10,
config={
# Enviroment specific
"env": "pursuit",
# General
"log_level": "INFO",
"num_gpus": 1,
"num_workers": 8,
"num_envs_per_worker": 8,
"learning_starts": 1000,
"buffer_size": int(1e5),
"compress_observations": True,
"sample_batch_size": 20,
"train_batch_size": 512,
"gamma": .99,
# Method specific
"multiagent": {
"policies": policies,
"policy_mapping_fn": (
lambda agent_id: policy_ids[agent_id]),
},
},
)
elif method == "DQN":
# plain DQN
tune.run(
"DQN",
stop={"episodes_total": 60000},
checkpoint_freq=10,
config={
# Enviroment specific
"env": "pursuit",
# General
"log_level": "ERROR",
"num_gpus": 1,
"num_workers": 8,
"num_envs_per_worker": 8,
"learning_starts": 1000,
"buffer_size": int(1e5),
"compress_observations": True,
"sample_batch_size": 20,
"train_batch_size": 512,
"gamma": .99,
# Method specific
"dueling": False,
"double_q": False,
"multiagent": {
"policies": policies,
"policy_mapping_fn": (
lambda agent_id: policy_ids[agent_id]),
},
},
)
elif method == "IMPALA":
tune.run(
"IMPALA",
stop={"episodes_total": 60000},
checkpoint_freq=10,
config={
# Enviroment specific
"env": "pursuit",
# General
"log_level": "ERROR",
"num_gpus": 1,
"num_workers": 8,
"num_envs_per_worker": 8,
"compress_observations": True,
"sample_batch_size": 20,
"train_batch_size": 512,
"gamma": .99,
"clip_rewards": True,
"lr_schedule": [[0, 0.0005],[20000000, 0.000000000001]],
# Method specific
"multiagent": {
"policies": policies,
"policy_mapping_fn": (
lambda agent_id: policy_ids[agent_id]),
},
},
)
elif method == "PPO":
tune.run(
"PPO",
stop={"episodes_total": 60000},
checkpoint_freq=10,
config={
# Enviroment specific
"env": "pursuit",
# General
"log_level": "ERROR",
"num_gpus": 1,
"num_workers": 8,
"num_envs_per_worker": 8,
"compress_observations": False,
"gamma": .99,
"lambda": 0.95,
"kl_coeff": 0.5,
"clip_rewards": True,
"clip_param": 0.1,
"vf_clip_param": 10.0,
"entropy_coeff": 0.01,
"train_batch_size": 5000,
"sample_batch_size": 100,
"sgd_minibatch_size": 500,
"num_sgd_iter": 10,
"batch_mode": 'truncate_episodes',
"vf_share_layers": True,
# Method specific
"multiagent": {
"policies": policies,
"policy_mapping_fn": (
lambda agent_id: policy_ids[agent_id]),
},
},
)
# psuedo-rainbow DQN
elif method == "RDQN":
tune.run(
"DQN",
stop={"episodes_total": 60000},
checkpoint_freq=10,
config={
# Enviroment specific
"env": "pursuit",
# General
"log_level": "ERROR",
"num_gpus": 1,
"num_workers": 8,
"num_envs_per_worker": 8,
"learning_starts": 1000,
"buffer_size": int(1e5),
"compress_observations": True,
"sample_batch_size": 20,
"train_batch_size": 512,
"gamma": .99,
# Method specific
"multiagent": {
"policies": policies,
"policy_mapping_fn": (
lambda agent_id: policy_ids[agent_id]),
},
},
)