Skip to content

Commit 8c52ace

Browse files
use a stable rng from StableRNGs for reproducibility (#170)
* use a stable rng from `StableRNGs` * update workflows * remove `GLOBAL_RNG` leftovers * workflows: use `@latest` action where possible * add explicit return * update * change `seed` to default `nothing` - add docs - remove `kw...` * restore `MersenneTwister` rng in `getRNG` * use a local `rng` foreach test * replace missing `kw...` * fix docstring * remove stale imports * revert chagnes to rng_from_rng_or_seed Co-authored-by: Simon Schoelly <[email protected]>
1 parent a3fb98b commit 8c52ace

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+594
-415
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ julia = "1.6"
2626
[extras]
2727
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
2828
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
29+
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
2930
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
3031

3132
[targets]
32-
test = ["Base64", "DelimitedFiles", "Test"]
33+
test = ["Base64", "DelimitedFiles", "StableRNGs", "Test"]

src/Graphs.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import LinearAlgebra: Diagonal, issymmetric, mul!
1616
using Random: AbstractRNG, GLOBAL_RNG, MersenneTwister, randperm, randsubseq!, seed!, shuffle, shuffle!
1717
using SparseArrays: SparseMatrixCSC, nonzeros, nzrange, rowvals
1818
import SparseArrays: blockdiag, sparse
19-
2019
import Base: adjoint, write, ==, <, *, , convert, isless, issubset, union, intersect,
2120
reverse, reverse!, isassigned, getindex, setindex!, show,
2221
print, copy, in, sum, size, eltype, length, ndims, transpose,

src/Parallel/Parallel.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using Distributed: @distributed
66
using Base.Threads: @threads, nthreads, Atomic, atomic_add!, atomic_cas!
77
using SharedArrays: SharedMatrix, SharedVector, sdata
88
using ArnoldiMethod
9-
using Random:shuffle
9+
using Random: AbstractRNG, shuffle
1010
import SparseArrays: sparse
1111
import Base: push!, popfirst!, isempty, getindex
1212

src/Parallel/centrality/betweenness.jl

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ betweenness_centrality(g::AbstractGraph, vs=vertices(g), distmx::AbstractMatrix=
22
parallel == :distributed ? distr_betweenness_centrality(g, vs, distmx; normalize=normalize, endpoints=endpoints) :
33
threaded_betweenness_centrality(g, vs, distmx; normalize=normalize, endpoints=endpoints)
44

5-
betweenness_centrality(g::AbstractGraph, k::Integer, distmx::AbstractMatrix=weights(g); normalize=true, endpoints=false, parallel=:distributed) =
6-
parallel == :distributed ? distr_betweenness_centrality(g, sample(vertices(g), k), distmx; normalize=normalize, endpoints=endpoints) :
7-
threaded_betweenness_centrality(g, sample(vertices(g), k), distmx; normalize=normalize, endpoints=endpoints)
5+
function betweenness_centrality(
6+
g::AbstractGraph, k::Integer, distmx::AbstractMatrix=weights(g);
7+
normalize=true, endpoints=false, parallel=:distributed,
8+
rng::Union{Nothing, AbstractRNG}=nothing, seed::Union{Nothing, Integer}=nothing
9+
)
10+
samples = sample(vertices(g), k; rng=rng, seed=seed)
11+
parallel == :distributed ? distr_betweenness_centrality(g, samples, distmx; normalize=normalize, endpoints=endpoints) :
12+
threaded_betweenness_centrality(g, samples, distmx; normalize=normalize, endpoints=endpoints)
13+
end
814

915
function distr_betweenness_centrality(g::AbstractGraph,
1016
vs=vertices(g),
@@ -36,8 +42,13 @@ function distr_betweenness_centrality(g::AbstractGraph,
3642
return betweenness
3743
end
3844

39-
distr_betweenness_centrality(g::AbstractGraph, k::Integer, distmx::AbstractMatrix=weights(g); normalize=true, endpoints=false) =
40-
distr_betweenness_centrality(g, sample(vertices(g), k), distmx; normalize=normalize, endpoints=endpoints)
45+
function distr_betweenness_centrality(
46+
g::AbstractGraph, k::Integer, distmx::AbstractMatrix=weights(g);
47+
normalize=true, endpoints=false, rng::Union{Nothing, AbstractRNG}=nothing, seed::Union{Nothing, Integer}=nothing
48+
)
49+
samples = sample(vertices(g), k; rng=rng, seed=seed)
50+
distr_betweenness_centrality(g, samples, distmx; normalize=normalize, endpoints=endpoints)
51+
end
4152

4253
function threaded_betweenness_centrality(g::AbstractGraph,
4354
vs=vertices(g),
@@ -71,5 +82,10 @@ function threaded_betweenness_centrality(g::AbstractGraph,
7182
return betweenness
7283
end
7384

74-
threaded_betweenness_centrality(g::AbstractGraph, k::Integer, distmx::AbstractMatrix=weights(g); normalize=true, endpoints=false) =
75-
threaded_betweenness_centrality(g, sample(vertices(g), k), distmx; normalize=normalize, endpoints=endpoints)
85+
function threaded_betweenness_centrality(
86+
g::AbstractGraph, k::Integer, distmx::AbstractMatrix=weights(g);
87+
normalize=true, endpoints=false, rng::Union{Nothing, AbstractRNG}=nothing, seed::Union{Nothing, Integer}=nothing
88+
)
89+
samples = sample(vertices(g), k; rng=rng, seed=seed)
90+
threaded_betweenness_centrality(g, samples, distmx; normalize=normalize, endpoints=endpoints)
91+
end

src/Parallel/centrality/stress.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
stress_centrality(g::AbstractGraph, vs=vertices(g); parallel=:distributed) =
22
parallel == :distributed ? distr_stress_centrality(g, vs) : threaded_stress_centrality(g, vs)
33

4-
stress_centrality(g::AbstractGraph, k::Integer; parallel=:distributed) =
5-
parallel == :distributed ? distr_stress_centrality(g, sample(vertices(g), k)) :
6-
threaded_stress_centrality(g, sample(vertices(g), k))
4+
function stress_centrality(
5+
g::AbstractGraph, k::Integer;
6+
parallel=:distributed, rng::Union{Nothing, AbstractRNG}=nothing, seed::Union{Nothing, Integer}=nothing
7+
)
8+
samples = sample(vertices(g), k; rng=rng, seed=seed)
9+
parallel == :distributed ? distr_stress_centrality(g, samples) :
10+
threaded_stress_centrality(g, samples)
11+
end
712

813
function distr_stress_centrality(g::AbstractGraph,
914
vs=vertices(g))::Vector{Int64}
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
dominating_set(g, reps, MinimalDominatingSet(); parallel=:threads, seed=-1)
2+
dominating_set(g, reps, MinimalDominatingSet(); parallel=:threads, rng=nothing, seed=nothing)
33
44
Perform [`Graphs.dominating_set(g, MinimalDominatingSet())`](@ref) `reps` times in parallel
55
and return the solution with the fewest vertices.
@@ -9,6 +9,15 @@ and return the solution with the fewest vertices.
99
used. This implementation is more efficient if `reps` is large.
1010
- If `seed >= 0`, a random generator of each process/thread is seeded with this value.
1111
"""
12-
dominating_set(g::AbstractGraph{T}, reps::Integer, alg::MinimalDominatingSet; parallel=:threads, seed=-1) where T <: Integer =
13-
Graphs.Parallel.generate_reduce(g, (g::AbstractGraph{T})->Graphs.dominating_set(g, alg; seed=seed),
14-
(x::Vector{T}, y::Vector{T})->length(x)<length(y), reps; parallel=parallel)
12+
function dominating_set(
13+
g::AbstractGraph{T}, reps::Integer, alg::MinimalDominatingSet;
14+
parallel=:threads, rng::Union{Nothing, AbstractRNG}=nothing, seed::Union{Nothing, Integer}=nothing
15+
) where T <: Integer
16+
Graphs.Parallel.generate_reduce(
17+
g,
18+
(g::AbstractGraph{T})->Graphs.dominating_set(g, alg; rng=rng, seed=seed),
19+
(x::Vector{T}, y::Vector{T})->length(x)<length(y),
20+
reps;
21+
parallel=parallel
22+
)
23+
end
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
independent_set(g, reps, MaximalIndependentSet(); parallel=:threads)
2+
independent_set(g, reps, MaximalIndependentSet(); parallel=:threads, rng=nothing, seed=nothing)
33
44
Perform [`Graphs.independent_set(g, MaximalIndependentSet())`](@ref) `reps` times in parallel
55
and return the solution with the most vertices.
@@ -8,6 +8,15 @@ and return the solution with the most vertices.
88
- `parallel=:threads`: If `parallel=:distributed` then the multiprocessor implementation is
99
used. This implementation is more efficient if `reps` is large.
1010
"""
11-
independent_set(g::AbstractGraph{T}, reps::Integer, alg::MaximalIndependentSet; parallel=:threads) where T <: Integer =
12-
Graphs.Parallel.generate_reduce(g, (g::AbstractGraph{T})->Graphs.independent_set(g, alg),
13-
(x::Vector{T}, y::Vector{T})->length(x)>length(y), reps; parallel=parallel)
11+
function independent_set(
12+
g::AbstractGraph{T}, reps::Integer, alg::MaximalIndependentSet;
13+
parallel=:threads, rng::Union{Nothing, AbstractRNG}=nothing, seed::Union{Nothing, Integer}=nothing
14+
) where T <: Integer
15+
Graphs.Parallel.generate_reduce(
16+
g,
17+
(g::AbstractGraph{T})->Graphs.independent_set(g, alg; rng=rng, seed=seed),
18+
(x::Vector{T}, y::Vector{T})->length(x)>length(y),
19+
reps;
20+
parallel=parallel
21+
)
22+
end
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
vertex_cover(g, reps, RandomVertexCover(); parallel=:threads)
2+
vertex_cover(g, reps, RandomVertexCover(); parallel=:threads, rng=nothing, seed=nothing)
33
44
Perform [`Graphs.vertex_cover(g, RandomVertexCover())`](@ref) `reps` times in parallel
55
and return the solution with the fewest vertices.
@@ -8,6 +8,15 @@ and return the solution with the fewest vertices.
88
- `parallel=:threads`: If `parallel=:distributed` then the multiprocessor implementation is
99
used. This implementation is more efficient if `reps` is large.
1010
"""
11-
vertex_cover(g::AbstractGraph{T}, reps::Integer, alg::RandomVertexCover; parallel=:threads) where T <: Integer =
12-
Graphs.Parallel.generate_reduce(g, (g::AbstractGraph{T})->Graphs.vertex_cover(g, alg),
13-
(x::Vector{T}, y::Vector{T})->length(x)<length(y), reps; parallel=parallel)
11+
function vertex_cover(
12+
g::AbstractGraph{T}, reps::Integer, alg::RandomVertexCover;
13+
parallel=:threads, rng::Union{Nothing, AbstractRNG}=nothing, seed::Union{Nothing, Integer}=nothing
14+
) where T <: Integer
15+
Graphs.Parallel.generate_reduce(
16+
g,
17+
(g::AbstractGraph{T})->Graphs.vertex_cover(g, alg; rng=rng, seed=seed),
18+
(x::Vector{T}, y::Vector{T})->length(x)<length(y),
19+
reps;
20+
parallel=parallel
21+
)
22+
end

src/SimpleGraphs/SimpleGraphs.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ using LinearAlgebra
55
using Graphs
66
using SimpleTraits
77

8+
import Random: AbstractRNG
9+
810
import Base:
911
eltype, show, ==, Pair, Tuple, copy, length, issubset, reverse, zero, in, iterate
1012

@@ -13,9 +15,7 @@ import Graphs:
1315
src, dst, edgetype, nv, ne, vertices, edges, is_directed,
1416
has_vertex, has_edge, inneighbors, outneighbors, all_neighbors,
1517
deepcopy_adjlist, indegree, outdegree, degree, has_self_loops,
16-
num_self_loops, insorted, squash
17-
18-
using Random: GLOBAL_RNG, AbstractRNG
18+
num_self_loops, insorted, squash, rng_from_rng_or_seed
1919

2020
export AbstractSimpleGraph, AbstractSimpleEdge,
2121
SimpleEdge, SimpleGraph, SimpleGraphFromIterator, SimpleGraphEdge,

src/SimpleGraphs/generators/euclideangraphs.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
euclidean_graph(N, d; seed=-1, L=1., p=2., cutoff=-1., bc=:open)
2+
euclidean_graph(N, d; rng=nothing, seed=nothing, L=1., p=2., cutoff=-1., bc=:open)
33
44
Generate `N` uniformly distributed points in the box ``[0,L]^{d}``
55
and return a Euclidean graph, a map containing the distance on each edge and
@@ -20,9 +20,11 @@ Dict{Graphs.SimpleGraphs.SimpleEdge{Int64},Float64} with 4 entries:
2020
Edge 4 => 5 => 0.168372
2121
```
2222
"""
23-
function euclidean_graph(N::Int, d::Int;
24-
L=1., seed = -1, kws...)
25-
rng = Graphs.getRNG(seed)
23+
function euclidean_graph(
24+
N::Int, d::Int;
25+
L=1., rng::Union{Nothing, AbstractRNG}=nothing, seed::Union{Nothing, Integer}=nothing, kws...
26+
)
27+
rng = rng_from_rng_or_seed(rng, seed)
2628
points = rmul!(rand(rng, d, N), L)
2729
return (euclidean_graph(points; L=L, kws...)..., points)
2830
end
@@ -55,8 +57,10 @@ julia> g
5557
{10, 45} undirected simple Int64 graph
5658
```
5759
"""
58-
function euclidean_graph(points::Matrix;
59-
L=1., p=2., cutoff=-1., bc=:open)
60+
function euclidean_graph(
61+
points::Matrix;
62+
L=1., p=2., cutoff=-1., bc=:open
63+
)
6064
d, N = size(points)
6165
weights = Dict{SimpleEdge{Int},Float64}()
6266
cutoff < 0. && (cutoff = typemax(Float64))

0 commit comments

Comments
 (0)