Skip to content
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
19 changes: 11 additions & 8 deletions vmas/scenarios/dispersion.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2022-2023.
# Copyright (c) 2022-2024.
# ProrokLab (https://www.proroklab.org/)
# All rights reserved.

Expand All @@ -15,11 +15,14 @@ def make_world(self, batch_dim: int, device: torch.device, **kwargs):
n_agents = kwargs.get("n_agents", 4)
self.share_reward = kwargs.get("share_reward", False)
self.penalise_by_time = kwargs.get("penalise_by_time", False)

n_food = n_agents
self.food_radius = kwargs.get("food_radius", 0.05)
self.pos_range = kwargs.get("pos_range", 1.0)
n_food = kwargs.get("n_food", n_agents)

# Make world
world = World(batch_dim, device)
world = World(
batch_dim, device, x_semidim=self.pos_range, y_semidim=self.pos_range
)
# Add agents
for i in range(n_agents):
# Constraint: all agents have same action range and multiplier
Expand All @@ -32,9 +35,9 @@ def make_world(self, batch_dim: int, device: torch.device, **kwargs):
# Add landmarks
for i in range(n_food):
food = Landmark(
name=f"food {i}",
name=f"food_{i}",
collide=False,
shape=Sphere(radius=0.02),
shape=Sphere(radius=self.food_radius),
color=Color.GREEN,
)
world.add_landmark(food)
Expand All @@ -58,8 +61,8 @@ def reset_world_at(self, env_index: int = None):
device=self.world.device,
dtype=torch.float32,
).uniform_(
-1.0,
1.0,
-self.pos_range,
self.pos_range,
),
batch_index=env_index,
)
Expand Down
72 changes: 41 additions & 31 deletions vmas/scenarios/dropout.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
# Copyright (c) 2022-2023.
# Copyright (c) 2022-2024.
# ProrokLab (https://www.proroklab.org/)
# All rights reserved.
import math
from typing import Dict

import torch
from torch import Tensor

from vmas import render_interactively
from vmas.simulator.core import Agent, Landmark, Sphere, World
from vmas.simulator.scenario import BaseScenario
from vmas.simulator.utils import Color
from vmas.simulator.utils import Color, ScenarioUtils

DEFAULT_ENERGY_COEFF = 0.02

Expand All @@ -21,19 +20,24 @@ def make_world(self, batch_dim: int, device: torch.device, **kwargs):
self.energy_coeff = kwargs.get(
"energy_coeff", DEFAULT_ENERGY_COEFF
) # Weight of team energy penalty
self.start_same_point = kwargs.get("start_same_point", False)
self.agent_radius = 0.05
self.goal_radius = 0.03

# Make world
world = World(batch_dim, device)
# Add agents
for i in range(n_agents):
# Constraint: all agents have same action range and multiplier
agent = Agent(name=f"agent_{i}", collide=False)
agent = Agent(
name=f"agent_{i}", collide=False, shape=Sphere(radius=self.agent_radius)
)
world.add_agent(agent)
# Add landmarks
goal = Landmark(
name="goal",
collide=False,
shape=Sphere(radius=0.03),
shape=Sphere(radius=self.goal_radius),
color=Color.GREEN,
)
world.add_landmark(goal)
Expand All @@ -45,36 +49,42 @@ def make_world(self, batch_dim: int, device: torch.device, **kwargs):
return world

def reset_world_at(self, env_index: int = None):
for agent in self.world.agents:
# Random pos between -1 and 1
agent.set_pos(
torch.zeros(
(1, self.world.dim_p)
if env_index is not None
else (self.world.batch_dim, self.world.dim_p),
if self.start_same_point:
for agent in self.world.agents:
agent.set_pos(
torch.zeros(
(1, 2) if env_index is not None else (self.world.batch_dim, 2),
device=self.world.device,
dtype=torch.float,
),
batch_index=env_index,
)
ScenarioUtils.spawn_entities_randomly(
self.world.landmarks,
self.world,
env_index,
self.goal_radius + self.agent_radius + 0.01,
x_bounds=(-1, 1),
y_bounds=(-1, 1),
occupied_positions=torch.zeros(
1 if env_index is not None else self.world.batch_dim,
1,
2,
device=self.world.device,
dtype=torch.float32,
).uniform_(
-1.0,
1.0,
dtype=torch.float,
),
batch_index=env_index,
)
for landmark in self.world.landmarks:
# Random pos between -1 and 1
landmark.set_pos(
torch.zeros(
(1, self.world.dim_p)
if env_index is not None
else (self.world.batch_dim, self.world.dim_p),
device=self.world.device,
dtype=torch.float32,
).uniform_(
-1.0,
1.0,
),
batch_index=env_index,
else:
ScenarioUtils.spawn_entities_randomly(
self.world.policy_agents + self.world.landmarks,
self.world,
env_index,
self.goal_radius + self.agent_radius + 0.01,
x_bounds=(-1, 1),
y_bounds=(-1, 1),
)

for landmark in self.world.landmarks:
if env_index is None:
landmark.eaten = torch.full(
(self.world.batch_dim,), False, device=self.world.device
Expand Down
20 changes: 13 additions & 7 deletions vmas/scenarios/give_way.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Copyright (c) 2022-2023.
# Copyright (c) 2022-2024.
# ProrokLab (https://www.proroklab.org/)
# All rights reserved.
import math

import torch

from vmas import render_interactively
from vmas.simulator.core import Agent, World, Landmark, Sphere, Line, Box
from vmas.simulator.scenario import BaseScenario
Expand All @@ -21,6 +20,7 @@ def make_world(self, batch_dim: int, device: torch.device, **kwargs):
self.linear_friction = kwargs.get("linear_friction", 0.1)
self.mirror_passage = kwargs.get("mirror_passage", False)
self.done_on_completion = kwargs.get("done_on_completion", False)
self.observe_rel_pos = kwargs.get("observe_rel_pos", False)

# Reward params
self.pos_shaping_factor = kwargs.get("pos_shaping_factor", 1.0)
Expand Down Expand Up @@ -63,7 +63,7 @@ def make_world(self, batch_dim: int, device: torch.device, **kwargs):

# Add agents
blue_agent = Agent(
name="blue_agent_0",
name="agent_0",
rotatable=False,
linear_friction=self.linear_friction,
shape=Sphere(radius=self.agent_radius)
Expand All @@ -79,7 +79,7 @@ def make_world(self, batch_dim: int, device: torch.device, **kwargs):
blue_agent, world, controller_params, "standard"
)
blue_goal = Landmark(
name="blue goal",
name="goal_0",
collide=False,
shape=Sphere(radius=self.agent_radius / 2),
color=Color.BLUE,
Expand All @@ -89,7 +89,7 @@ def make_world(self, batch_dim: int, device: torch.device, **kwargs):
world.add_landmark(blue_goal)

green_agent = Agent(
name="green_agent_0",
name="agent_1",
color=Color.GREEN,
linear_friction=self.linear_friction,
shape=Sphere(radius=self.agent_radius)
Expand All @@ -106,7 +106,7 @@ def make_world(self, batch_dim: int, device: torch.device, **kwargs):
green_agent, world, controller_params, "standard"
)
green_goal = Landmark(
name="green goal",
name="goal_1",
collide=False,
shape=Sphere(radius=self.agent_radius / 2),
color=Color.GREEN,
Expand Down Expand Up @@ -302,11 +302,17 @@ def reward(self, agent: Agent):
)

def observation(self, agent: Agent):
rel = []
for a in self.world.agents:
if a != agent:
rel.append(agent.state.pos - a.state.pos)

observations = [
agent.state.pos,
agent.state.vel,
agent.state.pos,
]
if self.observe_rel_pos:
observations += rel

if self.obs_noise > 0:
for i, obs in enumerate(observations):
Expand Down
Loading