This notebook gathers the functions creating different kinds of environments for foraging and target search in various scenarios, adapted for their use in the reinforcement learning paradigm.
Libs
TargetEnv
/opt/hostedtoolcache/Python/3.12.16/x64/lib/python3.12/site-packages/fastcore/docments.py:499: UserWarning: docments unavailable for TargetEnv: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
warnings.warn(f'docments unavailable for {self.nm}: {type(e).__name__}: {e}')
def TargetEnv_timedelay( Nt:int=10, # Number of targets. L:float=1.3, # Size of the (squared) world. r:float=1.5, # Radius with center the target position. It defines the area in which agent detects the target. agent_step:int=1, # Displacement of one step. The default is 1. num_agents:int=1, # Number of agents that forage at the same time. The default is 1. > 1 not fully implemented tau:int=5, # Time threshold for target depletion):
Class defining the a Foraging environment with multiple targets and two actions: continue in the same direction and turn by a random angle.
ResetEnv
Search loop with fixed policy an arbitrary environment
def ResetEnv_2D( dist_target:float=0.2, # Distance from init position and target radius_target:float=0.5, # Radius of the target D:float=1.0, # Diffusion coefficient of the walker):
def TurnResetEnv_2D( dist_target:float=0.2, # Distance from init position and target radius_target:float=0.5, # Radius of the target agent_step:float=1.0, # Diffusion coefficient of the walker):
Class defining a Foraging environment with a single target and three possible actions:
Continue in the same direction
Turn by a random angle
Reset to the origin
The agent makes steps of constant length given by agent_step.
Given the positions of agents and targets, and a radius r, this function checks which agents are within radius r of each target.
If shared_depletion=True (default): at most one agent is assigned per target (random selection among those within radius), preserving competition — targets are globally depleted so only one reward per target per step.
If shared_depletion=False: each agent is independently assigned to its nearest target within radius, with no competition — multiple agents can encounter the same target in the same step.
Each agent is assigned to at most one target. Returns an array agent_to_target where agent_to_target[i] is the target index for agent i, or -1 if none.
Optimized loop-based version of check_collective_encounter_og. No heap allocations: no reflected lists, no set, no (Na x Nt) distance matrix. Uses squared distances to avoid sqrt.
If shared_depletion=True: for each target, one random agent within radius is selected; an agent can only be assigned to one target (first-come wins). If shared_depletion=False: each agent is assigned to its nearest target within radius independently (deterministic).
Returns agent_to_target[i] = target index for agent i, or -1 if none.
Calculate the number of agents within the cone in the frontal direction for each agent.
Parameters: - positions: np.ndarray of shape (num_agents, 2), positions of all agents. - directions: np.ndarray of shape (num_agents,), directions of all agents in radians. - k: float, visual length of the cone. - theta: float, visual angle of the cone in radians.
Returns: - np.ndarray of shape (num_agents,), number of agents in the cone for each agent.
def CollectiveEnv( num_agents:int=5, # Number of agents. Nt:int=10, # Number of targets. L:float=1.3, # Size of the (squared) world. r:float=1.5, # Radius with center the target position. It defines the area in which agent detects the target. tau:int=5, # Time it takes for the targets to replenish. agent_step:int=1, # Displacement of one step. The default is 1. visual_range:float=2.0, # Visual range up to which one agent can see another. visual_angle:float=1.5707963267948966, # Visual angle within which one agent can see another. shared_depletion:bool=True, # If True, targets are depleted for all agents when one agent finds it. If False, targets are depleted only for the agent that finds it. The default is True. tau_reward:int=1, # Number of update_pos calls after which rewarded_agents falls back to zero. upd_pos_method:str='RND', # Method to update position. 'RND' for random angle turns, 'LR' for left/right turns. turn_angle:float=0.7853981633974483, # Angle for left/right turns if upd_pos_method is 'LR'.):
Class defining the a Foraging environment with multiple targets and two actions: continue in the same direction and turn by a random angle.
def CollectiveDirectionsEnv( num_agents:int=5, # Number of agents. Nt:int=10, # Number of targets. L:float=1.3, # Size of the (squared) world. r:float=1.5, # Radius with center the target position. It defines the area in which agent detects the target. tau:int=5, # Time it takes for the targets to replenish. agent_step:int=1, # Displacement of one step. The default is 1. visual_range:float=2.0, # Visual range up to which one agent can see another. visual_angle:float=1.5707963267948966, # Visual angle within which one agent can see another. shared_depletion:bool=True, # If True, targets are depleted for all agents when one agent finds it. If False, targets are depleted only for the agent that finds it. The default is True. tau_reward:int=1, # Number of update_pos calls after which rewarded_agents falls back to zero. upd_pos_method:str='RND', # Method to update position. 'RND' for random angle turns, 'LR' for left/right turns. turn_angle:float=0.7853981633974483, # Angle for left/right turns if upd_pos_method is 'LR'.):
Class defining the a Foraging environment with multiple targets and two actions: continue in the same direction and turn by a random angle.
Collective with fixed agents in target positions
We use this one to match the approximation to the theory, where agents can see targets. We place some agents in the target positions, they are always tagged as reward and they do not move.
def CollectiveEnv_target_visual( num_agents:int=5, # Number of moving agents. Nt:int=10, # Number of targets. L:float=1.3, # Size of the (squared) world. r:float=1.5, # Radius with center the target position. It defines the area in which agent detects the target. tau:int=5, # Time it takes for the targets to replenish. agent_step:int=1, # Displacement of one step. The default is 1. visual_range:float=2.0, # Visual range up to which one agent can see another. visual_angle:float=1.5707963267948966, # Visual angle within which one agent can see another. shared_depletion:bool=True, # If True, targets are depleted for all moving agents when one agent finds it. tau_reward:int=1, # Number of update_pos calls after which rewarded_agents falls back to zero. upd_pos_method:str='RND', # Method to update position. 'RND' for random angle turns, 'LR' for left/right turns. turn_angle:float=0.7853981633974483, # Angle for left/right turns if upd_pos_method is 'LR'.):
Class defining a foraging environment with moving agents plus Nt ancilla agents fixed at target positions. Ancilla are always rewarded and are only used for visual cones.