,<)" when calling functions within constraint and objective-6ren"> ,<)" when calling functions within constraint and objective-我的代码出现以下错误,我完全不知道错误的来源: @error: Equation Definition Equation without an equality (=) or inequality (-6ren">
gpt4 book ai didi

python - 壁虎错误 : "Equation without an equality (=) or inequality (>,<)" when calling functions within constraint and objective

转载 作者:行者123 更新时间:2023-12-04 11:40:15 24 4
gpt4 key购买 nike

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

@error: Equation Definition
Equation without an equality (=) or inequality (>,<)
true
STOPPING...
我正在寻求确定最小化函数 'was_constraint' 的结果的解决方案 'x',但要满足由 'warf_moodys_constraint' 设置的约束。这些函数返回一个浮点值,当我将初始起始向量“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 不会对 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 将模型编译成字节码,并使用稀疏非线性求解器(例如 IPOPT)进行求解。或 APOPT .如果您不能使用 Gekko 变量,那么可能是 scipy.opitimize.minimize()函数将是更好的选择。这是一个 tutorial with that optimizer .

关于python - 壁虎错误 : "Equation without an equality (=) or inequality (>,<)" when calling functions within constraint and objective,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63892478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com