我有多个点代表x,y平面上的障碍物。对于该环境中的某一特定点,我如何才能找到最大限度的清除方向?
例如,在下面的图片中,有三个点-障碍(黑色),和点我想移动,所以它得到最多的清除(红色)。箭头是它应该移动的方向,以最快的速度从所有其他点上最大限度地利用它的间隙。
对于单一的障碍,这是微不足道的,但我真的不知道如何做多个障碍。
发布于 2020-01-25 17:40:22
我认为解决您的问题的一个好方法是定义一个成本函数,例如
f(red_pt, black_pts)=sum_i norm2(black_pts[i]-red_pt)
并使其最大化。一个快速和肮脏的解决方案是使用梯度上升。
import matplotlib.pyplot as plt
import numpy as np
def toy_problem():
x=np.array([
[10,5],
[3,4],
[5,10]]).astype("float")
init_pt=np.array([[6,5]]).astype("float")
return x, init_pt
def cost_function(x, black_pts):
s=0
jacobian=np.zeros([1,2])
for i in range(black_pts.shape[0]):
a=x[0,0]-black_pts[i,0]
b=x[0,1]-black_pts[i,1]
nrm2=a**2+b**2
jacobian[0,0]+=2*a
jacobian[0,1]+=2*b
s+=nrm2
return s, jacobian
def gradient_ascent(x, cost_function, black_pts,num_it=20):
lr=1e-2
for i in range(num_it):
val, jacobian=cost_function(x,black_pts)
x=x+lr*jacobian
plt.plot(black_pts[:,0],black_pts[:,1],"k*");
plt.plot(x[0,0],x[0,1],"r*");
plt.show()
return x
if __name__=="__main__":
black_pts,init_pt=toy_problem()
x_final=gradient_ascent(init_pt,cost_function,black_pts)
为了获得更快的收敛速度,也就是,用你的话说,你可以使用二阶优化算法,比如利温伯格-马夸特,在使黑点的距离最小化的方向上移动得更快。一旦定义了成本函数,找到更快地增加距离的方向就是选择一个好的优化方法和超参数。
动画片,看起来像这样:
https://stackoverflow.com/questions/59910295
复制相似问题