In [1]:
using DA

In [93]:
function permutation2(array)
    len = length(array)
    perms = Array{Tuple{Int, Int}}(len * (len - 1))
    c = 1
    for i in 1:len
        for j in 1:len
            if i != j
                perms[c] = (i, j)
                c += 1
            end
        end
    end
    return perms
end


Out[93]:
permutation2 (generic function with 1 method)

In [102]:
num_props, num_resps = 10, 10
#caps = 5 * ones(Int, num_resps)
max_prop_pref, max_resp_pref = 2, num_props
original_prop_prefs, original_resp_prefs = generate_random_prefs(num_props, num_resps, max_prop_pref=max_prop_pref, max_resp_pref=max_resp_pref)


Out[102]:
(Array{Int64,1}[[2, 8], [4, 5], [3, 1], [8, 1], [2, 6], [2, 8], [7, 1], [9, 5], [5, 4], [9, 1]], Array{Int64,1}[[1, 3, 5, 7, 10, 9, 4, 8, 2, 6], [8, 1, 6, 2, 3, 10, 7, 5, 4, 9], [8, 7, 10, 2, 3, 5, 6, 1, 9, 4], [1, 3, 7, 10, 5, 8, 4, 2, 9, 6], [10, 4, 7, 1, 8, 3, 2, 6, 9, 5], [4, 8, 10, 2, 1, 9, 6, 3, 5, 7], [6, 7, 1, 5, 8, 4, 9, 2, 3, 10], [2, 7, 3, 4, 10, 5, 6, 9, 8, 1], [4, 8, 6, 10, 9, 2, 3, 7, 5, 1], [9, 1, 6, 3, 2, 4, 8, 5, 10, 7]])

In [103]:
#prop_prefs, resp_prefs = copy(original_prop_prefs), copy(original_resp_prefs)
seed = 0
srand(seed)
prop_prefs, resp_prefs = generate_random_prefs(num_props, num_resps, max_prop_pref=max_prop_pref, max_resp_pref=max_resp_pref)
println(original_prop_prefs)
println(prop_prefs)
prop_matched0, resp_matched0 = deferred_acceptance(prop_prefs, resp_prefs)
prop_ranks0 = [findfirst(original_prop_prefs[i], prop_matched0[i]) for i in 1:num_props]
prop_ranks = copy(prop_ranks0)
prefs_changing = true

println(prop_ranks0)
while prefs_changing
    prefs_changing = false
    for i in 1:num_props#shuffle(1:num_props)
        for (r1, r2) in permutation2(1:num_resps)
            new_prop_prefs = copy(prop_prefs)
            new_prop_prefs[i] = [r1, r2]
            prop_matched, resp_matched = deferred_acceptance(new_prop_prefs, resp_prefs)
            prop_i_rank = findfirst(original_prop_prefs[i], prop_matched[i])
            if prop_i_rank != 0 && (prop_ranks[i] == 0 || prop_i_rank < prop_ranks[i])
                #println(i, "success")
                prop_ranks[i] = prop_i_rank
                prop_prefs[i] = new_prop_prefs[i]
                prefs_changing = true
            end
        end
    end
    println(prop_ranks)
end
println(prop_prefs)


Array{Int64,1}[[2, 8], [4, 5], [3, 1], [8, 1], [2, 6], [2, 8], [7, 1], [9, 5], [5, 4], [9, 1]]
Array{Int64,1}[[8, 2], [1, 7], [2, 3], [2, 5], [3, 1], [3, 7], [9, 6], [1, 8], [8, 3], [10, 1]]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 1, 0, 0, 2, 0, 1, 1, 1, 1]
[1, 1, 2, 1, 2, 2, 1, 1, 1, 1]
[1, 1, 1, 1, 2, 2, 1, 1, 1, 1]
[1, 1, 1, 1, 2, 2, 1, 1, 1, 1]
Array{Int64,1}[[8, 2], [1, 4], [2, 3], [1, 8], [2, 6], [1, 8], [2, 7], [2, 9], [2, 5], [3, 9]]

In [ ]: