Traceback (most recent call last): File "E:\1作业与文件\极地建筑\大作业\最短路径.py", line 75, in <module> path = astar_search(start, goal, grid) File "E:\1作业与文件\极地建筑\大作业\最短路径.py", line 29, in astar_search priority = new_cost + heuristic_cost_estimate(goal, next) File "E:\1作业与文件\极地建筑\大作业\最短路径.py", line 7, in heuristic_cost_estimate return np.linalg.norm(start - goal) TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'
时间: 2023-10-06 22:09:12 浏览: 45
这个错误提示显示 `np.linalg.norm()` 函数中的参数类型不是数组或向量,而是元组。这可能是因为在调用 `astar_search()` 函数时,传入的参数 `start` 和 `goal` 是元组类型,而不是数组或向量类型。
要解决这个问题,您可以将 `start` 和 `goal` 转换为 NumPy 数组类型,或者在 `astar_search()` 函数中使用元组进行计算,而不是使用 `np.linalg.norm()` 函数。
如果您想将 `start` 和 `goal` 转换为 NumPy 数组类型,请使用 `np.array()` 函数,例如:
```
start = np.array(start)
goal = np.array(goal)
```
这将把 `start` 和 `goal` 转换为 NumPy 数组类型,然后就可以在 `heuristic_cost_estimate()` 函数中使用 `np.linalg.norm()` 函数计算两个向量之间的距离了。
如果您想在 `astar_search()` 函数中使用元组进行计算,可以修改 `heuristic_cost_estimate()` 函数来接受元组作为参数,然后使用元组的索引来访问元素。例如:
```
def heuristic_cost_estimate(start, goal):
x1, y1 = start
x2, y2 = goal
return math.sqrt((x1-x2)**2 + (y1-y2)**2)
```
这将使用元组的索引来访问 `start` 和 `goal` 中的元素,然后计算它们之间的距离。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)