Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use arrays in gekko optimizer for python

Tags:

python

gekko

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.

like image 929
Radovan Omorjan Avatar asked Oct 23 '18 08:10

Radovan Omorjan


People also ask

What is Gekko optimization suite?

GEKKO Optimization Suite¶. Overview¶. GEKKO is a Python package for machine learning and optimization of mixed-integer and differential algebraic equations. It is coupled with large-scale solvers for linear, quadratic, nonlinear, and mixed integer programming (LP, QP, NLP, MILP, MINLP).

What is Gekko in Python?

Overview ¶ GEKKO is a Python package for machine learning and optimization of mixed-integer and differential algebraic equations. It is coupled with large-scale solvers for linear, quadratic, nonlinear, and mixed integer programming (LP, QP, NLP, MILP, MINLP).

What is Gekko array in PDE?

If the model contains a partial differential equation, the discretization in the other dimensions is performed with Gekko array operations as shown in the hyperbolic and parabolic PDE Gekko examples. Create an n-dimensional array (as defined in tuple input dimension ) of GEKKO variables of type type .

What is the value of Param in Gekko?

Param ( value = m. time) m. Equation ( k*y. dt () == -t*y) m. options. IMODE = 4 Solve this optimization problem from a web-browser interface or with GEKKO Python. eq = m. Param ( value = 40)


1 Answers

You can use the m.Array GEKKO function to create Variable, Parameter, FV, MV, SV, or CV as 1D or multi-dimensional arrays. Here is an example of using m.Array to declare the variables. In subsequent steps, I define the initial guess and the bounds.

import numpy as np
from gekko import GEKKO    
m = GEKKO()
x = m.Array(m.Var,(4))
# intial guess
ig = [1,5,5,1]
# lower bounds
i = 0
for xi in x:
    xi.value = ig[i]
    xi.lower = 1
    xi.upper = 5
    i += 1
#Equations
m.Equation(np.prod(x)>=25)
m.Equation(sum(x**2)==40)
#Objective
m.Obj(x[0]*x[3]*(x[0]+x[1]+x[2])+x[2])
m.solve()
print(x)

Here are the results:

The solution was found.

The final value of the objective function is    17.0140171270735     

 ---------------------------------------------------
 Solver         :  IPOPT (v3.12)
 Solution time  :   9.999999980209395E-003 sec
 Objective      :    17.0140171270735     
 Successful solution
 ---------------------------------------------------

[[1.000000057] [4.74299963] [3.8211500283] [1.3794081795]]
like image 123
John Hedengren Avatar answered Oct 26 '22 23:10

John Hedengren