Skip to content

Commit

Permalink
fixup #65
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hexaeder committed Oct 16, 2024
1 parent fa05fe8 commit cfe0f42
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
10 changes: 4 additions & 6 deletions src/sfdp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ function Base.iterate(iter::LayoutIterator{<:SFDP{Dim,Ptype,T}}) where {Dim,Ptyp
pin = [get(algo.pin, i, SVector{Dim,Bool}(false for _ in 1:Dim)) for i in 1:N]

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

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

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

return locs, (iter + 1, energy, step, progress, locs, pin, stopflag)
return locs, (iter + 1, energy, step, progress, locs, pin, rng, stopflag)
end

# Calculate Attractive force
Expand Down
10 changes: 4 additions & 6 deletions src/spring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ function Base.iterate(iter::LayoutIterator{<:Spring{Dim,Ptype}}) where {Dim,Ptyp
pin = [get(algo.pin, i, SVector{Dim,Bool}(false for _ in 1:Dim)) for i in 1:N]

# iteratorstate: #iter nr, old pos, pin
return (startpos, (1, startpos, pin))
return (startpos, (1, startpos, pin, rng))
end

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

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

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

return locs, (iteration + 1, locs, pin)
return locs, (iteration + 1, locs, pin, rng)
end

0 comments on commit cfe0f42

Please sign in to comment.