Projekt_AI-Automatyczny_saper/Engine/PathFinder.py

67 lines
1.9 KiB
Python
Raw Normal View History

2021-04-27 22:01:31 +02:00
from Constants import ROWS, COLS, MOVE
2021-04-27 21:10:01 +02:00
from Engine.BfsPathFinder import BfsPathFinder
from Engine.Node import Node
2021-04-12 22:54:33 +02:00
from Engine.Point import Point
2021-04-27 21:10:01 +02:00
from queue import PriorityQueue
2021-04-12 22:54:33 +02:00
2021-04-27 21:10:01 +02:00
class PathFinder(BfsPathFinder):
2021-04-12 22:54:33 +02:00
2021-05-18 00:21:14 +02:00
def __init__(self, board, dTree):
super().__init__(board, dTree)
2021-04-12 22:54:33 +02:00
self.board = board
self.openList = []
self.cameFrom = {}
self.gScore = {}
self.fScore = {}
2021-04-27 21:10:01 +02:00
def findBomb(self, startState):
x = Node(startState, None)
self.openList.append(x)
self.gScore[x] = 0
self.fScore[x] = 0
cameFrom = dict()
cameFrom[x] = None
2021-04-27 21:34:05 +02:00
while self.openList:
2021-04-27 21:10:01 +02:00
current = self.minKey(self.fScore, self.openList)
if self.checkGoal(current):
return self.constructActions(current, startState)
2021-04-27 21:34:05 +02:00
self.openList.remove(current)
2021-04-27 21:10:01 +02:00
for next in self.getNeighbour(current):
2021-04-27 22:01:31 +02:00
tentativeGScore = self.gScore.get(current) + self.weight(next)
2021-04-27 22:29:50 +02:00
if tentativeGScore < self.gScore.get(next, float('inf')):
2021-04-27 21:34:05 +02:00
self.cameFrom[next] = current
2021-04-27 21:10:01 +02:00
next.parent = current
self.gScore[next] = tentativeGScore
2021-04-27 22:01:31 +02:00
self.fScore[next] = tentativeGScore + self.heuristic(startState, next)
2021-04-27 21:34:05 +02:00
if next not in self.openList:
self.openList.append(next)
2021-04-12 22:54:33 +02:00
return []
2021-04-27 22:01:31 +02:00
def heuristic(self,startState,next):
return next.state.getPoint().distance(startState.getPoint())
2021-04-12 22:54:33 +02:00
def minKey(self, fscore, openlist):
2021-04-27 21:10:01 +02:00
minkey = Node
2021-04-12 22:54:33 +02:00
minValue = float('inf')
2021-04-27 21:10:01 +02:00
for node in openlist:
value = fscore.get(node, 10000)
2021-04-12 22:54:33 +02:00
if value < minValue:
minValue = value
2021-04-27 21:10:01 +02:00
minkey = node
2021-04-12 22:54:33 +02:00
return minkey
2021-04-27 22:01:31 +02:00
def weight(self, next):
2021-04-27 22:29:50 +02:00
if next.state.getPoint() in self.board.mudMap and next.action == MOVE:
return 10
2021-04-27 22:01:31 +02:00
return 1
2021-04-12 22:54:33 +02:00