我有2个设施,每个设施都有一条管道和安装成本。我也有16个要服务的客户,每个客户都有服务成本。我想为每个设施分配最多10个客户,以使管道,安装和服务成本最小化。
我已经实现了以下代码,但无法正常工作。应该为每个设施分配应服务的客户数量,并返回最低成本。但是,输出将井分配给所有设施(我认为)。
非常感谢您的帮助:
import numpy as np
from pulp import *
import random
CUSTOMERS = range(1,17) ## generate random Customer Ids
FACILITY =['FAC 1','FAC 2'] # Number and Name of Facilities
randomCosts = random.sample(range(90, 100), 2) ## Generate Random Installation Costs
actcost = dict(zip(FACILITY, randomCosts)) ## Assign installation cost to each facility
randompipelineCost = random.sample(range(5, 20), 2) ## Generate Random pipeline Costs
pipelineCost = dict(zip(FACILITY, randompipelineCost))## Assign pipeline cost to each facility
sizeOfPlatforms = [10,10] ## Size of Platforms
maxSizeOfPlatforms = dict(zip(FACILITY, sizeOfPlatforms)) ## Assign Size to each Facility
serviceRandom=[]
serviceCosts = {}
for facility in FACILITY: ## Generate Random Service Costs for each customer
serviceRandom=[]
for i in range (16):
serviceRandom.append(random.randrange(1, 101, 1))
service = dict(zip(CUSTOMERS, serviceRandom))
serviceCosts[facility]=service
print 'CUSTOMERS', CUSTOMERS
print 'FACILITY', FACILITY
print 'Facility Cost', actcost
print 'pipeline Cost',pipelineCost
print 'service Cost', serviceCosts
prob = LpProblem("FacilityLocation",LpMinimize)
##Decision Variables
use_facility = LpVariable.dicts("UseFacility", FACILITY,0,1,LpBinary)
use_customer = LpVariable.dicts("UseCustomer",[(i,j) for i in CUSTOMERS for j in FACILITY],1)
## Objective Function
prob += lpSum(actcost[j]*use_facility[j] for j in FACILITY) + lpSum(pipelineCost[j]*use_facility[j] for j in FACILITY)+ lpSum(serviceCosts[j][i]*use_customer[(i,j)] for i in CUSTOMERS for j in FACILITY)
# Constraints
for j in FACILITY:
prob += lpSum(use_customer[(i,j)] for i in CUSTOMERS) <= maxSizeOfPlatforms[j]
for j in FACILITY:
prob += lpSum(use_facility[j] for j in FACILITY) <=1.0 ##Constraint 1
##Solution
prob.solve()
print ("Status:", LpStatus[prob.status])
TOL = 0.00001
## print Decision Variables
for i in FACILITY:
if use_facility[i].varValue > TOL:
print("Establish Facility at Site",i)
for v in prob.variables():
print(v.name,"=", v.varValue)
##optimal Solution
print ("The cost of production in dollars for one year=", value(prob.objective))
答案 0 :(得分:0)
有4个不同的问题。
1)
use_customer = LpVariable.dicts("UseCustomer",[(i,j) for i in CUSTOMERS for j in FACILITY],1)
这等效于说每个变量都是小数,应大于一个。也就是说,您正在设置lowBound=1
。我猜您想在这里说这个变量是二进制的:
use_customer = LpVariable.dicts("UseCustomer",[(i,j) for i in CUSTOMERS for j in FACILITY], cat=LpBinary)
2)
第二个问题是您不限制将客户分配给至少一家工厂(至少因为该问题是最小化问题并且您的成本严格为正,所以客户永远不会分配给一个以上的客户工具,但在下文中,我会假设恰好是一个)
for i in CUSTOMERS:
prob += lpSum(use_customer[(i,j)] for j in FACILITY) == 1.0
3)
我不确定您想在这里说什么
for j in FACILITY:
prob += lpSum(use_facility[j] for j in FACILITY) <=1.0 ##Constraint 1
每次您对所有设施进行求和并将其值的总和限制为最大。我想这是一个错误。
4)
最后,您不会将变量use_facility
和use_customer
链接在一起。也就是说,变量use_facility
的值永远不会大于0。由于它是二进制,我假设use_facility[j]
代表激活成本。因此,您需要添加以激活设施,即如果至少有一个客户使用设施j,则会激活设施:
for j in FACILITY:
for i in CUSTOMERS:
prob += use_facility[j] >= lpSum(use_customer[(i,j)])
放在一起:
##Decision Variables
use_facility = LpVariable.dicts("UseFacility", FACILITY, cat=LpBinary)
use_customer = LpVariable.dicts("UseCustomer",[(i,j) for i in CUSTOMERS for j in FACILITY], cat=LpBinary)
## Objective Function
prob += lpSum(actcost[j]*use_facility[j] for j in FACILITY) + lpSum(pipelineCost[j]*use_facility[j] for j in FACILITY)+ lpSum(serviceCosts[j][i]*use_customer[(i,j)] for i in CUSTOMERS for j in FACILITY)
# Constraints
for j in FACILITY:
prob += lpSum(use_customer[(i,j)] for i in CUSTOMERS) <= maxSizeOfPlatforms[j]
for i in CUSTOMERS:
prob += lpSum(use_customer[(i,j)] for j in FACILITY) == 1
for j in FACILITY:
for i in CUSTOMERS:
prob += use_facility[j] >= lpSum(use_customer[(i,j)])