Skip to content

Commit

Permalink
param docs
Browse files Browse the repository at this point in the history
  • Loading branch information
matteobettini committed Dec 22, 2024
1 parent 7916afc commit ebb451c
Showing 1 changed file with 86 additions and 60 deletions.
146 changes: 86 additions & 60 deletions vmas/scenarios/football.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,80 +28,49 @@


class Scenario(BaseScenario):
def make_world(self, batch_dim: int, device: torch.device, **kwargs):
self.init_params(**kwargs)
world = self.init_world(batch_dim, device)
self.init_agents(world)
self.init_ball(world)
self.init_background(world)
self.init_walls(world)
self.init_goals(world)
self.init_traj_pts(world)
self.left_goal_pos = torch.tensor(
[-self.pitch_length / 2 - self.ball_size / 2, 0],
device=device,
dtype=torch.float,
)
self.right_goal_pos = -self.left_goal_pos
self._done = torch.zeros(batch_dim, device=device, dtype=torch.bool)
self._sparse_reward_blue = torch.zeros(
batch_dim, device=device, dtype=torch.float32
)
self._sparse_reward_red = self._sparse_reward_blue.clone()
self._render_field = True
self.min_agent_dist_to_ball_blue = None
self.min_agent_dist_to_ball_red = None

self._reset_agent_range = torch.tensor(
[self.pitch_length / 2, self.pitch_width],
device=device,
)
self._reset_agent_offset_blue = torch.tensor(
[-self.pitch_length / 2 + self.agent_size, -self.pitch_width / 2],
device=device,
)
self._reset_agent_offset_red = torch.tensor(
[-self.agent_size, -self.pitch_width / 2], device=device
)
self._agents_rel_pos_to_ball = None
return world

def reset_world_at(self, env_index: int = None):
self.reset_agents(env_index)
self.reset_ball(env_index)
self.reset_walls(env_index)
self.reset_goals(env_index)
self.reset_controllers(env_index)
if env_index is None:
self._done[:] = False
else:
self._done[env_index] = False

def init_params(self, **kwargs):
# Scenario config
self.viewer_size = kwargs.pop("viewer_size", (1200, 800))

# Agents config
self.n_blue_agents = kwargs.pop("n_blue_agents", 3)
self.n_red_agents = kwargs.pop("n_red_agents", 3)
# What agents should be learning and what controlled by the heuristic (ai)
self.ai_red_agents = kwargs.pop("ai_red_agents", True)
self.ai_blue_agents = kwargs.pop("ai_blue_agents", False)

# When you have 5 blue agents there is the options of introducing physical differences with the following roles:
# 1 goalkeeper -> slow and big
# 2 defenders -> normal size and speed (agent_size, u_multiplier, max_speed)
# 2 attackers -> small and fast
self.physically_different = kwargs.pop("physically_different", False)

# Agent spawning
self.spawn_in_formation = kwargs.pop("spawn_in_formation", False)
self.only_blue_formation = kwargs.pop("only_blue_formation", True)
self.only_blue_formation = kwargs.pop(
"only_blue_formation", True
) # Only spawn blue agents in formation
self.formation_agents_per_column = kwargs.pop("formation_agents_per_column", 2)
self.randomise_formation_indices = kwargs.pop(
"randomise_formation_indices", False
)
self.formation_noise = kwargs.pop("formation_noise", 0.2)
) # If False, each agent will always be in the same formation spot
self.formation_noise = kwargs.pop(
"formation_noise", 0.2
) # Noise on formation positions

# Ai config
self.n_traj_points = kwargs.pop("n_traj_points", 0)
self.ai_speed_strength = kwargs.pop("ai_strength", 1.0)
self.ai_decision_strength = kwargs.pop("ai_decision_strength", 1.0)
self.ai_precision_strength = kwargs.pop("ai_precision_strength", 1.0)
self.n_traj_points = kwargs.pop(
"n_traj_points", 0
) # Number of spline trajectory points to plot for heuristic (ai) agents
self.ai_speed_strength = kwargs.pop(
"ai_strength", 1.0
) # The speed of the ai 0<=x<=1
self.ai_decision_strength = kwargs.pop(
"ai_decision_strength", 1.0
) # The decision strength of the ai 0<=x<=1
self.ai_precision_strength = kwargs.pop(
"ai_precision_strength", 1.0
) # The precision strength of the ai 0<=x<=1
self.disable_ai_red = kwargs.pop("disable_ai_red", False)

