Gekko:使用之前定义的变量

Gekko: Using previous defined variables

这是我第一次尝试使用 Gekko 优化。

My problem is:

Having 100 dollars, I have to decide how much money (in percentage) allocate for buying or saving. The profit of saving 1 dollar is 15 USD and the profit of buying is 12 USD. I have to consider a factor of 5% to the amount allocated for saving, so it results on:

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() 函数上使用上限约束。另外,方程应该是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