如何正确应用等式边界约束

How to properly apply equality boundary constraint

我正在使用 Python SCIPY 最小化优化倒立摆。目前,我无法弄清楚如何实现此处显示的边界约束: Boundary Constraints

这是我的代码:

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



#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, x1, x2, ..., u0, ... 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], x[k+1,2], x[k+1,3], u[k+1])
         f0 = f(x[k,0],x[k,1],x[k,2],x[k,3],u[k])
         c[k] = x[k+1]-x[k]-(h/2)*(f1+f0)
         return c.reshape(-1)



#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):
    xf = y[:24]
    return xf[d]

#bounds
b = (-dmax,dmax)
c = (-umax,umax)  
bnds = [(b), (-np.inf, np.inf), (-np.inf, np.inf),(-np.inf, np.inf),(b),(-np.inf, np.inf),(-np.inf, np.inf),(-np.inf, np.inf),(b),(-np.inf, np.inf), 
(-np.inf, np.inf), (-np.inf, np.inf),(b),(-np.inf, np.inf),(-np.inf, np.inf),(-np.inf, np.inf),(b),(-np.inf, np.inf),(-np.inf, np.inf),(-np.inf, np.inf),
(b),(-np.inf, np.inf),(-np.inf, np.inf),(-np.inf, np.inf),(c),(c),(c),(c),(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) 
print(sol)

这是我坚持的具体部分。我试图实现一些东西。我对“constraint_start”有信心,但对“constraint_end”没有信心,因为这些值不全是 0,应该应用于我的最后 4 个状态。 (注意:我的配置点按顺序组织为 24 个状态变量和 6 个控制变量)。代码的开头在注释中显示了这一点。

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

#End point
def constraint_end(y):
    xf = y[:24]
    return xf[d]

我没有看到很多(任何?)constraints 的例子,无论是在 docs 还是在 SO 问题中。但这是在数组的两个元素之间应用相等约束的成功尝试。

首先是一个简单的单变量最小化:

In [50]: minimize(lambda x: x**2+x+1, [0])
Out[50]: 
      fun: 0.75
 hess_inv: array([[1]])
      jac: array([2.23517418e-08])
  message: 'Optimization terminated successfully.'
     nfev: 6
      nit: 1
     njev: 3
   status: 0
  success: True
        x: array([-0.5])

现在是同样的事情,但是写了一个 (2,) 变量,以及 x[1] == x[0]+1

的约束
In [51]: minimize(lambda x: x[0]**2+x[1], [0,0],
   constraints=[{'type':'eq','fun':lambda x:x[0]-x[1]+1}])
Out[51]: 
     fun: 0.75
     jac: array([-1.,  1.])
 message: 'Optimization terminated successfully'
    nfev: 16
     nit: 5
    njev: 5
  status: 0
 success: True
       x: array([-0.50000001,  0.49999999])

约束fun returns 0 如果 x 元素之间的关系得到满足。