Detail: https://github.com/myuuuuun/oyama_seminar2016/tree/master/exercise/ex02
In [1]:
include("test_deferred_acceptance.jl")
Out[1]:
In [109]:
include("matching.jl")
include("matching_tools.jl")
Out[109]:
In [3]:
srand(613)
m_prefs, f_prefs = random_prefs(4, 3, allow_unmatched=true)
println(m_prefs)
println(f_prefs)
println(Matching.gale_shapley_T(m_prefs, f_prefs))
In [4]:
function make_preferences(loop::Int=1000, m_size::Int=1000, f_size::Int=1000, seed::Int64=617)
srand(seed)
m_prefs = Array{Int64}(loop, f_size+1, m_size)
f_prefs = Array{Int64}(loop, m_size+1, f_size)
for i in 1:loop
m_pref, f_pref = random_prefs(m_size, f_size, allow_unmatched=true)
m_prefs[i, :, :] = m_pref
f_prefs[i, :, :] = f_pref
end
return m_prefs, f_prefs
end
function speedtest(func::Function, m_prefs, f_prefs, loop)
m_size = size(m_prefs, 3)
f_size = size(f_prefs, 3)
for i in 1:loop
m_pref = reshape(m_prefs[i, :, :], f_size+1, m_size)
f_pref = reshape(f_prefs[i, :, :], m_size+1, f_size)
end
end
Out[4]:
In [5]:
m_size = 1000
f_size = 1000
loop = 100
@time m_prefs, f_prefs = make_preferences(loop, m_size, f_size)
@time speedtest(Matching.gale_shapley_T, m_prefs, f_prefs, loop)
In [107]:
function is_stable_matching{T<:Int}(
m_matched::AbstractArray{T, 1}, f_matched::AbstractArray{T, 1},
m_prefs::AbstractArray{T, 2}, f_prefs::AbstractArray{T, 2})
m_size = size(m_prefs, 1)
f_size = size(f_prefs, 1)
f_rankings = argsort(m_prefs)
m_rankings = argsort(f_prefs)
for i in 1:m_size
f = m_matched[i]
for j in 1:m_size
# jにとって現在マッチしている相手よりも, fの方が順位が上なら
f_j = m_matched[j]
if f_rankings[j, f] < f_rankings[j, f_j]
# 更にfにとってiよりもjの順位が上なら, Blocking pair
if m_rankings[f, j] < m_rankings[f, i]
println("Blocking pair man: %s woman: %s" % (j, f_j))
return false
end
end
end
end
return true
end
Out[107]:
In [110]:
srand(613)
m_prefs, f_prefs = random_prefs(4, 3, allow_unmatched=true)
println(m_prefs)
println(f_prefs)
m_matched, f_matched = Matching.gale_shapley_T(m_prefs, f_prefs)
println(m_matched, f_matched)
println(Matching.is_stable_matching(m_matched, f_matched, m_prefs', f_prefs'))
m_matching2 = Array{Int64}([0, 2, 0, 3])
f_matching2 = Matching.make_f_matched_from_m_matched(m_matching2, 4, 3)
println(m_matching2, f_matching2)
println(Matching.is_stable_matching(m_matching2, f_matching2, m_prefs', f_prefs'))
In [113]:
srand(613)
m_prefs, f_prefs = random_prefs(4, 3, allow_unmatched=true)
println(Matching.gale_shapley_T(m_prefs, f_prefs))
println("Stable matching: $(Matching.find_all_stable_matchings(m_prefs', f_prefs'))")
Stable matchingがたくさんあることもある
In [114]:
srand(613)
for i in 1:10
m_prefs, f_prefs = random_prefs(4, 3, allow_unmatched=true)
println("$(i) 個目のPreference")
println(m_prefs)
println(f_prefs)
println(Matching.gale_shapley_T(m_prefs, f_prefs))
println("Stable matching: $(Matching.find_all_stable_matchings(m_prefs', f_prefs'))")
println("\n")
end