weighted puddles, fixed wall destination

This commit is contained in:
Makrellka 2022-04-28 20:34:03 +02:00
parent 7324fc47d9
commit 718362190f
5 changed files with 20 additions and 150 deletions

View File

@ -11,7 +11,7 @@ from PatchAgent import PatchAgent
from PatchType import PatchType from PatchType import PatchType
from data.GameConstants import GameConstants from data.GameConstants import GameConstants
from decision.ActionType import ActionType from decision.ActionType import ActionType
from pathfinding.dlaMarcina import PathFinderOnStates, PathFinderState from pathfinding.Pathfinder import PathFinderOnStates, PathFinderState
from util.PathDefinitions import GridLocation, GridWithWeights from util.PathDefinitions import GridLocation, GridWithWeights
@ -43,7 +43,7 @@ class GameModel(Model):
self.grid.place_agent(self.forklift_agent, (x, y)) self.grid.place_agent(self.forklift_agent, (x, y))
self.forklift_agent.current_position = (x, y) self.forklift_agent.current_position = (x, y)
start, goal = (x, y), (8, 8) start, goal = (x, y), (2, 3)
pathFinder = PathFinderOnStates( pathFinder = PathFinderOnStates(
self.game_constants, self.game_constants,

View File

@ -13,7 +13,7 @@ class GameConstants:
# order_pos: GridLocation, # order_pos: GridLocation,
# special_positions: Dict[ItemType, GridLocation], # special_positions: Dict[ItemType, GridLocation],
walls: [GridLocation], walls: [GridLocation],
puddles: [GridLocation] diffTerrain: [GridLocation]
): ):
self.grid_width = grid_width self.grid_width = grid_width
self.grid_height = grid_height self.grid_height = grid_height
@ -21,4 +21,4 @@ class GameConstants:
# self.order_pos = order_pos # self.order_pos = order_pos
# self.special_positions = special_positions # self.special_positions = special_positions
self.walls = walls self.walls = walls
self.puddles = puddles self.diffTerrain = diffTerrain

View File

@ -58,10 +58,10 @@ if __name__ == '__main__':
scale = base / gridWidth scale = base / gridWidth
diagram4 = GridWithWeights(gridWidth, gridHeight) 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 = 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) grid = CanvasGrid(agent_portrayal, gridWidth, gridHeight, scale * gridWidth, scale * gridHeight)

View File

@ -70,6 +70,9 @@ class PathFinderOnStates:
return True return True
def createState(self, currState: PathFinderState, action: ActionType) -> PathFinderState: def createState(self, currState: PathFinderState, action: ActionType) -> PathFinderState:
if currState.agent_position in self.game_constants.diffTerrain:
cost = currState.cost + 5
else:
cost = currState.cost + 1 cost = currState.cost + 1
last_action = action last_action = action
action_taken: List[ActionType] = [] action_taken: List[ActionType] = []
@ -97,30 +100,29 @@ class PathFinderOnStates:
# generowanie stanu # generowanie stanu
# sprawdz w ktorym kierunku obrocony # sprawdz w ktorym kierunku obrocony
possibleNextStates: List[PathFinderState] = [] possibleNextStates: List[PathFinderState] = []
if self.isMovePossible(currState):
possibleNextStates.append(self.createState(currState, ActionType.MOVE))
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):
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))
possibleNextStates.append(self.createState(currState, ActionType.ROTATE_UP)) 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: elif currState.agent_direction == Direction.left:
possibleNextStates.append(self.createState(currState, ActionType.ROTATE_RIGHT)) possibleNextStates.append(self.createState(currState, ActionType.ROTATE_RIGHT))
possibleNextStates.append(self.createState(currState, ActionType.ROTATE_UP)) possibleNextStates.append(self.createState(currState, ActionType.ROTATE_UP))
possibleNextStates.append(self.createState(currState, ActionType.ROTATE_DOWN)) 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: elif currState.agent_direction == Direction.right:
possibleNextStates.append(self.createState(currState, ActionType.ROTATE_UP)) 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):
possibleNextStates.append(self.createState(currState, ActionType.MOVE))
return possibleNextStates return possibleNextStates
def getActionList(self) -> List[ActionType]: def getActionList(self) -> List[ActionType]:
@ -130,10 +132,10 @@ class PathFinderOnStates:
item: PrioritizedItem = self.queue.get() item: PrioritizedItem = self.queue.get()
best_state: PathFinderState = item.item 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 break
# dodajesz do kolejki stany z expansion (po cost)
for state in self.expansion(best_state): for state in self.expansion(best_state):
s_tuple = (state.agent_position[0], state.agent_position[1], state.agent_direction) s_tuple = (state.agent_position[0], state.agent_position[1], state.agent_direction)
@ -143,26 +145,3 @@ class PathFinderOnStates:
already_visited[s_tuple] = state already_visited[s_tuple] = state
return best_state.action_taken 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

View File

@ -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)