Оптимизация транспорта (PuLP)

В задаче транспортной оптимизации PuLP:

from pulp import *
Warehouses = ["A","B"]

# Creates a dictionary for the number of units of supply for each supply node
supply = {"A": 1000,
        "B": 4000}

# Creates a list of all demand nodes
Bars = ["1", "2", "3", "4", "5"]

# Creates a dictionary for the number of units of demand for each demand node
demand = {"1": 500,
        "2": 900,
        "3": 1800,
        "4": 200,
        "5": 700}
# Creates a list of costs of each transportation path
costs = [   #Bars
        #1 2 3 4 5
         [2,4,5,2,1],#A  Warehouses
        [3,1,3,2,3] #B
         ]
# Creates the prob variable to contain the problem data
prob = LpProblem("Beer Distribution Problem",LpMinimize)
# Creates a list of tuples containing all the possible routes for transport
Routes = [(w,b) for w in Warehouses for b in Bars]
# A dictionary called route_vars is created to contain the referenced variables (the routes)
route_vars = LpVariable.dicts("Route",(Warehouses,Bars),0,None,LpInteger)

Во время работы следующий код:

# The objective function is added to prob first
prob += lpSum([route_vars[w][b]*costs[w][b] for (w,b) in Routes]), "Sum of Transporting Costs"

Я получаю следующую ошибку:

TypeError: индексы списка должны быть целыми числами, а не str


person IndigoChild    schedule 11.03.2018    source источник
comment
Каков ваш желаемый результат?   -  person johnashu    schedule 11.03.2018
comment
Попробуйте оценить costs["A"]["2"]. Это то, что пытается сделать Pulp.   -  person Erwin Kalvelagen    schedule 11.03.2018
comment
привет @ErwinKalvelagen, cost [A] [2] дает мне TypeError: индексы списка должны быть целыми числами или срезами, а не str   -  person IndigoChild    schedule 11.03.2018
comment
@johnashu, желаемый результат указан в документации по транспортной проблеме на веб-странице Coin.   -  person IndigoChild    schedule 11.03.2018
comment
@IndigoChild: чем сложнее вы дадите людям понять, чего вы хотите, тем меньше у вас шансов найти ответ.   -  person johnashu    schedule 11.03.2018
comment
Привет @ johnashu, зацените это: coin-or.org/PuLP/CaseStudies/ a_transportation_problem.html   -  person IndigoChild    schedule 11.03.2018


Ответы (2)


Заменять

costs = [   #Bars
        #1 2 3 4 5
         [2,4,5,2,1],#A  Warehouses
        [3,1,3,2,3] #B
         ]

by

costs = { "A" : {"1" : 2, "2" : 4, "3" : 5, "4" : 2, "5" : 1 },
          "B" : {"1" : 3, "2" : 1, "3" : 3, "4" : 2, "5" : 3 }}

Пульпа допрашивает costs["A"]["2"], а не costs[0][1].

person Erwin Kalvelagen    schedule 11.03.2018

В учебнике пропущена одна строчка

Добавь это

costs = makeDict((Warehouses, Bars),costs)
person Adam_Hu    schedule 29.06.2020