Projekt_AI-Automatyczny_saper/Engine/GeneticFinder.py

59 lines
2.0 KiB
Python
Raw Normal View History

2021-06-14 22:20:20 +02:00
from Engine.Node import Node
from Engine.PathFinder import PathFinder
class GeneticFinder(PathFinder):
def __init__(self, board, dTree):
super().__init__(board, dTree)
self.board = board
self.openList = []
self.cameFrom = {}
self.gScore = {}
self.fScore = {}
def bombDistance(self,startState, endState):
x = Node(startState, None)
self.openList.append(x)
self.gScore[x] = 0
self.fScore[x] = 0
self.endState = endState
cameFrom = dict()
cameFrom[x] = None
while self.openList:
current = self.minKey(self.fScore, self.openList)
if self.checkGoal(current):
return self.constructActions(current, startState)
self.openList.remove(current)
for next in self.getNeighbour(current):
tentativeGScore = self.gScore.get(current) + self.weight(next)
if tentativeGScore < self.gScore.get(next, float('inf')):
self.cameFrom[next] = current
next.parent = current
self.gScore[next] = tentativeGScore
self.fScore[next] = tentativeGScore + self.heuristic(startState, next)
if next not in self.openList:
self.openList.append(next)
return []
def checkGoal(self, current):
if current.state.getPoint().__eq__(self.endState.getPoint()):
bomb = self.board.getBomb(current.state.getPoint())
bombMapping = bomb.getMapping()
if(bombMapping[0] != None):
actionOnBomb = self.dTree.mapAction(self.dTree.getTree().predict([bombMapping])[0])
self.goal = Node(current, actionOnBomb)
else:
self.goal = Node(current, None)
return True
return False
def getDistance(self,startState, endState):
distance = 0
for state in self.bombDistance(startState,endState):
distance += 1
return distance