Skip to content

Commit 41202b4

Browse files
authored
Merge pull request #31 from JuliaRobotics/feature/templatedsolverparams
Feature/templatedsolverparams
2 parents 8caa62b + 7e346d2 commit 41202b4

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

src/NeedsAHome.jl

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
export hasFactor, hasVariable
2+
export hasFactor, hasVariable, isInitialized
33

44
"""
55
$SIGNATURES
@@ -18,3 +18,25 @@ Return `::Bool` on whether `dfg` contains the variable `lbl::Symbol`.
1818
function hasVariable(dfg::G, label::Symbol)::Bool where {G <: AbstractDFG}
1919
return haskey(dfg.labelDict, label) # haskey(vertices(dfg.g), label)
2020
end
21+
22+
23+
"""
24+
$SIGNATURES
25+
26+
Returns state of vertex data `.initialized` flag.
27+
28+
Notes:
29+
- used by both factor graph variable and Bayes tree clique logic.
30+
TODO: Refactor
31+
"""
32+
function isInitialized(var::DFGVariable; key::Symbol=:default)::Bool
33+
return var.solverDataDict[key].initialized
34+
end
35+
function isInitialized(fct::DFGFactor; key::Symbol=:default)::Bool
36+
return fct.solverDataDict[key].initialized
37+
end
38+
function isInitialized(dfg::G, label::Symbol; key::Symbol=:default)::Bool where G <: AbstractDFG
39+
# nothing to do for factors
40+
# return dfg.g.vertices[dfg.labelDict[label]].dfgNode.solverDataDict[key].initialized
41+
return true
42+
end

src/services/GraphsDFG.jl

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ end
2727
vertex_index(v::GraphsNode) = v.index
2828

2929
# Exports
30-
export GraphsDFG
30+
export GraphsDFG, NoSolverParams, AbstractParams
3131
export exists
3232
export getLabelDict, getDescription, setDescription, getInnerGraph, getAddHistory, getSolverParams, setSolverParams
3333

@@ -45,20 +45,32 @@ export getSubgraph
4545
export isFullyConnected, hasOrphans
4646
export toDot, toDotFile
4747

48-
mutable struct GraphsDFG <: AbstractDFG
48+
abstract type AbstractParams end
49+
50+
mutable struct NoSolverParams <: AbstractParams
51+
end
52+
53+
mutable struct GraphsDFG{T<:AbstractParams} <: AbstractDFG
4954
g::FGType
5055
description::String
5156
nodeCounter::Int64
5257
labelDict::Dict{Symbol, Int64}
5358
addHistory::Vector{Symbol} #TODO: Discuss more - is this an audit trail?
54-
solverParams::Any # Solver parameters
59+
solverParams::T # Solver parameters
60+
GraphsDFG{T}(g::FGType=Graphs.incdict(GraphsNode,is_directed=false),
61+
d::String="Graphs.jl implementation",
62+
n::Int64=0,
63+
l::Dict{Symbol, Int64}=Dict{Symbol, Int64}(),
64+
a::Vector{Symbol}=Symbol[];
65+
params::T=NoSolverParams()) where T <: AbstractParams = new{T}(g, d, n, l, a, params)
5566
end
5667

57-
"""
58-
$(SIGNATURES)
59-
Create a new in-memory Graphs.jl-based DFG factor graph.
60-
"""
61-
GraphsDFG() = GraphsDFG(Graphs.incdict(GraphsNode,is_directed=false), "Graphs.jl implementation", 0, Dict{Symbol, Int64}(), Symbol[], nothing)
68+
69+
# """
70+
# $(SIGNATURES)
71+
# Create a new in-memory Graphs.jl-based DFG factor graph.
72+
# """
73+
# GraphsDFG() = GraphsDFG(Graphs.incdict(GraphsNode,is_directed=false), "Graphs.jl implementation", 0, Dict{Symbol, Int64}(), Symbol[], NoSolverParams())
6274

6375
# Accessors
6476
getLabelDict(dfg::GraphsDFG) = dfg.labelDict
@@ -67,7 +79,11 @@ setDescription(dfg::GraphsDFG, description::String) = dfg.description = descript
6779
getInnerGraph(dfg::GraphsDFG) = dfg.g
6880
getAddHistory(dfg::GraphsDFG) = dfg.addHistory
6981
getSolverParams(dfg::GraphsDFG) = dfg.solverParams
70-
setSolverParams(dfg::GraphsDFG, solverParams::Any) = dfg.solverParams = solverParams
82+
83+
# setSolverParams(dfg::GraphsDFG, solverParams) = dfg.solverParams = solverParams
84+
function setSolverParams(dfg::GraphsDFG, solverParams::P) where P <: AbstractParams
85+
dfg.solverParams = solverParams
86+
end
7187

