简体   繁体   中英

Gekko: Using previous defined variables

It is my first try using Gekko optimization.

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)

My attempt writing this on Gekko is as follows:

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))))

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. Also, should the equation be m.Equation(save_quantity==x_save/100 * factor * money) instead of 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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