AI/AStar.py

92 lines
3.4 KiB
Python
Raw Normal View History

2020-04-28 12:18:15 +02:00
import numpy
from AStarState import AStarState
class AStar:
def returnPath(self, currentNode, grid):
2020-04-28 12:18:15 +02:00
path = []
noRows, noColumns = numpy.shape(grid)
result = [[-1 for i in range(noColumns)] for j in range(noRows)]
current = currentNode
while current is not None:
path.append(current.position)
current = current.parent
path = path[::-1]
startValue = 0
for i in range(len(path)):
result[path[i][0]][path[i][1]] = startValue
startValue+=1
return result
######################################################### w current jest koszt do przebycia
2020-05-15 13:20:04 +02:00
def search(self, start, end, grid, cost, flaga): #start to kordy wózka, end miejsce podjęcia paczki
2020-04-28 12:18:15 +02:00
noRows, noColumns = numpy.shape(grid)
startNode = AStarState(None, tuple(start))
endNode = AStarState(None, tuple(end))
startNode.g = 0
startNode.h = 0
startNode.f = 0
endNode.g = 0
endNode.h = 0
endNode.f = 0
toVisit = []
visited = []
toVisit.append(startNode)
iterations = 0
max = (len(grid) // 2) ** 10
moves = [[-1, 0], [1, 0], [0, 1], [0, -1]]
while len(toVisit)>0:
iterations=iterations+1
current = toVisit[0]
currentIndeks = 0
for indeks, item in enumerate(toVisit):
if item.g<current.g:
current = item
currentIndeks = indeks
if iterations>max and flaga == 0: #podprojekt genetyczne
return current.g
2020-04-28 12:18:15 +02:00
if iterations>max:
return self.returnPath(current, grid)
visited.append(current)
toVisit.pop(currentIndeks)
if current==endNode and flaga == 0:
return current.g
2020-04-28 12:18:15 +02:00
if current==endNode:
return self.returnPath(current, grid) #zwracanie wagi przejscia
2020-04-28 12:18:15 +02:00
children = []
for new in moves:
positions = (current.position[0]+new[0], current.position[1]+new[1])
2020-04-28 12:18:15 +02:00
if (positions[0] > (noRows - 1) or
positions[0] < 0 or
positions[1] > (noColumns - 1) or
positions[1] < 0):
continue
""" podprojekt genetyczne"""
if grid[positions[0]][positions[1]] == 2 and flaga == 0:
children.append(AStarState(current, positions))
continue
2020-04-28 12:18:15 +02:00
if grid[positions[0]][positions[1]]!=0:
continue
2020-04-28 12:18:15 +02:00
children.append(AStarState(current, positions))
for child in children:
2020-04-28 12:18:15 +02:00
if len([visitedChild for visitedChild in visited if visitedChild==child])>0:
continue
2020-04-28 12:18:15 +02:00
if child.position[0]<=(len(grid)-4) and child.position[0]>=3 and child.position[1]>=4 and child.position[1]<=(len(grid[0])-1):
child.g = current.g + (10 * cost)
else:
child.g = current.g + cost
child.h = (((child.position[0]-endNode.position[0]) ** 2) + ((child.position[1]-endNode.position[1]) ** 2))
2020-06-09 17:57:00 +02:00
child.f = child .g + child.h
2020-04-28 12:18:15 +02:00
if len([i for i in toVisit if child==i and child.g>i.g])>0:
continue
2020-04-28 12:18:15 +02:00
toVisit.append(child)