基本上,我试图使每个目的地只有一个原点 所有目的地必须只有一个源 而且并非所有来源都必须使用 我希望有人能帮助我,我知道这不是支撑的方式,而是我得到的
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
定义为:我看到的另一个问题是第二组约束:
也就是说,对于每个起点,到所有目的地的总流量必须为1。但在你的问题陈述中,你说:
但是,有了这个约束,您将强制使用每个原点。删除此约束将解决您的问题:
请注意,如果您希望给定目的地的需求大于1,但希望强制目的地仅接收来自单个来源地的输入,则需要为每个“路由”添加二进制变量,以控制每个路由是否打开。然后,您将设置一个约束,即如果这些二进制变量为true,则数量变量只能为非零,然后您可以将每个目标中的这些二进制变量之和限制为==1
相关问题 更多 >
编程相关推荐