Helson
Helson

Reputation: 33

Variable indexing in gekko

How can I index variables to solve a nonlinear optimization problem in python gekko package? For example, how can I write the objective function below into a single equation indexing the variable x?

For example:

Upvotes: 2

Views: 222

Answers (1)

John Hedengren
John Hedengren

Reputation: 14346

Use the m.Array() function to create a variable (or parameter) array. The np.prod() and m.sum() functions are used to define the objective function m.Maximize() and the equation m.Equation().

import numpy as np
from gekko import GEKKO
n = 5
m = GEKKO(remote=False)
x = m.Array(m.Var,n,value=1)
m.Maximize(np.prod([m.sqrt(xi) for xi in x]))
m.Equation(m.sum(x)==100)
m.solve(disp=False)
print(x)

Solution:

[[20.0] [20.0] [20.0] [20.0] [20.0]]

Upvotes: 2

Related Questions