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
Changes from 2 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
23 changes: 20 additions & 3 deletions pettingzoo/mpe/_mpe_utils/simple_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pettingzoo.utils.agent_selector import AgentSelector

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
DYNAMIC_RESCALING = True
jjshoots marked this conversation as resolved.
Show resolved Hide resolved


def make_env(raw_env):
Expand Down Expand Up @@ -116,6 +117,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 +301,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,11 +319,18 @@ def draw(self):
y = (y / cam_range) * self.height // 2 * 0.9
x += self.width // 2
y += self.height // 2

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

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
self.screen, entity.color * 200, (x, y), radius
)
pygame.draw.circle(
self.screen, (0, 0, 0), (x, y), entity.size * 350, 1
self.screen, (0, 0, 0), (x, y), radius, 1
) # borders
assert (
0 < x < self.width and 0 < y < self.height
Expand Down
Loading