GEKKO Error: "Equation without an equality (=) or inequality (>,<)" when calling functions within constraint and objective

GEKKO Error: "Equation without an equality (=) or inequality (>,<)" when calling functions within constraint and objective

我的代码出现以下错误,我完全不知道错误的来源:

@error: Equation Definition
Equation without an equality (=) or inequality (>,<)
true
STOPPING...

我正在寻求确定解决方案 'x',该解决方案可最小化函数 'was_constraint' 的结果,但要满足 'warf_moodys_constraint' 设置的约束条件。函数 return 是一个浮点值,当我将初始起始向量 'x' 分别传递给每个函数时,我没有收到任何源自这些函数的错误。谁能告诉我哪里可能出错了?

def was_constraint(sol_g, df, orig):
    sol = gekko_to_numpy(sol_g)
    x1 = orig.loc["Denominator","WAS"]*orig.loc["Current","WAS"]
    x2 = (sol*df["All-In Rate"]).sum()/100
    y1 = orig.loc["Denominator","WAS"]+sum(sol)
    return y1/(x1+x2)

def warf_moodys_constraint(sol_g, df, orig):
    sol = gekko_to_numpy(sol_g)
    x1 = orig.loc["Denominator","Moodys WARF"]*orig.loc["Current","Moodys WARF"]
    x2 = sum(np.where(sol > 0, sol*df["Moody's WARF"], 0))
    y1 = orig.loc["Denominator","Moodys WARF"] +sum(np.where(sol > 0, sol, 0))
    return 3000 - (x1+x2)/y1 

def gekko_to_numpy(sol_g):
    res = np.zeros(len(sol_g))
    for i in range(len(sol_g)):
        res[i] = sol_g[i].value.value
    return res

clo_data = pd.read_excel('CLO.xlsx', sheet_name='CLO')
m = GEKKO()
x = [m.Var() for i in range(len(clo_data["Holdings"]))]

for i in range(len(clo_data["Lower Bound"])):
    x[i].lower = 0
    x[i].upper = 1000000

m.Equation(warf_moodys_constraint(x, clo_data, metrics)>=0)
m.Obj(was_constraint(x, clo_data, metrics))
m.options.IMODE = 3 #steady state optimization
m.solve()

您需要根据 Gekko 变量定义方程。将 Gekko 变量转换为 Numpy 数组的方法无法定义方程,因为 Gekko 不会将 call-backs 转换为 Python 函数。

def gekko_to_numpy(sol_g):
    res = np.zeros(len(sol_g))
    for i in range(len(sol_g)):
        res[i] = sol_g[i].value.value
    return res

Gekko 在 运行 文件夹中构建了 gk_model0.apm 模型,您可以使用 m.open_folder() 查看该文件夹。当您使用 m.solve() 求解时,Gekko 将模型编译为 byte-code 并使用稀疏非线性求解器(例如 IPOPTAPOPT 求解)。如果您不能使用 Gekko 变量,那么 scipy.opitimize.minimize() 函数可能是更好的选择。这里有一个tutorial with that optimizer.