I tried to convert an example from gekko
python
optimizer by using the list, array x[]
instead of variables x1
..x4
. This is the code which gives the result, but I think it is not correct
from gekko import GEKKO
import numpy as np
# Initialize Model
m = GEKKO(remote=False)
#help(m)
#define parameter
eq = m.Param(value=40)
#initialize variables
x = [m.Var(value=1,lb=1,ub=5) for i in range(4)]
x[1].value=5
x[2].value=5
#Equations
m.Equation(np.prod([x[i] for i in range(0,4)])>=25)
m.Equation(np.sum([x[i]**2 for i in range(0,4)])==eq)
#Objective
m.Obj(x[0]*x[3]*(x[0]+x[1]+x[2])+x[2])
#Set global options
m.options.IMODE = 3 #steady state optimization
#Solve simulation
m.solve() # solve on public server
#Results
print('')
print('Results')
print('x1: ' + str(x[0].value))
print('x2: ' + str(x[1].value))
print('x3: ' + str(x[2].value))
print('x4: ' + str(x[3].value))
Please anyone could help me out how to use list, array of variables in gekko
. This seems to me less elegant and I was wondering is there is a way of using Array() function instead of Var(). I can not figure out how and when we can use Array() function.