python - 无法在 Python 中解压不可迭代的方法对象错误
问题描述
我正在为 Python 中的 A* 搜索构建可视化工具,并在尝试计算节点的 g 分数时遇到以下错误。
line 115, in g
x1, y1 = s
TypeError: cannot unpack non-iterable method object
请参见下面的代码:
from graphics import *
import pygame, sys
import math
import numpy as np
from queue import PriorityQueue
pygame.init()
# #set the screen size and captions
size = (500, 500)
rows = 50
margin = 1
screen = pygame.display.set_mode(size)
pygame.display.set_caption("A* Pathfinding")
screen.fill((173, 172, 166))
#define colors
aqua = (0, 255, 255)
black = (0, 0, 0)
blue = (0, 0, 255)
green = (0, 128, 0)
red = (255, 0, 0)
white = (255, 255, 255)
purple = (128, 0, 128)
#initialize PriorityQueue
q = PriorityQueue()
#define all possible states of each node
class Node:
def __init__(self, row, col, width):
self.row = row
self.col = col
self.x = row*width
self.y = col*width
self.color = white
self.width = width
self.neighbors = [(col+1, row), (col-1, row), (col, row+1), (col, row-1)]
#self.neighbors = []
def get_pos(self):
return self.row, self.col
def is_closed(self):
return self.color == red
def is_open(self):
return self.color == aqua
def is_barrier(self):
return self.color == black
def is_start(self):
return self.color == green
def is_end(self):
return self.color == blue
def reset(self):
return self.color == white
def close(self):
self.color = red
def open_node(self):
self.color = blue
def barrier(self):
self.color = black
def start(self):
self.color = green
def end(self):
self.color = aqua
def path(self):
self.color = purple
def init_grid():
board = []
for i in range(rows):
board.append([])
for j in range(rows):
node = Node(i, j, size[0]/rows)
board[i].append(node)
return board
#heuristic
def h(c, end):
x1, y1 = c
x2, y2 = end
return abs(x1 - x2) + abs(y1 - y2)
#distance between current node and start node
def g(c, s):
x1, y1 = s
x2, y2 = c
return abs(x1 - x2) + abs(y1 - y2)
board = init_grid()
barrier = []
frontier = {}
path = {}
#starting conditions using Node class and associated methods
start = board[5][5]
goal = board[30][35]
current = start
count = 0
q.put(0, (start))
#main game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#allows user to draw barriers
elif pygame.mouse.get_pressed()[0]:
pos = pygame.mouse.get_pos()
y_pos = int(pos[0] // (board[0][0].width + margin))
x_pos = int(pos[1] // (board[0][0].width + margin))
board[x_pos][y_pos].barrier()
barrier.append(board[x_pos][y_pos].get_pos())
#draw the grid
for i in range(rows):
for j in range(rows):
color = board[i][j].color
#print(board[i][j].color)
if board[i][j] == start:
board[i][j].start()
elif board[i][j] == goal:
board[i][j].end()
pygame.draw.rect(screen, color, [j*(board[0][0].width + margin), i*(board[0][0].width + margin), board[0][0].width, board[0][0].width])
#game loop
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
while not q.empty():
for neighbor in current.neighbors:
g_temp = g(current.get_pos, start.get_pos) + 1
if g_temp < g(neighbor, start.get_pos()):
f = g_temp + h(current.get_pos(), goal.get_pos())
if neighbor not in frontier:
q.put((f, neighbor))
frontier.add(neighbor)
board[neighbor[0]][neighbor[1]].open_node()
if current != goal:
new = q.get()[1]
path.add(current)
current = board[new[0]][new[1]]
if current == goal:
break
pygame.display.flip()
pygame.quit()
我试过调试这个问题,但我不明白为什么它给了我这个错误。该函数应该接受两个元组参数并返回一个整数作为 g 分数,我相信这是我在游戏循环中通过的。
如果我在这里遗漏了一些明显的错误,请告诉我,我还是编程新手。非常感谢您的时间和帮助!
解决方案
你有一段很长的代码。
看来您没有将元组传递给 g()。
我发现了至少一个错误:您必须在方法调用之后添加“()”,否则您将方法名称而不是方法值传递给 g()。
您的代码中的示例:
g_temp = g(current.get_pos, start.get_pos) + 1
应该:
g_temp = g(current.get_pos(), start.get_pos()) + 1
这大概就是问题所在。我没有检查所有其他行,因为这段代码很长;)测试并回来告诉我们。
推荐阅读
- flutter - 在 Navigator 2.0 中使用 bloc
- google-chrome - 创建按钮以下载 vcard 并打开联系人应用程序
- c++ - dds pub 很晚才收到不匹配通知.. 可以对其进行调整以使其更快吗?
- r - 用动态规划算法实现 R 中的矩阵
- laravel - InvalidArgumentException 无法找到组件 [queryUrl-sort] 的类或视图 [Forrestedw\QueryUrlBuilder\Sort]
- python - 如何定义一个函数,不管你是否调用类
- reactjs - 如何在 ReactJS 中匹配 ID
- python - 具有不同序列长度的 Matplotlib 时间序列线图
- python - 使用json从pandas数据框中提取str
- python - 使用opencv从侧面检测马的眨眼