In [47]:
module matching_simulation

immutable Matching
    apply_prefs::Array
    accept_prefs::Array
end

function Base.call(f::Matching, typ::ASCIIString)
    apply_prefs = f.apply_prefs
    accept_prefs = f.accept_prefs
    
    if typ == "DA1"
        apply_matched, accept_matched = DA1(apply_prefs, accept_prefs)
    elseif typ == "DA2"
        emp()
    elseif typ == "DA2"
        emp()
    else 
        print("Please input 'DA1' or 'DA2' to 'typ'")
    end
    return apply_matched, accept_matched
end

function DA1(apply_prefs::Array, accept_prefs::Array) 
    da = zeros(Int64, length(accept_prefs[1,:]))
    rem = [i for i in 1:length(apply_prefs[1,:])]
    depth = ones(Int64, length(accept_prefs[:,1])-1)
    
    while length(rem) != 0
        apply = rem[end]
        d = depth[apply]
        if d == length(accept_prefs[:,1])
            pop!(rem)
        end
        applied = apply_prefs[d, apply]
        if applied == 0
            pop!(rem)
        else
            if findfirst(accept_prefs[:,applied], apply) < findfirst(accept_prefs[:,applied], da[applied])
                pop!(rem)
                if da[applied] != 0
                    push!(rem, da[applied])
                    depth[da[applied]] += 1
                end
                da[applied] = apply
            else
                depth[apply] += 1
            end
        end
    end

    accept_matched = da
    apply_matched = zeros(Int64, length(apply_prefs[1,:]))
    
    for i in 1:length(accept_matched)
        if accept_matched[i] != 0
            apply_matched[accept_matched[i]] = i
        end
    end
    return apply_matched, accept_matched
end

function g()
    return 1
end

end


WARNING: replacing module matching_simulation
Out[47]:
matching_simulation

学校選択制に関するシミュレーション

シミュレーションの概要

・学校数:20校
・各学区内に居住する生徒数:100名
・受け入れ定員数はすべての学校で共通
・各生徒の学校に対する選好順位は以下の数式によってランダムに生成した効用の大小に基づいて決定される.
$$ u_{ij} = I_{ij}\theta + \alpha v_{j} + (1-\alpha) v_{ij} $$


In [ ]:
function utility(num_st, num_sch, I, theta, alpha, v_i, v_ij)
    utility = Array(Float64, num_sch, num_st)
    for st in 1:num_st
        for sch in 1:num_sch
end

In [34]:
Array(Int64, 2, 10)


Out[34]:
2x10 Array{Int64,2}:
 8028074750225097339  3474306407908849977  …  7958535038077909538
 4050497697956373102  4842004297732534580            537447984481

In [48]:
m_prefs[1,:]


Out[48]:
1x4 Array{Int64,2}:
 1  3  2  3

In [49]:
f = matching_simulation.Matching(m_prefs, f_prefs)
f("DA1")


Array{Int64,1}
Out[49]:
([1,2,3,0],[1,2,3])

In [4]:
include("matching_tools.jl")


Out[4]:
_randperm2d! (generic function with 1 method)

In [25]:
m_prefs, f_prefs = random_prefs(4, 4);

In [11]:
m_prefs


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

In [26]:
@time deferred_acceptance(m_prefs, f_prefs)


  0.000054 seconds (137 allocations: 55.813 KB)
Out[26]:
([2,4,3,1],[4,1,3,2])

In [19]:
include("deferred_acceptance.jl")


Out[19]:
deferred_acceptance (generic function with 2 methods)

In [9]:
m_prefs[:,1]


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

In [118]:
f = Matching.DA(m_prefs, f_prefs)


Out[118]:
Matching.DA(5x4 Array{Int64,2}:
 3  2  2  4
 4  4  1  1
 2  3  0  3
 1  1  4  2
 0  0  3  0,5x4 Array{Int64,2}:
 1  3  4  4
 0  2  2  2
 2  0  3  1
 4  1  1  0
 3  4  0  3)

In [119]:
f("DA1")


LoadError: BoundsError: attempt to access 5x4 Array{Int64,2}:
 3  2  2  4
 4  4  1  1
 2  3  0  3
 1  1  4  2
 0  0  3  0
  at index [Colon(),5]
while loading In[119], in expression starting on line 1

 in throw_boundserror at abstractarray.jl:156
 in checkbounds at abstractarray.jl:159
 in _getindex at multidimensional.jl:185
 in call at In[112]:28

In [110]:
typeof('y')


Out[110]:
Char

In [111]:
typ = 'DA1'
if typ == 'DA1'
    g()
end


LoadError: syntax: invalid character literal
while loading In[111], in expression starting on line 1

In [49]:
hoge = "hige"
if hoge[1] == "h" && hoge[2:4] == "oge"
    println("hoge is hoge")
elseif hoge == "huge" || hoge == "hige"
    println("hoge is huge or hige")
else
    println("hoge is ", hoge)
end


hoge is huge or hige

In [76]:
rem = [i for i in 1:3]


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

In [62]:
typeof(typ)


Out[62]:
ASCIIString

In [ ]:


In [ ]:


In [61]:
typ = "DA1"


Out[61]:
"DA1"

In [58]:
if typ == 'DA1'
    return 1
end


LoadError: syntax: invalid character literal
while loading In[58], in expression starting on line 1

In [53]:
if hoge[1] == "h" 
    print(1)
end

In [74]:
da = Array(Float64, 10)


Out[74]:
10-element Array{Float64,1}:
 4.06619e251
 3.29752e-28
 5.11917e160
 1.71888e6  
 1.3429e-317
 0.0        
 0.0        
 0.0        
 0.0        
 0.0        

In [75]:
da


Out[75]:
10-element Array{Float64,1}:
 4.06619e251
 3.29752e-28
 5.11917e160
 1.71888e6  
 1.3429e-317
 0.0        
 0.0        
 0.0        
 0.0        
 0.0        

In [89]:
pop!(da)


Out[89]:
1.342901e-317

In [90]:
da


Out[90]:
4-element Array{Float64,1}:
 4.06619e251
 3.29752e-28
 5.11917e160
 1.71888e6  

In [92]:
da[end]


Out[92]:
1.718884396124092e6

In [104]:
da[4] = 10


Out[104]:
10

In [118]:
findfirst(da, 10)


Out[118]:
4

In [116]:
da


Out[116]:
10-element Array{Int64,1}:
  2
  2
  2
 10
  0
  0
  0
  0
  0
  0

In [90]:
typeof(2)


Out[90]:
Int64

In [92]:
"foo"*"goo"


Out[92]:
"foogoo"

In [2]:
parent(Vector)


LoadError: MethodError: `parent` has no method matching parent(::Type{Array{T,1}})
while loading In[2], in expression starting on line 1

In [ ]:
@co