Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 基本运输问题到聚合无容量限制网络设计问题_Python_Linear Programming_Pulp - Fatal编程技术网

Python 基本运输问题到聚合无容量限制网络设计问题

Python 基本运输问题到聚合无容量限制网络设计问题,python,linear-programming,pulp,Python,Linear Programming,Pulp,我正在用Python纸浆练习解决一些运输问题 我已经能够用这个代码解决一个基本的啤酒运输问题: import pulp as lp model=lp.LpProblem('Modelo de localización',lp.LpMinimize) fabricas=['A','B'] cap_fabricas={'A':1500,'B':1500} tiendas=['1','2','3'] cap_tiendas={'1':1000,'2':1000,'3':1000} rutas

我正在用
Python纸浆
练习解决一些运输问题

我已经能够用这个代码解决一个基本的啤酒运输问题:

import pulp as lp


model=lp.LpProblem('Modelo de localización',lp.LpMinimize)


fabricas=['A','B']
cap_fabricas={'A':1500,'B':1500}
tiendas=['1','2','3']
cap_tiendas={'1':1000,'2':1000,'3':1000}

rutas=[(i,j) for i in fabricas for j in tiendas]
dict_costes={'A':dict(zip(tiendas,[23,20,45])),'B':dict(zip(tiendas,[21,34,11]))}

x_ij=lp.LpVariable.dicts('rutas',(fabricas,tiendas),lowBound=0,cat=lp.LpInteger)
model+=lp.lpSum([x_ij[i][j]* dict_costes[i][j] for i in fabricas for j in tiendas]),'COSTES DE TRANSPORTE'
for i in fabricas:
    model+=lp.lpSum([x_ij[i][j] for j in tiendas])<=cap_fabricas[i]
for j in tiendas: 
    model+=lp.lpSum([x_ij[i][j] for i in fabricas])>=cap_tiendas[j]

model.solve(solver)


print('Solution status:',lp.LpStatus[model.status])
for v in model.variables():
    print('Quantity from:',v.name,' is',v.varValue(),'ud')
print('Cost is:',lp.value(model.objective),'€')
将纸浆作为lp导入
model=lp.LpProblem('Modelo de localización',lp.LpProblem)
fabricas=['A','B']
cap_fabricas={'A':1500,'B':1500}
tiendas=['1'、'2'、'3']
cap_tiendas={'1':1000,'2':1000,'3':1000}
rutas=[(i,j)表示法布里卡的i表示蒂恩达斯的j]
dict_costes={'A':dict(zip(tiendas,[23,20,45]),'B':dict(zip(tiendas,[21,34,11]))
x_ij=lp.LpVariable.dicts('rutas',(fabricas,tiendas),lowBound=0,cat=lp.LpInteger)
模型+=lp.lpSum([x_ij[i][j]*将i的成本计算为i,将i的成本计算为j的成本计算为i][j]),“运输成本”
对于fabricas中的i:
模型+=lp.lpSum([x_ij[i][j]代表田纳西州的j])=cap_tiendas[j]
模型求解(求解器)
打印('解决方案状态:',lp.LpStatus[model.status])
对于model.variables()中的v:
打印('数量来源:',v.name',is',v.varValue(),'ud')
打印('成本为:',lp.价值(型号目标),'欧元')
现在我必须解决一个
无容量限制的网络设计问题
,但在版本
聚合中

问题的定义是基本的运输问题,但只有一个货源和两个仓库。中间有一个中型仓库。

从视觉上看,模式如下所示:

但我不知道如何将其添加到模型中。我猜这类似于源数量,应该等于中间仓库数量,也应该等于命运仓库数量

知道如何将其添加到模型中吗?

运行:

import pulp as lp

# Model Data
nodes = [1, 2, 3, 4]
routes = [(1,2), (2,3), (3,4), (2,4), (1,4)]
fixed_costs = {(1,2):5, (2,3):25, (3,4):20, (2,4):15, (1,4):10}
var_costs = {(1,2):5, (2,3):5, (3,4):5, (2,4):5, (1,4):30}
demand = {1:0, 2:0, 3:1, 4:1}
source_capacity = {1:sum(demand.values()), 2:0, 3:0, 4:0}

# Declare Problem
model=lp.LpProblem('network opt',lp.LpMinimize)

# Are routes open?
rt_open = lp.LpVariable.dicts('rt_open',routes,lowBound=0,cat=lp.LpBinary)

# Flow along routes:
flow = lp.LpVariable.dicts('flow',routes,lowBound=0,cat=lp.LpInteger)

# units sourced at each node
source = lp.LpVariable.dicts('source',nodes,lowBound=0,cat=lp.LpInteger)

# Objective
model += lp.lpSum([fixed_costs[r]*rt_open[r] + var_costs[r]*flow[r] for r in routes])

# Constraints
for i in nodes:
    # Constrains on sourcing:
    model += source[i] <= source_capacity[i]

    # Constraint on flow: source + flow in = demand + flow out
    model += source[i] + lp.lpSum([flow[r] for r in routes if r[1] == i]) == \
                                demand[i] + lp.lpSum([flow[r] for r in routes if r[0] == i])

for r in routes:
    # Flow can only go along open routes
    model += flow[r] <= rt_open[r]*sum(demand.values())

# Solve problem & print results
model.solve()
print('Solution status:',lp.LpStatus[model.status])

for v in model.variables():
    print(v.name,': ',v.varValue)

print('Cost is:',lp.value(model.objective))

我不懂这个图表。这些数字(和括号中的数字)代表什么?目标是什么?@kabdulla目标是最小化总成本。括号中的数字是可变成本,即通过该弧发送的每个单元的可变成本。括号外的数字是固定成本。如果使用arc(二进制),则应用哪种方法?目标是发送从1到3和4的两个产品。(每个节点一个)
flow_(1,_2) :  2.0
flow_(1,_4) :  0.0
flow_(2,_3) :  1.0
flow_(2,_4) :  1.0
flow_(3,_4) :  0.0
rt_open_(1,_2) :  1.0
rt_open_(1,_4) :  0.0
rt_open_(2,_3) :  1.0
rt_open_(2,_4) :  1.0
rt_open_(3,_4) :  0.0
source_1 :  2.0
source_2 :  0.0
source_3 :  0.0
source_4 :  0.0
Cost is: 65.0