List Index out of range bei A* Wege finden.
Verfasst: Sonntag 11. Februar 2018, 10:27
Ich schreibe gerade ein Programm welches ein Bild einscannt und den Weg zwischen zwei Punkten finden. Dazu habe ich jetzt auch schon den Code. Da sind aber noch ein paar Fehler drin wo ich keine Ahnung habe wie ich sie zu beheben habe. Hier erstmal der Code:
Ich habe das ganze Bild einscannen der einfachheitshalber weggenommen.Größtenteils bekomme ich da den Fehler wo ich das # gemacht habe. Nämlich 'List Index out of range'
Auf jeden Fall Danke im Voraus,
Bananasplit
Code: Alles auswählen
import numpy as np
from PIL import Image
import sys
import AAStar
import heapq
sys.setrecursionlimit(sys.maxsize)
weiss = (255,255,255)
schwarz = (0,0,0)
grun = (0,48,0)
rot = (48,0,0)
grau1 = (207,207,207)
grau2 = (43,43,43)
grau3 = (27,27,27)
grau4 = (23,23,23)
liste = []
walls = []
c1 = 0
c2 = 0
rechlist = []
rotlist = []
start = []
reachable = False
s = 0
r=0
nmap = []
bigwalls = []
func1 = 1
func2 = 1
func3 = 1
func4 = 1
index =1
Bild = Image.open('quax.png')
quaxerg = Bild.copy
w, h = Bild.size
Bildk = Bild.resize((int(w*0.5), int(h*0.5)),Image.BICUBIC)
Bildk.save('b.png')
w = w * 0.5
h = h * 0.5
h = int(h)
w = int(w)
nmap = np.array([
[0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,1,1,1,1,1,0,1,1,1,1,1,0,1],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,0,1,1,1,1,1,1,1,1,1,1,1,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,1,1,1,1,1,0,1,1,1,1,1,0,1],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,0,1,1,1,1,1,1,1,0,1,1,1,1],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,1,1,1,1,1,1,1,1,1,1,1,0,1],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0]])
class AStar():
def __init__(self, grid, x, y, goal_x, goal_y) :
self.grid = grid
self.initial_x = x
self.initial_y = y
self.x = self.initial_x
self.y = self.initial_y
self.goal_x = goal_x
self.goal_y = goal_y
self.neighbors = []
self.neighbor()
def neighbor(self):
print ('Ich bin dort!')
if ((self.x == self.goal_x) and (self.y == self.goal_y)) == True:
print('your done')
return 'your done'
self.grid[self.x][self.y] == 1
if self.x > 0:
if self.grid[self.x-1] [self.y] == 0:
self.neighbors.append([self.x - 1, self.y])
if self.x < self.grid.shape[0] - 1:
if self.grid[self.x+1][self.y] == 0:
self.neighbors.append([self.x+1,self.y])
if self.y > 0:
if self.grid[self.x][self.y - 1] == 0:
self.neighbors.append([self.x, self.y - 1])
if self.y < self.grid.shape[1] - 1:
if self.grid[self.x][self.y + 1] == 0:
self.neighbors.append([self.x, self.y + 1])
self.f_value()
def f_value(self):
print ('Ich bin da!')
h_values = []
g_values = []
for i in self.neighbors:
x_distance = abs(i[0] - self.goal_x)
y_distance = abs(i[1] - self.goal_y)
h_value = (x_distance + y_distance)
h_values.append(h_value)
for i in self.neighbors:
x_distance = abs(i[0] - self.initial_x)
y_distance = abs(i[1] - self.initial_y)
g_value = (x_distance + y_distance)
g_values.append(g_value)
self.f_values = [h + g for h, g in zip(h_values, g_values)]
self.path()
return (self.f_values)
def path(self):
global index
print ('Ich bin hier!')
for coordinate, i in enumerate(self.f_values):
if i <= min(self.f_values):
value = i
index = coordinate
self.x = self.neighbors[index][0] #
self.y = self.neighbors[index][1]
print('f_values:', self.f_values)
print('neighbors:', self.neighbors)
print('x:', self.x)
print('y:', self.y)
self.neighbors.pop(index)
self.f_values.pop(index)
self.neighbor()
Test = AStar(nmap, c1,c2,s,r)
func2 = Test.neighbor()
func3 = Test.f_value()
func4 = Test.path()
print (func2,func3,func4,'ich funktioniere')
Auf jeden Fall Danke im Voraus,
Bananasplit