In [1]:
Pkg.checkout("MyMatchingA")


INFO: Checking out MyMatchingA master...
INFO: Pulling MyMatchingA latest master...
INFO: No packages to install, update or remove

In [2]:
Pkg.test("MyMatchingA")


INFO: Testing MyMatchingA
one-to-one: Vector of Vectors: Error During Test
  Got an exception of type MethodError outside of a @test
  MethodError: no method matching deferred_acceptanceA(::Array{Array{Int64,1},1}, ::Array{Array{Int64,1},1})
   in macro expansion; at /Users/sasakiakihiro/.julia/v0.5/MyMatchingA/test/runtests.jl:207 [inlined]
   in macro expansion; at ./test.jl:674 [inlined]
   in macro expansion; at /Users/sasakiakihiro/.julia/v0.5/MyMatchingA/test/runtests.jl:201 [inlined]
   in macro expansion; at ./test.jl:674 [inlined]
   in anonymous at ./<missing>:?
   in include_from_node1(::String) at ./loading.jl:488
   in include_from_node1(::String) at /Applications/Julia-0.5.app/Contents/Resources/julia/lib/julia/sys.dylib:?
   in process_options(::Base.JLOptions) at ./client.jl:265
   in _start() at ./client.jl:321
   in _start() at /Applications/Julia-0.5.app/Contents/Resources/julia/lib/julia/sys.dylib:?
many-to-one: Vector of Vectors: Error During Test
  Got an exception of type MethodError outside of a @test
  MethodError: no method matching deferred_acceptanceA(::Array{Array{Int64,1},1}, ::Array{Array{Int64,1},1}, ::Array{Int64,1})
  Closest candidates are:
    deferred_acceptanceA(::Array{Int64,2}, ::Array{Int64,2}, ::Array{Int64,1}) at /Users/sasakiakihiro/.julia/v0.5/MyMatchingA/src/MyMatchingA.jl:6
   in macro expansion; at /Users/sasakiakihiro/.julia/v0.5/MyMatchingA/test/runtests.jl:223 [inlined]
   in macro expansion; at ./test.jl:674 [inlined]
   in macro expansion; at /Users/sasakiakihiro/.julia/v0.5/MyMatchingA/test/runtests.jl:218 [inlined]
   in macro expansion; at ./test.jl:674 [inlined]
   in anonymous at ./<missing>:?
   in include_from_node1(::String) at ./loading.jl:488
   in include_from_node1(::String) at /Applications/Julia-0.5.app/Contents/Resources/julia/lib/julia/sys.dylib:?
   in process_options(::Base.JLOptions) at ./client.jl:265
   in _start() at ./client.jl:321
   in _start() at /Applications/Julia-0.5.app/Contents/Resources/julia/lib/julia/sys.dylib:?
Test Summary:                    | Error  Total
  Testing deferred acceptance    |     2      2
  one-to-one: Vector of Vectors  |     1      1
  many-to-one: Vector of Vectors |     1      1
ERROR: LoadError: Some tests did not pass: 0 passed, 0 failed, 2 errored, 0 broken.
 in finish(::Base.Test.DefaultTestSet) at ./test.jl:498
 in macro expansion; at ./test.jl:681 [inlined]
 in anonymous at ./<missing>:?
 in include_from_node1(::String) at ./loading.jl:488
 in include_from_node1(::String) at /Applications/Julia-0.5.app/Contents/Resources/julia/lib/julia/sys.dylib:?
 in process_options(::Base.JLOptions) at ./client.jl:265
 in _start() at ./client.jl:321
 in _start() at /Applications/Julia-0.5.app/Contents/Resources/julia/lib/julia/sys.dylib:?
while loading /Users/sasakiakihiro/.julia/v0.5/MyMatchingA/test/runtests.jl, in expression starting on line 17
=============================[ ERROR: MyMatchingA ]=============================

failed process: Process(`/Applications/Julia-0.5.app/Contents/Resources/julia/bin/julia -Ccore2 -J/Applications/Julia-0.5.app/Contents/Resources/julia/lib/julia/sys.dylib --compile=yes --depwarn=yes --check-bounds=yes --code-coverage=none --color=yes --compilecache=yes /Users/sasakiakihiro/.julia/v0.5/MyMatchingA/test/runtests.jl`, ProcessExited(1)) [1]

