Home > Mobile >  KeyError: (1, 5) Python
KeyError: (1, 5) Python

Time:12-05

I wrote code for linear programming. The code is below.


from pulp import LpProblem, LpVariable, LpStatus, LpMinimize, GLPK, value

M = 3

N = 5

a = range(1,M 1)

a1 = range(M)

b = range(N 1)

b1 =  range(N)

xindx = [(a[i],b[j]) for j in b1 for i in a1]

model =  LpProblem("Transportation LP Problem", LpMinimize)

x = LpVariable.dicts("X",xindx,0,None)

model  = 2190  x[1,1]   46650  x[1,2]   25110  x[1,3]   8040  x[1,4]   6720 * x[1,5] \

  1800*x[2,1]   24600  x[2,2]   50610  x[2,3]   46200  x[2,4]   57780  x[2,5] \

  1500*x[3,1]   45960  x[3,2]   24420  x[3,3]   7350  x[3,4]   6030  x[3,5],"Transportation cost"

model  = x[1,1]   x[1,2]   x[1,3]   x[1,4]   x[1,5] <= 300.0, "Supply Pt 1"

model  = x[2,1]   x[2,2]   x[2,3]   x[2,4]   x[2,5] <= 260.0, "Supply Pt 2"

model  = x[3,1]   x[3,2]   x[3,3]   x[3,4]   x[3,5] <= 258.0, "Supply Pt 3"

model  = x[1,1]   x[2,1]   x[3,1] <= 200.0, "Demand Pt 1"

model  = x[1,2]   x[2,2]   x[3,2] <= 100.0, "Demand Pt 2"

model  = x[1,3]   x[2,3]   x[3,3] <= 250.0, "Demand Pt 3"

model  = x[1,4]   x[2,4]   x[3,4] <= 185.0, "Demand Pt 4"

model  = x[1,5]   x[2,5]   x[3,5] <= 100.0, "Demand Pt 5"

model.solve(GLPK())

print ("Status:",LpStatus[model.status])

for v in model.variables():

         print(v.name,"=",v.varValue)

print ("Objective Function", value(model.objective))

Then I got this:


Warning (from warnings module):

  File "C:\Users\CTM\AppData\Local\Programs\Python\Python310\lib\site-packages\pulp\pulp.py", line 1352

    warnings.warn("Spaces are not permitted in the name. Converted to '_'")

UserWarning: Spaces are not permitted in the name. Converted to '_'

Traceback (most recent call last):

  File "D:\class.py", line 16, in <module>

    model  = 2190  x[1,1]   46650  x[1,2]   25110  x[1,3]   8040  x[1,4]   6720 * x[1,5] \

KeyError: (1, 5)

I can't figure out the problem. Please help. Many thanks.

Please help me to figure out the problem.

............................................................................................................................................................................................................................................................................................................................................

CodePudding user response:

Check your indexes in:

model  = 2190 * x[1,1]   46650 * x[1,2]   25110 * x[1,3]   8040 * x[1,4]   6720 * x[1,5] \
  1800*x[2,1]   24600 * x[2,2]   50610 * x[2,3]   46200 * x[2,4]   57780 * x[2,5] \
  1500*x[3,1]   45960 * x[3,2]   24420 * x[3,3]   7350 * x[3,4]   6030 * x[3,5],"Transportation cost"

model  = x[1,1]   x[1,2]   x[1,3]   x[1,4]   x[1,5] <= 300.0, "Supply Pt 1"
model  = x[2,1]   x[2,2]   x[2,3]   x[2,4]   x[2,5] <= 260.0, "Supply Pt 2"
model  = x[3,1]   x[3,2]   x[3,3]   x[3,4]   x[3,5] <= 258.0, "Supply Pt 3"

model  = x[1,1]   x[2,1]   x[3,1] <= 200.0, "Demand Pt 1"
model  = x[1,2]   x[2,2]   x[3,2] <= 100.0, "Demand Pt 2"
model  = x[1,3]   x[2,3]   x[3,3] <= 250.0, "Demand Pt 3"
model  = x[1,4]   x[2,4]   x[3,4] <= 185.0, "Demand Pt 4"
model  = x[1,5]   x[2,5]   x[3,5] <= 100.0, "Demand Pt 5"

They are starting at 1,0 , 2,0 , 3,0 and going until 1,4 , 2,4 , 3,4. Not to 1,5 , 2,5 , 3,5

See Code below:

from pulp import LpProblem, LpVariable, LpStatus, LpMinimize, GLPK, value

M = 3
N = 5
a = range(1,M 1)
a1 = range(M)
b = range(N 1)
b1 =  range(N)

xindx = [(a[i],b[j]) for j in b1 for i in a1] #{(1, 0): X_(1,_0), (2, 0): X_(2,_0), (3, 0): X_(3,_0), (1, 1): X_(1,_1), (2, 1): X_(2,_1), (3, 1): X_(3,_1), (1, 2): X_(1,_2), (2, 2): X_(2,_2), (3, 2): X_(3,_2), (1, 3): X_(1,_3), (2, 3): X_(2,_3), (3, 3): X_(3,_3), (1, 4): X_(1,_4), (2, 4): X_(2,_4), (3, 4): X_(3,_4)}

model =  LpProblem("Transportation_LP_Problem", LpMinimize)

x = LpVariable.dicts("X",xindx,0,None)

model  = 2190 * x[1,0]   46650 * x[1,1]   25110 * x[1,2]   8040 * x[1,3]   6720 * x[1,4] \
  1800*x[2,0]   24600 * x[2,1]   50610 * x[2,2]   46200 * x[2,3]   57780 * x[2,4] \
  1500*x[3,0]   45960 * x[3,1]   24420 * x[3,2]   7350 * x[3,3]   6030 * x[3,4],"Transportation cost"

model  = x[1,0]   x[1,1]   x[1,2]   x[1,3]   x[1,4] <= 300.0, "Supply Pt 1"
model  = x[2,0]   x[2,1]   x[2,2]   x[2,3]   x[2,4] <= 260.0, "Supply Pt 2"
model  = x[3,0]   x[3,1]   x[3,2]   x[3,3]   x[3,4] <= 258.0, "Supply Pt 3"

model  = x[1,0]   x[2,0]   x[3,0] <= 200.0, "Demand Pt 1"
model  = x[1,1]   x[2,1]   x[3,1] <= 100.0, "Demand Pt 2"
model  = x[1,2]   x[2,2]   x[3,2] <= 250.0, "Demand Pt 3"
model  = x[1,3]   x[2,3]   x[3,3] <= 185.0, "Demand Pt 4"
model  = x[1,4]   x[2,4]   x[3,4] <= 100.0, "Demand Pt 5"

model.solve(GLPK())

print ("Status:",LpStatus[model.status])

for v in model.variables():
         print(v.name,"=",v.varValue)

print ("Objective Function", value(model.objective))

CodePudding user response:

Try changing the code where you define a,a1,b,b1

a = range(1,M 1,1)
a1 = range(1,M,1)
b = range(1,N 1,1)
b1 = range(1,N,1)
  • Related