Gekko uses gradient-based solvers so the equations shouldn't change iteration-to-iteration. There is a way to get the count of non-zero variables that is compatible with gradient based solvers. Here is an alternative form to give you count and max for your x
vector. This uses if3
, max3
, and min3
as found in the documentation on Model Building Functions with logical conditions.
from gekko import GEKKO
import numpy as np
# Setup gekko (taken from their MINLP tutorial with more iterations).
m = GEKKO(remote=False)
# Define variables as arrays.
y = m.Array(m.Var, (7), value=1, lb=1, ub=12, integer=True)
x = m.Array(m.Var, (18), value=0, lb=0, ub=8, integer=True)
# Example of the user-defined target values b as constraints (actually input args).
m.Equation(x[2] + y[1] == 7)
m.Equation(x[12] + y[2] == 5)
# This is the L0 constraint.
# m.Equation(np.count_nonzero(x) >= np.max(x))
eps = 0.05 # decision point for a "zero" value
count = m.sum([m.if3(x_i-eps,0,1) for x_i in x])
max_x = 0
for x_i in x:
max_x = m.Intermediate(m.max3(max_x,x_i))
m.Equation(count >= max_x)
# Finally, the objective function (intermediates for readability).
k = np.array([m.min3(y_i, 3) for y_i in y])
x_cost = m.Intermediate(m.sum(x * (x + 1)))
y_cost = m.Intermediate(m.sum((k - 1) * (k + 2) + 2.5 * (y - k) * (y + k - 3)))
m.Minimize(x_cost + y_cost)
# Solve.
m.options.SOLVER = 3 # Initialize with IPOPT
m.solve(disp=True)
m.options.SOLVER = 1 # APOPT is an MINLP solver
m.solver_options = ('minlp_maximum_iterations 500',
'minlp_max_iter_with_int_sol 10',
'minlp_as_nlp 0',
'nlp_maximum_iterations 50',
'minlp_branch_method 1',
'minlp_integer_tol 0.05',
'minlp_gap_tol 0.01')
m.solve(disp=True)
I used IPOPT to give a non-integer solution for initialization and then APOPT to find the optimal Integer solution. This approach produces a successful solution with x
>>> x
array([[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0],
[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]],
dtype=object)
and y
>>> y
array([[1.0], [7.0], [5.0], [1.0], [1.0], [1.0], [1.0]], dtype=object)
You probably aren't intending a zero solution for x
so you may need to add constraints such as m.Equation(count>=n)
or change eps=0.05
to find a non-zero value or push the solver away from zero at a local minimum. With m.Equation(count>=n)
and eps=0.5
it finds a better solution:
n=3, obj=63
n=5, obj=52
Here is the solution of x
and y
when obj=52
(best solution found).
>>> x
array([[0.0], [1.0], [4.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0],
[0.0], [1.0], [0.0], [2.0], [0.0], [0.0], [0.0], [1.0], [0.0]],
dtype=object)
>>> y
array([[1.0], [3.0], [3.0], [1.0], [1.0], [1.0], [1.0]], dtype=object)
You may need to adjust eps
that is used in if3
to adjust when a value is counted as non-zero or adjust minlp_integer_tol 0.05
that is a solver option to determine the integer tolerance. Here is the final script with the additional inequality constraint that gives the best solution.
from gekko import GEKKO
import numpy as np
# Setup gekko (taken from their MINLP tutorial with more iterations).
m = GEKKO(remote=False)
# Define variables as arrays.
y = m.Array(m.Var, (7), value=1, lb=1, ub=12, integer=True)
x = m.Array(m.Var, (18), value=0, lb=0, ub=8, integer=True)
# Example of the user-defined target values b as constraints (actually input args).
m.Equation(x[2] + y[1] == 7)
m.Equation(x[12] + y[2] == 5)
# This is the L0 constraint.
# m.Equation(np.count_nonzero(x) >= np.max(x))
eps = 0.5 # threshold for a "zero" value
count = m.sum([m.if3(x_i-eps,0,1) for x_i in x])
max_x = 0
for x_i in x:
max_x = m.Intermediate(m.max3(max_x,x_i))
m.Equation(count >= max_x)
# Finally, the objective function (intermediates for readability).
k = np.array([m.min3(y_i, 3) for y_i in y])
x_cost = m.Intermediate(m.sum(x * (x + 1)))
y_cost = m.Intermediate(m.sum((k - 1) * (k + 2) + 2.5 * (y - k) * (y + k - 3)))
m.Minimize(x_cost + y_cost)
m.Equation(count>=5)
# Solve.
m.options.SOLVER = 3 # Initialize with IPOPT
m.solve(disp=True)
m.options.SOLVER = 1 # APOPT is an MINLP solver
m.solver_options = ('minlp_maximum_iterations 500',
'minlp_max_iter_with_int_sol 10',
'minlp_as_nlp 0',
'nlp_maximum_iterations 50',
'minlp_branch_method 1',
'minlp_integer_tol 0.05',
'minlp_gap_tol 0.01')
m.solve(disp=True)
You may be able to adjust n
or some solver options to get a better solution. I hope this helps point you in the right direction.