================================================================================
MyMatchingA had test errors

 in #test#61(::Bool, ::Function, ::Array{AbstractString,1}) at ./pkg/entry.jl:749
 in (::Base.Pkg.Entry.#kw##test)(::Array{Any,1}, ::Base.Pkg.Entry.#test, ::Array{AbstractString,1}) at ./<missing>:0
 in (::Base.Pkg.Dir.##2#3{Array{Any,1},Base.Pkg.Entry.#test,Tuple{Array{AbstractString,1}}})() at ./pkg/dir.jl:31
 in cd(::Base.Pkg.Dir.##2#3{Array{Any,1},Base.Pkg.Entry.#test,Tuple{Array{AbstractString,1}}}, ::String) at ./file.jl:59
 in #cd#1(::Array{Any,1}, ::Function, ::Function, ::Array{AbstractString,1}, ::Vararg{Array{AbstractString,1},N}) at ./pkg/dir.jl:31
 in (::Base.Pkg.Dir.#kw##cd)(::Array{Any,1}, ::Base.Pkg.Dir.#cd, ::Function, ::Array{AbstractString,1}, ::Vararg{Array{AbstractString,1},N}) at ./<missing>:0
 in #test#3(::Bool, ::Function, ::String, ::Vararg{String,N}) at ./pkg/pkg.jl:258
 in test(::String, ::Vararg{String,N}) at ./pkg/pkg.jl:258

In [9]:
module MyMatchingA
export my_deferred_acceptance

# 多対一のケース
function my_deferred_acceptance(prop_prefs::Array{Int64,2}, resp_prefs::Array{Int64,2}, caps::Array{Int64,1})
    m = size(prop_prefs, 2)
    n = size(resp_prefs, 2)
    
    prop_matches = fill(-1, m)
    
    prop_next_to_propose = ones(Int64, m)
    L = sum(caps)
    resp_matches = zeros(Int64, L)
    
    indptr = Array(Int,n+1)
    indptr[1] = 1
    for i in 1:n
        indptr[i+1] = indptr[i] + caps[i]
    end    
    
    resp_rankings = (m+1)*ones(Int, m, n)    
    
    for j in 1:n
       for i in 1:length(resp_prefs[j])
           b = length(resp_prefs[j])
           a = resp_prefs[j][i]
           resp_rankings[a,j] = i
       end
    end
    
    
    while true
        i = get_single(prop_matches)
        if i == 0
            break
        end
        if prop_next_to_propose[i] > length(prop_prefs[i])
            prop_matches[i] = 0
            continue
        end        
            

        #学生iが入りたい大学jを探す.
        j = prop_prefs[i][prop_next_to_propose[i]]
        #if j == 0
        #    prop_matches[i] = 0
        #    continue
        #end
        
        #大学jのリストを探す
        #要修正?
        p = resp_matches[indptr[j]:indptr[j+1]-1]
        
        if resp_rankings[i,j] >= m+1
            prop_next_to_propose[i] += 1
            continue
        end

        #大学jが受け入れ可能なら受け入れる.
        a = findfirst(p,0)
        if a != 0
            resp_matches[indptr[j]-1+a] = i
            prop_matches[i] = j

        #大学jが定員オーバーしている場合
        else
        #大学jがiの方を選好していれば,受け入れる.
            list_comp = Array(Int, length(p))
            for k in 1:length(p)
                b = resp_rankings[p[k],j]
                list_comp[k] = b
            end
            b_max = maximum(list_comp)
            c = findfirst(resp_rankings[:,j], b_max)
            
            if resp_rankings[i,j] < resp_rankings[c,j]
                d = findfirst(resp_matches, c)
                resp_matches[d] = i
                prop_matches[i] = j
                prop_matches[c] = -1
            end
        end
        prop_next_to_propose[i] += 1
    end
    
    return prop_matches, resp_matches, indptr    
end

#どこにも決まってない学生を返す.(存在しなければ0)
function get_single(partners)
    m = size(partners, 1)
    for i in 1:m
        if partners[i] == -1
          return i
        end        
    end
    return 0   
end


end



# 一対一のケース
function my_deferred_acceptance1(prop_prefs::Array{Int64,2},resp_prefs::Array{Int64,2})
    caps = ones(Int, size(resp_prefs, 2))
    prop_matches, resp_matches, indptr =
        my_deferred_acceptance(prop_prefs, resp_prefs, caps)
    return prop_matches, resp_matches
end


WARNING: replacing module MyMatchingA
Out[9]:
my_deferred_acceptance1 (generic function with 1 method)

In [3]:
using MyMatchingA

In [4]:
prop_prefs = [3  1  3  1;
              1  2  2  2;
              2  3  1  3;
              0  0  0  0]


Out[4]:
4×4 Array{Int64,2}:
 3  1  3  1
 1  2  2  2
 2  3  1  3
 0  0  0  0

In [3]:
prop_prefs=Array{Int}[[3,1,2,0],[1,2,3,0],[3,2,1,0],[1,2,3,0]]


Out[3]:
4-element Array{Array{Int64,N},1}:
 [3,1,2,0]
 [1,2,3,0]
 [3,2,1,0]
 [1,2,3,0]

In [4]:
prop_prefs = [3 1 2 0;
              1 2 3 0;
              3 2 1 0;
              1 2 3 0]


Out[4]:
4×4 Array{Int64,2}:
 3  1  2  0
 1  2  3  0
 3  2  1  0
 1  2  3  0

In [5]:
resp_prefs = [4  4  4;
              1  1  2;
              3  2  1;
              2  3  3;
              0  0  0]


Out[5]:
5×3 Array{Int64,2}:
 4  4  4
 1  1  2
 3  2  1
 2  3  3
 0  0  0

In [12]:
resp_prefs = Array{Int}[[4,1,3,2,0],[4,1,2,3,0],[4,2,1,3,0]]


Out[12]:
3-element Array{Array{Int64,N},1}:
 [4,1,3,2,0]
 [4,1,2,3,0]
 [4,2,1,3,0]

In [5]:
resp_prefs = [4 1 3 2 0;
              4 1 2 3 0;
              4 2 1 3 0]


Out[5]:
3×5 Array{Int64,2}:
 4  1  3  2  0
 4  1  2  3  0
 4  2  1  3  0

In [ ]:


In [6]:
caps = [2,3,2]


Out[6]:
3-element Array{Int64,1}:
 2
 3
 2

In [15]:
typeof(prop_prefs)


Out[15]:
Array{Int64,2}

In [16]:
typeof(resp_prefs)


Out[16]:
Array{Int64,2}

In [17]:
typeof(caps)


Out[17]:
Array{Int64,1}

In [7]:
deferred_acceptanceA(prop_prefs,resp_prefs,caps)


BoundsError: attempt to access 3-element Array{Int64,1} at index [4]

 in deferred_acceptanceA(::Array{Int64,2}, ::Array{Int64,2}, ::Array{Int64,1}) at /Users/sasakiakihiro/.julia/v0.5/MyMatchingA/src/MyMatchingA.jl:18

In [35]:
list_comp = Array(Int, 3)


Out[35]:
3-element Array{Int64,1}:
 4638988464
 4577296608
 4577296672

In [36]:
typeof(findfirst(caps,2))


Out[36]:
Int64

In [37]:
using Matching

In [38]:
deferred_acceptance(prop_prefs,resp_prefs,caps)


MethodError: no method matching deferred_acceptance(::Array{Array{Int64,N},1}, ::Array{Int64,2}, ::Array{Int64,1})
Closest candidates are:
  deferred_acceptance(::Array{Int64,2}, ::Array{Int64,2}, ::Array{Int64,1}) at /Users/sasakiakihiro/.julia/v0.5/Matching/src/deferred_acceptance.jl:233
  deferred_acceptance(::Array{Int64,2}, ::Array{Int64,2}, ::Array{Int64,1}, ::Array{Int64,1}) at /Users/sasakiakihiro/.julia/v0.5/Matching/src/deferred_acceptance.jl:49
  deferred_acceptance{P<:Matching.DAProposal}(::Array{Int64,2}, ::Array{Int64,2}, ::Array{Int64,1}, ::Type{P<:Matching.DAProposal}) at /Users/sasakiakihiro/.julia/v0.5/Matching/src/deferred_acceptance.jl:233
  ...

In [39]:
typeof(size(prop_prefs, 2))


Out[39]:
Int64

In [40]:
size(resp_prefs, 2)


Out[40]:
3

In [41]:
m = size(prop_prefs, 2)
    n = size(resp_prefs, 2)


Out[41]:
3

In [42]:
prop_matches = fill(-1, m)


Out[42]:
1-element Array{Int64,1}:
 -1

In [43]:
prop_next_to_propose = ones(Int64, m)


Out[43]:
1-element Array{Int64,1}:
 1

In [44]:
L = sum(caps)
    resp_matches = zeros(Int64, L)


Out[44]:
7-element Array{Int64,1}:
 0
 0
 0
 0
 0
 0
 0

In [45]:
indptr = Array(Int,n+1)
indptr[1] = 1
    for i in 1:n
        indptr[i+1] = indptr[i] + caps[i]
    end

In [46]:
typeof(indptr)


Out[46]:
Array{Int64,1}

In [48]:
resp_rankings = (m+1)*ones(Int, m, n)    
    
    for j in 1:n
       for i in 1:length(resp_prefs[j])
           b = length(resp_prefs[j])
           a = resp_prefs[j][i]
           resp_rankings[a,j] = i
       end
    end


BoundsError: attempt to access 1×3 Array{Int64,2} at index [4,1]

 in setindex!(::Array{Int64,2}, ::Int64, ::Int64, ::Int64) at ./array.jl:416
 in macro expansion; at ./In[48]:7 [inlined]
 in anonymous at ./<missing>:?

In [49]:
typeof(resp_rankings)


Out[49]:
Array{Int64,2}

In [50]:
resp_rankings


Out[50]:
1×3 Array{Int64,2}:
 2  2  2

In [ ]: