Skip to content

Commit

Permalink
Ensure that random_kruskal_mst doesn't generate the same spanning tre…
Browse files Browse the repository at this point in the history
…e every time (#120)

* Ensure that random_kruskal_mst doesn't generate the same spanning tree every time

* Stop running tests for macOS

As it appears that this is an upstream issue with installation on macOS,
it's probably best to remove the macOS test for now until it gets
resolved. In the meantime, tests for Windows and Linux should suffice.

* Add documentation about the AbstractRNG type and update type annotations

* Add example of AbstractRNG type to recom func
  • Loading branch information
InnovativeInventor authored Jul 23, 2021
1 parent aa00c07 commit 6893a4f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
julia-version: ['1.5.3']
os: [ubuntu-latest, windows-latest, macos-10.15]
julia-version: ['1.6.1']
os: [ubuntu-latest, windows-latest]

steps:
- uses: actions/checkout@v2
Expand Down
5 changes: 3 additions & 2 deletions src/balance_edges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ end
random_kruskal_mst(graph::BaseGraph,
edges::Array{Int, 1},
nodes::Array{Int, 1},
rng=MersenneTwister(1234))::BitSet
rng::AbstractRNG=Random.default_rng())
Generates and returns a random minimum spanning tree from the subgraph induced
by `edges` and `nodes`, using Kruskal's MST algorithm.
Expand All @@ -65,14 +65,15 @@ The `graph` represents the entire graph of the plan, where as `edges` and
- graph: Underlying Graph object
- edges: Array of edges of the sub-graph
- nodes: Set of nodes of the sub-graph
- rng: A random number generator that implements the [AbstractRNG type](https://docs.julialang.org/en/v1/stdlib/Random/#Random.AbstractRNG) (e.g. `Random.default_rng()` or `MersenneTwister(1234)`)
*Returns* a BitSet of edges that form a mst.
"""
function random_kruskal_mst(
graph::BaseGraph,
edges::Array{Int,1},
nodes::Array{Int,1},
rng = MersenneTwister(1234),
rng::AbstractRNG = Random.default_rng(),
)::BitSet
weights = rand(rng, length(edges))
return kruskal_mst(graph, edges, nodes, weights)
Expand Down
9 changes: 7 additions & 2 deletions src/recom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ end
- pop_constraint: PopulationConstraint to adhere to
- num_tries: num times to try getting a balanced cut from a subgraph
before giving up
- rng: A random number generator that implements the
[AbstractRNG type](https://docs.julialang.org/en/v1/stdlib/Random/#Random.AbstractRNG)
(e.g. `Random.default_rng()` or `MersenneTwister(1234)`)
"""
function get_valid_proposal(
graph::BaseGraph,
Expand All @@ -189,7 +192,7 @@ function get_valid_proposal(
D₁, D₂, sg_edges, sg_nodes = sample_subgraph(graph, partition, rng)

for _ = 1:num_tries
mst_edges = random_kruskal_mst(graph, sg_edges, collect(sg_nodes))
mst_edges = random_kruskal_mst(graph, sg_edges, collect(sg_nodes), rng)

# see if we can get a population-balanced cut in this mst
proposal = get_balanced_proposal(
Expand Down Expand Up @@ -272,7 +275,9 @@ step of the chain.
representing the likelihood of accepting the
proposal. Should accept a `Partition` as input.
- rng: Random number generator. The user can pass in their
own; otherwise, we use the default RNG from Random.
own; otherwise, we use the default RNG from Random. Must
implement the [AbstractRNG type](https://docs.julialang.org/en/v1/stdlib/Random/#Random.AbstractRNG)
(e.g. `Random.default_rng()` or `MersenneTwister(1234)`).
- no\\_self\\_loops: If this is true, then a failure to accept a new state
is not considered a self-loop; rather, the chain
simply generates new proposals until the acceptance
Expand Down

0 comments on commit 6893a4f

Please sign in to comment.