使用 scipy minimze 时,我不明白“线性搜索的正向导数”错误

问题描述

我正在使用梯形搭配和 scipy 最小化优化倒立摆以获得最小力以稳定它。

问题被设置为有 6 个搭配点——6 个点,每个点有 4 个状态,每个点都有一个控制输入。这给出了总共 30 个变量。为了方便起见,我将它们安排为 24 个状态变量,然后是 6 个输入。

我使用的是 SLSQP 方法,因为我的问题是非线性的。运行代码时,我收到此错误

[Running] python -u 
     fun: 1446.2686782969988
     jac: array([  0.,0.,-40.,24.17993164,22.36561584,-37.04786682,-11.2978363,40.        ])
 message: 'Positive directional derivative for linesearch'
    nfev: 1149
     nit: 40
    njev: 36
  status: 8
 success: False
       x: array([ 0.00000000e+00,0.00000000e+00,-8.16655789e-02,1.27974038e-01,-8.16655788e-01,1.27974038e+00,-1.52639829e-02,-8.86584111e-02,1.48067175e+00,-3.44606486e+00,2.14468772e-01,-5.28533619e-01,8.16655809e-01,-9.52687211e-01,1.48067177e-01,-1.25347576e-01,-1.48067177e+00,4.98454763e+00,5.44014336e-01,1.70907144e+00,-2.00000000e+01,1.20899666e+01,1.11828057e+01,-1.85239300e+01,-5.64891675e+00,2.00000000e+01])

我正在努力寻找有关此错误含义以及如何解决错误的资源。这是我当前的所有代码。我目前想知道这是否与我最初的猜测或容差有关,但我目前正在猜测。

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import minimize
from scipy.optimize import Bounds



#parameters
m1 = 1
m2 = 0.3
g = 9.81
l = 0.5
dmax = 2.0
umax = 20.0
T = 2
d = 1
h = 0.2

# (x0,u0) (x1,u1) ....
# +-------+--------+-------+------+--------+------> 1
# y = [x0,u0,x1,u1,...,x5,u5]
# Alt.:
# y = [x0,x2,... u5]
# x = y[:24]
# u = y[-6:] = y[24:]

#Objective 
def objective(x):      
    return np.sum(x[-6:]**2)


#EOM's SS
# dx_i = statespace(xi[0],xi[1],xi[2],xi[3],ui)
def statespace(y1,y2,ydot1,ydot2,u):    # Should only provide the 4 states; the others are fixed paramters.

    dy1 = ydot1
    dy2 = ydot2
    dydot1 = ((l*m2*np.sin(y1)*y1*y1) + u + (m2*g*np.cos(y1)*np.sin(y1))) / (m1 + m2*(1-np.cos(y1)**2))
    dydot2 = -1*((l*m2*np.cos(y2)*np.sin(y2)*y2*y2) + u*np.cos(y2) + ((m1+m2)*g*np.sin(y2))) / (l*m1 + l*m2*(1-np.cos(y2)**2))
    return np.array([dy1,dy2,dydot1,dydot2])      


#trap(y) == 0
def trap(y,f=statespace):
    x = y[:24].reshape(6,4)
    u = y[24:]
    c = np.zeros((5,4))
    for k in range(5):
         f1 = f(x[k+1,0],x[k+1,1],2],3],u[k+1])
         f0 = f(x[k,x[k,u[k])
         c[k] = x[k+1]-x[k]-(h/2)*(f1+f0)
    return(c.reshape(-1))        # Be careful with spacing.  This would end the execution prematurely.



#Initial Guess
#steps = 
#x0 = [# of states + # of controls]x[# of steps] * steps  
x0 = np.zeros(30)



# End points:

# Starting point:
def constraint_start(y):
    x0 = y[:4]
    return x0

#End point
def constraint_end(y):
    q1 = [0,d]
    q2 = [0,np.pi]
    xf = np.array([q1,q2]).reshape(4)                                
    return (xf - y[20:24])


#bounds
b = (-dmax,dmax)
c = (-umax,umax)  
bnds = [(b),(-np.inf,np.inf),(c),(c)]

con1 = {'type': 'eq','fun': constraint_start}
con2 = {'type': 'eq','fun': constraint_end}
con3 = {'type': 'eq','fun': trap}
cons = (con1,con2,con3)                  

sol = minimize(objective,x0,bounds = bnds,constraints = cons,method = 'SLSQP') 
print(sol)


解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)