用palp求解线性规划python问题

2024-09-28 03:18:08 发布

您现在位置:Python中文网/ 问答频道 /正文

基本上,我试图使每个目的地只有一个原点 所有目的地必须只有一个源 而且并非所有来源都必须使用 我希望有人能帮助我,我知道这不是支撑的方式,而是我得到的

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))


Tags: toinimportsendfororigindestinationquantity
1条回答
网友
1楼 · 发布于 2024-09-28 03:18:08

我想你已经接近你想要的了,但是你有一些问题。首先,您将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。但在你的问题陈述中,你说:

all destinations must have only one source and not all origins necessarily have to be used

但是,有了这个约束,您将强制使用每个原点。删除此约束将解决您的问题:

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,但希望强制目的地仅接收来自单个来源地的输入,则需要为每个“路由”添加二进制变量,以控制每个路由是否打开。然后,您将设置一个约束,即如果这些二进制变量为true,则数量变量只能为非零,然后您可以将每个目标中的这些二进制变量之和限制为==1

相关问题 更多 >

    热门问题