问题描述
我有一个二维平面中点的有序列表。我想找到最短的路线,以便它按照给定的顺序至少在距离每个点 X(或更近)的范围内。我如何找到这条路线?
我意识到决定路线的点(那里的方向改变)将位于周长 X 的圆上,以输入点本身为中心,但我没有进一步了解。
我正在用 Python 实现它,但很乐意提供任何理论帮助。
解决方法
也许有一种解析方法,但是这个问题当然可以用凸编程来解决。这是一个带有输出 EPS 的 cvxpy 的 Python 实现。
import cvxpy as cp
import numpy as np
def objective(points):
return np.sum(np.linalg.norm(points[1:] - points[:-1]))
def shortest_path(x,centers):
points = cp.reshape(cp.Variable(centers.size),centers.shape)
cost = cp.sum(cp.norm(points[1:] - points[:-1],axis=1))
constraints = []
for i,center in enumerate(centers):
constraints.append(cp.SOC(x,points[i] - center))
prob = cp.Problem(cp.Minimize(cost),constraints)
prob.solve()
return points.value
def main():
x = 0.05
centers = np.random.random_sample((10,2))
points = shortest_path(x,centers)
print("%!PS-Adobe-3.0 EPSF-3.0")
print("%%BoundingBox:0 0 504 504")
print("72 dup translate")
print("360 dup scale")
print("0 setlinewidth")
for center in centers:
print(*center,x,360,"arc")
print("stroke")
for i,point in enumerate(points):
print(*point,"lineto" if i else "moveto")
print("stroke")
if __name__ == "__main__":
main()
,
看来您必须找到与路线中下一个点相距 x minimux x 个点的点的最小距离。
我建议找到起点和所有圆 cirumfrence 之间的最短距离,然后选择最短的值并再次重复相同的步骤。
所以它会像。
你有 4 个点,你从点 1(起点在圆 1 的圆弧处)开始,
使用shortest distance formula from cirumfrence计算最短距离
对于其余的 3 个圆(将计算从点 1 到所有 3 个圆的圆周的距离),然后选择三个圆的最小距离。
现在移动到第二点(距离最短的那个)并在左边的另外两个点重复相同的操作。
circle1(x0,y0)
circle2(xx0,yy0)
circle3(xxx0,yyy0)
circle4(xxxx0,yyyy0)
cirle 1 (x0,y0) is the center and you have **p1** on circumfrence p1(x1,y1)
from p1 calculate the shortest distance to all three circles like
distance from p1 to circle2
Distp1Toc2= sqrt(square(x1-xx0) + square(y1-yy0) -r) // this will give you shortest distance from point 1 to a point on circle2 cirumfrence
repeat this for circle3 and cirlce4 to calculate **Dist_p1_To_c3** and **Dist_p1_To_c4** then select the minimum distance
lets consider **Dist_p1_To_c2** is minimum and now from the point Dist_p1_To_c2 again calculate the distance to circle 3 and circle4 to find minimum distance.