聚合无容量网络设计问题的基本运输问题
Basic transportation problem to Aggregated Uncapacitated Network Design Problem
我正在和 Python Pulp
一起练习解决一些交通问题。
我已经能够用这段代码解决一个基本的啤酒运输问题:
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),'€')
现在我必须解决 Uncapacitated Network Design Problem
但在 aggregate
.
版本中
问题定义如基本的运输问题,只是一个货源,两个仓库。中间是1个中型仓库
视觉上的架构是这样的:
但我不知道如何在模型中添加它。我猜是源头数量之类的,应该等于中仓数量,等于天命仓。
知道如何将其添加到模型中吗?
运行:
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))
Returns:
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
我正在和 Python Pulp
一起练习解决一些交通问题。
我已经能够用这段代码解决一个基本的啤酒运输问题:
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),'€')
现在我必须解决 Uncapacitated Network Design Problem
但在 aggregate
.
问题定义如基本的运输问题,只是一个货源,两个仓库。中间是1个中型仓库
视觉上的架构是这样的:
但我不知道如何在模型中添加它。我猜是源头数量之类的,应该等于中仓数量,等于天命仓。
知道如何将其添加到模型中吗?
运行:
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))
Returns:
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