Skip to content
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

Added radius rescaling to simple env #1213

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions pettingzoo/mpe/_mpe_utils/simple_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def __init__(
render_mode=None,
continuous_actions=False,
local_ratio=None,
dynamic_rescaling=False,
):
super().__init__()

Expand All @@ -66,6 +67,7 @@ def __init__(
self.world = world
self.continuous_actions = continuous_actions
self.local_ratio = local_ratio
self.dynamic_rescaling = dynamic_rescaling

self.scenario.reset_world(self.world, self.np_random)

Expand Down Expand Up @@ -116,6 +118,11 @@ def __init__(
dtype=np.float32,
)

# Get the original cam_range
# This will be used to scale the rendering
all_poses = [entity.state.p_pos for entity in self.world.entities]
self.original_cam_range = np.max(np.abs(np.array(all_poses)))

self.steps = 0

self.current_actions = [None] * self.num_agents
Expand Down Expand Up @@ -295,6 +302,10 @@ def draw(self):
all_poses = [entity.state.p_pos for entity in self.world.entities]
cam_range = np.max(np.abs(np.array(all_poses)))

# The scaling factor is used for dynamic rescaling of the rendering - a.k.a Zoom In/Zoom Out effect
# The 0.9 is a factor to keep the entities from appearing "too" out-of-bounds
scaling_factor = 0.9 * self.original_cam_range / cam_range

# update geometry and text positions
text_line = 0
for e, entity in enumerate(self.world.entities):
Expand All @@ -309,12 +320,15 @@ def draw(self):
y = (y / cam_range) * self.height // 2 * 0.9
x += self.width // 2
y += self.height // 2
pygame.draw.circle(
self.screen, entity.color * 200, (x, y), entity.size * 350
) # 350 is an arbitrary scale factor to get pygame to render similar sizes as pyglet
pygame.draw.circle(
self.screen, (0, 0, 0), (x, y), entity.size * 350, 1
) # borders

# 350 is an arbitrary scale factor to get pygame to render similar sizes as pyglet
if self.dynamic_rescaling:
radius = entity.size * 350 * scaling_factor
else:
radius = entity.size * 350

pygame.draw.circle(self.screen, entity.color * 200, (x, y), radius)
pygame.draw.circle(self.screen, (0, 0, 0), (x, y), radius, 1) # borders
assert (
0 < x < self.width and 0 < y < self.height
), f"Coordinates {(x, y)} are out of bounds."
Expand Down
13 changes: 11 additions & 2 deletions pettingzoo/mpe/simple/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
### Arguments
``` python
simple_v3.env(max_cycles=25, continuous_actions=False)
simple_v3.env(max_cycles=25, continuous_actions=False, dynamic_rescaling=False)
```
Expand All @@ -40,6 +40,8 @@
`continuous_actions`: Whether agent action spaces are discrete(default) or continuous
`dynamic_rescaling`: Whether to rescale the size of agents and landmarks based on the screen size
"""

import numpy as np
Expand All @@ -52,7 +54,13 @@


class raw_env(SimpleEnv, EzPickle):
def __init__(self, max_cycles=25, continuous_actions=False, render_mode=None):
def __init__(
self,
max_cycles=25,
continuous_actions=False,
render_mode=None,
dynamic_rescaling=False,
):
EzPickle.__init__(
self,
max_cycles=max_cycles,
Expand All @@ -68,6 +76,7 @@ def __init__(self, max_cycles=25, continuous_actions=False, render_mode=None):
render_mode=render_mode,
max_cycles=max_cycles,
continuous_actions=continuous_actions,
dynamic_rescaling=dynamic_rescaling,
)
self.metadata["name"] = "simple_v3"

Expand Down
14 changes: 12 additions & 2 deletions pettingzoo/mpe/simple_adversary/simple_adversary.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
### Arguments
``` python
simple_adversary_v3.env(N=2, max_cycles=25, continuous_actions=False)
simple_adversary_v3.env(N=2, max_cycles=25, continuous_actions=False, dynamic_rescaling=False)
```
Expand All @@ -50,6 +50,8 @@
`continuous_actions`: Whether agent action spaces are discrete(default) or continuous
`dynamic_rescaling`: Whether to rescale the size of agents and landmarks based on the screen size
"""

import numpy as np
Expand All @@ -62,7 +64,14 @@


class raw_env(SimpleEnv, EzPickle):
def __init__(self, N=2, max_cycles=25, continuous_actions=False, render_mode=None):
def __init__(
self,
N=2,
max_cycles=25,
continuous_actions=False,
render_mode=None,
dynamic_rescaling=False,
):
EzPickle.__init__(
self,
N=N,
Expand All @@ -79,6 +88,7 @@ def __init__(self, N=2, max_cycles=25, continuous_actions=False, render_mode=Non
render_mode=render_mode,
max_cycles=max_cycles,
continuous_actions=continuous_actions,
dynamic_rescaling=dynamic_rescaling,
)
self.metadata["name"] = "simple_adversary_v3"

Expand Down
13 changes: 11 additions & 2 deletions pettingzoo/mpe/simple_crypto/simple_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
### Arguments

``` python
simple_crypto_v3.env(max_cycles=25, continuous_actions=False)
simple_crypto_v3.env(max_cycles=25, continuous_actions=False, dynamic_rescaling=False)
```


Expand All @@ -54,6 +54,8 @@

`continuous_actions`: Whether agent action spaces are discrete(default) or continuous

`dynamic_rescaling`: Whether to rescale the size of agents and landmarks based on the screen size

"""

import numpy as np
Expand All @@ -73,7 +75,13 @@


class raw_env(SimpleEnv, EzPickle):
def __init__(self, max_cycles=25, continuous_actions=False, render_mode=None):
def __init__(
self,
max_cycles=25,
continuous_actions=False,
render_mode=None,
dynamic_rescaling=False,
):
EzPickle.__init__(
self,
max_cycles=max_cycles,
Expand All @@ -89,6 +97,7 @@ def __init__(self, max_cycles=25, continuous_actions=False, render_mode=None):
render_mode=render_mode,
max_cycles=max_cycles,
continuous_actions=continuous_actions,
dynamic_rescaling=dynamic_rescaling,
)
self.metadata["name"] = "simple_crypto_v3"

Expand Down
14 changes: 12 additions & 2 deletions pettingzoo/mpe/simple_push/simple_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@
### Arguments
``` python
simple_push_v3.env(max_cycles=25, continuous_actions=False)
simple_push_v3.env(max_cycles=25, continuous_actions=False, dynamic_rescaling=False)
```
`max_cycles`: number of frames (a step for each agent) until game terminates
`dynamic_rescaling`: Whether to rescale the size of agents and landmarks based on the screen size
"""

import numpy as np
Expand All @@ -57,7 +60,13 @@


class raw_env(SimpleEnv, EzPickle):
def __init__(self, max_cycles=25, continuous_actions=False, render_mode=None):
def __init__(
self,
max_cycles=25,
continuous_actions=False,
render_mode=None,
dynamic_rescaling=False,
):
EzPickle.__init__(
self,
max_cycles=max_cycles,
Expand All @@ -73,6 +82,7 @@ def __init__(self, max_cycles=25, continuous_actions=False, render_mode=None):
render_mode=render_mode,
max_cycles=max_cycles,
continuous_actions=continuous_actions,
dynamic_rescaling=dynamic_rescaling,
)
self.metadata["name"] = "simple_push_v3"

Expand Down
12 changes: 10 additions & 2 deletions pettingzoo/mpe/simple_reference/simple_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@


``` python
simple_reference_v3.env(local_ratio=0.5, max_cycles=25, continuous_actions=False)
simple_reference_v3.env(local_ratio=0.5, max_cycles=25, continuous_actions=False, dynamic_rescaling=False)
```


Expand All @@ -51,6 +51,8 @@

`continuous_actions`: Whether agent action spaces are discrete(default) or continuous

`dynamic_rescaling`: Whether to rescale the size of agents and landmarks based on the screen size

"""

import numpy as np
Expand All @@ -64,7 +66,12 @@

class raw_env(SimpleEnv, EzPickle):
def __init__(
self, local_ratio=0.5, max_cycles=25, continuous_actions=False, render_mode=None
self,
local_ratio=0.5,
max_cycles=25,
continuous_actions=False,
render_mode=None,
dynamic_rescaling=False,
):
EzPickle.__init__(
self,
Expand All @@ -86,6 +93,7 @@ def __init__(
max_cycles=max_cycles,
continuous_actions=continuous_actions,
local_ratio=local_ratio,
dynamic_rescaling=dynamic_rescaling,
)
self.metadata["name"] = "simple_reference_v3"

Expand Down
13 changes: 11 additions & 2 deletions pettingzoo/mpe/simple_speaker_listener/simple_speaker_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
### Arguments
``` python
simple_speaker_listener_v4.env(max_cycles=25, continuous_actions=False)
simple_speaker_listener_v4.env(max_cycles=25, continuous_actions=False, dynamic_rescaling=False)
```
Expand All @@ -46,6 +46,8 @@
`continuous_actions`: Whether agent action spaces are discrete(default) or continuous
`dynamic_rescaling`: Whether to rescale the size of agents and landmarks based on the screen size
"""

import numpy as np
Expand All @@ -58,7 +60,13 @@


class raw_env(SimpleEnv, EzPickle):
def __init__(self, max_cycles=25, continuous_actions=False, render_mode=None):
def __init__(
self,
max_cycles=25,
continuous_actions=False,
render_mode=None,
dynamic_rescaling=False,
):
EzPickle.__init__(
self,
max_cycles=max_cycles,
Expand All @@ -74,6 +82,7 @@ def __init__(self, max_cycles=25, continuous_actions=False, render_mode=None):
render_mode=render_mode,
max_cycles=max_cycles,
continuous_actions=continuous_actions,
dynamic_rescaling=dynamic_rescaling,
)
self.metadata["name"] = "simple_speaker_listener_v4"

Expand Down
6 changes: 5 additions & 1 deletion pettingzoo/mpe/simple_spread/simple_spread.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
### Arguments
``` python
simple_spread_v3.env(N=3, local_ratio=0.5, max_cycles=25, continuous_actions=False)
simple_spread_v3.env(N=3, local_ratio=0.5, max_cycles=25, continuous_actions=False, dynamic_rescaling=False)
```
Expand All @@ -49,6 +49,8 @@
`continuous_actions`: Whether agent action spaces are discrete(default) or continuous
`dynamic_rescaling`: Whether to rescale the size of agents and landmarks based on the screen size
"""

import numpy as np
Expand All @@ -68,6 +70,7 @@ def __init__(
max_cycles=25,
continuous_actions=False,
render_mode=None,
dynamic_rescaling=False,
):
EzPickle.__init__(
self,
Expand All @@ -90,6 +93,7 @@ def __init__(
max_cycles=max_cycles,
continuous_actions=continuous_actions,
local_ratio=local_ratio,
dynamic_rescaling=dynamic_rescaling,
)
self.metadata["name"] = "simple_spread_v3"

Expand Down
6 changes: 5 additions & 1 deletion pettingzoo/mpe/simple_tag/simple_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def bound(x):
### Arguments
``` python
simple_tag_v3.env(num_good=1, num_adversaries=3, num_obstacles=2, max_cycles=25, continuous_actions=False)
simple_tag_v3.env(num_good=1, num_adversaries=3, num_obstacles=2, max_cycles=25, continuous_actions=False, dynamic_rescaling=False)
```
Expand All @@ -60,6 +60,8 @@ def bound(x):
`continuous_actions`: Whether agent action spaces are discrete(default) or continuous
`dynamic_rescaling`: Whether to rescale the size of agents and landmarks based on the screen size
"""

import numpy as np
Expand All @@ -80,6 +82,7 @@ def __init__(
max_cycles=25,
continuous_actions=False,
render_mode=None,
dynamic_rescaling=False,
):
EzPickle.__init__(
self,
Expand All @@ -99,6 +102,7 @@ def __init__(
render_mode=render_mode,
max_cycles=max_cycles,
continuous_actions=continuous_actions,
dynamic_rescaling=dynamic_rescaling,
)
self.metadata["name"] = "simple_tag_v3"

Expand Down
Loading
Loading