Skip to content

Instantly share code, notes, and snippets.

@HaruKawamata
Created June 6, 2018 02:43
Show Gist options
  • Save HaruKawamata/210322a25df69eb867fc5a372c24a4c2 to your computer and use it in GitHub Desktop.
Save HaruKawamata/210322a25df69eb867fc5a372c24a4c2 to your computer and use it in GitHub Desktop.
from gurobipy import *
import random
# Data and ranges
nHospitalSites = 30
nSuburbs = 55
MaxSuburbsPerHospital = 7
MaxPopulation = 500000
H = range(nHospitalSites)
S = range(nSuburbs)
random.seed(3)
FixedCost = [random.randint(5000000,10000000) for h in H]
Population = [random.randint(60000,90000) for s in S]
# Travel distance - multiply by population moved to get travel cost
Dist = [[random.randint(0,50) for s in S] for h in H]
# Set up model and set the gap on the answer to 0
m = Model()
m.setParam('MIPGap', 0)
X = { (s, h): m.addVar(vtype = GRB.BINARY) for s in S for h in H }
m.setObjective(quicksum((FixedCost[h] + Dist[h][s]*Population[s])*X[s,h] for s in S for h in H), GRB.MINIMIZE)
for h in H:
m.addConstr(quicksum(X[s,h] for s in S) <= MaxSuburbsPerHospital)
for h in H:
m.addConstr(quicksum(X[s,h]*Population[s] for s in S) <= MaxPopulation)
for s in S:
m.addConstr(quicksum(X[s,h] for h in H) == 1)
m.optimize()
for h in H:
for s in S:
if X[s,h].x == 1:
print "Make this hospital: " + str(h)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment