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

[TESTING] for orphaned FGs #519

Merged
merged 5 commits into from
Jan 16, 2020
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
18 changes: 15 additions & 3 deletions src/JunctionTree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -432,13 +432,25 @@ function buildTreeFromOrdering!(dfg::G,
end

println("Find potential functions for each clique")
cliq = getClique(tree, 1) # start at the root #TODO might not always be the root in case of multiple roots and deleted nodes in light graphs?
buildCliquePotentials(dfg, tree, cliq, solvable=solvable); # fg does not have the marginals as fge does

for cliqIds in getCliqueIds(tree)
if isRoot(tree, cliqIds)
cliq = getClique(tree, cliqIds) # start at the root
buildCliquePotentials(dfg, tree, cliq, solvable=solvable); # fg does not have the marginals as fge does
end
end
tree.buildTime = (time_ns()-t0)/1e9
return tree
end

isRoot(treel::AbstractBayesTree, cliq::TreeClique) = isRoot(tree, cliq.index)

function isRoot(treel::MetaBayesTree, cliqKey::Int)
length(MetaGraphs.inneighbors(treel.bt, cliqKey)) == 0
end

function isRoot(treel::BayesTree, cliqKey::Int)
length(Graphs.in_neighbors(getClique(treel, cliqKey), treel.bt)) == 0
end

"""
$SIGNATURES
Expand Down
2 changes: 1 addition & 1 deletion src/SolveTree01.jl
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ function dwnPrepOutMsg(fg::G,
logger=ConsoleLogger()) where {G <: AbstractDFG, T}
# pack all downcoming conditionals in a dictionary too.
with_logger(logger) do
if cliq.index != 1
if cliq.index != 1 #TODO there may be more than one root
@info "Dwn msg keys $(keys(dwnMsgs[1].p))"
@info "fg vars $(ls(fg))"
end # ignore root, now incoming dwn msg
Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ include("testBasicGraphs.jl")
include("testlocalconstraintexamples.jl")
end

include("testSolveOrphanedFG.jl")

# include("priorusetest.jl")

include("testExplicitMultihypo.jl")
Expand Down
64 changes: 64 additions & 0 deletions test/testSolveOrphanedFG.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# test forest of graphs can solve with CSM, specifically #518

using IncrementalInference
using Statistics
using Test



@testset "Test forest of orphaned graphs" begin

fg = initfg()
addVariable!(fg, :x0, ContinuousScalar)
addFactor!(fg, [:x0;], Prior(Normal(0,0.1)))
addVariable!(fg, :x1, ContinuousScalar)
addFactor!(fg, [:x0;:x1], LinearConditional(Normal(10,0.1)))
addVariable!(fg, :x2, ContinuousScalar)
addFactor!(fg, [:x1;:x2], LinearConditional(Normal(10,0.1)))

addVariable!(fg, :x10, ContinuousScalar)
addFactor!(fg, [:x10;], Prior(Normal()))
addVariable!(fg, :x11, ContinuousScalar)
addFactor!(fg, [:x10;:x11], LinearConditional(Normal(-10,1.0)))
addVariable!(fg, :x12, ContinuousScalar)
addFactor!(fg, [:x11;:x12], LinearConditional(Normal(-10,1.0)))

# dfgplot(fg)
# getSolverParams(fg).drawtree = true
# getSolverParams(fg).showtree = true
# solve factor graph with two orphaned components
vo = Symbol[:x12, :x2, :x0, :x11, :x1, :x10]
tree, smt, hist = solveTree!(fg, variableOrder=vo)

# test tree will have two different root nodes
@test getVariableOrder(tree) == vo

@test getParent(tree, getCliq(tree, :x1)) |> length == 0
@test getParent(tree, getCliq(tree, :x10)) |> length == 0

@test getChildren(tree, getCliq(tree, :x1)) |> length == 1
@test getChildren(tree, getCliq(tree, :x10)) |> length == 1

@test getChildren(tree, getCliq(tree, :x2)) |> length == 0
@test getChildren(tree, getCliq(tree, :x12)) |> length == 0


## Test the numerical values are correct

@test getKDE(fg, :x0) |> getPoints |> mean |> abs < 1.0
@test (getKDE(fg, :x1) |> getPoints |> mean) - 10 |> abs < 2.0
@test (getKDE(fg, :x2) |> getPoints |> mean) - 20 |> abs < 3.0

@test getKDE(fg, :x10) |> getPoints |> mean |> abs < 2.0
@test (getKDE(fg, :x11) |> getPoints |> mean) + 10 |> abs < 4.0
@test (getKDE(fg, :x12) |> getPoints |> mean) + 20 |> abs < 5.0



# using RoMEPlotting
# Gadfly.set_default_plot_size(35cm, 25cm)
#
# plotKDE(fg, ls(fg))


end