Skip to content

Commit cfe0f42

Browse files
committed
fixup #65
its much better to pass the rng via iterator state, otherwise both identical positions get the same random force thus move in the same direction. This was messing up an example in the docs
1 parent fa05fe8 commit cfe0f42

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

src/sfdp.jl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ function Base.iterate(iter::LayoutIterator{<:SFDP{Dim,Ptype,T}}) where {Dim,Ptyp
7676
pin = [get(algo.pin, i, SVector{Dim,Bool}(false for _ in 1:Dim)) for i in 1:N]
7777

7878
# iteratorstate: (#iter, energy, step, progress, old pos, pin, stopflag)
79-
return startpos, (1, typemax(T), one(T), 0, startpos, pin, false)
79+
return startpos, (1, typemax(T), one(T), 0, startpos, pin, rng, false)
8080
end
8181

8282
function Base.iterate(iter::LayoutIterator{<:SFDP}, state)
8383
algo, adj_matrix = iter.algorithm, iter.adj_matrix
84-
iter, energy0, step, progress, locs0, pin, stopflag = state
84+
iter, energy0, step, progress, locs0, pin, rng, stopflag = state
8585
K, C, tol = algo.K, algo.C, algo.tol
8686

8787
# stop if stopflag (tol reached) or nr of iterations reached
@@ -109,9 +109,7 @@ function Base.iterate(iter::LayoutIterator{<:SFDP}, state)
109109
end
110110
if any(isnan, force)
111111
# if two points are at the exact same location use random force in any direction
112-
# copy rng from alg struct to not advance the "initial" rng state
113-
# otherwise algo(g)==algo(g) might be broken
114-
force += randn(copy(algo.rng), Ftype)
112+
force = randn(rng, Ftype)
115113
end
116114
mask = (!).(pin[i]) # where pin=true mask will multiply with 0
117115
locs[i] = locs[i] .+ (step .* (force ./ norm(force))) .* mask
@@ -124,7 +122,7 @@ function Base.iterate(iter::LayoutIterator{<:SFDP}, state)
124122
stopflag = true
125123
end
126124

127-
return locs, (iter + 1, energy, step, progress, locs, pin, stopflag)
125+
return locs, (iter + 1, energy, step, progress, locs, pin, rng, stopflag)
128126
end
129127

130128
# Calculate Attractive force

src/spring.jl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ function Base.iterate(iter::LayoutIterator{<:Spring{Dim,Ptype}}) where {Dim,Ptyp
7575
pin = [get(algo.pin, i, SVector{Dim,Bool}(false for _ in 1:Dim)) for i in 1:N]
7676

7777
# iteratorstate: #iter nr, old pos, pin
78-
return (startpos, (1, startpos, pin))
78+
return (startpos, (1, startpos, pin, rng))
7979
end
8080

8181
function Base.iterate(iter::LayoutIterator{<:Spring}, state)
8282
algo, adj_matrix = iter.algorithm, iter.adj_matrix
83-
iteration, old_pos, pin = state
83+
iteration, old_pos, pin, rng = state
8484
iteration >= algo.iterations && return nothing
8585

8686
# The optimal distance bewteen vertices
@@ -114,9 +114,7 @@ function Base.iterate(iter::LayoutIterator{<:Spring}, state)
114114
else
115115
# if two points are at the exact same location
116116
# use random force in any direction
117-
# copy rng from alg struct to not advance the "initial" rng state
118-
# otherwise algo(g)==algo(g) might be broken
119-
force_vec += randn(copy(algo.rng), Ftype)
117+
force_vec += randn(rng, Ftype)
120118
end
121119

122120
end
@@ -134,5 +132,5 @@ function Base.iterate(iter::LayoutIterator{<:Spring}, state)
134132
locs[i] += force[i] .* scale .* mask
135133
end
136134

137-
return locs, (iteration + 1, locs, pin)
135+
return locs, (iteration + 1, locs, pin, rng)
138136
end

0 commit comments

Comments
 (0)