首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >传输优化(PuLP)

传输优化(PuLP)
EN

Stack Overflow用户
提问于 2018-03-11 08:57:58
回答 2查看 1.5K关注 0票数 2

在PuLP的传输优化问题中:

代码语言:javascript
复制
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)

在运行时,以下代码:

代码语言:javascript
复制
# 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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-11 20:54:49

替换

代码语言:javascript
复制
costs = [   #Bars
        #1 2 3 4 5
         [2,4,5,2,1],#A  Warehouses
        [3,1,3,2,3] #B
         ]

通过

代码语言:javascript
复制
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]

票数 4
EN

Stack Overflow用户

发布于 2020-06-29 16:24:18

本教程漏掉了一行

加上这个

代码语言:javascript
复制
costs = makeDict((Warehouses, Bars),costs)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49218316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档