Numpy 避开障碍物时的路径优化错误
我试图用平方距离法找到最小路径,避免起点和终点之间的障碍 为此,我在起点和终点之间定义了n个点,并计算优化路径和直线路径之间的平方距离。优化路径必须与障碍物保持最小距离。得到的优化路径是优化路径和直线路径之间的最小平方距离 我实现了如下代码,但在优化过程中,我收到以下错误: 无法将输入数组从形状(27)广播到形状(27,3) 它看起来像Scipy.minimize将阵列的形状从三维阵列更改为一维阵列。你能提出一些建议来解决这个问题吗Numpy 避开障碍物时的路径优化错误,numpy,optimization,scipy,path-finding,minimization,Numpy,Optimization,Scipy,Path Finding,Minimization,我试图用平方距离法找到最小路径,避免起点和终点之间的障碍 为此,我在起点和终点之间定义了n个点,并计算优化路径和直线路径之间的平方距离。优化路径必须与障碍物保持最小距离。得到的优化路径是优化路径和直线路径之间的最小平方距离 我实现了如下代码,但在优化过程中,我收到以下错误: 无法将输入数组从形状(27)广播到形状(27,3) 它看起来像Scipy.minimize将阵列的形状从三维阵列更改为一维阵列。你能提出一些建议来解决这个问题吗 import numpy as np import matp
import numpy as np
import matplotlib.pyplot as plt
import random
from mpl_toolkits.mplot3d import Axes3D
from scipy.optimize import minimize
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
## Setting Input Data:
startPoint = np.array([1,1,0])
endPoint = np.array([0,8,0])
obstacle = np.array([5,5,0])
## Get degree of freedom coordinates based on specified number of segments:
numberOfPoints = 10
pipelineStraightVector = endPoint - startPoint
normVector = pipelineStraightVector/np.linalg.norm(pipelineStraightVector)
stepSize = np.linalg.norm(pipelineStraightVector)/numberOfPoints
pointCoordinates = []
for n in range(numberOfPoints-1):
point = [normVector[0]*(n+1)*stepSize+startPoint[0],normVector[1]*(n+1)*stepSize+startPoint[1],normVector[2]*(n+1)*stepSize+startPoint[2]]
pointCoordinates.append(point)
DOFCoordinates = np.array(pointCoordinates)
## Assign a random z value for the DOF coordinates - change later:
for coordinate in range(len(DOFCoordinates)):
DOFCoordinates[coordinate][2] = random.uniform(-1.0, -0.0)
##ax.scatter(DOFCoordinates[coordinate][0],DOFCoordinates[coordinate][1],DOFCoordinates[coordinate][2])
## function to calculate the squared residual:
def distance(a,b):
dist = ((a[0]-b[0])**2 + (a[1]-b[1])**2 + (a[2]-b[2])**2)
return dist
## Get Straight Path Coordinates:
def straightPathCoordinates(DOF):
allCoordinates = np.zeros((2+len(DOF),3))
allCoordinates[0] = startPoint
allCoordinates[1:len(DOF)+1]=DOF
allCoordinates[1+len(DOF)]=endPoint
return allCoordinates
pathPositions = straightPathCoordinates(DOFCoordinates)
## Set Degree of FreeDom Coordinates during optimization:
def setDOFCoordinates(DOF):
print 'DOF',DOF
allCoordinates = np.zeros((2+len(DOF),3))
allCoordinates[0] = startPoint
allCoordinates[1:len(DOF)+1]=DOF
allCoordinates[1+len(DOF)]=endPoint
return allCoordinates
## Objective Function: Set Degree of FreeDom Coordinates and Get Square Distance between optimized and straight path coordinates:
def f(DOF):
newCoordinates = setDOFCoordinates(DOF)
print DOF
sumDistance = 0.0
for coordinate in range(len(pathPositions)):
squaredDistance = distance(newCoordinates[coordinate],pathPositions[coordinate])
sumDistance += squaredDistance
return sumDistance
## Constraints: all coordinates need to be away from an obstacle with a certain distance:
constraint = []
minimumDistanceToObstacle = 0
for coordinate in range(len(DOFCoordinates)+2):
cons = {'type': 'ineq', 'fun': lambda DOF: minimumDistanceToObstacle-((obstacle[0] - setDOFCoordinates(DOF)[coordinate][0])**2 +(obstacle[1] - setDOFCoordinates(DOF)[coordinate][1])**2+(obstacle[2] - setDOFCoordinates(DOF)[coordinate][2])**2)}
constraint.append(cons)
## Get Initial Guess:
starting_guess = DOFCoordinates
## Run the minimization:
objectiveFunction = lambda DOF: f(DOF)
result = minimize(objectiveFunction,starting_guess,constraints=constraint, method='COBYLA')
print result.x
print DOFCoordinates
ax.plot([startPoint[0],endPoint[0]],[startPoint[1],endPoint[1]],[startPoint[2],endPoint[2]])
ax.scatter(obstacle[0],obstacle[1],obstacle[2])
所需的结果是一组点及其在点a和点B之间的位置,这些点可以避开障碍物并返回最小距离 这是因为要最小化的输入必须与1D阵列一起工作 从scipy 要最小化的目标函数
fun(x, *args) -> float
其中x是一个具有形状(n,)的一维数组,args是完全指定函数所需的固定参数的元组
x0:ndarray,形状(n,)
初步猜测。大小为(n,)、的实元素数组,
其中“n”是自变量的数量
这意味着您应该在输入中使用start_guess.ravel()
,并更改setDOFCoordinates
以处理一维数组