2

I am trying to solve a MINLP problem using GEKKO. My code is the following:

m = GEKKO(remote = True)
m.options.SOLVER = 3

m.solver_options = ['minlp_maximum_iterations 500', \
                # minlp iterations with integer solution
                'minlp_max_iter_with_int_sol 10', \
                # treat minlp as nlp
                'minlp_as_nlp 0', \
                # nlp sub-problem max iterations
                'nlp_maximum_iterations 50', \
                # 1 = depth first, 2 = breadth first
                'minlp_branch_method 1', \
                # maximum deviation from whole number
                'minlp_integer_tol 0.05', \
                # covergence tolerance
                'minlp_gap_tol 0.01']

# Array Variable
rows  = nb_phases + 3*b_max*(nb_phases+1)#48
columns = 1
x = np.empty((rows,columns),dtype=object)
for i in range(3*nb_phases*b_max+nb_phases+1):
    for j in range(columns):
        x[i,j] = m.Var(value = xinit[i,j], lb = LB[i,j], ub = UB[i,j], integer = False)

for i in range(3*nb_phases*b_max+nb_phases+1, (3*nb_phases+3)*b_max+nb_phases):
    for j in range(columns):
        x[i,j] = m.Var(value = xinit[i,j], lb = LB[i,j], ub = UB[i,j], integer = True)

# Constraints
#m.axb(A = A,b = B, x = x, etype = '<=', sparse = False)
m.axb(A,B, etype = '<=',sparse=False) 

#m.axb(A = A_eq,b = B_eq, x = x, etype = '=', sparse = False)
m.axb(A_eq,B_eq, etype = '=',sparse=False)

for i in range(rows):
    for j in range(columns):
        m.Minimize((x[i,j]-i*j)**2)

#Solver
m.solve(disp = True)

When calling the axb function, if I declare the variable x in the arguments as the following:

m.axb(A = A,b = B, x = x, etype = '<=', sparse = False)

I get the error : List x must be composed of GEKKO parameters or variables. I don't really understand why I get this error since x is a gekko variable.

If I don't declare the variable x in the arguments of the axb function:

m.axb(A,B, etype = '<=',sparse=False)

I get the following error: AXB Missing Configuration File, Error: AXB object missing: axb1.txt, Example config file: axb1.txt

I was thinking maybe the issue is that x is not defined as an array. Therefore, considering x[i,j], I tried to explicit the equation Ax<=b by coding the matrix product A.x in a loop to avoid calling m.axb but I am not sure how to declare the equations after. My code is the following:

Ax = []

for i in range(rows):
    temp = []
    for j in range(columns):
        temp.append(A[i,j]*x[j,0])
    Ax.append(sum(temp))

for i in range(rows):
    m.Equations(Ax[i] <= B[i])

I get the error: 'int' object is not subscriptable

Is anyone able to help me figure out how to solve this problem? Is there a way of defining x as an array? (Since some of its elements are integers and some aren't)

Thanks a lot !

Zineb
  • 97
  • 6

1 Answers1

1

Here is a solution that works with the newer version of Gekko that is not yet released but is available on GitHub. You'll need to put the newest version of gekko.py (v1.0) in the Lib/site_packages/gekko folder and the local executable (apm.exe for Windows, apm_mac for MacOS, apm for Linux) in the Lib/site_packages/gekko/bin folder to use remote=False.

from gekko import GEKKO
import numpy as np
m = GEKKO(remote = False)
m.options.SOLVER = 3
nb_phases = 2
b_max = 3

m.solver_options = ['minlp_maximum_iterations 500', \
                # minlp iterations with integer solution
                'minlp_max_iter_with_int_sol 10', \
                # treat minlp as nlp
                'minlp_as_nlp 0', \
                # nlp sub-problem max iterations
                'nlp_maximum_iterations 50', \
                # 1 = depth first, 2 = breadth first
                'minlp_branch_method 1', \
                # maximum deviation from whole number
                'minlp_integer_tol 0.05', \
                # covergence tolerance
                'minlp_gap_tol 0.01']

# Array Variable
rows  = nb_phases + 3*b_max*(nb_phases+1)#48
columns = 1
xinit = np.ones(rows)
LB = np.zeros(rows)
UB = np.ones(rows)*10.0
#x = m.Array(m.Var,(rows))
x = np.empty(rows,dtype=object)
for i in range(3*nb_phases*b_max+nb_phases+1):
    x[i] = m.Var(value = xinit[i], lb = LB[i], ub = UB[i], integer = False)

for i in range(3*nb_phases*b_max+nb_phases+1, (3*nb_phases+3)*b_max+nb_phases):
    x[i] = m.Var(value = xinit[i], lb = LB[i], ub = UB[i], integer = True)

# Constraints
#m.axb(A = A,b = B, x = x, etype = '<=', sparse = False)
A = np.ones((1,rows)); B = np.zeros(1)
m.axb(A,B,x,etype = '<=',sparse=False) 

#m.axb(A = A_eq,b = B_eq, x = x, etype = '=', sparse = False)
m.axb(A,B,x,etype = '=',sparse=False)

for i in range(rows):
    m.Minimize((x[i]-i)**2)

#Solver
m.options.SOLVER = 1
m.solve(disp = True)

This produces the solution:

 ----------------------------------------------------------------
 APMonitor, Version 1.0.0
 APMonitor Optimization Suite
 ----------------------------------------------------------------
 
 
 --------- APM Model Size ------------
 Each time step contains
   Objects      :  2
   Constants    :  0
   Variables    :  29
   Intermediates:  0
   Connections  :  58
   Equations    :  29
   Residuals    :  29
 
 Number of state variables:    29
 Number of total equations: -  2
 Number of slack variables: -  0
 ---------------------------------------
 Degrees of freedom       :    27
 
 ----------------------------------------------
 Steady State Optimization with APOPT Solver
 ----------------------------------------------
Iter:     1 I:  0 Tm:     -0.00 NLPi:    2 Dpth:    0 Lvs:    0 Obj:  7.71E+03 Gap:  0.00E+00
 Successful solution
 
 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :  0.019000000000000003 sec
 Objective      :  7714.
 Successful solution
 ---------------------------------------------------
John Hedengren
  • 12,068
  • 1
  • 21
  • 25
  • 1
    Dear John, thanks a lot for your answer. Now the code is running but the solution doesn't seem correct. I will post my new question in another post. Thanks a lot for your time. – Zineb Apr 28 '21 at 12:23
  • Thanks - I'll take a look at the new question. – John Hedengren Apr 28 '21 at 18:14