[英]How to solve Linear Programming Problem with more than one optimal solution using pulp
[英]Solve linear programming python problem using Pulp
基本上我试图让每个目的地只有一个来源所有目的地必须只有一个来源并且不一定必须使用所有来源我希望有人可以帮助我,我知道这不是正确的方式,而是我得到的
from pulp import*
import pandas as pd
origin = ["a","b","c","d","e","f","g","h"]
destination = ["1","2","3","4","5","6","7","8","9","10"]
offer = {"a":3,"b":3,"c":3,"d":3,"e":3,"f":4,"g":3,"h":3}
demand = {"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1}
cost_to_send = {
"a":{"1":1,"2":1,"3":1},
"b":{"2":1,"3":1,"9":1},
"c":{"5":1,"6":1,"7":1},
"d":{"7":1,"9":1,"10":1},
"e":{"3":1,"6":1,"8":1},
"f":{"1":1,"4":1,"7":1,"9":1},
"g":{"4":1,"5":1,"9":1},
"h":{"1":1,"4":1,"8":1}
}
prob = LpProblem("Exercise", LpMinimize)
Routes = [(i,j) for i in origin for j in destination]
quantity = LpVariable.dicts("quantity de envio",(origin,destination),0)
prob += lpSum(quantity[i][j]*cost_to_send[i][j] for (i,j) in Routes)
for j in destination:
prob += lpSum(quantity[i][j] for i in origin) == demand[j]
for i in origin:
prob += lpSum(quantity[i][j] for j in destination) == 1
prob.solve()
print("Status: ", LpStatus[prob.status])
for v in prob.variables():
if v.varValue > 0:
print(v.name, "=", v.varValue)
print("Answer ", value(prob.objective))
我认为你很接近你想要的,但你有一些问题。 首先,您将Routes
定义为所有可能的路由,而您只为某些路由定义了cost_to_send
。
假设定义了成本的路线是可行的路线,您最好将Routes
定义为:
Routes = [(i, j) for i in origin for j in destination if j in cost_to_send[i]]
我可以看到的另一个问题是您的第二组约束:
for i in origin:
prob += lpSum(quantity[i][j] for j in destination) == 1
这就是说,对于每个起点,到所有目的地的总流量必须为 1。但在您的问题陈述中,您说:
所有目的地必须只有一个来源,并且不一定必须使用所有来源
但是有了这个限制,你就强制使用每个原点。 删除此约束您的问题可以解决:
from pulp import *
import pandas as pd
origin = ["a","b","c","d","e","f","g","h"]
destination = ["1","2","3","4","5","6","7","8","9","10"]
offer = {"a":3,"b":3,"c":3,"d":3,"e":3,"f":4,"g":3,"h":3}
demand = {"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1}
cost_to_send = {
"a":{"1":1,"2":1,"3":1},
"b":{"2":1,"3":1,"9":1},
"c":{"5":1,"6":1,"7":1},
"d":{"7":1,"9":1,"10":1},
"e":{"3":1,"6":1,"8":1},
"f":{"1":1,"4":1,"7":1,"9":1},
"g":{"4":1,"5":1,"9":1},
"h":{"1":1,"4":1,"8":1}
}
prob = LpProblem("Exercise", LpMinimize)
# Routes = [(i,j) for i in origin for j in destination]
Routes = [(i, j) for i in origin for j in destination if j in cost_to_send[i]]
quantity = LpVariable.dicts("quantity de envio",Routes,0)
prob += lpSum(quantity[(i,j)]*cost_to_send[i][j] for (i,j) in Routes)
for j in destination:
prob += lpSum(quantity[(i,j)] for i in origin if (i,j) in Routes) == demand[j]
#for i in origin:
# prob += lpSum(quantity[i][j] for j in destination) == 1
prob.solve()
print("Status: ", LpStatus[prob.status])
for v in prob.variables():
if v.varValue > 0:
print(v.name, "=", v.varValue)
print("Answer ", value(prob.objective))
Returns:
Status: Optimal
quantity_de_envio_('a',_'1') = 1.0
quantity_de_envio_('a',_'2') = 1.0
quantity_de_envio_('a',_'3') = 1.0
quantity_de_envio_('b',_'9') = 1.0
quantity_de_envio_('c',_'5') = 1.0
quantity_de_envio_('c',_'6') = 1.0
quantity_de_envio_('d',_'10') = 1.0
quantity_de_envio_('f',_'4') = 1.0
quantity_de_envio_('f',_'7') = 1.0
quantity_de_envio_('h',_'8') = 1.0
Answer 10.0
请注意,如果您希望对给定目的地的需求大于 1,但希望强制目的地仅接收来自单个来源的输入 - 那么您将需要为每个“路线”添加二进制变量以控制每个路线是开放的。 然后,您将设置一个约束,即如果这些二进制变量为真,则数量变量只能为非零,然后您可以将这些二进制变量的总和限制到每个目的地为 == 1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.