【问题标题】:Applying the A* Path 3:3D Coordinate Pairs -- Python ValueError: Length of ascending (2) != length of by (3)应用 A* 路径 3:3D 坐标对 -- Python ValueError: 升序长度 (2) != 长度为 (3)
【发布时间】:2020-08-01 06:53:12
【问题描述】:

我正在尝试运行这篇文章Applying the A* Path 3:3D Coordinate Pairs中的这段代码;但是,我收到此错误:

Traceback(最近一次调用最后一次): 文件“C:/Users/Win/AndrewJonesP3.py”,第 64 行,在 coord_pairs = coord_pairs.sort_values(['x1', 'y1', 'z1'], 升序=[True ,True]) 文件“C:\Python38\lib\site-packages\pandas\core\frame.py”,第 4914 行,位于 sort_values 引发价值错误( ValueError: 升序长度 (2) != 长度为 (3)

非常感谢任何帮助。

import numpy as np
import heapq
import pandas as pd
from collections import OrderedDict
import matplotlib.pyplot as plt
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D




# coordinate pairs
x1 = [1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2
      ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,3 ,3 ,3 ,3 ,3 ,3 ,3 ,3 ,3 ,3 ,3 ,3 ,3 ,3 ,3 ,3 ,3 ,3
      ,3 ,3 ,3 ,3 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1
      ,1 ,1 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 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 ,2 ,3 ,3 ,3 ,3 ,3 ,3 ,1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ,2 ,2 ,3 ,3 ,3 ,3 ,1 ,1 ,1 ,1
      ,2 ,2 ,3 ,3 ,3 ,3 ,3 ,3 ,1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ,2 ,2 ,3 ,3 ,3 ,3 ,3 ,3 ,1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ,2 ,2 ,3 ,3 ,3 ,3
      ,1 ,1 ,1 ,1 ,2 ,2 ,3 ,3 ,3 ,3 ,3 ,3 ,1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ,2 ,2 ,3 ,3 ,3 ,3 ,1 ,1 ,1 ,1 ,2 ,2 ,2 ,2 ,2 ,3 ,3 ,3
      ,2 ,2 ,2 ,2 ,2 ,3 ,3 ,3 ,3 ,3 ,2 ,2 ,2 ,3 ,3 ,3 ,2 ,2 ,2 ,2 ,2 ,3 ,3 ,3 ,3 ,3 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,3 ,3 ,3 ,3
      ,3 ,3 ,3 ,3 ,2 ,2 ,2 ,2 ,2 ,3 ,3 ,3 ,3 ,3 ,2 ,2 ,2 ,3 ,3 ,3 ,2 ,2 ,2 ,2 ,2 ,3 ,3 ,3 ,3 ,3 ,2 ,2 ,2 ,3 ,3 ,3]



coord_pairs = pd.DataFrame( OrderedDict((('x1', pd.Series(x1)), ('y1', pd.Series(y1)), ('z1', pd.Series(z1)), ('x2', pd.Series(x2)), ('y2', pd.Series(y2)), ('z2', pd.Series(z2)))))
coord_pairs = coord_pairs.sort_values(['x1', 'y1', 'z1'], ascending=[True ,True])
print(coord_pairs)


# specify start and goal positions
start = (1 ,1 ,1)
goal = (3 ,3 ,3)


##############################################################################

# a* path finding functions

def available_neighbours(current_x ,current_y ,current_z):
      return list(zip
          (coord_pairs.loc[(coord_pairs.x1 == current_x) & (coord_pairs.y1 == current_y) & (coord_pairs.z1 == current_z)] [["x2"]].x2,
          coord_pairs.loc[(coord_pairs.x1 == current_x) & (coord_pairs.y1 == current_y) & (coord_pairs.z1 == current_z)] [["y2"]].y2,
          coord_pairs.loc[(coord_pairs.x1 == current_x) & (coord_pairs.y1 == current_y) & (coord_pairs.z1 == current_z)] [["z2"]].z2))

def heuristic(a, b):
      return np.sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2 + (b[2] - a[2]) ** 2)


def astar(start, goal):
      close_set = set()
      came_from = {}
      gscore = {start: 0}
      fscore = {start: heuristic(start, goal)}
      oheap = []
      heapq.heappush(oheap, (fscore[start], start))
      while oheap:
            current = heapq.heappop(oheap)[1]
            neighbours = available_neighbours(current[0], current[1], current[2])
            if current == goal:
                  data = []
                  while current in came_from:
                        data.append(current)
                        current = came_from[current]
                  return data

            close_set.add(current)
            for x, y, z in neighbours:
                  neighbour = x, y, z
                  tentative_g_score = gscore[current] + heuristic(current, neighbour)
                  if neighbour in close_set and tentative_g_score >= gscore.get(neighbour, 0):
                        continue
                  if tentative_g_score < gscore.get(neighbour, 0) or neighbour not in [i[1] for i in oheap]:
                        came_from[neighbour] = current
                        gscore[neighbour] = tentative_g_score
                        fscore[neighbour] = tentative_g_score + heuristic(neighbour, goal)
                        heapq.heappush(oheap, (fscore[neighbour], neighbour))

            return False


