Python&;NumPy:使用条件搜索最小值(使用特定的数据类型)
我正在用Python开发一个A*路径查找算法,并使用以下数据类型将数据很好地塞入2D NumPy数组中:Python&;NumPy:使用条件搜索最小值(使用特定的数据类型),python,numpy,indexing,where,Python,Numpy,Indexing,Where,我正在用Python开发一个A*路径查找算法,并使用以下数据类型将数据很好地塞入2D NumPy数组中: numpy.dtype([ ('open', bool), ('closed', bool), ('parent', object), ('g', int), ('f', int) ]) 接下来,我需要解释一下: current := the node in openset having the lowest f_score[] value 此位将为我提供最低“f”值
numpy.dtype([
('open', bool),
('closed', bool),
('parent', object),
('g', int),
('f', int)
])
接下来,我需要解释一下:
current := the node in openset having the lowest f_score[] value
此位将为我提供最低“f”值的索引(工作数组定义为pathArray):
…此位将查找“打开”为真的所有索引:
numpy.where(pathArray['open'])
如何将这些条件结合使用,在“打开”为真的情况下找到最低的“f”值?我对
numpy
不太熟悉,但我仍然“认为”使用内置函数无法做到这一点。但我还是会尽力解释。Wikipedia
的意思是,要做到这一点,您需要类似于优先级队列的东西:
current := the node in openset having the lowest f_score[] value
您需要非常快地完成这项工作,我建议您构建一个二进制堆,并将其用作优先级队列。这可以用python轻松完成。这是一篇非常好的文章,解释了堆
和优先级队列
及其在python中的实现。
祝你好运不要在pathArray['f']
上使用np.argmin
,你可以在pathArray[pathArray['open']]['f']
上使用它。当然,您必须调整结果,以便将其与pathArray['f']
一起使用
另一种方法是沿着'f'
字段对pathArray
进行排序,然后找到排序为['open']
的第一个条目
current := the node in openset having the lowest f_score[] value