From 15839b4bc4707ce8fdc3068acf1dc12bcaa5736c Mon Sep 17 00:00:00 2001 From: majkellll Date: Mon, 19 Jun 2023 10:19:05 +0200 Subject: [PATCH] =?UTF-8?q?Zmiana=20b=C5=82=C4=99du=20w=20konstruktorze=20?= =?UTF-8?q?successor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bfs.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/bfs.py b/bfs.py index 711ed8a..28f96b5 100644 --- a/bfs.py +++ b/bfs.py @@ -8,16 +8,16 @@ from queue import Queue, PriorityQueue from turnCar import turn_left_orientation, turn_right_orientation -class Successor: +class Successor: # klasa reprezentuje sukcesora, stan i akcję którą można po nim podjąć def __init__(self, state: AgentState, action: AgentActionType, cost: int, predicted_cost: int) -> None: self.state = state self.action = action self.cost = cost - self.predicted_cost = cost + self.predicted_cost = predicted_cost -class SuccessorList: +class SuccessorList: # lista sukcesorów, czyli możliwych ścieżek po danym stanie succ_list: list[Successor] def __init__(self, succ_list: list[Successor]) -> None: @@ -31,17 +31,17 @@ class SuccessorList: def find_path_to_nearest_can(startState: AgentState, grid: Dict[Tuple[int, int], GridCellType], city: City) -> List[ - AgentActionType]: + AgentActionType]: # znajduje ścieżkę do najbliższego kosza na smieci visited: List[AgentState] = [] - queue: PriorityQueue[SuccessorList] = PriorityQueue() + queue: PriorityQueue[SuccessorList] = PriorityQueue() # kolejka priorytetowa przechodująca listę sukcesorów queue.put(SuccessorList([Successor(startState, AgentActionType.UNKNOWN, 0, _heuristics(startState.position, city))])) - while not queue.empty(): + while not queue.empty(): # dopóki kolejka nie jest pusta, pobiera z niej aktualny element current = queue.get() previous = current.succ_list[-1] visited.append(previous.state) - if is_state_success(previous.state, grid): + if is_state_success(previous.state, grid): # jeśli ostatni stan w liście jest stanem końcowym (agent dotarł do śmietnika) return extract_actions(current) successors = get_successors(previous, grid, city) @@ -61,7 +61,7 @@ def find_path_to_nearest_can(startState: AgentState, grid: Dict[Tuple[int, int], return [] -def extract_actions(successors: SuccessorList) -> list[AgentActionType]: +def extract_actions(successors: SuccessorList) -> list[AgentActionType]: # wyodrębnienie akcji z listy sukcesorów, z pominięciem uknown output: list[AgentActionType] = [] for s in successors.succ_list: if s.action != AgentActionType.UNKNOWN: @@ -70,7 +70,7 @@ def extract_actions(successors: SuccessorList) -> list[AgentActionType]: def get_successors(succ: Successor, grid: Dict[Tuple[int, int], GridCellType], city: City) -> List[Successor]: - result: List[Successor] = [] + result: List[Successor] = [] # generuje następników dla danego stanu, turn_left_cost = 1 + succ.cost turn_left_state = AgentState(succ.state.position, turn_left_orientation(succ.state.orientation)) @@ -128,7 +128,7 @@ def get_next_cell(state: AgentState) -> Tuple[int, int]: def is_state_success(state: AgentState, grid: Dict[Tuple[int, int], GridCellType]) -> bool: next_cell = get_next_cell(state) try: - return grid[next_cell] == GridCellType.GARBAGE_CAN + return grid[next_cell] == GridCellType.GARBAGE_CAN # agent dotarł do śmietnika except KeyError: return False @@ -137,7 +137,7 @@ def get_cost_for_action(action: AgentActionType, cell_type: GridCellType) -> int if action in [AgentActionType.TURN_LEFT, AgentActionType.TURN_RIGHT]: return 1 if cell_type == GridCellType.SPEED_BUMP and action == AgentActionType.MOVE_FORWARD: - return 10 + return -10000 if action == AgentActionType.MOVE_FORWARD: return 3