I'm trying to solve a non linear mixed integer problem with gekko and im getting into a loop

Viewed 66

The error that im getting is the following "@error: Inequality Definition". I think that I'm getting into a loop. I'm also not pretty confident about my way that i set my parameters. I'm new in gekko and i can't find my fault on my code. The error occurs in the line of "m.solve()".

m=GEKKO()
#create variables
demand = m.Array(m.Param,5)
t=0
for v in [1150,2320,3200,4100,5400]:
    demand[t].value = v
    t=t+1
   
A=m.Array(m.Param,(2,5))
t=0
g=0

for v in [2,2,4,6,2.3,1,1.55,3.85,2,5.5]:
    A[g][t].value = v
    t=t+1
    if t==5:
        t=0
        g=1

t=0
g=0         
Start=m.Array(m.Param,(2,5))
for v in [180,220,300,70,150,100,120,150,20,200]:
    Start[g][t].value = v
    t=t+1
    if t==5:
        t=0
        g=1                
t=0
g=0                    
C=m.Array(m.Param,(2,5)) 
for v in [15,25,45,40,25,3,16.2,42,3,60]:       
    C[g][t].value = v
    t=t+1
    if t==5:
        t=0
        g=1                
 #create variables
ni = 2; nj = 5; 
y = m.Array(m.Var,(ni,nj),lb=0,ub=1, integer=True)    
x = m.Array(m.Var,(ni,nj),lb=0,integer=True)     
# add limitations to model                       
m.Equation(y[i][j] * Start[i][j] - x[i][j] <= 0 for i in range(ni)  for j in range(nj))                                               
m.Equation(demand[j] <=m.sum([m.sum([x[i][j] for i in range(ni) ])])for j in range(nj))
#add optimization equation

m.Minimize(m.sum([m.sum([A[i][j] * x[i][j] - C[i][j] * y[i][j] * x[i][j]**0.5 for i in range(ni)])  for j in range(nj)]))                                                                             
  
m.options.IMODE=3
m.options.Solver=1
m.solve()
print(x)
print(y)
print(m.options.OBJFCNVAL)
1 Answers

Try using m.Equations() and generate a list of equations with square brackets. I re-wrote your Objective so that it is easier to understand. You can have multiple objectives and Gekko adds them together so it is already doing the summation.

from gekko import GEKKO
m=GEKKO()
#create variables
demand = m.Array(m.Param,5)
for i,v in enumerate([1150,2320,3200,4100,5400]):
    demand[i].value = v
   
A=m.Array(m.Param,(2,5))
t=0; g=0
for v in [2,2,4,6,2.3,1,1.55,3.85,2,5.5]:
    A[g][t].value = v
    t=t+1
    if t==5:
        t=0; g=1

t=0; g=0         
Start=m.Array(m.Param,(2,5))
for v in [180,220,300,70,150,100,120,150,20,200]:
    Start[g][t].value = v
    t=t+1
    if t==5:
        t=0; g=1                
t=0; g=0                    
C=m.Array(m.Param,(2,5)) 
for v in [15,25,45,40,25,3,16.2,42,3,60]:       
    C[g][t].value = v
    t=t+1
    if t==5:
        t=0; g=1                
#create variables
ni = 2; nj = 5; 
y = m.Array(m.Var,(ni,nj),lb=0,ub=1, integer=True)    
x = m.Array(m.Var,(ni,nj),lb=0,integer=True)     
# add limitations to model                       
m.Equations([[y[i][j] * Start[i][j] - x[i][j] <= 0 \
              for i in range(ni)] \
                for j in range(nj)])                                               
m.Equations([demand[j] <=m.sum([m.sum([x[i][j] \
              for i in range(ni) ])])
                for j in range(nj)])

#add optimization equation
for i in range(ni):
    for j in range(nj):
        m.Minimize(A[i][j]*x[i][j] - C[i][j]*y[i][j] * x[i][j]**0.5)
        
m.options.IMODE=3
m.options.Solver=1
m.solve()
print(x)
print(y)
print(m.options.OBJFCNVAL)

This gives a solution that looks suspiciously easy to solve so you may want to check your equations one more time.

 --------- APM Model Size ------------
 Each time step contains
   Objects      :           10
   Constants    :            0
   Variables    :           80
   Intermediates:            0
   Connections  :           25
   Equations    :           25
   Residuals    :           25
 
 Number of state variables:             45
 Number of total equations: -           25
 Number of slack variables: -           15
 ---------------------------------------
 Degrees of freedom       :              5
 
 ----------------------------------------------
 Steady State Optimization with APOPT Solver
 ----------------------------------------------
Iter:     1 I:  0 Tm:      0.00 NLPi:    2 Dpth:    0 Lvs:    0 Obj:  5.68E+04 Gap:  0.00E+00
 Successful solution
 
 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :   1.529999999911524E-002 sec
 Objective      :    56760.0000000000     
 Successful solution
 ---------------------------------------------------
 
[[[1150.0] [2320.0] [3200.0] [4100.0] [5400.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]]]
Related