diff --git a/GameModel.py b/GameModel.py index 1630c8c..3ee570d 100644 --- a/GameModel.py +++ b/GameModel.py @@ -11,7 +11,7 @@ from PatchAgent import PatchAgent from PatchType import PatchType from data.GameConstants import GameConstants from decision.ActionType import ActionType -from pathfinding.dlaMarcina import PathFinderOnStates, PathFinderState +from pathfinding.Pathfinder import PathFinderOnStates, PathFinderState from util.PathDefinitions import GridLocation, GridWithWeights @@ -43,7 +43,7 @@ class GameModel(Model): self.grid.place_agent(self.forklift_agent, (x, y)) self.forklift_agent.current_position = (x, y) - start, goal = (x, y), (8, 8) + start, goal = (x, y), (2, 3) pathFinder = PathFinderOnStates( self.game_constants, diff --git a/data/GameConstants.py b/data/GameConstants.py index a54b75e..a6da7c9 100644 --- a/data/GameConstants.py +++ b/data/GameConstants.py @@ -13,7 +13,7 @@ class GameConstants: # order_pos: GridLocation, # special_positions: Dict[ItemType, GridLocation], walls: [GridLocation], - puddles: [GridLocation] + diffTerrain: [GridLocation] ): self.grid_width = grid_width self.grid_height = grid_height @@ -21,4 +21,4 @@ class GameConstants: # self.order_pos = order_pos # self.special_positions = special_positions self.walls = walls - self.puddles = puddles + self.diffTerrain = diffTerrain diff --git a/main.py b/main.py index 1339552..17cec37 100644 --- a/main.py +++ b/main.py @@ -58,10 +58,10 @@ if __name__ == '__main__': scale = base / gridWidth diagram4 = GridWithWeights(gridWidth, gridHeight) - diagram4.walls = [(6, 5), (6, 6), (6, 7), (6, 8), (2, 3), (2, 4)] + diagram4.walls = [(6, 5), (6, 6), (6, 7), (6, 8), (2, 3), (2, 4), (3, 4), (4, 4), (6, 4)] diagram5 = GridWithWeights(gridWidth, gridHeight) - diagram5.puddles = [(2, 2), (2, 5)] + diagram5.puddles = [(2, 2), (2, 5), (5, 4)] grid = CanvasGrid(agent_portrayal, gridWidth, gridHeight, scale * gridWidth, scale * gridHeight) diff --git a/pathfinding/dlaMarcina.py b/pathfinding/Pathfinder.py similarity index 77% rename from pathfinding/dlaMarcina.py rename to pathfinding/Pathfinder.py index 4d51508..77f7e57 100644 --- a/pathfinding/dlaMarcina.py +++ b/pathfinding/Pathfinder.py @@ -43,7 +43,7 @@ class PathFinderOnStates: def evaluate(self, currState: PathFinderState) -> float: # koszt dojscia do danego stanu+ heura - 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: if currState.agent_direction == Direction.top: @@ -70,7 +70,10 @@ class PathFinderOnStates: return True def createState(self, currState: PathFinderState, action: ActionType) -> PathFinderState: - cost = currState.cost + 1 + if currState.agent_position in self.game_constants.diffTerrain: + cost = currState.cost + 5 + else: + cost = currState.cost + 1 last_action = action action_taken: List[ActionType] = [] action_taken.extend(currState.action_taken) @@ -97,30 +100,29 @@ class PathFinderOnStates: # generowanie stanu # sprawdz w ktorym kierunku obrocony possibleNextStates: List[PathFinderState] = [] + if self.isMovePossible(currState): + possibleNextStates.append(self.createState(currState, ActionType.MOVE)) + if currState.agent_direction == Direction.top: possibleNextStates.append(self.createState(currState, ActionType.ROTATE_RIGHT)) possibleNextStates.append(self.createState(currState, ActionType.ROTATE_LEFT)) possibleNextStates.append(self.createState(currState, ActionType.ROTATE_DOWN)) - if self.isMovePossible(currState): - possibleNextStates.append(self.createState(currState, ActionType.MOVE)) + elif currState.agent_direction == Direction.down: possibleNextStates.append(self.createState(currState, ActionType.ROTATE_RIGHT)) possibleNextStates.append(self.createState(currState, ActionType.ROTATE_LEFT)) possibleNextStates.append(self.createState(currState, ActionType.ROTATE_UP)) - if self.isMovePossible(currState): - possibleNextStates.append(self.createState(currState, ActionType.MOVE)) + elif currState.agent_direction == Direction.left: possibleNextStates.append(self.createState(currState, ActionType.ROTATE_RIGHT)) possibleNextStates.append(self.createState(currState, ActionType.ROTATE_UP)) possibleNextStates.append(self.createState(currState, ActionType.ROTATE_DOWN)) - if self.isMovePossible(currState): - possibleNextStates.append(self.createState(currState, ActionType.MOVE)) + elif currState.agent_direction == Direction.right: possibleNextStates.append(self.createState(currState, ActionType.ROTATE_UP)) possibleNextStates.append(self.createState(currState, ActionType.ROTATE_LEFT)) possibleNextStates.append(self.createState(currState, ActionType.ROTATE_DOWN)) - if self.isMovePossible(currState): - possibleNextStates.append(self.createState(currState, ActionType.MOVE)) + return possibleNextStates def getActionList(self) -> List[ActionType]: @@ -130,10 +132,10 @@ class PathFinderOnStates: item: PrioritizedItem = self.queue.get() best_state: PathFinderState = item.item - if best_state.agent_position == self.goal: + if best_state.agent_position == self.goal or (self.heuristic(best_state.agent_position, self.goal) == 1 + and self.goal in self.game_constants.walls): break - # dodajesz do kolejki stany z expansion (po cost) for state in self.expansion(best_state): s_tuple = (state.agent_position[0], state.agent_position[1], state.agent_direction) @@ -143,26 +145,3 @@ class PathFinderOnStates: already_visited[s_tuple] = state return best_state.action_taken - - # do kosztu dokładam koszt starego stanu plus 1 - # def a_star(self,stateTree:StateTree, start: Tuple[int, int], goal: Tuple[int, int]): - # frontier = PriorityQueue() - # frontier.put(start, 0) - # came_from = dict() - # cost_so_far = dict() - # came_from[start] = None - # cost_so_far[start] = 0 - # - # while not frontier.empty(): - # current = frontier.get() - # - # if current == goal: - # break - # - # for next in graph.neighbors(current): - # new_cost = cost_so_far[current] + graph.cost(current, next) - # if next not in cost_so_far or new_cost < cost_so_far[next]: - # cost_so_far[next] = new_cost - # priority = new_cost + heuristic(goal, next) - # frontier.put(next, priority) - # came_from[next] = current diff --git a/pathfinding/nastepnik.py b/pathfinding/nastepnik.py deleted file mode 100644 index 800d054..0000000 --- a/pathfinding/nastepnik.py +++ /dev/null @@ -1,109 +0,0 @@ -from enum import Enum -from typing import Tuple, Dict - -from data import GameConstants, Direction - -def getHotSpot(game:GameConstants) -> (int, int): - pass - -def isOrderReady(game:GameConstants) -> bool: - pass - -def getReadyOrderId(game:GameConstants) -> int: - pass - -class Action(): - - - def __init__(self, game: GameConstants): - self.game = GameConstants - - def rotate(self, direction: Direction) -> GameConstants: - self.game.agentDirection = direction - pass - - def move(self) -> GameConstants: - # w zaleznosci od kierunku napierdala do przodu - pass - - def special(self) -> GameConstants: - # w zaleznosci od miejsca gdzie jest i czy ma cos na lapie odklada albo bierze przedmiot - pass - - def orderOut(self, orderId: int) -> GameConstants: - # nalicza punkty wypierdalaorder i czysci orderStock - pass - -def heuristic(a: Tuple[int, int], b: Tuple[int, int]) -> float: - (x1, y1) = a - - (x2, y2) = b - return abs(x1 - x2) + abs(y1 - y2) - -class PossibleMoves(Enum): - move = 1 - rotateLeft = 2 - rotateDown = 3 - rotateRight = 4 - rotateTop = 5 - - -def getRotationEvaluation(direction: Direction): - # get evaluationForMoveAfterRotation - return 1.0 - -def evaluateMoves(game: GameConstants) -> Dict: - posibleMoves = Dict - currPos = game.agentPos - gameCopy = game.getCopy() - getRotationEvaluation() - posibleMoves[PossibleMoves.move] = heuristic(currPos, Action(gameCopy).move().agentPos) - posibleMoves[PossibleMoves.rotateTop] = getRotationEvaluation(Direction.Direction.top) - posibleMoves[PossibleMoves.rotateLeft] = getRotationEvaluation(Direction.Direction.left) - posibleMoves[PossibleMoves.rotateDown] = getRotationEvaluation(Direction.Direction.down) - posibleMoves[PossibleMoves.rotateRight] = getRotationEvaluation(Direction.Direction.right) - return posibleMoves - -def getIndex(value: float, dict: Dict) -> PossibleMoves: - for key, val in dict: - if val == value: - return key - -def getMaxFromList(list: [float]) -> float: - maxi = -10000000 - for i in range(len(list)): - if list[i] > maxi: - maxi = list[i] - - return maxi - -def getBestPossibleMove(game: GameConstants) -> PossibleMoves: - movesDict = evaluateMoves() - bestChoice = getMaxFromList(movesDict.values()) - return getIndex(bestChoice, movesDict) - -def getBestMove(game: GameConstants) -> GameConstants: - gameCopy = game.getCopy() - bestPossibleMove = getBestPossibleMove(gameCopy) - - if bestPossibleMove == PossibleMoves.move: - return Action(gameCopy).move() - elif bestPossibleMove == PossibleMoves.rotateTop: - return Action(gameCopy).rotate(Direction.Direction.top) - elif bestPossibleMove == PossibleMoves.rotateLeft: - return Action(gameCopy).rotate(Direction.Direction.left) - elif bestPossibleMove == PossibleMoves.rotateDown: - return Action(gameCopy).rotate(Direction.Direction.down) - else: - return Action(gameCopy).rotate(Direction.Direction.right) - - - -def nastepnik(game: GameConstants) -> GameConstants: - if isOrderReady(game) > -1: - return Action(game.getCopy()).orderOut(getReadyOrderId(game)) - elif game.agentPos == getHotSpot(game): - return Action(game.getCopy()).special() - else: - return getBestMove(game) -