我正在尝试通过GEKKO建模和解决一个非线性优化问题。目标函数如下:
m.Minimize(k*(w1*abs(x0-L10)+w2*abs(x1-L20))+β*Dindf[0]*(w1*alpha_beta(x0)+w2*alpha_beta(x1)+w1***alpha_beta**(x2)+w2*alpha_beta(x3)) +
k*(w1*abs(x2-x0) + w2*abs(x3-x1)+w1*abs(x4-x2) + w2*abs(x5-x3)))
问题是我需要alpha_beta(x)函数(wrt到决策变量,如下所示)是一个段形式,我如何编码这个函数,以便我可以解决这个问题?
def alpha_beta(x):
a = 0.0019727939
b = 0.0078887
Lmin, Lnom, Lmax = 0.8035, 2.3811, 3.084
return np.piecewise(x, [np.logical_and(Lmin <= x, x < Lnom),
np.logical_and(Lnom <= x, x <= Lmax)],
[lambda x: a * ((x - Lnom)**2) + 0.006226,
lambda x: b * ((x - Lnom)**2) + 0.006226, 0])
非常感谢!
使用m.if3()
Gekko函数根据x
的值使用不同的函数。同时,将目标函数中的abs()
函数替换为Gekko版本的m.abs2()
或m.abs3()
(优先)。下面是一个示例脚本:
from gekko import GEKKO
m = GEKKO()
x = m.Var(lb=0.8035,ub=3.084)
def alpha_beta(x):
a = 0.0019727939
b = 0.0078887
Lnom = 2.3811
e1 = a*(x-Lnom)**2 + 0.006226
e2 = b*(x-Lnom)**2 + 0.006226
return m.if3(x-Lnom,e1,e2)
m.Minimize(alpha_beta(x)+m.abs3(-x))
m.solve()
生成解决方案x=0.8035
。
Number of state variables: 9
Number of total equations: - 6
Number of slack variables: - 4
---------------------------------------
Degrees of freedom : -1
* Warning: DOF <= 0
----------------------------------------------
Steady State Optimization with APOPT Solver
----------------------------------------------
Iter: 1 I: 0 Tm: 0.00 NLPi: 3 Dpth: 0 Lvs: 0 Obj: 8.15E-01 Gap: 0.00E+00
Successful solution
---------------------------------------------------
Solver : APOPT (v1.0)
Solution time : 1.290000000153668E-002 sec
Objective : 0.814635932386315
Successful solution
---------------------------------------------------