Skip to content

Instantly share code, notes, and snippets.

@ZacLN
Created April 20, 2018 06:24
Show Gist options
  • Save ZacLN/c69e07b3fcaed5e6bde614f3eac9cfd2 to your computer and use it in GitHub Desktop.
Save ZacLN/c69e07b3fcaed5e6bde614f3eac9cfd2 to your computer and use it in GitHub Desktop.
using JuMP, GLPK, GLPKMathProgInterface
########## Inputs
N = 4 # number of departments
n = 300 # number of candidates
# defines preferences over each department (columns) for each candidate (rows)
# each cell indicates utility of getting job (from 0 to 1).
# TODO: use nonlinear (exp) preferences
# importance of matching each candidates preferences can be altered by multiplying a row by a constant (0..1),
# e.g. may not want to give full weight to those who got their first choice in a previous round
prefs = vcat([collect(linspace(0,1,N))[randperm(N)]' for i = 1:n]...)
jobheld = rand(1:N, n)
########## Model
begin
mod = Model(solver=GLPKSolverMIP())
# choice array `x` where xij is binary indicating whether to give the job `j` to candidate `i`
@variable(mod, x[1:n,1:N], Bin)
for i = 1:n
@constraint(mod, sum(x[i,1:N]) == 1) # only 1 job per person
@constraint(mod, x[i,jobheld[i]] == 0) # can't move to a department you're already in
end
# limit of jobs at department 1 and 2 to 5 and 15 respec.
@constraint(mod, sum(x[1:n,1]) <= 5)
@constraint(mod, sum(x[1:n,2]) <= 15)
@objective(mod, Max, sum(x.*prefs))
solve(mod)
val =getvalue(x)
end
firstchoiceperdepartment = sum(prefs.==1.0,1)
firstchoice = [indmax(prefs[i,:]) for i = 1:n]
jobafter = [indmax(val[i,:]) for i = 1:n]
sum(firstchoice .== jobafter)/n
T = [sum((jobheld.==i) .& (jobafter.==j)) for i = 1:N, j = 1:N]
println("No. of first choices by departments:")
println(firstchoiceperdepartment)
println()
println("No. assigned to each department:")
println(sum(val.>0, 1))
println()
println("Transition matrix: ")
display(T)
# Example output
# No. of first choices by departments:
# [76 74 65 85]
# No. assigned to each department:
# [5 15 123 157]
# Transition matrix:
# 4×4 Array{Int64,2}:
# 0 0 32 53
# 0 0 25 41
# 4 7 0 63
# 1 8 66 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment