How can set variable names in an "Array" of variables in GEKKO

Viewed 216

I have a (n x p) matrix of FV parameters in a MINLP problem.

When I define the FV's using list comprehension as below, I end up getting an in feasibility

for i in range(n):
    test[i] = [m.FV(lb=0, ub=1, integer=True, name="example) for j in range(p)]

On the other hand, when I use an array I do get a solution quickly.

for i in range(n):
   test[i] = m.Array(m.FV, p, lb=0, ub=1, integer=True)

I would like to add variable names to my FV's so that I can quickly troubleshoot and select the parameters in the optimal solution. This is fairly straight forward with the list comprehension, but with the array my only option seems to make duplicate names, which isn't too useful.

1 Answers

The issue is that renaming the variable as example or some other name is causing the test variable to no longer be an integer type. The underlying APMonitor engine recognizes variables that start with int as integer type. Changing to something else makes it so the solver is not required to find an integer solution and it is successful at finding a non-integer solution.

It is generally much easier to solve a problem as a continuous versus integer problem. I recommend initializing with:

m.options.SOLVER = 1
m.solver_options = ['minlp_as_nlp 1']
m.solve()

and then solve again with to get an integer solution (if possible):

m.options.SOLVER = 1
m.solver_options = ['minlp_as_nlp 0']
m.solve()

If it fails on the second solve then there is likely no integer solution or else the solver failed to converge in the number of required iterations. You can increase the iterations with:

m.options.MAX_ITER=1000

If it is having a hard time finding an integer solution then try adding an additional option to do a depth-first search:

m.options.SOLVER = 1
m.solver_options = ['minlp_as_nlp 0', \
                    'minlp_branch_method 1']
m.options.MAX_ITER=1000
m.solve()

There is more information on setting solver options in Gekko: How to set solver options (such as error tolerance) in Python Gekko? and more information on the solver options.

Related