避免障碍的同时路径优化出错

时间:2019-02-12 07:37:22

标签: numpy optimization scipy path-finding minimization

我正在尝试使用平方距离方法找到在起点和终点之间避免障碍的最小路径。

为此,我在起点和终点之间定义了n个点-并计算了优化路径和直线路径之间的平方距离。优化路径必须与障碍物保持最小距离。所得的优化路径是优化路径与直线路径之间的最小平方距离。

我已经实现了以下代码,但是在优化过程中,出现以下错误:

无法将输入数组从形状(27)广播到形状(27,3)

看起来像Scipy.minimize将数组的形状从3-D数组更改为1D数组。您能否提出解决此问题的任何建议?


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之间的位置,它们避开了障碍并返回了最小距离。

1 个答案:

答案 0 :(得分:1)

这是因为要最小化的输入必须与一维数组配合使用,

从肮脏的notes

  

要最小化的目标函数。

false
     

其中x是形状为(n,)的一维数组,而args是完全指定函数所需的固定参数的元组。

     

x0:ndarray,形状(n,)

     

初步猜测。大小为(n,)的实元素数组    其中“ n”是自变量的数量。

这意味着您应该在输入中使用 fun(x, *args) -> float 并更改starting_guess.ravel()以使用一维数组。