Merge pull request 'Zmiana błędu w konstruktorze successor' (#37) from minor_upgrades into master
Reviewed-on: #37
This commit is contained in:
commit
b19778b796
22
bfs.py
22
bfs.py
@ -8,16 +8,16 @@ from queue import Queue, PriorityQueue
|
|||||||
from turnCar import turn_left_orientation, turn_right_orientation
|
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:
|
def __init__(self, state: AgentState, action: AgentActionType, cost: int, predicted_cost: int) -> None:
|
||||||
self.state = state
|
self.state = state
|
||||||
self.action = action
|
self.action = action
|
||||||
self.cost = cost
|
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]
|
succ_list: list[Successor]
|
||||||
|
|
||||||
def __init__(self, succ_list: list[Successor]) -> None:
|
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[
|
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] = []
|
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))]))
|
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()
|
current = queue.get()
|
||||||
previous = current.succ_list[-1]
|
previous = current.succ_list[-1]
|
||||||
visited.append(previous.state)
|
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)
|
return extract_actions(current)
|
||||||
|
|
||||||
successors = get_successors(previous, grid, city)
|
successors = get_successors(previous, grid, city)
|
||||||
@ -61,7 +61,7 @@ def find_path_to_nearest_can(startState: AgentState, grid: Dict[Tuple[int, int],
|
|||||||
return []
|
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] = []
|
output: list[AgentActionType] = []
|
||||||
for s in successors.succ_list:
|
for s in successors.succ_list:
|
||||||
if s.action != AgentActionType.UNKNOWN:
|
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]:
|
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_cost = 1 + succ.cost
|
||||||
turn_left_state = AgentState(succ.state.position, turn_left_orientation(succ.state.orientation))
|
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:
|
def is_state_success(state: AgentState, grid: Dict[Tuple[int, int], GridCellType]) -> bool:
|
||||||
next_cell = get_next_cell(state)
|
next_cell = get_next_cell(state)
|
||||||
try:
|
try:
|
||||||
return grid[next_cell] == GridCellType.GARBAGE_CAN
|
return grid[next_cell] == GridCellType.GARBAGE_CAN # agent dotarł do śmietnika
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return False
|
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]:
|
if action in [AgentActionType.TURN_LEFT, AgentActionType.TURN_RIGHT]:
|
||||||
return 1
|
return 1
|
||||||
if cell_type == GridCellType.SPEED_BUMP and action == AgentActionType.MOVE_FORWARD:
|
if cell_type == GridCellType.SPEED_BUMP and action == AgentActionType.MOVE_FORWARD:
|
||||||
return 10
|
return -10000
|
||||||
if action == AgentActionType.MOVE_FORWARD:
|
if action == AgentActionType.MOVE_FORWARD:
|
||||||
return 3
|
return 3
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user