def update_viy_pos(self): positions = [] for i in range(self.agent_map.map_size.height): for j in range(self.agent_map.map_size.width): if self.agent_map[Position(i, j)] == P_VIY: positions.append(Position(i, j)) if len(positions) == 1: self.agent_map[positions[0]] = VIY
def get_min_pos(self): min_pos = Position(0, 0) for i in range(len(self.map)): for j in range(len(self.map[i])): if self.map[i][j] < self.map[min_pos.x][ min_pos.y] and self.pos.x != i and self.pos.y != j: min_pos = Position(i, j) return min_pos
def __init__(self, map: Map): self.world_map = map self.agent_map = self.get_agent_map() # position self.x = 1 self.y = 1 self.world_map.agent_pos = Position(self.x, self.y) self.agent_map.agent_pos = Position(self.x, self.y) self.can_kick = True
def update_map(self): """ Analizes agent's sensors and updates map """ self.agent_map[self.pos] = self.world_map[self.pos] self.update_positions(self.check_pos(Position(self.x + 1, self.y))) self.update_positions(self.check_pos(Position(self.x - 1, self.y))) self.update_positions(self.check_pos(Position(self.x, self.y + 1))) self.update_positions(self.check_pos(Position(self.x, self.y - 1))) self.update_viy_pos()
def get_moves(self, check_func): moves = [] if check_func(Position(self.pos.x - 1, self.pos.y)): moves.append(self.go_left) if check_func(Position(self.pos.x + 1, self.pos.y)): moves.append(self.go_right) if check_func(Position(self.pos.x, self.pos.y - 1)): moves.append(self.go_bottom) if check_func(Position(self.pos.x, self.pos.y + 1)): moves.append(self.go_top) return moves
def kick(self, pos): if self.world_map[pos] == VIY and self.can_kick: self.world_map[pos] = EMPTY for i in range(self.agent_map.map_size.height): for j in range(self.agent_map.map_size.width): if self.agent_map[Position(i, j)] == P_VIY: self.agent_map[Position(i, j)] = ANY elif self.agent_map[Position(i, j)] == VIY: self.agent_map[Position(i, j)] = SAFE print('VIY KILLED') self.can_kick = False
def get_available_moves(self): moves = [] def check_move(pos): if self.is_available_pos(pos): moves.append(pos) check_move(Position(self.x + 1, self.y)) check_move(Position(self.x - 1, self.y)) check_move(Position(self.x, self.y + 1)) check_move(Position(self.x, self.y - 1)) return moves
def dp(self, node: Node, temp_matrix: list): cur_node = temp_matrix[node.pos.x][node.pos.y] if cur_node.total_weight / cur_node.count > node.total_weight / node.count: node.visited = True temp_matrix[node.pos.x][node.pos.y] = node positions = (Position(node.pos.x - 1, node.pos.y), Position(node.pos.x + 1, node.pos.y), Position(node.pos.x, node.pos.y - 1), Position(node.pos.x, node.pos.y + 1)) for pos in positions: if not temp_matrix[pos.x][pos.y].visited: new_node = Node(from_node=node, count=node.count + 1, total_weight=node.total_weight + self.map[pos.x][pos.y], pos=pos) self.dp(new_node, temp_matrix)
def build_temp_matrix(self): temp = [] for i in range(len(self.map)): temp.append([]) for j in range(len(self.map[i])): node = Node() if not self.env.check_cell(Position(i, j)): node.visited = True temp[-1].append(node) return temp
def go_bottom(self): self._move(Position(self.pos.x, self.pos.y - 1))
def go_top(self): self._move(Position(self.pos.x, self.pos.y + 1))
def go_right(self): self._move(Position(self.pos.x + 1, self.pos.y))
def go_left(self): self._move(Position(self.pos.x - 1, self.pos.y))
def update_positions(self, item): self.update_pos(Position(self.x + 1, self.y), item) self.update_pos(Position(self.x - 1, self.y), item) self.update_pos(Position(self.x, self.y + 1), item) self.update_pos(Position(self.x, self.y - 1), item)
def pos(self): return Position(self.x, self.y)
def move(self, pos): self.x = pos.x self.y = pos.y self.world_map.agent_pos = Position(self.x, self.y) self.agent_map.agent_pos = Position(self.x, self.y)