8

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.

Nathaniel
  • 3,230
  • 11
  • 18
Radovan Omorjan
  • 241
  • 2
  • 8

3 Answers3

9

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]]
John Hedengren
  • 12,068
  • 1
  • 21
  • 25
  • 1
    I think I figured out now. Thank you. Wish you good luck in developing gekko – Radovan Omorjan Apr 04 '19 at 18:20
  • I'm glad to hear that. Let us know if we can develop new features that facilitate your application. – John Hedengren Apr 05 '19 at 12:20
  • 1
    Thank you for your answer. I might miss some additional source of information regarding Gekko, but the first thing a potential user finds about Gekko is its Documentation and the very useful 18 Application Examples. I think that Documentation will be much better if the m.Array() is explained with few more sentences and maybe a single additional example using m.Array() in more details. Best Regards, Radovan – Radovan Omorjan Apr 06 '19 at 17:06
  • Thanks for the suggestion. Here is one example (Problem #3): https://gekko.readthedocs.io/en/latest/examples.html?highlight=array Questions like yours are also very helpful on StackOverflow. – John Hedengren Apr 06 '19 at 18:50
  • Thank you for pointing me out to these examples. I think I've already examined them many times by expploring and experimenting with Gekko. One more question, if you do not mind. I excuse myself if that is already answered somewhere. Regarding the first example (System of linear equations) I tried and failed to give m.Equations([eq]) something like eq = np.dot(A,x) == b. How could one solve a large system of linear equations? Does m.Equations() even accepts a vector of boolean expression obtained by matrix manipulation (multiplication, inverting, transposing)? – Radovan Omorjan Apr 07 '19 at 08:40
  • I added a couple examples and a few new features to Gekko yesterday to help with large and sparse matrices. Please see https://github.com/BYU-PRISM/GEKKO/blob/master/examples/test_qobj_axb.py and https://github.com/BYU-PRISM/GEKKO/blob/master/examples/test_qobj_axb_sparse.py (for sparse matrices). The new features will be supported in the new release 0.2rc6 that should also be out today. It will be available with "pip install gekko==0.2rc6". – John Hedengren Apr 16 '19 at 00:03
  • Additional matrix operation examples with linear programming (dense and sparse matrices) are available: https://apmonitor.com/pdc/index.php/Main/LinearProgramming The `axb` and `state_space` functions are described in more detail in the documentation: https://gekko.readthedocs.io/en/latest/model_methods.html?highlight=axb#pre-built-objects – TexasEngineer May 30 '22 at 21:36
4

This one will work as well.

#Equations
m.Equation(np.prod(np.asarray(x))>=25)
m.Equation(np.sum(np.asarray(x)**2)==eq)
Radovan Omorjan
  • 241
  • 2
  • 8
1

Here is a simple example of solving the system of linear equations and the example of using for loop for many equations.

import numpy as np
from gekko import GEKKO

m = GEKKO(remote=False)

# Random 3x3
A = np.random.rand(3,3)
# Random 3x1
b = np.random.rand(3)
# Gekko array 3x1
x = m.Array(m.Var,(3))

# solve Ax = b
eqn = np.dot(A,x)
for i in range(3):
   m.Equation(eqn[i]==b[i])
m.solve(disp=False)
X = [x[i].value for i in range(3)]
print(X)
print(b)
print(np.dot(A,X))

with the the correct output. With the result X (np.dot(A,X)==b) - correct!

[[-0.45756768428], [1.0562541773], [0.10058435163]]
[0.64342498 0.34894335 0.5375324 ]
[[0.64342498]
[0.34894335]
[0.5375324 ]]

In the recent Gekko 0.2rc6 there is also introduced axb() function for linear programing. This might be the same problem solved with this function, but I am not sure how to get the correct result.

m = GEKKO(remote=False)

# Random 3x3
A = np.random.rand(3,3)
# Random 3x1
b = np.random.rand(3)
# Gekko array 3x1
x = m.Array(m.Var,(3))

# solve Ax = b
m.axb(A,b,x=x)
m.solve(disp=False)
X = [x[i].value for i in range(3)]
print(X)
print(b)
print(np.dot(A,X))

but it seems I missed something because the output is not the solution??? With the result X (np.dot(A,X)==b) - is not correct!

[[0.2560342704], [0.7543346092], [-0.084190799732]]
[0.27262652 0.61028723 0.74616952]
[[0.4201021 ]
[0.5206979 ]
[0.39195592]]
Radovan Omorjan
  • 241
  • 2
  • 8
  • Could you post this as a new question? I think I missed your answer that had another question. – John Hedengren Apr 21 '21 at 15:50
  • Hello, I am not sure about the question. It might be the one about the m.axb() function. Here is the link to the question https://stackoverflow.com/q/67217773/5011402 – Radovan Omorjan Apr 22 '21 at 17:05