def get_free_pos_limited( grid, pos: GridPos, radius: int = 1, metric: EnvironmentMetric = EnvironmentMetric.EUCLIDEAN, ) -> GridPos: """ Get a free position on the grid only [radius] from the passed position. Autor: Benjamin Eder :param grid: :param pos: :param radius: :param metric: :return: """ possible_positions = [] env_size = radius * 2 + 1 grid_size = grid.get_size() cur_row = int(pos.row()) cur_col = int(pos.col()) if metric == EnvironmentMetric.MANHATTAN: for r in range(0, env_size): offset = abs(radius - r) check_row = cur_row - radius + r for c in range(offset, env_size - offset): check_column = cur_col - radius + c possible_positions.append((check_row, check_column)) elif metric == EnvironmentMetric.EUCLIDEAN: for r in range(0, env_size): check_row = cur_row - radius + r for c in range(0, env_size): check_column = cur_col - radius + c distance = np.round(np.sqrt((radius - r)**2 + (radius - c)**2)) if 0 < distance <= radius: possible_positions.append((check_row, check_column)) else: raise ValueError('Metric not implemented') # Filter positions that are no more in the Grid or are already used possible_positions = list( filter( lambda pos: 0 <= pos[0] < grid_size and 0 <= pos[1] < grid_size and (pos[0] != cur_row or pos[1] != cur_col) and not grid.is_occupied( GridPos(np.uint(pos[0]), np.uint(pos[1]))), possible_positions)) if len(possible_positions) == 0: raise ValueError("No free positions available. ") random_choice = random.choice(possible_positions) return GridPos(np.uint(random_choice[0]), np.uint(random_choice[1]))
def update_gui_state(self, grid_pos: GridPos, agent_state: AgentState) -> None: self.trigger_gui_event( Events.AGENT_CHANGE_GUI, { "row": grid_pos.row(), "col": grid_pos.col(), "state": agent_state.value }) self.__logger.debug( f"Agent update, at position {grid_pos.row()},{grid_pos.col()}, new state: {agent_state}" )
def set_agent(self, agent: Agent, grid_pos: GridPos) -> None: self.__grid[grid_pos.row()][grid_pos.col()] = agent
def get_agent(self, grid_pos: GridPos) -> Agent: return self.__grid[grid_pos.row()][grid_pos.col()]