7288
"""
7389
$(SIGNATURES)
@@ -125,7 +141,7 @@ function addFactor!(dfg::GraphsDFG, variables::Vector{DFGVariable}, factor::DFGF
125141
Graphs.add_edge!(dfg.g, edge)
126142
end
127143
# Track insertion
128-
push!(dfg.addHistory, factor.label)
144+
# push!(dfg.addHistory, factor.label)
129145

130146
return true
131147
end
@@ -428,7 +444,7 @@ Optionally provide a distance to specify the number of edges should be followed.
428444
Optionally provide an existing subgraph addToDFG, the extracted nodes will be copied into this graph. By default a new subgraph will be created.
429445
Note: By default orphaned factors (where the subgraph does not contain all the related variables) are not returned. Set includeOrphanFactors to return the orphans irrespective of whether the subgraph contains all the variables.
430446
"""
431-
function getSubgraphAroundNode(dfg::GraphsDFG, node::T, distance::Int64=1, includeOrphanFactors::Bool=false, addToDFG::GraphsDFG=GraphsDFG())::GraphsDFG where T <: DFGNode
447+
function getSubgraphAroundNode(dfg::GraphsDFG{P}, node::T, distance::Int64=1, includeOrphanFactors::Bool=false, addToDFG::GraphsDFG=GraphsDFG{P}())::GraphsDFG where {P <: AbstractParams, T <: DFGNode}
432448
if !haskey(dfg.labelDict, node.label)
433449
error("Variable/factor with label '$(node.label)' does not exist in the factor graph")
434450
end

test/interfaceTests.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
dfg = testDFGAPI()
1+
dfg = testDFGAPI{NoSolverParams}()
22
v1 = DFGVariable(:a)
33
v2 = DFGVariable(:b)
44
f1 = DFGFactor{Int, :Symbol}(:f1)
@@ -21,7 +21,7 @@ end
2121
@test ls(dfg, r"a") == [v1]
2222
@test lsf(dfg, r"f*") == [f1]
2323
# Accessors
24-
@test getAddHistory(dfg) == [:a, :b, :f1]
24+
@test getAddHistory(dfg) == [:a, :b] #, :f1
2525
@test getDescription(dfg) != nothing
2626
@test getLabelDict(dfg) != nothing
2727
# Existence
@@ -96,7 +96,7 @@ end
9696

9797
# Now make a complex graph for connectivity tests
9898
numNodes = 10
99-
dfg = testDFGAPI()
99+
dfg = testDFGAPI{NoSolverParams}()
100100
verts = map(n -> DFGVariable(Symbol("x$n")), 1:numNodes)
101101
map(v -> addVariable!(dfg, v), verts)
102102
map(n -> addFactor!(dfg, [verts[n], verts[n+1]], DFGFactor{Int, :Symbol}(Symbol("x$(n)x$(n+1)f1"))), 1:(numNodes-1))
@@ -132,4 +132,5 @@ end
132132
@test toDot(dfg) == "graph graphname {\n18 [\"label\"=\"x8x9f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n2 [\"label\"=\"x2\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n2 -- 11\n2 -- 12\n16 [\"label\"=\"x6x7f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n11 [\"label\"=\"x1x2f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n7 [\"label\"=\"x7\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n7 -- 16\n7 -- 17\n9 [\"label\"=\"x9\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n9 -- 18\n9 -- 19\n10 [\"label\"=\"x10\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n10 -- 19\n19 [\"label\"=\"x9x10f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n17 [\"label\"=\"x7x8f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n8 [\"label\"=\"x8\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n8 -- 17\n8 -- 18\n6 [\"label\"=\"x6\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n6 -- 15\n6 -- 16\n4 [\"label\"=\"x4\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n4 -- 13\n4 -- 14\n3 [\"label\"=\"x3\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n3 -- 12\n3 -- 13\n5 [\"label\"=\"x5\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n5 -- 14\n5 -- 15\n13 [\"label\"=\"x3x4f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n14 [\"label\"=\"x4x5f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n15 [\"label\"=\"x5x6f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n12 [\"label\"=\"x2x3f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n1 [\"label\"=\"x1\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n1 -- 11\n}\n"
133133

134134
@test toDotFile(dfg, "something.dot") == nothing
135+
Base.rm("something.dot")
135136
end

0 commit comments

Comments
 (0)