Spaces
Grid Space
- class econsimulacra.spaces.base.GridSpace(config, registered_classes, prng)[source]
Bases:
objectGrid space containing agents and extensible cell information.
- get_cell(pos)[source]
Get cell information at a position.
- Parameters:
pos (tuple[int, ...]) – Position whose cell information is requested.
- Returns:
A safe copy of the effective cell information.
- Return type:
Cell
Note
Positions without explicit overrides inherit
default_cell.
- get_cell_attrs(pos)[source]
Get arbitrary attributes at a position.
- Parameters:
pos (tuple[int, ...]) – Position whose attributes are requested.
- Returns:
A shallow copy of the cell attribute mapping.
- Return type:
Note
Mutating the returned dictionary does not mutate the grid. Use
update_cell_attrsfor runtime interventions.
- update_cell_access(pos, traversable=None, spawnable=None)[source]
Update built-in access controls at a position.
- Parameters:
- Returns:
None.
- Return type:
None
Note
This method supports interventions such as temporary road closures. Existing agents at the position are not displaced.
- update_cell_attrs(pos, updates)[source]
Merge arbitrary attributes into a cell.
- Parameters:
- Returns:
None.
- Return type:
None
Note
Attribute values are deliberately not interpreted by
GridSpace.
- can_spawn(agent_id, pos)[source]
Determine whether an agent may initially spawn at a position.
- Parameters:
- Returns:
Whether the cell permits initial placement.
- Return type:
Note
The base implementation only checks
CellAccess.spawnable. Subclasses may useagent_idand arbitrary attributes to add rules.
- can_enter(agent_id, current_pos, new_pos)[source]
Determine whether an agent may enter a position.
- Parameters:
- Returns:
Whether the candidate cell permits traversal.
- Return type:
Note
The base implementation only checks
CellAccess.traversable. The other arguments are extension points for agent-specific rules.
- iter_neighbors(agent_id, pos)[source]
Yield positions reachable by one geometric movement.
- Parameters:
- Returns:
In-bounds neighboring positions.
- Return type:
Note
The base implementation uses a Moore neighborhood to preserve the existing diagonal movement behavior. Subclasses may override it for mobility-specific neighborhoods.
- get_spawnable_positions(agent_id)[source]
Collect valid initial-position candidates for an agent.
- Parameters:
agent_id (int) – ID of the agent being placed.
- Returns:
Spawnable cells satisfying colocation settings.
- Return type:
Note
When initial colocation is disabled, occupied positions are excluded.
- get_pos(agent_id)[source]
Get an agent’s current position.
- Parameters:
agent_id (int) – ID of the agent whose position is requested.
- Returns:
The agent’s current position.
- Return type:
Note
ValueErroris raised when the agent is not in this space.
- get_agents(pos)[source]
Get agent IDs at a position.
- Parameters:
- Returns:
A copy of the agent IDs at the position.
- Return type:
Note
Returning a copy prevents callers from mutating spatial indexes.
- place_agent(agent_id, pos)[source]
Place an agent at an explicit or randomly selected initial position.
- Parameters:
- Returns:
None.
- Return type:
None
Note
Both explicit and random placement respect
spawnableandallowInitialColocatedAgents.
- get_colocated_agents(agent_id)[source]
Get other agents sharing an agent’s current position.
- Parameters:
agent_id (int) – ID of the reference agent.
- Returns:
IDs of other agents at the same position.
- Return type:
Note
The reference agent is excluded from the returned set.
- get_near_agents(center_pos, max_distance=1)[source]
Get agent IDs within a Manhattan-distance radius.
- Parameters:
- Returns:
IDs of agents within the specified radius.
- Return type:
Note
For compatibility, the center must currently contain at least one agent.
max_distancemust be non-negative.
- get_nearby_info(agent_id, max_distance=1)[source]
Get a mapping of nearby positions to their cell information.
- Parameters:
- Returns:
Mapping from positions to cell information.
- Return type:
dict[Position, Cell]
Note
The reference agent’s position is included.
max_distancemust be non-negative.
- remove_agent(agent_id)[source]
Remove an agent from the grid space.
- Parameters:
agent_id (int) – ID of the agent to remove.
- Returns:
None.
- Return type:
None
Note
The recorded initial position is retained for compatibility with movement-history consumers.
- move_agent(agent_id, new_pos)[source]
Move an agent directly to a traversable position.
- Parameters:
- Returns:
None.
- Return type:
None
Note
This low-level operation validates bounds and traversal access but does not require adjacency and does not reject colocated agents.
- move_many_agents(agent_id2new_pos)[source]
Move multiple agents after validating every target position.
- Parameters:
agent_id2new_pos (dict[int, tuple[int, ...]]) – Mapping from agent IDs to direct movement targets.
- Returns:
None.
- Return type:
None
Note
Validation occurs before mutation to avoid partial updates. Colocation is intentionally allowed during movement.
- calc_next_pos(current_pos, destination_pos, velocity=1, agent_id=None)[source]
Calculate the next position along a shortest traversable path.
- Parameters:
- Returns:
- The position reached this step, or
None when no traversable path exists.
- The position reached this step, or
- Return type:
Note
velocitymust be a positive integer. Intermediate cells remain part of the path, so higher velocity cannot skip over obstacles.
- calc_next_path(current_pos, destination_pos, velocity=1, agent_id=None)[source]
Calculate the path segment traversed during the current step.
- Parameters:
- Returns:
- Path segment including the current
position, or
Nonewhen no traversable path exists.
- Return type:
Note
Returning the segment lets callers calculate exact per-cell resource consumption while
calc_next_pospreserves its existing interface.