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