0
votes

Here is the problem I need to solve:

Given 2 cities (c1, c2), 2 incinerators (I1 and I2) and 2 landfills (L1 and L2). Each city produces an amount of garbage and must take it to an incinerator (i1 or i2), then the garbage must be taken to a landfill (L1 or L2). I need to minimize the costs, which are basically the distances (say $1/mile) plus the costs to use each incinerator.

Here's what I've got so far. The error I'm getting is:
"TypeError: list indices must be integers or slices, not str"

from pulp import *

cities = ["c1","c2"]
incinerators = ["i1","i2"]
landfills = ["L1","L2"]

#Distances between cities and incinerators are given

distCI = [  #cities
         #c1, c2
         [30, 5], #i1
         [36, 42],#i2   incinerators
         ]

#Distances between incinerators and landfills  are given

distIL = [  #incinerators
        #i1, i2
        [5, 8], #L1  landfills
        [9, 6], #L2
        ]

# creates a dictionary of the garbage produced by each city
demand = {
    "c1": [500], # amount of waste produced by c1
    "c2": [400] # amount of waste produced by c2
}

# created a dictionary with the capacity of each incinerator and its usage cost (per ton)
incidata = {# Incinerator, capacity, cost
         "i1":   [500, 40],
         "i2":   [500, 30]
            }

landdata = {# landfill   maxcapacity
         "L1":   [200],
         "L2":   [200]
            }

# creates a tuple with all the possible routes
route1 = [(c,i) for c in cities for i in incinerators]
route2 = [(i,l) for i in incinerators for l in landfills]

flow1 = LpVariable.dicts("route1",(cities,incinerators),0,None,LpInteger)
flow2 = LpVariable.dicts("route2",(incinerators,landfills),0,None,LpInteger)

prob = LpProblem("GarbageProblem",LpMinimize)

# The objective function is :
# sum of the flow from cities to incinerators multiplied by their respective distances, plus
# sum of the flow from incinerators to landfills multiplied by their respective distances
prob += lpSum([flow1[c][i] * distCI[c][i] for (c,i) in route1] + [flow2[i][l] * distIL[i][l] for     (i,l) in route2] )

# Creates all problem constraints - this ensures the amount going into each node is at least equal to     the amount leaving
prob+= lpSum([flow1[c,i] for (c,i) in route1 if c==1] )<=500 #garbage produced by city 1
prob+= lpSum([flow1[c,i] for (c,i) in route1 if c==2] )<=400 #gargabe produced by city 2
prob+= lpSum([flow1[c,i] for (c,i) in route1 if i==1] )<=500 #max capacity of incinerator 2
prob+= lpSum([flow1[c,i] for (c,i) in route1 if i==2] )<=500 #max capacity of incinerator 2
prob+= lpSum([flow2[i,l] for (i,l) in route2 if l==1] )<=200 #max capacity of landfill 1
prob+= lpSum([flow2[i,l] for (i,l) in route2 if l==2] )<=200 #max capacity of landfill 2

prob.solve() #solve using PulP's choice

# The status of the solution is printed to the screen
print("Status:", LpStatus[prob.status])

# Each of the variables is printed with it's resolved optimum value
for v in prob.variables():
    print(v.name, "=", v.varValue)

# The optimised objective function value is printed to the screen
print("Total Cost = ", value(prob.objective))
2

2 Answers

0
votes

distCI is a list of lists, so you have to access the elements with integers, such as distCI[0][0].

The error stems from the objective function because each element in cities and incinerators are strings, so you can't access distCI[c][I]

You can likely solve this problem by making distCI a dictionary and having the keys be a tuple of (c, i).

0
votes

Thank you Eric. I got rid of the error by changing distCI and distIL to dictionaries containing distances between the nodes. Now I'm getting an infeasible solution, however I already know what the solution should be and it is not infeasible.

from pulp import *

cities = ["c1","c2"]
incinerators = ["i1","i2"]
landfills = ["L1","L2"]

#Distances between cities and incinerators are given

distCI = { #cities, incinerators, distances
        "c1": {"i1": 30, "i2": 5},
        "c2": {"i1": 36, "i2": 42}
        }

distIL = {  #incinerators, landfills distances
        "i1": {"L1": 5, "L2": 8},
        "i2": {"L1": 9, "L2": 6}
        }

# creates a dictionary of the garbage produced by each city
demand = {
    "c1": [500], # amount of waste produced by c1
    "c2": [400] # amount of waste produced by c2
}

# created a dictionary with the capacity of each incinerator and its usage cost (per ton)
incidata = {# Incinerator, capacity, cost
         "i1":   [500, 40],
         "i2":   [500, 30]
            }

landdata = {# landfill   maxcapacity
         "L1": 200,
         "L2": 200
            }

# creates tuples of the possible routes
route1 = [(c,i) for c in cities for i in incinerators]
route2 = [(i,l) for i in incinerators for l in landfills]

flow1 = LpVariable.dicts("route1",(cities,incinerators),0,None,LpInteger)
flow2 = LpVariable.dicts("route2",(incinerators,landfills),0,None,LpInteger)

prob = LpProblem("GarbageProblem",LpMinimize)


# The objective function is :
# sum of the flow from cities to incinerators multiplied by their respective distances, plus
# sum of the flow from incinerators to landfills multiplied by their respective distances
prob += lpSum([flow1[c][i] * distCI[c][i] for (c,i) in route1] + [flow2[i][l] * distIL[i][l] for (i,l) in route2] )

# Creates all problem constraints - this ensures the amount going into each node is at least equal to the amount leaving

prob+= lpSum([flow1[c,i] for (c,i) in route1 if c==1] )==500 #garbage produced by city 1
prob+= lpSum([flow1[c,i] for (c,i) in route1 if c==2] )==400 #gargabe produced by city 2
prob+= lpSum([flow1[c,i] for (c,i) in route1 if i==1] )<=500 #max capacity of incinerator 2
prob+= lpSum([flow1[c,i] for (c,i) in route1 if i==2] )<=500 #max capacity of incinerator 2
prob+= lpSum([flow2[i,l] for (i,l) in route2 if l==1] )<=200 #max capacity of landfill 1
prob+= lpSum([flow2[i,l] for (i,l) in route2 if l==2] )<=200 #max capacity of landfill 2

prob.solve() #solve using PulP's choice

# The status of the solution is printed to the screen
print("Status:", LpStatus[prob.status])

# Each of the variables is printed with it's resolved optimum value
for v in prob.variables():
    print(v.name, "=", v.varValue)

# The optimised objective function value is printed to the screen
print("Total Cost = ", value(prob.objective))