简体   繁体   English

在 Python (gekko) 和 m.Equations 中制定非线性优化问题

[英]Formulate a nonlinear optimization problem in Python (gekko) and with m.Equations

I would like to solve an optimization problem with Gekko.我想用 Gekko 解决一个优化问题。 The non-linear constraints are generated in a function and passed on as a vector.非线性约束在 function 中生成并作为向量传递。 Since there are vectors, I use m.Equations and m.Array .由于有向量,我使用m.Equationsm.Array To make it as simple as possible, I have simplified my problem so that the code remains short and simple.为了使其尽可能简单,我简化了我的问题,使代码保持简短和简单。 I think my mistake is in how I use m.Equations .我认为我的错误在于我如何使用m.Equations The error message is:错误信息是:

Exception: @error: Equation Definition An equation without equality (=) or inequality (>,<) false STOPPING...例外:@error:方程式定义不等于 (=) 或不等式 (>,<) 的方程式错误停止...

This is my code:这是我的代码:

import numpy as np
from gekko import GEKKO

def powerf(x):
    U = np.array([[x[2]], [x[3] * m.exp(x[4] * 1j)]])
    G = np.array([[x[0] + x[1] * 1j], [0]])
    L = np.array([[0],[1]])

    y = np.array([[-18.8301*1j, 18.8566*1j], [18.8566*1j, -18.8301*1j]])
    I = np.matmul(y, U)
    Iconj = np.real(I) - np.imag(I) * 1j

    S = np.matmul(np.diagflat(Iconj), U)
    P = np.real(S) - np.real(G) + np.real(L)
    Q = np.imag(S) - np.imag(G) + np.imag(L)
    return np.concatenate((P,Q)).reshape(4,)

m = GEKKO()
x =  m.Array(m.Var,5)
ig = [0,  0,    1,    1,      0]
lb = [0, -2, 0.95, 0.95, -np.pi]
ub = [5,  2, 1.10, 1.10,  np.pi]

for i in range(len(x)):
    x[i].value = ig[i]
    x[i].lower = lb[i]
    x[i].upper = ub[i]

m.Equations([powerf(x) == np.zeros(4,)])
m.Obj(x[0])

m.solve()

The m.Equations() function needs a list of equations. m.Equations() function 需要一个方程列表。 Here is one way to pose those:这是摆姿势的一种方法:

y = powerf(x)
m.Equations([y[i]==0 for i in range(4)])

The resulting model can be viewed with m.open_folder() .可以使用m.open_folder()查看生成的 model 。

Model
Variables
    v1 = 0, <= 5, >= 0
    v2 = 0, <= 2, >= -2
    v3 = 1, <= 1.1, >= 0.95
    v4 = 1, <= 1.1, >= 0.95
    v5 = 0, <= 3.141592653589793, >= -3.141592653589793
End Variables
Equations
    ((((((((((-0-18.8301j))*(v3))+((18.8566j)*(((v4)*(exp(((v5)*(1j))))))))-0j))*(v3))+((0)*(((v4)*(exp(((v5)*(1j))))))))-(v1+((v2)*(1j))))+0)=0
    (((((0)*(v3))+((((((18.8566j)*(v3))+(((-0-18.8301j))*(((v4)*(exp(((v5)*(1j))))))))-0j))*(((v4)*(exp(((v5)*(1j))))))))-0)+1)=0
    True
    True
    minimize v1
End Equations

End Model

Equations 3 and 4 from the list do not appear to have Gekko variables so they return True .列表中的方程式 3 和 4 似乎没有 Gekko 变量,因此它们返回True One other thing to consider is that Gekko doesn't currently support imaginary numbers natively so the problem needs to reformed into separate variables for imaginary value and real values such as x_real[i] and x_im[i] .另一件需要考虑的事情是,Gekko 目前不支持虚数,因此需要将问题重新转换为用于虚数和实数的单独变量,例如x_real[i]x_im[i] A more readable form of the objective is m.Minimize(x[0]) .更易读的目标形式是m.Minimize(x[0])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM