I have 2 facilities, each has a pipeline and installation cost. I also have 16 customers to be serviced, each customer has a service cost. I want to assign each facility a maximum of 10 customers such that the cost of pipeline, installation and service costs are minimized.
I have implemented the following code but it is not working properly. It is supposed to assign each facility to the number of Customers it should serve and returns the minimum costs. However, the output assign the wells to all facilities (i think).
Your help is much appreciated:
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))