Skip to content
This repository has been archived by the owner on Jun 23, 2023. It is now read-only.

Commit

Permalink
🚚 move WIP code to a branch
Browse files Browse the repository at this point in the history
  • Loading branch information
tpoisot committed Jul 8, 2019
1 parent 743fb82 commit 25ef338
Showing 1 changed file with 0 additions and 176 deletions.
176 changes: 0 additions & 176 deletions src/rand/shuffle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 25ef338

Please sign in to comment.