-
Notifications
You must be signed in to change notification settings - Fork 157
/
add_new_env.py
135 lines (117 loc) · 4.66 KB
/
add_new_env.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
# MIT License
# Copyright (c) 2023 Replicable-MARL
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""
An example of integrating new tasks into MARLLib
About ma-gym: https://github.com/koulanurag/ma-gym
doc: https://github.com/koulanurag/ma-gym/wiki
Learn how to transform the environment to be compatible with MARLlib:
please refer to the paper: https://arxiv.org/abs/2210.13708
Install ma-gym before use
"""
import numpy as np
from ray.rllib.env.multi_agent_env import MultiAgentEnv
from gym.spaces import Dict as GymDict, Box
from ma_gym.envs.checkers import Checkers
from ma_gym.envs.switch import Switch
from marllib import marl
from marllib.envs.base_env import ENV_REGISTRY
import time
# register all scenario with env class
REGISTRY = {}
REGISTRY["Checkers"] = Checkers
REGISTRY["Switch2"] = Switch
# provide detailed information of each scenario
# mostly for policy sharing
policy_mapping_dict = {
"Checkers": {
"description": "two team cooperate",
"team_prefix": ("red_", "blue_"),
"all_agents_one_policy": True,
"one_agent_one_policy": True,
},
"Switch2": {
"description": "two team cooperate",
"team_prefix": ("red_", "blue_"),
"all_agents_one_policy": True,
"one_agent_one_policy": True,
},
}
# must inherited from MultiAgentEnv class
class RLlibMAGym(MultiAgentEnv):
def __init__(self, env_config):
map = env_config["map_name"]
env_config.pop("map_name", None)
self.env = REGISTRY[map](**env_config)
# assume all agent same action/obs space
self.action_space = self.env.action_space[0]
self.observation_space = GymDict({"obs": Box(
low=0.0,
high=1.0,
shape=(self.env.observation_space[0].shape[0],),
dtype=np.dtype("float64"))})
self.agents = ["red_0", "blue_0"]
self.num_agents = len(self.agents)
env_config["map_name"] = map
self.env_config = env_config
def reset(self):
original_obs = self.env.reset()
obs = {}
for i, name in enumerate(self.agents):
obs[name] = {"obs": np.array(original_obs[i])}
return obs
def step(self, action_dict):
action_ls = [action_dict[key] for key in action_dict.keys()]
o, r, d, info = self.env.step(action_ls)
rewards = {}
obs = {}
for i, key in enumerate(action_dict.keys()):
rewards[key] = r[i]
obs[key] = {
"obs": np.array(o[i])
}
dones = {"__all__": True if sum(d) == self.num_agents else False}
return obs, rewards, dones, {}
def close(self):
self.env.close()
def render(self, mode=None):
self.env.render()
time.sleep(0.05)
return True
def get_env_info(self):
env_info = {
"space_obs": self.observation_space,
"space_act": self.action_space,
"num_agents": self.num_agents,
"episode_limit": 100,
"policy_mapping_info": policy_mapping_dict
}
return env_info
if __name__ == '__main__':
# register new env
ENV_REGISTRY["magym"] = RLlibMAGym
# initialize env
env = marl.make_env(environment_name="magym", map_name="Checkers", abs_path="../../examples/config/env_config/magym.yaml")
# pick mappo algorithms
mappo = marl.algos.mappo(hyperparam_source="test")
# customize model
model = marl.build_model(env, mappo, {"core_arch": "mlp", "encode_layer": "128-128"})
# start learning
mappo.fit(env, model, stop={'episode_reward_mean': 2000, 'timesteps_total': 10000000}, local_mode=True, num_gpus=1,
num_workers=2, share_policy='all', checkpoint_freq=50)