Why Indexing error returns in python GEKKO

Viewed 97

I want to transform LINGO code to Python GEKKO code. Here is Lingo code, lingo results and gekko codes. I cant write second and third constraints. It returns indexing error but, I dont understand why? Can someone help? (It's a graph coloring problem)

from gekko import GEKKO    
import numpy as np
m = GEKKO(remote=False)
# x = m.Array(m.Var,(7,5),lb=0,ub=1,integer=True)
x = m.Array(m.Var,(6,6),lb=0,ub=1,integer=True)
y= np.array([1, 2, 3, 4, 5, 6])
country=6
arcs=np.array([[1,3],
[5,4],
[3,6],
[2,4],
[2,5],
[2,6],
[4,5],
[4,6]])

for i in range(6):
    m.Minimize(y)


for i in range(6):
    # for j in range(2):
        # m.Equation(m.sum(x[i,j])==1)
        m.Equation(m.sum(x[i,:])==1) 


for k in range (6):
     for i in range(8):
            m.Equation(x[arcs[i,1],k]+x[arcs[i,2],k]<=1)        
             # m.Equation(x[arcs[i,1],k]+x[arcs[i,2],k])<=1)      
             # m.Equation(m.sum(x[arcs[i,1],k],x[arcs[i,2],k]))<=1) 

Revised version is=

from gekko import GEKKO    
import numpy as np

m = GEKKO(remote=False)

x = m.Array(m.Var,(6,6),lb=0,ub=1,integer=True)
y = m.Array(m.Var,6,lb=0,ub=1,integer=True)
y= np.array([1, 2, 3, 4, 5, 6])
country=6
arcs=np.array([[1,3],
[1,4],
[3,4],
[3,4],
[4,5],
[2,6],
[4,5],
[4,6]])

for i in range(6):
    m.Minimize(y[i])

for i in range(6):
    m.Equation(m.sum(x[i,:])==1) 

for k in range (6):
     for i in range(8):
            m.Equation(x[arcs[i,0]-1,k-1]+x[arcs[i,1]-1,k-1]<=1)


for i in range(6): 
           m.Equation(m.sum(x[i,:]<=y[i])

m.options.solver = 1
m.solve()
print('Objective Function: ' + str(m.options.objfcnval))
print(x)
print(y)

now it gives invalid syntax error for m.solve and m options?

1 Answers

A couple things that you need to consider for Python:

  • Lists and arrays are zero-index so you need to shift them by -1 relative to the LINDO / LINGO language.
  • The objective function y is a list of constants. Gekko generates warnings that there are no variables in that expression.

Here is a corrected version of your Python script that you probably need to supplement with a correct objective statement and any additional equations that are needed.

from gekko import GEKKO    
import numpy as np
m = GEKKO(remote=False)
x = m.Array(m.Var,(6,6),lb=0,ub=1,integer=True)
y= np.array([1, 2, 3, 4, 5, 6])
country=6
arcs=np.array([[1,3],
[1,4],
[3,4],
[2,4],
[2,5],
[2,6],
[4,5],
[4,6]])

for i in range(6):
    m.Minimize(y[i])

for i in range(6):
    m.Equation(m.sum(x[i,:])==1) 

for k in range (6):
     for i in range(8):
            m.Equation(x[arcs[i,0]-1,k]\
                      +x[arcs[i,1]-1,k]<=1)

m.solve()

Response to Edit

The revised version is missing a closing parenthesis on the m.sum(). Here is a corrected version.

from gekko import GEKKO    
import numpy as np

m = GEKKO(remote=False)

x = m.Array(m.Var,(6,6),lb=0,ub=1,integer=True)
y = m.Array(m.Var,6,lb=0,ub=1,integer=True)
y= np.array([1, 2, 3, 4, 5, 6])
country=6
arcs=np.array([[1,3],[1,4],[3,4],[3,4],[4,5],[2,6],[4,5],[4,6]])

for i in range(6):
    m.Minimize(y[i])
for i in range(6):
    m.Equation(m.sum(x[i,:])==1) 
for k in range (6):
     for i in range(8):
            m.Equation(x[arcs[i,0]-1,k-1]+x[arcs[i,1]-1,k-1]<=1)

for i in range(6): 
           m.Equation(m.sum(x[i,:])<=y[i])
m.options.solver = 1
m.solve()
print('Objective Function: ' + str(m.options.objfcnval))
print(x)
print(y)

You can find additional tips on troubleshooting gekko applications with tutorial 18.

Related