Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize layer assignment problem #16

Merged
merged 19 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/layering.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
# This contains algorithms for breaking up a DAG into layers

function layer_by_longest_path_to_source(graph)
"Calculate the layer of each node"
function layer_by_longest_path_to_source(graph, force_layer)
dists = longest_paths(graph, sources(graph))
layer_groups = IterTools.groupby(i->dists[i], sort(vertices(graph), by=i->dists[i]))
return collect.(layer_groups)
force_layers!(graph, dists, force_layer)
layer_groups = collect.(IterTools.groupby(i->dists[i], sort(vertices(graph), by=i->dists[i])))
return layer_groups
end

"Correct the layer of each node, according to the optional parameter by user"
function force_layers!(graph, dists, force_layer::Vector{Pair{Int, Int}})
# must process from end to beginning as otherwise can't move things after to make space
ordered_forced_layers = sort(force_layer, by=last; rev=true)
for (node_id, target_layer) in ordered_forced_layers
curr_layer = dists[node_id]
if target_layer < curr_layer
@warn "Ignored force_layer for node $node_id; curr layer ($curr_layer) > desired layer ($target_layer)"
elseif any(dists[child] <= target_layer for child in outneighbors(graph, node_id))
@warn "Ignored force_layer for node $node_id; as placing it at $target_layer would place it on same layer, or later than it's children."
else
dists[node_id] = target_layer
end
end
return dists
end


Expand All @@ -23,12 +41,12 @@ function add_dummy_nodes!(graph, layer2nodes)
dag_or_error(graph)
nondummy_nodes = vertices(graph)
# mapping from edges in the original graph to paths in the graph with dummy nodes
edge_to_paths = Dict(e => eltype(graph)[] for e in edges(graph))
edge_to_paths = Dict(e => eltype(graph)[] for e in edges(graph))
node2layer = node2layer_lookup(layer2nodes) # doesn't have dummy nodes, doesn't need them
for cur_node in vertices(graph)
cur_layer = node2layer[cur_node]
for out_edge in filter(e -> src(e) == cur_node, collect(edges(graph))) # need to copy as outwise will mutate when the graph is mutated
out_node = out_edge.dst
out_node = out_edge.dst
out_layer = node2layer[out_node]
cur_layer < out_layer || throw(DomainError(node2layer, "Layer assigmenment must be strictly monotonic"))
path = get!(edge_to_paths, out_edge, eltype(graph)[])
Expand All @@ -54,4 +72,4 @@ function add_dummy_nodes!(graph, layer2nodes)
is_dummy_mask = trues(nv(graph))
is_dummy_mask[nondummy_nodes] .= false
return is_dummy_mask, edge_to_paths
end
end
14 changes: 11 additions & 3 deletions src/zarate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Base.@kwdef struct Zarate <: AbstractLayout
end

"""
solve_positions(::Zarate, graph)
solve_positions(::Zarate, graph; force_layer)

Returns:
- `xs`: the xs coordinates of vertices in the layout
Expand All @@ -39,6 +39,12 @@ more crossings than optimal : edges should instead be routed through these diffe
`paths` contains for each edge, a Tuple of vectors, representing that route through the
different nodes as x and y coordinates.

Optional arguments:

`force_layer`: Vector{Pair{Int, Int}}
specifies the layer for each node
e.g. [3=>1, 5=>5] specifies layer 1 for node 3 and layer 5 to node 5

# Example:
```julia
using Graphs, Plots
Expand All @@ -52,11 +58,13 @@ for e in edges(g)
end
```
"""
function solve_positions(layout::Zarate, original_graph)
function solve_positions(
layout::Zarate, original_graph; force_layer = Vector{Pair{Int, Int}}()
)
graph = copy(original_graph)

# 1. Layer Assigment
layer2nodes = layer_by_longest_path_to_source(graph)
layer2nodes = layer_by_longest_path_to_source(graph, force_layer)
is_dummy_mask, edge_to_path = add_dummy_nodes!(graph, layer2nodes)

# 2. Layer Ordering
Expand Down
21 changes: 12 additions & 9 deletions test/demos.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ function quick_plot(graph, xs, ys, paths)
end
end

function quick_plot_solve_paths(layout, graph)
xs, ys, paths = solve_positions(layout, graph)
function quick_plot_solve_paths(layout, graph; kwargs...)
xs, ys, paths = solve_positions(layout, graph; kwargs...)
quick_plot(graph, xs, ys, paths)
end
function quick_plot_solve_direct(layout, graph)
xs, ys, _ = solve_positions(layout, graph)
function quick_plot_solve_direct(layout, graph; kwargs...)
xs, ys, _ = solve_positions(layout, graph; kwargs...)
quick_plot(graph, xs, ys)
end

Expand All @@ -42,19 +42,21 @@ end
@plottest quick_plot(SimpleDiGraph(Edge.([1=>2, 2=>3])), [1,2,5], [1,2,3], paths) ref_filename true 0.05
end

function test_example(layout, graph_name, tol=0.05)
function test_example(layout, graph_name, tol=0.05; kwargs...)
@testset "$graph_name" begin
@testset "$graph_name direct" begin
graph = getfield(Examples, graph_name)
ref_filename = joinpath(@__DIR__, "references", string(typeof(layout)), "direct", "$graph_name.png")
filename = "$graph_name" * join("_" .* string.(keys(kwargs))) * ".png"
ref_filename = joinpath(@__DIR__, "references", string(typeof(layout)), "direct", filename)
mkpath(dirname(ref_filename))
@plottest quick_plot_solve_direct(layout, graph) ref_filename true tol
@plottest quick_plot_solve_direct(layout, graph; kwargs...) ref_filename true tol
end
@testset "$graph_name paths" begin
graph = getfield(Examples, graph_name)
ref_filename = joinpath(@__DIR__, "references", string(typeof(layout)), "paths", "$graph_name.png")
filename = "$graph_name" * join("_" .* string.(keys(kwargs))) * ".png"
ref_filename = joinpath(@__DIR__, "references", string(typeof(layout)), "paths", filename)
mkpath(dirname(ref_filename))
@plottest quick_plot_solve_paths(layout, graph) ref_filename true tol
@plottest quick_plot_solve_paths(layout, graph; kwargs...) ref_filename true tol
end
end
end
Expand All @@ -67,6 +69,7 @@ end
test_example(layout, :two_lines, 0.02)
test_example(layout, :xcross)
test_example(layout, :tree, 0.07)
test_example(layout, :two_lines, 0.07; force_layer=[6=>3, 8=>4])
#test_example(layout, :large_depgraph) # too big
#test_example(layout, :extra_large_depgraph) # too big
end
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ using Plots
using Graphs
using LayeredLayouts
using Test
# using Gtk # enable for replacing test images
using VisualRegressionTests

include("examples.jl")
Expand Down