Задача оптимизации с использованием PulP - ошибка в целевой функции - PullRequest
0 голосов
/ 11 апреля 2020

Вот проблема, которую мне нужно решить:

Учитывая 2 города (c1, c2), 2 мусоросжигательных завода (I1 и I2) и 2 полигона (L1 и L2). Каждый город производит количество мусора и должен сдать его в мусоросжигательный завод (i1 или i2), затем мусор должен быть вывезен на свалку (L1 или L2). Мне нужно минимизировать затраты, которые в основном представляют собой расстояния (скажем, 1 доллар / миля) плюс затраты на использование каждого мусоросжигательного завода.

Вот что я получил до сих пор. Я получаю сообщение об ошибке:
"Ошибка типа: индексы списка должны быть целыми или кусочками, а не строкой"

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 ]

0 голосов
/ 12 апреля 2020

Спасибо, Эри c. Я избавился от ошибки, изменив distCI и distIL на словари, содержащие расстояния между узлами. Сейчас я получаю неосуществимое решение, однако я уже знаю, каким должно быть решение, и оно неосуществимо.

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))
0 голосов
/ 11 апреля 2020

distCI представляет собой список списков, поэтому вам нужно получить доступ к элементам с целыми числами, например distCI[0][0].

Ошибка связана с целевой функцией, поскольку каждый элемент в городах и мусоросжигательных установках является строками, поэтому вы не можете получить доступ к distCI[c][I]

Вероятно, эту проблему можно решить, задав distCI a словарь и ключи должны быть кортежем (c, i).

...