[英]Gekko: Using previous defined variables
这是我第一次尝试使用 Gekko 优化。
我的问题是:
有 100 美元,我必须决定分配多少(百分比)用于购买或储蓄。 存1美元的利润是15美元,买入的利润是12美元。 我必须考虑分配用于储蓄的金额的 5%,因此结果如下:
x_save: % saving
factor = 0.05 </pre>
save_quantity = min(x_save * 1.05, 100)
我在 Gekko 上写这篇文章的尝试如下:
m = GEKKO()
m.options.SOLVER = 3
### Initialize variables
x_save = m.Var(value=0 , lb=0 , ub=60, integer=True)
money = 100
saving_return = 15
buying_return = 12
factor = 1.05
save_quantity = m.min2(x_save * factor, 100) * money
buy_quantity = money - save_quantity
m.Obj(-(save_quantity * saving_return + buy_quantity * buying_return))
m.solve(disp=False)
我有这样的错误 (-(((i7) (15))+(((100-(100-i6))) (12))))
请问,有人可以帮我吗? 我不知道我是否以正确的方式写我的问题
对save_quantity
使用上限约束,而不是m.min2()
function。 另外,方程式应该是m.Equation(save_quantity==x_save/100 * factor * money)
而不是m.Equation(save_quantity==x_save * factor * money)
吗?
from gekko import GEKKO
m = GEKKO()
m.options.SOLVER = 1
x_save = m.Var(value=0, lb=0, ub=60, integer=True)
money = 100
saving_return = 15
buying_return = 12
factor = 1.05
save_quantity = m.Var(ub=100)
m.Equation(save_quantity==x_save/100 * factor * money)
buy_quantity = money - save_quantity
m.Maximize(save_quantity * saving_return \
+ buy_quantity * buying_return)
m.solve(disp=False)
print(x_save.value[0])
print(save_quantity.value[0])
解决方案是:
x_save = 60.0
save_quantity = 63.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.