##############################################################################

# calculate route

route = astar(start, goal)
route = route + [start]
route = route[::-1]
print(route)

##############################################################################

# visualise the path


# extract x and y coordinates from route list
x_coords = []
y_coords = []
z_coords = []


for i in (range(0 ,len(route))):
      x = route[i][0]
      y = route[i][1]
      z = route[i][2]
      x_coords.append(x)
      y_coords.append(y)
      z_coords.append(z)

x_coords = np.array(x_coords)
y_coords = np.array(y_coords)
z_coords = np.array(z_coords)



fig = plt.figure(figsize=(12 ,12))
ax = fig.add_subplot(111, projection='3d')

ax.scatter3D(goal[0] ,goal[1] ,goal[2], marker = "*", color = "red", s = 100)
ax.scatter3D(1 ,1 ,1 ,marker = "o", color = "black", s = 100)
ax.scatter3D(1 ,2 ,1 ,marker = "o", color = "black", s = 100)
ax.scatter3D(1 ,3 ,1 ,marker = "o", color = "black", s = 100)
ax.scatter3D(2 ,1 ,1 ,marker = "o", color = "black", s = 100)
ax.scatter3D(2 ,2 ,1 ,marker = "o", color = "black", s = 100)
ax.scatter3D(2 ,3 ,1 ,marker = "o", color = "black", s = 100)
ax.scatter3D(3 ,1 ,1 ,marker = "o", color = "black", s = 100)
ax.scatter3D(3 ,2 ,1 ,marker = "o", color = "black", s = 100)
ax.scatter3D(3 ,3 ,1 ,marker = "o", color = "black", s = 100)
ax.scatter3D(1 ,1 ,2 ,marker = "o", color = "black", s = 100)
ax.scatter3D(1 ,2 ,2 ,marker = "o", color = "black", s = 100)
ax.scatter3D(1 ,3 ,2 ,marker = "o", color = "black", s = 100)
ax.scatter3D(2 ,1 ,2 ,marker = "o", color = "black", s = 100)
ax.scatter3D(2 ,2 ,2 ,marker = "x", color = "black", s = 100)
ax.scatter3D(2 ,3 ,2 ,marker = "o", color = "black", s = 100)
ax.scatter3D(3 ,1 ,2 ,marker = "o", color = "black", s = 100)
ax.scatter3D(3 ,2 ,2 ,marker = "o", color = "black", s = 100)
ax.scatter3D(3 ,3 ,2 ,marker = "o", color = "black", s = 100)
ax.scatter3D(1 ,1 ,3 ,marker = "o", color = "black", s = 100)
ax.scatter3D(1 ,2 ,3 ,marker = "o", color = "black", s = 100)
ax.scatter3D(1 ,3 ,3 ,marker = "o", color = "black", s = 100)
ax.scatter3D(2 ,1 ,3 ,marker = "o", color = "black", s = 100)
ax.scatter3D(2 ,2 ,3 ,marker = "o", color = "black", s = 100)
ax.scatter3D(2 ,3 ,3 ,marker = "o", color = "black", s = 100)
ax.scatter3D(3 ,1 ,3 ,marker = "o", color = "black", s = 100)
ax.scatter3D(3 ,2 ,3 ,marker = "o", color = "black", s = 100)

#ax.scatter3D(3,3,3,marker = "o", color = "black", s = 100)

ax.plot3D(x_coords, y_coords, z_coords, color = "pink")

plt.show()

【问题讨论】:

    标签: python pandas matplotlib 3d valueerror


    【解决方案1】:

    替换

    ascending=[True, True] 
    

    ascending=[True, True, True] 
    

    因为是三个维度的

    【讨论】:

    • 您好 Mohnish,根据您的建议,我进行了更改并进一步改进。我现在收到以下错误:文件“c:/Users/Newtonian/Documents/Apath.py”,第 1705 行,在 route = route + [start] TypeError: unsupported operand type(s) for +: '布尔'和'列表'
    猜你喜欢
    • 1970-01-01
    • 2020-12-29
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 2017-08-23
    • 1970-01-01
    相关资源
    最近更新 更多