-
Notifications
You must be signed in to change notification settings - Fork 389
/
Copy pathtest_multiagent.py
332 lines (300 loc) · 15.2 KB
/
test_multiagent.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
"""Test script for multiagent problems.
This scripts runs the best model found by one of the executions of `multiagent.py`
Example
-------
To run the script, type in a terminal:
$ python test_multiagent.py --exp ./results/save-<env>-<num_drones>-<algo>-<obs>-<act>-<time_date>
"""
import os
import time
import argparse
from datetime import datetime
import pdb
import math
import numpy as np
import pybullet as p
import pickle
import matplotlib.pyplot as plt
import gym
from gym import error, spaces, utils
from gym.utils import seeding
from gym.spaces import Box, Dict
import torch
import torch.nn as nn
import ray
from ray import tune
from ray.tune import register_env
from ray.rllib.agents import ppo
from ray.rllib.agents.ppo import PPOTrainer, PPOTFPolicy
from ray.rllib.examples.policy.random_policy import RandomPolicy
from ray.rllib.utils.test_utils import check_learning_achieved
from ray.rllib.models.torch.torch_modelv2 import TorchModelV2
from ray.rllib.agents.callbacks import DefaultCallbacks
from ray.rllib.models.torch.fcnet import FullyConnectedNetwork
from ray.rllib.models import ModelCatalog
from ray.rllib.policy.sample_batch import SampleBatch
from gym_pybullet_drones.utils.enums import DroneModel, Physics
from gym_pybullet_drones.envs.multi_agent_rl.FlockAviary import FlockAviary
from gym_pybullet_drones.envs.multi_agent_rl.LeaderFollowerAviary import LeaderFollowerAviary
from gym_pybullet_drones.envs.multi_agent_rl.MeetupAviary import MeetupAviary
from gym_pybullet_drones.envs.single_agent_rl.BaseSingleAgentAviary import ActionType, ObservationType
from gym_pybullet_drones.utils.Logger import Logger
from gym_pybullet_drones.utils.utils import sync
import shared_constants
OWN_OBS_VEC_SIZE = None # Modified at runtime
ACTION_VEC_SIZE = None # Modified at runtime
############################################################
class CustomTorchCentralizedCriticModel(TorchModelV2, nn.Module):
"""Multi-agent model that implements a centralized value function.
It assumes the observation is a dict with 'own_obs' and 'opponent_obs', the
former of which can be used for computing actions (i.e., decentralized
execution), and the latter for optimization (i.e., centralized learning).
This model has two parts:
- An action model that looks at just 'own_obs' to compute actions
- A value model that also looks at the 'opponent_obs' / 'opponent_action'
to compute the value (it does this by using the 'obs_flat' tensor).
"""
def __init__(self, obs_space, action_space, num_outputs, model_config, name):
TorchModelV2.__init__(self, obs_space, action_space, num_outputs, model_config, name)
nn.Module.__init__(self)
self.action_model = FullyConnectedNetwork(
Box(low=-1, high=1, shape=(OWN_OBS_VEC_SIZE, )),
action_space,
num_outputs,
model_config,
name + "_action"
)
self.value_model = FullyConnectedNetwork(
obs_space,
action_space,
1,
model_config,
name + "_vf"
)
self._model_in = None
def forward(self, input_dict, state, seq_lens):
self._model_in = [input_dict["obs_flat"], state, seq_lens]
return self.action_model({"obs": input_dict["obs"]["own_obs"]}, state, seq_lens)
def value_function(self):
value_out, _ = self.value_model({"obs": self._model_in[0]}, self._model_in[1], self._model_in[2])
return torch.reshape(value_out, [-1])
############################################################
class FillInActions(DefaultCallbacks):
def on_postprocess_trajectory(self, worker, episode, agent_id, policy_id, policies, postprocessed_batch, original_batches, **kwargs):
to_update = postprocessed_batch[SampleBatch.CUR_OBS]
other_id = 1 if agent_id == 0 else 0
action_encoder = ModelCatalog.get_preprocessor_for_space(
# Box(-np.inf, np.inf, (ACTION_VEC_SIZE,), np.float32) # Unbounded
Box(-1, 1, (ACTION_VEC_SIZE,), np.float32) # Bounded
)
_, opponent_batch = original_batches[other_id]
# opponent_actions = np.array([action_encoder.transform(a) for a in opponent_batch[SampleBatch.ACTIONS]]) # Unbounded
opponent_actions = np.array([action_encoder.transform(np.clip(a, -1, 1)) for a in opponent_batch[SampleBatch.ACTIONS]]) # Bounded
to_update[:, -ACTION_VEC_SIZE:] = opponent_actions
############################################################
def central_critic_observer(agent_obs, **kw):
new_obs = {
0: {
"own_obs": agent_obs[0],
"opponent_obs": agent_obs[1],
"opponent_action": np.zeros(ACTION_VEC_SIZE), # Filled in by FillInActions
},
1: {
"own_obs": agent_obs[1],
"opponent_obs": agent_obs[0],
"opponent_action": np.zeros(ACTION_VEC_SIZE), # Filled in by FillInActions
},
}
return new_obs
############################################################
if __name__ == "__main__":
#### Define and parse (optional) arguments for the script ##
parser = argparse.ArgumentParser(description='Multi-agent reinforcement learning experiments script')
parser.add_argument('--exp', type=str, help='The experiment folder written as ./results/save-<env>-<num_drones>-<algo>-<obs>-<act>-<time_date>', metavar='')
ARGS = parser.parse_args()
#### Parameters to recreate the environment ################
NUM_DRONES = int(ARGS.exp.split("-")[2])
OBS = ObservationType.KIN if ARGS.exp.split("-")[4] == 'kin' else ObservationType.RGB
# Parse ActionType instance from file name
action_name = ARGS.exp.split("-")[5]
ACT = [action for action in ActionType if action.value == action_name]
if len(ACT) != 1:
raise AssertionError("Result file could have gotten corrupted. Extracted action type does not match any of the existing ones.")
ACT = ACT.pop()
#### Constants, and errors #################################
if OBS == ObservationType.KIN:
OWN_OBS_VEC_SIZE = 12
elif OBS == ObservationType.RGB:
print("[ERROR] ObservationType.RGB for multi-agent systems not yet implemented")
exit()
else:
print("[ERROR] unknown ObservationType")
exit()
if ACT in [ActionType.ONE_D_RPM, ActionType.ONE_D_DYN, ActionType.ONE_D_PID]:
ACTION_VEC_SIZE = 1
elif ACT in [ActionType.RPM, ActionType.DYN, ActionType.VEL]:
ACTION_VEC_SIZE = 4
elif ACT == ActionType.PID:
ACTION_VEC_SIZE = 3
else:
print("[ERROR] unknown ActionType")
exit()
#### Initialize Ray Tune ###################################
ray.shutdown()
ray.init(ignore_reinit_error=True)
#### Register the custom centralized critic model ##########
ModelCatalog.register_custom_model("cc_model", CustomTorchCentralizedCriticModel)
#### Register the environment ##############################
temp_env_name = "this-aviary-v0"
if ARGS.exp.split("-")[1] == 'flock':
register_env(temp_env_name, lambda _: FlockAviary(num_drones=NUM_DRONES,
aggregate_phy_steps=shared_constants.AGGR_PHY_STEPS,
obs=OBS,
act=ACT
)
)
elif ARGS.exp.split("-")[1] == 'leaderfollower':
register_env(temp_env_name, lambda _: LeaderFollowerAviary(num_drones=NUM_DRONES,
aggregate_phy_steps=shared_constants.AGGR_PHY_STEPS,
obs=OBS,
act=ACT
)
)
elif ARGS.exp.split("-")[1] == 'meetup':
register_env(temp_env_name, lambda _: MeetupAviary(num_drones=NUM_DRONES,
aggregate_phy_steps=shared_constants.AGGR_PHY_STEPS,
obs=OBS,
act=ACT
)
)
else:
print("[ERROR] environment not yet implemented")
exit()
#### Unused env to extract the act and obs spaces ##########
if ARGS.exp.split("-")[1] == 'flock':
temp_env = FlockAviary(num_drones=NUM_DRONES,
aggregate_phy_steps=shared_constants.AGGR_PHY_STEPS,
obs=OBS,
act=ACT
)
elif ARGS.exp.split("-")[1] == 'leaderfollower':
temp_env = LeaderFollowerAviary(num_drones=NUM_DRONES,
aggregate_phy_steps=shared_constants.AGGR_PHY_STEPS,
obs=OBS,
act=ACT
)
elif ARGS.exp.split("-")[1] == 'meetup':
temp_env = MeetupAviary(num_drones=NUM_DRONES,
aggregate_phy_steps=shared_constants.AGGR_PHY_STEPS,
obs=OBS,
act=ACT
)
else:
print("[ERROR] environment not yet implemented")
exit()
observer_space = Dict({
"own_obs": temp_env.observation_space[0],
"opponent_obs": temp_env.observation_space[0],
"opponent_action": temp_env.action_space[0],
})
action_space = temp_env.action_space[0]
#### Set up the trainer's config ###########################
config = ppo.DEFAULT_CONFIG.copy() # For the default config, see github.com/ray-project/ray/blob/master/rllib/agents/trainer.py
config = {
"env": temp_env_name,
"num_workers": 0, #0+ARGS.workers,
"num_gpus": int(os.environ.get("RLLIB_NUM_GPUS", "0")), # Use GPUs iff `RLLIB_NUM_GPUS` env var set to > 0
"batch_mode": "complete_episodes",
"callbacks": FillInActions,
"framework": "torch",
}
#### Set up the model parameters of the trainer's config ###
config["model"] = {
"custom_model": "cc_model",
}
#### Set up the multiagent params of the trainer's config ##
config["multiagent"] = {
"policies": {
"pol0": (None, observer_space, action_space, {"agent_id": 0,}),
"pol1": (None, observer_space, action_space, {"agent_id": 1,}),
},
"policy_mapping_fn": lambda x: "pol0" if x == 0 else "pol1", # # Function mapping agent ids to policy ids
"observation_fn": central_critic_observer, # See rllib/evaluation/observation_function.py for more info
}
#### Restore agent #########################################
agent = ppo.PPOTrainer(config=config)
with open(ARGS.exp+'/checkpoint.txt', 'r+') as f:
checkpoint = f.read()
agent.restore(checkpoint)
#### Extract and print policies ############################
policy0 = agent.get_policy("pol0")
print("action model 0", policy0.model.action_model)
print("value model 0", policy0.model.value_model)
policy1 = agent.get_policy("pol1")
print("action model 1", policy1.model.action_model)
print("value model 1", policy1.model.value_model)
#### Create test environment ###############################
if ARGS.exp.split("-")[1] == 'flock':
test_env = FlockAviary(num_drones=NUM_DRONES,
aggregate_phy_steps=shared_constants.AGGR_PHY_STEPS,
obs=OBS,
act=ACT,
gui=True,
record=False
)
elif ARGS.exp.split("-")[1] == 'leaderfollower':
test_env = LeaderFollowerAviary(num_drones=NUM_DRONES,
aggregate_phy_steps=shared_constants.AGGR_PHY_STEPS,
obs=OBS,
act=ACT,
gui=True,
record=False
)
elif ARGS.exp.split("-")[1] == 'meetup':
test_env = MeetupAviary(num_drones=NUM_DRONES,
aggregate_phy_steps=shared_constants.AGGR_PHY_STEPS,
obs=OBS,
act=ACT,
gui=True,
record=False
)
else:
print("[ERROR] environment not yet implemented")
exit()
#### Show, record a video, and log the model's performance #
obs = test_env.reset()
logger = Logger(logging_freq_hz=int(test_env.SIM_FREQ/test_env.AGGR_PHY_STEPS),
num_drones=NUM_DRONES
)
if ACT in [ActionType.ONE_D_RPM, ActionType.ONE_D_DYN, ActionType.ONE_D_PID]:
action = {i: np.array([0]) for i in range(NUM_DRONES)}
elif ACT in [ActionType.RPM, ActionType.DYN, ActionType.VEL]:
action = {i: np.array([0, 0, 0, 0]) for i in range(NUM_DRONES)}
elif ACT==ActionType.PID:
action = {i: np.array([0, 0, 0]) for i in range(NUM_DRONES)}
else:
print("[ERROR] unknown ActionType")
exit()
start = time.time()
for i in range(6*int(test_env.SIM_FREQ/test_env.AGGR_PHY_STEPS)): # Up to 6''
#### Deploy the policies ###################################
temp = {}
temp[0] = policy0.compute_single_action(np.hstack([action[1], obs[1], obs[0]])) # Counterintuitive order, check params.json
temp[1] = policy1.compute_single_action(np.hstack([action[0], obs[0], obs[1]]))
action = {0: temp[0][0], 1: temp[1][0]}
obs, reward, done, info = test_env.step(action)
test_env.render()
if OBS==ObservationType.KIN:
for j in range(NUM_DRONES):
logger.log(drone=j,
timestamp=i/test_env.SIM_FREQ,
state= np.hstack([obs[j][0:3], np.zeros(4), obs[j][3:15], np.resize(action[j], (4))]),
control=np.zeros(12)
)
sync(np.floor(i*test_env.AGGR_PHY_STEPS), start, test_env.TIMESTEP)
# if done["__all__"]: obs = test_env.reset() # OPTIONAL EPISODE HALT
test_env.close()
logger.save_as_csv("ma") # Optional CSV save
logger.plot()
#### Shut down Ray #########################################
ray.shutdown()