NameError at / free variable 'y' 在封闭范围内赋值前被引用
NameError at / free variable 'y' referenced before assignment in enclosing scope
我需要为我的大学做一个小项目 - 创建 webapp 来解决交通问题,我正在制作 MVP 版本。我将 Django 用于 webapp,将 PuLP 用于传输问题部分。
启动我的问题解决逻辑时收到错误消息:
NameError at / free variable 'y' referenced before assignment in
enclosing scope
行:
prob += lpSum([route_vars[x][y] for x in Distributors]) <= supply[x], "Sum of Products out of Warehouse %s"%x
我正在使用他们 GitHub here
中的 PuLP 示例
我这部分的代码如下:
Warehouses = [0,1,2]
supply = { 0: deliver1,
1: deliver2,
2: deliver3
}
Distributors = [0, 1, 2]
demand = { 0: receiver1,
1: receiver2,
2: receiver3
}
costs = [ #dsitributors -static variables for debugging will change that later on
#D E F
[3, 5, 7],#A Warehouse
[12, 10, 9],#B Warehouse
[13, 3, 9],#C Warehouse
]
prob = LpProblem("Transportation Problem",LpMinimize)
Routes = [(x,y) for x in Warehouses for y in Distributors]
route_vars = LpVariable.dicts("Route",(Warehouses,Distributors),0,None,LpInteger)
prob += lpSum([route_vars[x][y]*costs[x][y] for (x,y) in Routes]), "Sum of Transporting Costs"
for x in Warehouses:
prob += lpSum([route_vars[x][y] for x in Distributors]) <= supply[x], "Sum of Products out of Warehouse %s"%x
for y in Distributors:
prob += lpSum([route_vars[x][y] for y in Warehouses]) >= demand[y], "Sum of Products into Distributors %s"%y
prob.writeLP("TransportationProblem.lp")
prob.solve()
print("Status:", LpStatus[prob.status])
for v in prob.variables():
print(v.name, "=", v.varValue)
print("Total Cost of transportation = ", value(prob.objective))
我想那只是我犯的一些愚蠢的错误,但无法真正找到它...另外,我通过数字而不是名称来命名仓库和分销商是我接收
的解决方法
TypeError: list indices must be integers or slices, not str
同一行。
在供应约束中,将Distributors
赋值给y
(目前y
未定义),像这样:
for x in Warehouses:
prob += lpSum([route_vars[x][y] for y in Distributors]) <= supply[x], "Sum of Products out of Warehouse %s"%x
同样的错误适用于需求约束,其中 Warehouses
应分配给 x
。
对于部署 Apache,mod_wsgi 是一种选择。 https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/modwsgi/
我需要为我的大学做一个小项目 - 创建 webapp 来解决交通问题,我正在制作 MVP 版本。我将 Django 用于 webapp,将 PuLP 用于传输问题部分。
启动我的问题解决逻辑时收到错误消息:
NameError at / free variable 'y' referenced before assignment in enclosing scope
行:
prob += lpSum([route_vars[x][y] for x in Distributors]) <= supply[x], "Sum of Products out of Warehouse %s"%x
我正在使用他们 GitHub here
中的 PuLP 示例我这部分的代码如下:
Warehouses = [0,1,2]
supply = { 0: deliver1,
1: deliver2,
2: deliver3
}
Distributors = [0, 1, 2]
demand = { 0: receiver1,
1: receiver2,
2: receiver3
}
costs = [ #dsitributors -static variables for debugging will change that later on
#D E F
[3, 5, 7],#A Warehouse
[12, 10, 9],#B Warehouse
[13, 3, 9],#C Warehouse
]
prob = LpProblem("Transportation Problem",LpMinimize)
Routes = [(x,y) for x in Warehouses for y in Distributors]
route_vars = LpVariable.dicts("Route",(Warehouses,Distributors),0,None,LpInteger)
prob += lpSum([route_vars[x][y]*costs[x][y] for (x,y) in Routes]), "Sum of Transporting Costs"
for x in Warehouses:
prob += lpSum([route_vars[x][y] for x in Distributors]) <= supply[x], "Sum of Products out of Warehouse %s"%x
for y in Distributors:
prob += lpSum([route_vars[x][y] for y in Warehouses]) >= demand[y], "Sum of Products into Distributors %s"%y
prob.writeLP("TransportationProblem.lp")
prob.solve()
print("Status:", LpStatus[prob.status])
for v in prob.variables():
print(v.name, "=", v.varValue)
print("Total Cost of transportation = ", value(prob.objective))
我想那只是我犯的一些愚蠢的错误,但无法真正找到它...另外,我通过数字而不是名称来命名仓库和分销商是我接收
的解决方法TypeError: list indices must be integers or slices, not str
同一行。
在供应约束中,将Distributors
赋值给y
(目前y
未定义),像这样:
for x in Warehouses:
prob += lpSum([route_vars[x][y] for y in Distributors]) <= supply[x], "Sum of Products out of Warehouse %s"%x
同样的错误适用于需求约束,其中 Warehouses
应分配给 x
。
对于部署 Apache,mod_wsgi 是一种选择。 https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/modwsgi/