简体   繁体   English

python中的设施位置问题与纸浆

[英]Facility Location Problem in python with pulp

I do a facility location problem solution with pulp in python, it calculates the solution path correctly, but there is a problem with the total cost.我在python中用纸浆做了一个设施位置问题解决方案,它正确计算了解决方案路径,但是总成本存在问题。

from pulp import *

Customer = [1, 2, 3, 4]
Facility = ['Fac-1', 'Fac-2', 'Fac-3']
Demand = {1: 50, 2: 50, 3: 75, 4: 75}
Max_Supply = {'Fac-1': 100, 'Fac-2': 100, 'Fac-3': 500}
fixed_cost = {'Fac-1': 100.123, 'Fac-2': 100.456, 'Fac-3': 100.789}
transportation_cost = {'Fac-1': {1: 100.1, 2: 100.4, 3: 200.7, 4: 200.1}, 'Fac-2': {1: 200.2, 2: 200.5, 3: 100.8, 4: 200.11}, 'Fac-3': {1: 2000.3, 2: 2000.6, 3: 2000.9, 4: 100.12}}

prob = LpProblem("Capacitated_Facility_Location_Problem", LpMinimize)

use_facility = LpVariable.dicts("Use Facility", Facility, 0, 1, LpBinary)

ser_customer = LpVariable.dicts("Service", [(i,j) for i in Customer for j in Facility], 0)

prob += lpSum(fixed_cost[j]*use_facility[j] for j in Facility) + lpSum(transportation_cost[j][i]*ser_customer[(i,j)] for j in Facility for i in Customer)

for i in Customer:
    prob += lpSum(ser_customer[(i,j)] for j in Facility) == Demand[i]

for j in Facility:
    prob += lpSum(ser_customer[(i,j)] for i in Customer) <= Max_Supply[j]*use_facility[j]

for i in Customer:
    for j in Facility:
        prob += ser_customer[(i,j)] <= Demand[i]*use_facility[j]

for v in prob.variables():
    if v.varValue > 0:
        print(v.name, "=", v.varValue)

print("Total Cost = ", value(prob.objective))

Output:输出:

  • Service_(1,_'Fac_1') = 50.0服务_(1,_'Fac_1') = 50.0
  • Service_(2,_'Fac_1') = 50.0服务_(2,_'Fac_1') = 50.0
  • Service_(3,_'Fac_2') = 75.0服务_(3,_'Fac_2') = 75.0
  • Service_(4,_'Fac_3') = 75.0服务_(4,_'Fac_3') = 75.0
  • Use_Facility_Fac_1 = 1.0 Use_Facility_Fac_1 = 1.0
  • Use_Facility_Fac_2 = 1.0 Use_Facility_Fac_2 = 1.0
  • Use_Facility_Fac_3 = 1.0 Use_Facility_Fac_3 = 1.0
  • Total Cost = 25395.368000000002总成本 = 25395.368000000002

but total cost should be like below但总成本应该如下

  • transport cost = (100.1 + 100.4 + 100.8 + 100.12) = 401.42运输成本 = (100.1 + 100.4 + 100.8 + 100.12) = 401.42
  • fixed_cost = (100.123 + 100.456 + 100.789) = 301.368固定成本 = (100.123 + 100.456 + 100.789) = 301.368
  • Total Cost = 702.708总成本 = 702.708

The computer is correct....go figure.电脑是正确的....去图。 :) :)

In your objective, you are multiplying the transport cost times the service amount.在您的目标中,您将运输成本乘以服务量。 See the last line of the output below.请参阅下面输出的最后一行。 Also note how you can "break up" the expression for ease in troubleshooting and then just use the parts in the objective function (or elsewhere).还要注意如何“分解”表达式以便于排除故障,然后只使用目标函数(或其他地方)中的部分。

from pulp import *

Customer = [1, 2, 3, 4]
Facility = ['Fac-1', 'Fac-2', 'Fac-3']
Demand = {1: 50, 2: 50, 3: 75, 4: 75}
Max_Supply = {'Fac-1': 100, 'Fac-2': 100, 'Fac-3': 500}
fixed_cost = {'Fac-1': 100.123, 'Fac-2': 100.456, 'Fac-3': 100.789}
transportation_cost = {'Fac-1': {1: 100.1, 2: 100.4, 3: 200.7, 4: 200.1}, 'Fac-2': {1: 200.2, 2: 200.5, 3: 100.8, 4: 200.11}, 'Fac-3': {1: 2000.3, 2: 2000.6, 3: 2000.9, 4: 100.12}}

prob = LpProblem("Capacitated_Facility_Location_Problem", LpMinimize)

use_facility = LpVariable.dicts("Use Facility", Facility, 0, 1, LpBinary)

ser_customer = LpVariable.dicts("Service", [(i,j) for i in Customer for j in Facility], 0)

fixed = lpSum(fixed_cost[j]*use_facility[j] for j in Facility)
transpo = lpSum(transportation_cost[j][i]*ser_customer[(i,j)] for j in Facility for i in Customer)  
prob += fixed + transpo

for i in Customer:
    prob += lpSum(ser_customer[(i,j)] for j in Facility) == Demand[i]

for j in Facility:
    prob += lpSum(ser_customer[(i,j)] for i in Customer) <= Max_Supply[j]*use_facility[j]

for i in Customer:
    for j in Facility:
        prob += ser_customer[(i,j)] <= Demand[i]*use_facility[j]

prob.solve()
for v in prob.variables():
    if v.varValue > 0:
        print(v.name, "=", v.varValue)

print("Total Cost = ", value(prob.objective))
print(f'fixed: {value(fixed)}')
print(f'transpo: {value(transpo)}')
t = transportation_cost["Fac-1"][1] * ser_customer[1, "Fac-1"].varValue
print(f'the transpo for fac 1 to cust 1 is: {t}')

Output:输出:

Service_(1,_'Fac_1') = 50.0
Service_(2,_'Fac_1') = 50.0
Service_(3,_'Fac_2') = 75.0
Service_(4,_'Fac_3') = 75.0
Use_Facility_Fac_1 = 1.0
Use_Facility_Fac_2 = 1.0
Use_Facility_Fac_3 = 1.0
Total Cost =  25395.368000000002
fixed: 301.368
transpo: 25094.0
the transpo for fac 1 to cust 1 is: 5005.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM