def move_to_empty(self, agent: Agent) -> None: """ Moves agent to a random empty cell, vacating agent's old cell. """ pos = agent.pos if len(self.empties) == 0: raise Exception("ERROR: No empty cells") new_pos = agent.random.choice(sorted(self.empties)) self._place_agent(new_pos, agent) agent.pos = new_pos self._remove_agent(pos, agent)
def move_agent(self, agent: Agent, pos: FloatCoordinate) -> None: """Move an agent from its current position to a new position. Args: agent: The agent object to move. pos: Coordinate tuple to move the agent to. """ pos = self.torus_adj(pos) idx = self._agent_to_index[agent] self._agent_points[idx, 0] = pos[0] self._agent_points[idx, 1] = pos[1] agent.pos = pos
def move_agent(self, agent: Agent, pos: Coordinate) -> None: """ Move an agent from its current position to a new position. Args: agent: Agent object to move. Assumed to have its current location stored in a 'pos' tuple. pos: Tuple of new position to move the agent to. """ pos = self.torus_adj(pos) self._remove_agent(agent.pos, agent) self._place_agent(pos, agent) agent.pos = pos
def place_agent(self, agent: Agent, pos: FloatCoordinate) -> None: """Place a new agent in the space. Args: agent: Agent object to place. pos: Coordinate tuple for where to place the agent. """ pos = self.torus_adj(pos) if self._agent_points is None: self._agent_points = np.array([pos]) else: self._agent_points = np.append(self._agent_points, np.array([pos]), axis=0) self._index_to_agent[self._agent_points.shape[0] - 1] = agent self._agent_to_index[agent] = self._agent_points.shape[0] - 1 agent.pos = pos
def remove_agent(self, agent: Agent) -> None: """Remove an agent from the simulation. Args: agent: The agent object to remove """ if agent not in self._agent_to_index: raise Exception("Agent does not exist in the space") idx = self._agent_to_index[agent] del self._agent_to_index[agent] max_idx = max(self._index_to_agent.keys()) # Delete the agent's position and decrement the index/agent mapping self._agent_points = np.delete(self._agent_points, idx, axis=0) for a, index in self._agent_to_index.items(): if index > idx: self._agent_to_index[a] = index - 1 self._index_to_agent[index - 1] = a # The largest index is now redundant del self._index_to_agent[max_idx] agent.pos = None
def position_agent( self, agent: Agent, x: Union[int, str] = "random", y: Union[int, str] = "random" ) -> None: """Position an agent on the grid. This is used when first placing agents! Use 'move_to_empty()' when you want agents to jump to an empty cell. Use 'swap_pos()' to swap agents positions. If x or y are positive, they are used, but if "random", we get a random position. Ensure this random position is not occupied (in Grid). """ if x == "random" or y == "random": if len(self.empties) == 0: raise Exception("ERROR: Grid full") coords = agent.random.choice(sorted(self.empties)) else: coords = (x, y) agent.pos = coords self._place_agent(coords, agent)
def move_agent(self, agent: Agent, node_id: int) -> None: """ Move an agent from its current node to a new node. """ self._remove_agent(agent, agent.pos) self._place_agent(agent, node_id) agent.pos = node_id
def place_agent(self, agent: Agent, node_id: int) -> None: """ Place a agent in a node. """ self._place_agent(agent, node_id) agent.pos = node_id
def remove_agent(self, agent: Agent) -> None: """ Remove the agent from the grid and set its pos variable to None. """ pos = agent.pos self._remove_agent(pos, agent) agent.pos = None
def place_agent(self, agent: Agent, pos: Coordinate) -> None: """ Position an agent on the grid, and set its pos variable. """ self._place_agent(pos, agent) agent.pos = pos