generated from AlgebraicJulia/AlgebraicTemplate.jl
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Floyd-Warshall #70
Open
lukem12345
wants to merge
16
commits into
gr/acset2sym
Choose a base branch
from
llm/fw
base: gr/acset2sym
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Floyd-Warshall #70
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d2fa4a4
Set version to 0.1.7
algebraicjuliabot b14f09d
Added more exports (#44)
GeorgeR227 c9b8898
Add type rules for vectorfields
lukem12345 79f27e7
Add musical overload resolution
lukem12345 23fbf3f
Take advantage of :infer in type rules
lukem12345 2a6269c
Initial attempt at rewriting
GeorgeR227 2512181
Define Floyd-Warshall algorithm on Decapodes
lukem12345 31dc670
Remove unnecessary DataStructures dependency
lukem12345 d46cf86
Add docstrings and use multiple dispatch
lukem12345 db988a2
Explicitly create hyperedge list
lukem12345 0b52472
Clean by caching hyperedge order
lukem12345 7daf15b
Fix comment typo cod -> dom
lukem12345 303962d
Compute longest paths from terminals
lukem12345 dfe0e03
Just use -1 as weight
lukem12345 db2274f
Addition of generic graph struct
GeorgeR227 a93295a
Remove old graph_traversal file
GeorgeR227 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using DiagrammaticEquations | ||
using SymbolicUtils | ||
using SymbolicUtils.Rewriters | ||
using SymbolicUtils.Code | ||
using MLStyle | ||
|
||
const DECA_EQUALITY_SYMBOL = (==) | ||
|
||
to_symbolics(d::SummationDecapode, node::TraversalNode) = to_symbolics(d, node.index, Val(node.name)) | ||
|
||
function to_symbolics(d::SummationDecapode, op_index::Int, ::Val{:Op1}) | ||
input_sym = SymbolicUtils.Sym{Number}(d[d[op_index, :src], :name]) | ||
output_sym = SymbolicUtils.Sym{Number}(d[d[op_index, :tgt], :name]) | ||
op_sym = SymbolicUtils.Sym{(SymbolicUtils.FnType){Tuple{Number}, Number}}(d[op_index, :op1]) | ||
|
||
rhs = SymbolicUtils.Term{Number}(op_sym, [input_sym]) | ||
SymbolicUtils.Term{Number}(DECA_EQUALITY_SYMBOL, [output_sym, rhs]) | ||
end | ||
|
||
function to_symbolics(d::SummationDecapode, op_index::Int, ::Val{:Op2}) | ||
input1_sym = SymbolicUtils.Sym{Number}(d[d[op_index, :proj1], :name]) | ||
input2_sym = SymbolicUtils.Sym{Number}(d[d[op_index, :proj2], :name]) | ||
output_sym = SymbolicUtils.Sym{Number}(d[d[op_index, :res], :name]) | ||
op_sym = SymbolicUtils.Sym{(SymbolicUtils.FnType){Tuple{Number, Number}, Number}}(d[op_index, :op2]) | ||
|
||
rhs = SymbolicUtils.Term{Number}(op_sym, [input1_sym, input2_sym]) | ||
SymbolicUtils.Term{Number}(DECA_EQUALITY_SYMBOL, [output_sym, rhs]) | ||
end | ||
|
||
#XXX: Always converting + -> .+ here since summation doesn't store the style of addition | ||
# function to_symbolics(d::SummationDecapode, op_index::Int, ::Val{:Σ}) | ||
# Expr(EQUALITY_SYMBOL, c.output, Expr(:call, Expr(:., :+), c.inputs...)) | ||
# end | ||
|
||
function extract_symexprs(d::SummationDecapode) | ||
topo_list = topological_sort_edges(d) | ||
sym_list = [] | ||
for node in topo_list | ||
retrieve_name(d, node) != DerivOp || continue | ||
push!(sym_list, to_symbolics(d, node)) | ||
end | ||
sym_list | ||
end | ||
|
||
function apply_rewrites(d::SummationDecapode, rewriter) | ||
|
||
rewritten_list = [] | ||
for sym in extract_symexprs(d) | ||
res_sym = rewriter(sym) | ||
rewritten_sym = isnothing(res_sym) ? sym : res_sym | ||
push!(rewritten_list, rewritten_sym) | ||
end | ||
|
||
rewritten_list | ||
end | ||
|
||
# TODO: We need a way to get information like the d and ⋆ even when not in the ACSet | ||
# @syms Δ(x) d(x) ⋆(x) | ||
# lap_0_rule = @rule Δ(~x) => ⋆(d(⋆(d(~x)))) | ||
# rewriter = Postwalk(RestartedChain([lap_0_rule])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using DiagrammaticEquations | ||
using ACSets | ||
|
||
export TraversalNode, topological_sort_edges, number_of_ops, retrieve_name | ||
|
||
struct TraversalNode{T} | ||
index::Int | ||
name::T | ||
dom::AbstractVector | ||
cod::Int | ||
end | ||
|
||
TraversalNode(i, d::SummationDecapode, ::Val{:Op1}) = | ||
TraversalNode{Symbol}(i, d[i,:op1], [d[i,:src]], d[i,:tgt]) | ||
|
||
TraversalNode(i, d::SummationDecapode, ::Val{:Op2}) = | ||
TraversalNode{Symbol}(i, d[i,:op2], [d[i,:proj1],d[i,:proj2]], d[i,:res]) | ||
|
||
TraversalNode(i, d::SummationDecapode, ::Val{:Σ}) = | ||
TraversalNode{Symbol}(i, :+, d[incident(d,i,:summation),:summand], d[i,:sum]) | ||
|
||
retrieve_name(d::SummationDecapode, tsr::TraversalNode) = tsr.name | ||
|
||
# Induce a topological ordering of operations from a topological ordering of variables. | ||
# Taking Vᵢ(dom(e)ᵢ) like so is a structure preserving map. | ||
edge2cost(tsv, tsr::TraversalNode) = maximum(tsv[tsr.dom]) | ||
|
||
number_of_ops(d::SummationDecapode) = nparts(d, :Op1) + nparts(d, :Op2) + nparts(d, :Σ) | ||
|
||
start_nodes(d::SummationDecapode) = vcat(infer_states(d), incident(d, :Literal, :type)) | ||
|
||
# TODO: This could be Catlab'd. Hypergraph category? Migration to a DWD? | ||
""" function hyper_edge_list(d::SummationDecapode) | ||
|
||
Represent a Decapode as a directed hyper-edge list. | ||
|
||
Interpret a: | ||
- unary operation as a hyperedge of order (1,1) , | ||
- binary operation as a hyperedge of order (2,1) , and | ||
- summation as a hyperedge of order (|summands|,1) . | ||
""" | ||
function hyper_edge_list(d::SummationDecapode) | ||
[map(e -> TraversalNode(e, d, Val(:Op1)), parts(d, :Op1))..., | ||
map(e -> TraversalNode(e, d, Val(:Op2)), parts(d, :Op2))..., | ||
map(e -> TraversalNode(e, d, Val(:Σ )), parts(d, :Σ ))...] | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have a function that returns all of the "edge" symbols, so in this case |
||
|
||
""" function floyd_warshall(d::SummationDecapode) | ||
|
||
Return a |variable| × |variable| matrix of shortest paths via the Floyd-Warshall algorithm. | ||
|
||
Taking the maximum of the non-infinite short paths from state variables induces a topological ordering. | ||
|
||
https://en.wikipedia.org/wiki/Floyd–Warshall_algorithm | ||
""" | ||
function floyd_warshall(d::SummationDecapode) | ||
# Define weights. | ||
w(e) = (length(e.dom) == 1 && e.name ∈ [:∂ₜ,:dt]) ? -Inf : -1 | ||
# Init dists | ||
V = nparts(d, :Var) | ||
dist = fill(Inf, (V, V)) | ||
foreach(hyper_edge_list(d)) do e | ||
dist[(e.dom), e.cod] .= w(e) | ||
end | ||
for v in 1:V | ||
dist[v,v] = 0 | ||
end | ||
# Floyd-Warshall | ||
for k in 1:V | ||
for i in 1:V | ||
for j in 1:V | ||
if dist[i,j] > dist[i,k] + dist[k,j] | ||
dist[i,j] = dist[i,k] + dist[k,j] | ||
end | ||
end | ||
end | ||
end | ||
dist | ||
end | ||
|
||
""" function topological_sort_verts(d::SummationDecapode) | ||
|
||
Topologically sort the variables in a Decapode. | ||
|
||
The vector returned by this function maps each vertex to the order that it would be traversed in a topological sort traversal. If you want a list of vertices in the order of traversal, call `sortperm` on the output. | ||
""" | ||
function topological_sort_verts(d::SummationDecapode) | ||
m = floyd_warshall(d) | ||
map(parts(d,:Var)) do v | ||
minimum(filter(!isinf, m[v,infer_terminals(d)])) | ||
end | ||
end | ||
|
||
# TODO: Add in-place version for sorting a given hyperedge list. | ||
""" function topological_sort_edges(d::SummationDecapode) | ||
|
||
Topologically sort the edges in a Decapode. | ||
""" | ||
function topological_sort_edges(d::SummationDecapode) | ||
tsv = topological_sort_verts(d) | ||
op_order = hyper_edge_list(d) | ||
sort(op_order, by = x -> edge2cost(tsv,x)) | ||
end | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Call these src/tgt since we're talking about hypergraphs.