minor fixes

This commit is contained in:
Makrellka 2022-04-28 00:29:29 +02:00
parent 4caef2e9ae
commit 3c5dfc2e34

View File

@ -49,28 +49,28 @@ class PathFinderOnStates:
return currState.cost + self.heuristic(currState.agent_position, self.goal) return currState.cost + self.heuristic(currState.agent_position, self.goal)
def getPositionAfterMove(self, currState: PathFinderState) -> GridLocation: def getPositionAfterMove(self, currState: PathFinderState) -> GridLocation:
result : GridLocation result: GridLocation = None
if currState.agent_position == Direction.top: if currState.agent_position == Direction.top:
currState.agent_position[0] += 1
result = currState.agent_position
elif currState.agent_position == Direction.down:
currState.agent_position[0] -= 1
result= currState.agent_position
elif currState.agent_position == Direction.right:
currState.agent_position[1] += 1 currState.agent_position[1] += 1
result = currState.agent_position result = currState.agent_position
elif currState.agent_position == Direction.right: elif currState.agent_position == Direction.down:
currState.agent_position[1] -= 1 currState.agent_position[1] -= 1
result = currState.agent_position result = currState.agent_position
elif currState.agent_position == Direction.right:
currState.agent_position[0] += 1
result = currState.agent_position
elif currState.agent_position == Direction.left:
currState.agent_position[0] -= 1
result = currState.agent_position
return result return result
def isMovePossible(self, currState: PathFinderState)-> bool: def isMovePossible(self, currState: PathFinderState) -> bool:
positionAfterMove =self.getPositionAfterMove(currState) positionAfterMove = self.getPositionAfterMove(currState)
if positionAfterMove in self.game_constants.walls: if positionAfterMove in self.game_constants.walls:
return False return False
elif positionAfterMove in self.game_constants.grid_height: elif positionAfterMove[0] < 0 or positionAfterMove[0] > self.game_constants.grid_width:
return False return False
elif positionAfterMove in self.game_constants.grid_width: elif positionAfterMove[1] < 0 or positionAfterMove[1] > self.game_constants.grid_height:
return False return False
else: else:
return True return True
@ -78,7 +78,8 @@ class PathFinderOnStates:
def createState(self, currState: PathFinderState, action: ActionType) -> PathFinderState: def createState(self, currState: PathFinderState, action: ActionType) -> PathFinderState:
cost = currState.cost + 1 cost = currState.cost + 1
last_action = action last_action = action
action_taken = currState.action_taken.append(action) action_taken: List[ActionType] = []
action_taken.extend(currState.action_taken)
agent_position = currState.agent_position agent_position = currState.agent_position
agent_direction = currState.agent_direction agent_direction = currState.agent_direction
@ -100,13 +101,13 @@ class PathFinderOnStates:
# reprezentacja kazdego stanu co moge podjac z tego miejsca # reprezentacja kazdego stanu co moge podjac z tego miejsca
# generowanie stanu # generowanie stanu
# sprawdz w ktorym kierunku obrocony # sprawdz w ktorym kierunku obrocony
possibleNextStates: List[PathFinderState] possibleNextStates: List[PathFinderState] = []
if currState.agent_direction == Direction.top: if currState.agent_direction == Direction.top:
possibleNextStates.append(self.createState(currState, ActionType.ROTATE_RIGHT)) possibleNextStates.append(self.createState(currState, ActionType.ROTATE_RIGHT))
possibleNextStates.append(self.createState(currState, ActionType.ROTATE_LEFT)) possibleNextStates.append(self.createState(currState, ActionType.ROTATE_LEFT))
possibleNextStates.append(self.createState(currState, ActionType.ROTATE_DOWN)) possibleNextStates.append(self.createState(currState, ActionType.ROTATE_DOWN))
if self.isMovePossible(currState): if self.isMovePossible(currState):
possibleNextStates.append(self.createState(currState,ActionType.MOVE)) possibleNextStates.append(self.createState(currState, ActionType.MOVE))
elif currState.agent_direction == Direction.down: elif currState.agent_direction == Direction.down:
possibleNextStates.append(self.createState(currState, ActionType.ROTATE_RIGHT)) possibleNextStates.append(self.createState(currState, ActionType.ROTATE_RIGHT))
possibleNextStates.append(self.createState(currState, ActionType.ROTATE_LEFT)) possibleNextStates.append(self.createState(currState, ActionType.ROTATE_LEFT))
@ -120,7 +121,7 @@ class PathFinderOnStates:
if self.isMovePossible(currState): if self.isMovePossible(currState):
possibleNextStates.append(self.createState(currState, ActionType.MOVE)) possibleNextStates.append(self.createState(currState, ActionType.MOVE))
elif currState.agent_direction == Direction.right: elif currState.agent_direction == Direction.right:
possibleNextStates.append(self.createState(currState, ActionType.ROTATE_UPT)) possibleNextStates.append(self.createState(currState, ActionType.ROTATE_UP))
possibleNextStates.append(self.createState(currState, ActionType.ROTATE_LEFT)) possibleNextStates.append(self.createState(currState, ActionType.ROTATE_LEFT))
possibleNextStates.append(self.createState(currState, ActionType.ROTATE_DOWN)) possibleNextStates.append(self.createState(currState, ActionType.ROTATE_DOWN))
if self.isMovePossible(currState): if self.isMovePossible(currState):
@ -131,7 +132,7 @@ class PathFinderOnStates:
best_state: PathFinderState = self.queue.get() best_state: PathFinderState = self.queue.get()
while best_state.agent_position != self.goal and not self.queue.empty(): while best_state.agent_position != self.goal and not self.queue.empty():
#dodajesz do kolejki stany z expansion (po cost) # dodajesz do kolejki stany z expansion (po cost)
for state in self.expansion(best_state): for state in self.expansion(best_state):
self.queue.put(state, self.evaluate(state)) self.queue.put(state, self.evaluate(state))