-
Notifications
You must be signed in to change notification settings - Fork 112
[Environment] MAgent2 #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
76d911d
added MAgent2 task
JoseLuisC99 8d397ff
unset don on any
JoseLuisC99 41afc3f
dependencies moved to function
JoseLuisC99 b70002b
tasks with dead agents eliminated
JoseLuisC99 8df7590
unused references removed
JoseLuisC99 858f7f3
Merge branch 'facebookresearch:main' into magent2
JoseLuisC99 931ea1d
Update benchmarl/environments/magent/common.py
matteobettini 0b2a128
empty
matteobettini 787da42
Merge branch 'main' into magent2
matteobettini 8d72aff
rename schema
matteobettini 65de134
rename schema
matteobettini 04622ab
Revert "rename schema"
matteobettini 4beb8f7
ci
matteobettini 7a277e6
ci
matteobettini 62bfcb6
docs
matteobettini 163703a
empty
matteobettini 1162ad1
fixes
matteobettini ce4043d
fixes
matteobettini 573243f
fixes
matteobettini 64851a9
try less tests
matteobettini fbcff01
compsitespec -> composite
matteobettini cb35028
all tests apart reloading
matteobettini b4c678b
all tests apart reloading
matteobettini 23ed668
add reloading test
matteobettini 9ec5675
more docs on install
matteobettini 9c3829a
tests
matteobettini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| defaults: | ||
| - adversarial_pursuit_config | ||
| - _self_ | ||
|
|
||
| map_size: 45 | ||
| minimap_mode: False | ||
| tag_penalty: -0.2 | ||
| max_cycles: 500 | ||
| extra_features: False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| defaults: | ||
| - battle_config | ||
| - _self_ | ||
|
|
||
| map_size: 45 | ||
| minimap_mode: False | ||
| step_reward: -0.005 | ||
| dead_penalty: -0.1 | ||
| attack_penalty: -0.1 | ||
| attack_opponent_reward: 0.2 | ||
| max_cycles: 1000 | ||
| extra_features: False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| defaults: | ||
| - battlefield_config | ||
| - _self_ | ||
|
|
||
| map_size: 80 | ||
| minimap_mode: False | ||
| step_reward: -0.005 | ||
| dead_penalty: -0.1 | ||
| attack_penalty: -0.1 | ||
| attack_opponent_reward: 0.2 | ||
| max_cycles: 1000 | ||
| extra_features: False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| defaults: | ||
| - combined_arms_config | ||
| - _self_ | ||
|
|
||
| map_size: 45 | ||
| minimap_mode: False | ||
| step_reward: -0.005 | ||
| dead_penalty: -0.1 | ||
| attack_penalty: -0.1 | ||
| attack_opponent_reward: 0.2 | ||
| max_cycles: 1000 | ||
| extra_features: False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| defaults: | ||
| - gather_config | ||
| - _self_ | ||
|
|
||
| minimap_mode: False | ||
| step_reward: -0.01 | ||
| attack_penalty: -0.1 | ||
| dead_penalty: -1 | ||
| attack_food_reward: 0.5 | ||
| max_cycles: 500 | ||
| extra_features: False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| defaults: | ||
| - tiger_deer_config | ||
| - _self_ | ||
|
|
||
| map_size: 45 | ||
| minimap_mode: False | ||
| tiger_step_recover: -0.1 | ||
| deer_attacked: -0.1 | ||
| max_cycles: 500 | ||
| extra_features: False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # | ||
| # This source code is licensed under the license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
| # | ||
|
|
||
| from dataclasses import dataclass, MISSING | ||
|
|
||
|
|
||
| @dataclass | ||
| class TaskConfig: | ||
| map_size: int = MISSING | ||
| minimap_mode: bool = MISSING | ||
| tag_penalty: float = MISSING | ||
| max_cycles: int = MISSING | ||
| extra_features: bool = MISSING |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # | ||
| # This source code is licensed under the license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
| # | ||
|
|
||
| from dataclasses import dataclass, MISSING | ||
|
|
||
|
|
||
| @dataclass | ||
| class TaskConfig: | ||
| map_size: int = MISSING | ||
| minimap_mode: bool = MISSING | ||
| step_reward: float = MISSING | ||
| dead_penalty: float = MISSING | ||
| attack_penalty: float = MISSING | ||
| attack_opponent_reward: float = MISSING | ||
| max_cycles: int = MISSING | ||
| extra_features: bool = MISSING |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # | ||
| # This source code is licensed under the license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
| # | ||
|
|
||
| from dataclasses import dataclass, MISSING | ||
|
|
||
|
|
||
| @dataclass | ||
| class TaskConfig: | ||
| map_size: int = MISSING | ||
| minimap_mode: bool = MISSING | ||
| step_reward: float = MISSING | ||
| dead_penalty: float = MISSING | ||
| attack_penalty: float = MISSING | ||
| attack_opponent_reward: float = MISSING | ||
| max_cycles: int = MISSING | ||
| extra_features: bool = MISSING |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # | ||
| # This source code is licensed under the license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
| # | ||
|
|
||
| from dataclasses import dataclass, MISSING | ||
|
|
||
|
|
||
| @dataclass | ||
| class TaskConfig: | ||
| map_size: int = MISSING | ||
| minimap_mode: bool = MISSING | ||
| step_reward: float = MISSING | ||
| dead_penalty: float = MISSING | ||
| attack_penalty: float = MISSING | ||
| attack_opponent_reward: float = MISSING | ||
| max_cycles: int = MISSING | ||
| extra_features: bool = MISSING |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # | ||
| # This source code is licensed under the license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
| # | ||
|
|
||
| from typing import Callable, Dict, List, Optional | ||
|
|
||
| from torchrl.data import CompositeSpec | ||
| from torchrl.envs import EnvBase, PettingZooWrapper | ||
|
|
||
| from magent2.environments import ( | ||
| adversarial_pursuit_v4, | ||
| battle_v4, | ||
| battlefield_v5, | ||
| combined_arms_v6, | ||
| gather_v5, | ||
| tiger_deer_v4 | ||
| ) | ||
|
|
||
| from benchmarl.environments.common import Task | ||
|
|
||
| from benchmarl.utils import DEVICE_TYPING | ||
|
|
||
|
|
||
| class MAgentTask(Task): | ||
| """Enum for MAgent2 tasks.""" | ||
|
|
||
| ADVERSARIAL_PURSUIT = None | ||
| BATTLE = None | ||
| BATTLEFIELD = None | ||
| COMBINED_ARMS = None | ||
| GATHER = None | ||
| TIGER_DEER = None | ||
|
|
||
| def get_env_fun( | ||
| self, | ||
| num_envs: int, | ||
| continuous_actions: bool, | ||
| seed: Optional[int], | ||
| device: DEVICE_TYPING, | ||
| ) -> Callable[[], EnvBase]: | ||
|
|
||
| return lambda: PettingZooWrapper( | ||
| env=self.__get_env(), | ||
| return_state=True, | ||
| seed=seed, | ||
| done_on_any=True, | ||
JoseLuisC99 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| device=device | ||
| ) | ||
|
|
||
| def __get_env(self) -> EnvBase: | ||
| envs = { | ||
| "ADVERSARIAL_PURSUIT": adversarial_pursuit_v4, | ||
| "BATTLE": battle_v4, | ||
| "BATTLEFIELD": battlefield_v5, | ||
| "COMBINED_ARMS": combined_arms_v6, | ||
| "GATHER": gather_v5, | ||
| "TIGER_DEER": tiger_deer_v4 | ||
| } | ||
| if self.name not in envs: | ||
| raise Exception(f"{self.name} is not an environment of MAgent2") | ||
| return envs[self.name].parallel_env(**self.config, render_mode="rgb_array") | ||
|
|
||
| def supports_continuous_actions(self) -> bool: | ||
| return False | ||
|
|
||
| def supports_discrete_actions(self) -> bool: | ||
| return True | ||
|
|
||
| def has_state(self) -> bool: | ||
| return True | ||
|
|
||
| def has_render(self, env: EnvBase) -> bool: | ||
| return True | ||
|
|
||
| def max_steps(self, env: EnvBase) -> int: | ||
| return self.config["max_cycles"] | ||
|
|
||
| def group_map(self, env: EnvBase) -> Dict[str, List[str]]: | ||
| return env.group_map | ||
|
|
||
| def state_spec(self, env: EnvBase) -> Optional[CompositeSpec]: | ||
| return CompositeSpec({"state": env.observation_spec["state"].clone()}) | ||
|
|
||
| def action_mask_spec(self, env: EnvBase) -> Optional[CompositeSpec]: | ||
| observation_spec = env.observation_spec.clone() | ||
| for group in self.group_map(env): | ||
| group_obs_spec = observation_spec[group] | ||
| for key in list(group_obs_spec.keys()): | ||
| if key != "action_mask": | ||
| del group_obs_spec[key] | ||
| if group_obs_spec.is_empty(): | ||
| del observation_spec[group] | ||
| del observation_spec["state"] | ||
| if observation_spec.is_empty(): | ||
| return None | ||
| return observation_spec | ||
|
|
||
| def observation_spec(self, env: EnvBase) -> CompositeSpec: | ||
| observation_spec = env.observation_spec.clone() | ||
| for group in self.group_map(env): | ||
| group_obs_spec = observation_spec[group] | ||
| for key in list(group_obs_spec.keys()): | ||
| if key != "observation": | ||
| del group_obs_spec[key] | ||
| del observation_spec["state"] | ||
| return observation_spec | ||
|
|
||
| def info_spec(self, env: EnvBase) -> Optional[CompositeSpec]: | ||
| observation_spec = env.observation_spec.clone() | ||
| for group in self.group_map(env): | ||
| group_obs_spec = observation_spec[group] | ||
| for key in list(group_obs_spec.keys()): | ||
| if key != "info": | ||
| del group_obs_spec[key] | ||
| del observation_spec["state"] | ||
| return observation_spec | ||
|
|
||
| def action_spec(self, env: EnvBase) -> CompositeSpec: | ||
| return env.full_action_spec | ||
|
|
||
| @staticmethod | ||
| def env_name() -> str: | ||
| return "magent" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # | ||
| # This source code is licensed under the license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
| # | ||
|
|
||
| from dataclasses import dataclass, MISSING | ||
|
|
||
|
|
||
| @dataclass | ||
| class TaskConfig: | ||
| minimap_mode: bool = MISSING | ||
| step_reward: float = MISSING | ||
| attack_penalty: float = MISSING | ||
| dead_penalty: float = MISSING | ||
| attack_food_reward: float = MISSING | ||
| max_cycles: int = MISSING | ||
| extra_features: bool = MISSING |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # | ||
| # This source code is licensed under the license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
| # | ||
|
|
||
| from dataclasses import dataclass, MISSING | ||
|
|
||
|
|
||
| @dataclass | ||
| class TaskConfig: | ||
| map_size: int = MISSING | ||
| minimap_mode: bool = MISSING | ||
| tiger_step_recover: float = MISSING | ||
| deer_attacked: float = MISSING | ||
| max_cycles: int = MISSING | ||
| extra_features: bool = MISSING |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.