How to implement max3 on each element of the GEKKO array?

Viewed 21

I want to apply max3 function in GEKKO on each element of a GEKKO array. For example:

from gekko import GEKKO

m = GEKKO()

a = m.Array(m.Var, dim = (5,))
b = m.Array(m.Var, dim = (5,))

I would like to compare each element of a with b and get a 5 dimensional GEKKO vector. So, I implement

c = m.max3(a, b)
print(c)
print(c.value)

It results into a GEKKO variable of size 1 with value 0. Is there a way to get a 5 dimensional GEKKO vector without using for loop?

Thank you.

1 Answers

The m.max3() function only operates on single gekko parameters or variables, not arrays. A list comprehension is a single-line statement to create the maximum result.

from gekko import GEKKO
m = GEKKO()

# define a and b
a = m.Array(m.Param, dim = (5,))
b = m.Array(m.Param, dim = (5,))
for i in range(5):
    a[i].value = i; b[i].value = i**2-4

# create c as a list comprehension
c = [m.max3(a[i], b[i]) for i in range(5)]

m.solve()

for x in [a,b,c]:
    print(x)

The result is:

 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :   1.749999998719431E-002 sec
 Objective      :   0.000000000000000E+000
 Successful solution
 ---------------------------------------------------
 
[[0.0] [1.0] [2.0] [3.0] [4.0]]
[[-4.0] [-3.0] [0.0] [5.0] [12.0]]
[[0.0], [1.0], [2.0], [5.0], [12.0]]
Related