-
Notifications
You must be signed in to change notification settings - Fork 384
/
multiagent.py
309 lines (273 loc) · 14.7 KB
/
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
"""Learning script for multi-agent problems.
Agents are based on `ray[rllib]`'s implementation of PPO and use a custom centralized critic.
Example
-------
To run the script, type in a terminal:
$ python multiagent.py --num_drones <num_drones> --env <env> --obs <ObservationType> --act <ActionType> --algo <alg> --workers <num_workers>
Notes
-----
Check Ray's status at:
http://127.0.0.1:8265
"""
import os
import time
import argparse
from datetime import datetime
from sys import platform
import subprocess
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
from ray.rllib.models.torch.fcnet import FullyConnectedNetwork
import ray
from ray import tune
from ray.tune.logger import DEFAULT_LOGGERS
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.agents.callbacks import DefaultCallbacks
from ray.rllib.models.torch.torch_modelv2 import TorchModelV2
from ray.rllib.models import ModelCatalog
from ray.rllib.policy.sample_batch import SampleBatch
from ray.rllib.env.multi_agent_env import ENV_STATE
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
import shared_constants
OWN_OBS_VEC_SIZE = None # Modified at runtime
ACTION_VEC_SIZE = None # Modified at runtime
#### Useful links ##########################################
# Workflow: github.com/ray-project/ray/blob/master/doc/source/rllib-training.rst
# ENV_STATE example: github.com/ray-project/ray/blob/master/rllib/examples/env/two_step_game.py
# Competing policies example: github.com/ray-project/ray/blob/master/rllib/examples/rock_paper_scissors_multiagent.py
############################################################
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('--num_drones', default=2, type=int, help='Number of drones (default: 2)', metavar='')
parser.add_argument('--env', default='leaderfollower', type=str, choices=['leaderfollower', 'flock', 'meetup'], help='Task (default: leaderfollower)', metavar='')
parser.add_argument('--obs', default='kin', type=ObservationType, help='Observation space (default: kin)', metavar='')
parser.add_argument('--act', default='one_d_rpm', type=ActionType, help='Action space (default: one_d_rpm)', metavar='')
parser.add_argument('--algo', default='cc', type=str, choices=['cc'], help='MARL approach (default: cc)', metavar='')
parser.add_argument('--workers', default=0, type=int, help='Number of RLlib workers (default: 0)', metavar='')
ARGS = parser.parse_args()
#### Save directory ########################################
filename = os.path.dirname(os.path.abspath(__file__))+'/results/save-'+ARGS.env+'-'+str(ARGS.num_drones)+'-'+ARGS.algo+'-'+ARGS.obs.value+'-'+ARGS.act.value+'-'+datetime.now().strftime("%m.%d.%Y_%H.%M.%S")
if not os.path.exists(filename):
os.makedirs(filename+'/')
#### Print out current git commit hash #####################
if platform == "linux" or platform == "darwin":
git_commit = subprocess.check_output(["git", "describe", "--tags"]).strip()
with open(filename+'/git_commit.txt', 'w+') as f:
f.write(str(git_commit))
#### Constants, and errors #################################
if ARGS.obs==ObservationType.KIN:
OWN_OBS_VEC_SIZE = 12
elif ARGS.obs==ObservationType.RGB:
print("[ERROR] ObservationType.RGB for multi-agent systems not yet implemented")
exit()
else:
print("[ERROR] unknown ObservationType")
exit()
if ARGS.act in [ActionType.ONE_D_RPM, ActionType.ONE_D_DYN, ActionType.ONE_D_PID]:
ACTION_VEC_SIZE = 1
elif ARGS.act in [ActionType.RPM, ActionType.DYN, ActionType.VEL]:
ACTION_VEC_SIZE = 4
elif ARGS.act == ActionType.PID:
ACTION_VEC_SIZE = 3
else:
print("[ERROR] unknown ActionType")
exit()
#### Uncomment to debug slurm scripts ######################
# 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.env == 'flock':
register_env(temp_env_name, lambda _: FlockAviary(num_drones=ARGS.num_drones,
aggregate_phy_steps=shared_constants.AGGR_PHY_STEPS,
obs=ARGS.obs,
act=ARGS.act
)
)
elif ARGS.env == 'leaderfollower':
register_env(temp_env_name, lambda _: LeaderFollowerAviary(num_drones=ARGS.num_drones,
aggregate_phy_steps=shared_constants.AGGR_PHY_STEPS,
obs=ARGS.obs,
act=ARGS.act
)
)
elif ARGS.env == 'meetup':
register_env(temp_env_name, lambda _: MeetupAviary(num_drones=ARGS.num_drones,
aggregate_phy_steps=shared_constants.AGGR_PHY_STEPS,
obs=ARGS.obs,
act=ARGS.act
)
)
else:
print("[ERROR] environment not yet implemented")
exit()
#### Unused env to extract the act and obs spaces ##########
if ARGS.env == 'flock':
temp_env = FlockAviary(num_drones=ARGS.num_drones,
aggregate_phy_steps=shared_constants.AGGR_PHY_STEPS,
obs=ARGS.obs,
act=ARGS.act
)
elif ARGS.env == 'leaderfollower':
temp_env = LeaderFollowerAviary(num_drones=ARGS.num_drones,
aggregate_phy_steps=shared_constants.AGGR_PHY_STEPS,
obs=ARGS.obs,
act=ARGS.act
)
elif ARGS.env == 'meetup':
temp_env = MeetupAviary(num_drones=ARGS.num_drones,
aggregate_phy_steps=shared_constants.AGGR_PHY_STEPS,
obs=ARGS.obs,
act=ARGS.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]
#### Note ##################################################
# RLlib will create ``num_workers + 1`` copies of the
# environment since one copy is needed for the driver process.
# To avoid paying the extra overhead of the driver copy,
# which is needed to access the env's action and observation spaces,
# you can defer environment initialization until ``reset()`` is called
#### 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 + 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
}
#### Ray Tune stopping conditions ##########################
stop = {
"timesteps_total": 120000, # 100000 ~= 10'
# "episode_reward_mean": 0,
# "training_iteration": 0,
}
#### Train #################################################
results = tune.run(
"PPO",
stop=stop,
config=config,
verbose=True,
checkpoint_at_end=True,
local_dir=filename,
)
# check_learning_achieved(results, 1.0)
#### Save agent ############################################
checkpoints = results.get_trial_checkpoints_paths(trial=results.get_best_trial('episode_reward_mean',
mode='max'
),
metric='episode_reward_mean'
)
with open(filename+'/checkpoint.txt', 'w+') as f:
f.write(checkpoints[0][0])
#### Shut down Ray #########################################
ray.shutdown()