python - 使用 python 优化网格连接
问题描述
我有以下情况:
(1) 我有一个大网格。在某些情况下,我想进一步观察该网格中的特定点/单元。每个单元格分别有一个和ID
坐标。所以在这种情况下,让我们只观察一个单元格——在图像上标记为 C,它位于网格的边缘。通过一些公式,我可以获得一阶(在图像上标记为 1)和二阶(在图像上标记为 2)的所有相邻单元格。X
Y
(2) 在进一步的条件下,我识别出相邻单元格中的一些单元格,并在第二张图像上用橙色标记。我想要做的是通过优化距离和 takih 仅考虑距离来将所有橙色单元格相互连接min()
。我的第一次尝试是仅通过计算到低阶细胞的距离来观察细胞。因此,当查看相邻单元格 2 中的单元格时,我只查看 1 中的单元格。图 2 显示了连接的解决方案,但它不是最优的,因为理想的解决方案将比较所有单元格的距离,而不仅仅是低邻阶单元格的距离。通过这样做,我得到了图 3 所示的情况。问题是细胞当然没有连接到中心。该怎么办?
当前代码是:
CO
- 中心点列表。
data
- df 所有带有 X,Y 值的 ID
CO_list = CO['ID'].tolist()
neighbor100 = []
for p in IskanjeCO_list:
d = get_neighbors100k2(p, len(data)) #function that finds the ID's of neighbours of the first order
neighbor100.append(d)
neighbor200 = []
for p in IskanjeCO_list:
d = get_neighbors200k2(p, len(data)) #function that finds the ID's of neighbours of the second order
neighbor200.append(d)
flat100 = []
for i in neighbor100:
for j in i:
flat100.append(j)
flat200 = []
for i in neighbor200:
for j in i:
flat200.append(j)
neighbors100 = flat100
neighbors200 = flat200
data_sosedi100 = data.iloc[flat100,].reset_index(drop=True)
data_sosedi200 = data.iloc[flat200,].reset_index(drop=True)
dist200 = []
for b in flat200:
d = ((pd.DataFrame((data_sosedi100['X']* - data.iloc[b,]['X'])**2
+ (data_sosedi100['Y'] - data.iloc[b,]['Y'])**2 )**0.5)).sum(1)
dist200.append(d.min())
data_sosedi200['dist'] = dist200
data_sosedi200['id'] = None
for e in CO_list:
data_sosedi200.loc[data_sosedi200['FID_2'].isin((get_neighbors200k2(e, len(data)))),'id'] = e
你有什么建议可以进一步优化吗?我希望我展示了整个图像。如果需要,我会进一步澄清。如果您看到代码的一部分,我将能够进一步优化此循环,我将非常感激!
解决方案
我手动定义了要使用的点: import numpy as np from operator import itemgetter, attrgetter
nodes = [[-2,1], [-2,0], [-1,0], [0,0], [1,1], [2,1], [2,0], [1,2], [2,2]]
center = [0,0]
def find_neighbor(node):
n=[]
for i in range(-1,2):
for j in range(-1,2):
if not (i ==0 and j ==0):
n.append([node[0]+i,node[1]+j])
return [N for N in n if N in nodes]
def distance_to_center(node):
return np.sqrt(node[0]**2+node[1]**2)
def distance_between_two_nodes(node1, node2):
return np.sqrt((node1[0]-node2[0])**2+(node1[1]-node2[1])**2)
def next_node_closest_to_center(node):
min = distance_to_center(node)
next_node = node
for n in find_neighbor(node):
if distance_to_center(n) < min:
min = distance_to_center(n)
next_node = n
return next_node, min
def get_path_to_center(node):
node_path = [node]
distance = 0.
while node!= center:
new_node = next_node_closest_to_center(node)[0]
distance += distance_between_two_nodes(node, new_node)
node_path.append(new_node)
node=new_node
return node_path,distance
def furthest_nodes_from_center(nodes):
max = 0.
for n in nodes:
if get_path_to_center(n)[1] > max:
furthest_nodes_pathwise = []
max = get_path_to_center(n)[1]
furthest_nodes_pathwise.append(n)
elif get_path_to_center(n)[1] == max:
furthest_nodes_pathwise.append(n)
return furthest_nodes_pathwise
def farthest_node_from_center(nodes):
max = 0.
farthest_node = center
for n in nodes:
if distance_to_center(n) > max:
max = distance_to_center(n)
farthest_node = n
return farthest_node
def closest_node_to_center(nodes):
min = distance_to_center(farthest_node_from_center(nodes))
for n in nodes:
if distance_to_center(n) < min:
min = distance_to_center(n)
closest_node = n
return closest_node
def closest_node_center_with_furthest_distance(node_selection):
if len(node_selection) == 1:
return node_selection[0]
else:
return closest_node_to_center(node_selection)
print(closest_node_center_with_furthest_distance(furthest_nodes_from_center(nodes)))
输出:
[2, 0]
[Finished in 0.266s]
通过在所有节点上运行,我现在可以确定在路径上最远但仍然最接近中心距离的节点是[2,0]
和不是[2,2]
。所以我们从那里开始。要找到另一边的数据,只需像我说的那样将数据拆分为负 x 值和正值。如果您在仅包含负 x 值单元格的列表上运行它,您将得到[-2,1]
现在您有了 2 个起始单元格[2,0]
,[-2,1]
我将让您使用我的评论中的步骤找出导航到通过所有单元格的中心的算法(您现在可以跳过第 1 步,因为这是发布的答案)
推荐阅读
- python - 按值而不是引用在列表中分配值
- smtp - 为什么即使在配置 SMTP 之后 prestashop 电子邮件也无法正常工作?
- algorithm - 从给定数字中获取所有可能的操作组合的算法
- html - 背景图像未使用 CSS 显示
- c++ - 如何为我的游戏设置主菜单场景?QT C++
- node.js - 如何使用 listObjectV2 和 NodeJs 从 S3 AWS 获取对象列表?
- java - 如何找到所有可用的 JDBC 驱动程序?
- android-studio - 如何在 Flutter 项目中解析符号 @mipmap 和 @style?
- c# - 我如何将 IUnitOfWork 与 BaseController 一起使用
- python - Python 字符串表达式格式