【问题标题】:How to properly apply equality boundary constraint如何正确应用等式边界约束
【发布时间】:2023-03-16 21:11:01
【问题描述】:

我正在使用 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 个控制变量)。代码的最开始在 cmets 中显示了这一点。

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

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

【问题讨论】:

    标签: python scipy scipy-optimize scipy-optimize-minimize


    【解决方案1】:

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

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

    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])
    

    如果满足x 元素之间的关系,则约束fun 返回0。

    【讨论】:

    • 非常感谢您的回答。我对这样的例子没意见,但对我的例子感到困惑。基本上,我需要说我的最后 4 个值被限制为 (0,d) 和 (0,pi)。因此,这样的事情有意义吗? def constraint_end(y): y = [(0,d),[0,np.pi]] xf = y[20:24]
    • xf 应该是什么?在我的示例中,如果(某种组合)值符合条件,则约束函数返回 0(标量)。如果他们不这样做,则非零。我的建议是从一个可行的例子开始,然后逐步朝着更大的方向努力。您需要了解并验证复杂性的每一个增量。
    猜你喜欢
    • 2023-04-03
    • 2013-01-12
    • 1970-01-01
    • 2020-03-29
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    相关资源
    最近更新 更多