# Task sizes
Expand All @@ -117,7 +86,9 @@ def init_params(self, **kwargs):
self.u_multiplier = kwargs.pop("u_multiplier", 0.1)

# Actions shooting
self.enable_shooting = kwargs.pop("enable_shooting", False)
self.enable_shooting = kwargs.pop(
"enable_shooting", False
) # Whether to enable an extra 2 actions (for rotation and shooting). Only avaioable for non-ai agents
self.u_rot_multiplier = kwargs.pop("u_rot_multiplier", 0.0003)
self.u_shoot_multiplier = kwargs.pop("u_shoot_multiplier", 0.6)
self.shooting_radius = kwargs.pop("shooting_radius", 0.08)
Expand All @@ -131,12 +102,16 @@ def init_params(self, **kwargs):
self.dense_reward = kwargs.pop("dense_reward", True)
self.pos_shaping_factor_ball_goal = kwargs.pop(
"pos_shaping_factor_ball_goal", 10.0
)
) # Reward for moving the ball towards the opponents' goal. This can be annealed in a curriculum.
self.pos_shaping_factor_agent_ball = kwargs.pop(
"pos_shaping_factor_agent_ball", 0.1
)
) # Reward for moving the closest agent to the ball in a team closer to it.
# This is useful for exploration and can be annealed in a curriculum.
# This reward does not trigger if the agent is less than distance_to_ball_trigger from the ball or the ball is moving
self.distance_to_ball_trigger = kwargs.pop("distance_to_ball_trigger", 0.4)
self.scoring_reward = kwargs.pop("scoring_reward", 100.0)
self.scoring_reward = kwargs.pop(
"scoring_reward", 100.0
) # Discrete reward for scoring

# Observations
self.observe_teammates = kwargs.pop("observe_teammates", True)
Expand All @@ -150,6 +125,57 @@ def init_params(self, **kwargs):
)
ScenarioUtils.check_kwargs_consumed(kwargs)

def make_world(self, batch_dim: int, device: torch.device, **kwargs):
self.init_params(**kwargs)
world = self.init_world(batch_dim, device)
self.init_agents(world)
self.init_ball(world)
self.init_background(world)
self.init_walls(world)
self.init_goals(world)
self.init_traj_pts(world)

# Cached values
self.left_goal_pos = torch.tensor(
[-self.pitch_length / 2 - self.ball_size / 2, 0],
device=device,
dtype=torch.float,
)
self.right_goal_pos = -self.left_goal_pos
self._done = torch.zeros(batch_dim, device=device, dtype=torch.bool)
self._sparse_reward_blue = torch.zeros(
batch_dim, device=device, dtype=torch.float32
)
self._sparse_reward_red = self._sparse_reward_blue.clone()
self._render_field = True
self.min_agent_dist_to_ball_blue = None
self.min_agent_dist_to_ball_red = None

self._reset_agent_range = torch.tensor(
[self.pitch_length / 2, self.pitch_width],
device=device,
)
self._reset_agent_offset_blue = torch.tensor(
[-self.pitch_length / 2 + self.agent_size, -self.pitch_width / 2],
device=device,
)
self._reset_agent_offset_red = torch.tensor(
[-self.agent_size, -self.pitch_width / 2], device=device
)
self._agents_rel_pos_to_ball = None
return world

def reset_world_at(self, env_index: int = None):
self.reset_agents(env_index)
self.reset_ball(env_index)
self.reset_walls(env_index)
self.reset_goals(env_index)
self.reset_controllers(env_index)
if env_index is None:
self._done[:] = False
else:
self._done[env_index] = False

def init_world(self, batch_dim: int, device: torch.device):
# Make world
world = World(
Expand Down

0 comments on commit ebb451c

Please sign in to comment.