I have used Pulp package to solve a MILP problem with inequality constraints. The MILP problem attempts to minimize the cost of installing facility platforms to service customers with pipelines such that each platform can handle certain number of customers. An example of the code is shown below:
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, 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)])
My question, how does Pulp or the default solver CPLEX handle these constraints?
If at the end of your program you add
prob.solve(pulp.CPLEX_CMD(keepFiles=True))
print(pulp.LpStatus[prob.status])
for variable in prob.variables():
print ("{} = {}".format(variable.name, variable.varValue))
then you'll see some solutions:
CUSTOMERS range(1, 17)
FACILITY ['FAC 1', 'FAC 2']
Optimal
UseCustomer_(1,_'FAC_1') = 1.0
UseCustomer_(1,_'FAC_2') = 0.0
UseCustomer_(10,_'FAC_1') = 0.0
UseCustomer_(10,_'FAC_2') = 1.0
but also two files: FacilityLocation-pulp and FacilityLocation-pulp.sol that will help you see what happened.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.