From 25ef338a4bca95c94e33fafda94e00e7ad722698 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Poisot?= Date: Mon, 8 Jul 2019 09:58:01 -0400 Subject: [PATCH] :truck: move WIP code to a branch --- src/rand/shuffle.jl | 176 -------------------------------------------- 1 file changed, 176 deletions(-) diff --git a/src/rand/shuffle.jl b/src/rand/shuffle.jl index 1fb85038b..c35c36133 100644 --- a/src/rand/shuffle.jl +++ b/src/rand/shuffle.jl @@ -126,179 +126,3 @@ function shuffle!(N::BinaryNetwork; constraint::Symbol=:degree) f(N) end - -function quanti_shuffle_valid(a, i1, i2) - u1 = length(unique(i1)) == 2 - u2 = length(unique(i2)) == 2 - mi = minimum(a[i1,i2]) != zero(eltype(a)) - return u1 & u2 & mi -end - -""" -TODO -""" -function shuffle!(N::BipartiteQuantitativeNetwork{Int64,K}; constraint::Symbol=:degree, lims=(0, Inf)) where {K} - constraint ∈ [:degree, :generality, :vulnerability, :fill] || throw(ArgumentError("The constraint argument you specificied ($(constraint)) is invalid -- see ?shuffle! for a list.")) - first(lims) < last(lims) || throw(ArgumentError("The interval you used for limits, $(lims), is invalid")) - all(first(lims) .<= N.A .<= last(lims)) || throw(ArgumentError("The interval you used for limits, $(lims), is smaller than the range of network values")) - - # List of compatible swap schemes under different constraints - same_row = [[-1 1; -1 1], [-1 1; 0 0], [-1 1; 1 -1], [0 0; -1 1], [0 0; 1 -1], [1 -1; -1 1], [1 -1; 0 0], [1 -1; 1 -1],] - same_col = [[-1 -1; 1 1], [-1 0; 1 0], [-1 1; 1 -1], [0 -1; 0 1], [0 1; 0 -1], [1 -1; -1 1], [1 0; -1 0], [1 1; -1 -1],] - same_all = filter(x -> x ∈ same_row, same_col) - same_lnk = unique(vcat(same_row, same_col)) - - # Pick the correcty matrix series - m = same_lnk - constraint == :degree && (m = same_all) - constraint == :vulnerability && (m = same_col) - constraint == :generality && (m = same_row) - - permuted = false - while !permuted - s1 = rand(1:richness(N; dims=1), 2) - s2 = rand(1:richness(N; dims=2), 2) - while !quanti_shuffle_valid(N.A, s1, s2) - s1 = rand(1:richness(N; dims=1), 2) - s2 = rand(1:richness(N; dims=2), 2) - end - st1 = filter(a -> all(first(lims).<=a.<=last(lims)), [N[s1,s2] .+ x for x in m]) - st2 = filter(a -> all((N[s1,s2].!=0).==(a.!=0)), st1) - if length(st2) > 0 - permuted = true - k = rand(st2) - for i in 1:2, j in 1:2 - N[s1[i],s2[j]] = k[i,j] - end - end - end -end - -""" -TODO -""" -function shuffle!(N::BipartiteQuantitativeNetwork{Float64,K}; ε::Float64=1e-2, constraint::Symbol=:degree, lims=(0.0, Inf)) where {K} - constraint ∈ [:degree, :generality, :vulnerability, :fill] || throw(ArgumentError("The constraint argument you specificied ($(constraint)) is invalid -- see ?shuffle! for a list.")) - first(lims) < last(lims) || throw(ArgumentError("The interval you used for limits, $(lims), is invalid")) - all(first(lims) .<= N.A .<= last(lims)) || throw(ArgumentError("The interval you used for limits, $(lims), is smaller than the range of network values")) - - # List of compatible swap schemes under different constraints - same_row = [[-1 1; -1 1], [-1 1; 0 0], [-1 1; 1 -1], [0 0; -1 1], [0 0; 1 -1], [1 -1; -1 1], [1 -1; 0 0], [1 -1; 1 -1],] - same_col = [[-1 -1; 1 1], [-1 0; 1 0], [-1 1; 1 -1], [0 -1; 0 1], [0 1; 0 -1], [1 -1; -1 1], [1 0; -1 0], [1 1; -1 -1],] - same_all = filter(x -> x ∈ same_row, same_col) - same_lnk = unique(vcat(same_row, same_col)) - - # Pick the correcty matrix series - m = same_lnk - constraint == :degree && (m = same_all) - constraint == :vulnerability && (m = same_col) - constraint == :generality && (m = same_row) - m = m.*ε - - permuted = false - while !permuted - s1 = rand(1:richness(N; dims=1), 2) - s2 = rand(1:richness(N; dims=2), 2) - while !quanti_shuffle_valid(N.A, s1, s2) - s1 = rand(1:richness(N; dims=1), 2) - s2 = rand(1:richness(N; dims=2), 2) - end - st1 = filter(a -> all(first(lims).<=a.<=last(lims)), [N[s1,s2] .+ x for x in m]) - st2 = filter(a -> all((N[s1,s2].!=0).==(a.!=0)), st1) - if length(st2) > 0 - permuted = true - k = rand(st2) - for i in 1:2, j in 1:2 - N[s1[i],s2[j]] = k[i,j] - end - end - end -end - -""" -TODO -""" -function shuffle!(N::UnipartiteQuantitativeNetwork{Int64,K}; constraint::Symbol=:degree, lims=(0, Inf)) where {K} - constraint ∈ [:degree, :generality, :vulnerability, :fill] || throw(ArgumentError("The constraint argument you specificied ($(constraint)) is invalid -- see ?shuffle! for a list.")) - first(lims) < last(lims) || throw(ArgumentError("The interval you used for limits, $(lims), is invalid")) - all(first(lims) .<= N.A .<= last(lims)) || throw(ArgumentError("The interval you used for limits, $(lims), is smaller than the range of network values")) - - # List of compatible swap schemes under different constraints - same_row = [[-1 1; -1 1], [-1 1; 0 0], [-1 1; 1 -1], [0 0; -1 1], [0 0; 1 -1], [1 -1; -1 1], [1 -1; 0 0], [1 -1; 1 -1],] - same_col = [[-1 -1; 1 1], [-1 0; 1 0], [-1 1; 1 -1], [0 -1; 0 1], [0 1; 0 -1], [1 -1; -1 1], [1 0; -1 0], [1 1; -1 -1],] - same_all = filter(x -> x ∈ same_row, same_col) - same_lnk = unique(vcat(same_row, same_col)) - - # Pick the correcty matrix series - m = same_lnk - constraint == :degree && (m = same_all) - constraint == :vulnerability && (m = same_col) - constraint == :generality && (m = same_row) - - permuted = false - while !permuted - s1 = rand(1:richness(N; dims=1), 2) - while !quanti_shuffle_valid(N.A, s1, s1) - s1 = rand(1:richness(N; dims=1), 2) - end - st1 = filter(a -> all(first(lims).<=a.<=last(lims)), [N[s1,s1] .+ x for x in m]) - st2 = filter(a -> all((N[s1,s1].!=0).==(a.!=0)), st1) - if length(st2) > 0 - permuted = true - k = rand(st2) - for i in 1:2, j in 1:2 - N[s1[i],s1[j]] = k[i,j] - end - end - end -end - -""" -TODO -""" -function shuffle!(N::UnipartiteQuantitativeNetwork{Float64,K}; ε::Float64=1e-2, constraint::Symbol=:degree, lims=(0.0, Inf)) where {K} - constraint ∈ [:degree, :generality, :vulnerability, :fill] || throw(ArgumentError("The constraint argument you specificied ($(constraint)) is invalid -- see ?shuffle! for a list.")) - first(lims) < last(lims) || throw(ArgumentError("The interval you used for limits, $(lims), is invalid")) - all(first(lims) .<= N.A .<= last(lims)) || throw(ArgumentError("The interval you used for limits, $(lims), is smaller than the range of network values")) - - # List of compatible swap schemes under different constraints - same_row = [[-1 1; -1 1], [-1 1; 0 0], [-1 1; 1 -1], [0 0; -1 1], [0 0; 1 -1], [1 -1; -1 1], [1 -1; 0 0], [1 -1; 1 -1],] - same_col = [[-1 -1; 1 1], [-1 0; 1 0], [-1 1; 1 -1], [0 -1; 0 1], [0 1; 0 -1], [1 -1; -1 1], [1 0; -1 0], [1 1; -1 -1],] - same_all = filter(x -> x ∈ same_row, same_col) - same_lnk = unique(vcat(same_row, same_col)) - - # Pick the correcty matrix series - m = same_lnk - constraint == :degree && (m = same_all) - constraint == :vulnerability && (m = same_col) - constraint == :generality && (m = same_row) - m = m.*ε - - permuted = false - while !permuted - s1 = rand(1:richness(N; dims=1), 2) - while !quanti_shuffle_valid(N.A, s1, s1) - s1 = rand(1:richness(N; dims=1), 2) - end - st1 = filter(a -> all(first(lims).<=a.<=last(lims)), [N[s1,s1] .+ x for x in m]) - st2 = filter(a -> all((N[s1,s1].!=0).==(a.!=0)), st1) - if length(st2) > 0 - permuted = true - k = rand(st2) - for i in 1:2, j in 1:2 - N[s1[i],s1[j]] = k[i,j] - end - end - end -end - -""" - shuffle(N::T; kwargs...) where {T <: QuantitativeNetwork} - -Shuffle a quantitative network -- see shuffle! -""" -function shuffle(N::T; kwargs...) where {T <: QuantitativeNetwork} - Y = copy(N) - shuffle!(Y; kwargs...) - return Y -end