简体   繁体   English

Gekko:使用先前定义的变量

[英]Gekko: Using previous defined variables

It is my first try using Gekko optimization.这是我第一次尝试使用 Gekko 优化。

My problem is:我的问题是:

Having 100 dollars, I have to decide how much money (in percentage) allocate for buying or saving.有 100 美元,我必须决定分配多少(百分比)用于购买或储蓄。 The profit of saving 1 dollar is 15 USD and the profit of buying is 12 USD.存1美元的利润是15美元,买入的利润是12美元。 I have to consider a factor of 5% to the amount allocated for saving, so it results on:我必须考虑分配用于储蓄的金额的 5%,因此结果如下:

x_save: % saving
factor = 0.05 </pre>
save_quantity = min(x_save * 1.05, 100)

My attempt writing this on Gekko is as follows:我在 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)

And I have an error like this (-(((i7) (15))+(((100-(100-i6))) (12))))我有这样的错误 (-(((i7) (15))+(((100-(100-i6))) (12))))

Please, can someone help me?请问,有人可以帮我吗? I do not know if I am writting my problem in the right way我不知道我是否以正确的方式写我的问题

Use an upper bound constraint on save_quantity instead of the m.min2() function.save_quantity使用上限约束,而不是m.min2() function。 Also, should the equation be m.Equation(save_quantity==x_save/100 * factor * money) instead of m.Equation(save_quantity==x_save * factor * money) ?另外,方程式应该是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])

The solution is:解决方案是:

x_save = 60.0
save_quantity = 63.0

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

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