Skip to content

Commit b683df9

Browse files
authored
Merge pull request #209 from JuliaRobotics/feature/4Q19/solvablesearch
adding solvable for tree work
2 parents c096dea + 88883d8 commit b683df9

File tree

8 files changed

+153
-130
lines changed

8 files changed

+153
-130
lines changed

src/GraphsDFG/services/GraphsDFG.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ Optionally specify a label regular expression to retrieves a subset of the varia
238238
function getVariables(dfg::GraphsDFG,
239239
regexFilter::Union{Nothing, Regex}=nothing;
240240
tags::Vector{Symbol}=Symbol[],
241-
solvable::Int=0 )::Vector{DFGVariable}
241+
solvable::Int=0)::Vector{DFGVariable}
242242
#
243-
variables = map(v -> v.dfgNode, filter(n -> (n.dfgNode isa DFGVariable) && (solvable <= isSolvable(n.dfgNode)), Graphs.vertices(dfg.g)))
243+
variables = map(v -> v.dfgNode, filter(n -> (n.dfgNode isa DFGVariable) && (solvable != 0 ? solvable <= isSolvable(n.dfgNode) : true), Graphs.vertices(dfg.g)))
244244
# filter on solvable
245245

246246
# filter on regex
@@ -261,8 +261,9 @@ end
261261
List the DFGFactors in the DFG.
262262
Optionally specify a label regular expression to retrieves a subset of the factors.
263263
"""
264-
function getFactors(dfg::GraphsDFG, regexFilter::Union{Nothing, Regex}=nothing)::Vector{DFGFactor}
265-
factors = map(v -> v.dfgNode, filter(n -> n.dfgNode isa DFGFactor, Graphs.vertices(dfg.g)))
264+
function getFactors(dfg::GraphsDFG, regexFilter::Union{Nothing, Regex}=nothing; solvable::Int=0)::Vector{DFGFactor}
265+
factors = map(v -> v.dfgNode, filter(n -> (n.dfgNode isa DFGFactor) && (solvable != 0 ? solvable <= isSolvable(n.dfgNode) : true), Graphs.vertices(dfg.g)))
266+
266267
if regexFilter != nothing
267268
factors = filter(f -> occursin(regexFilter, String(f.label)), factors)
268269
end
@@ -281,14 +282,14 @@ end
281282
$(SIGNATURES)
282283
Retrieve a list of labels of the immediate neighbors around a given variable or factor.
283284
"""
284-
function getNeighbors(dfg::GraphsDFG, node::T; ready::Union{Nothing, Int}=nothing, backendset::Union{Nothing, Int}=nothing)::Vector{Symbol} where T <: DFGNode
285+
function getNeighbors(dfg::GraphsDFG, node::T; solvable::Int=0, backendset::Union{Nothing, Int}=nothing)::Vector{Symbol} where T <: DFGNode
285286
if !haskey(dfg.labelDict, node.label)
286287
error("Variable/factor with label '$(node.label)' does not exist in the factor graph")
287288
end
288289
vert = dfg.g.vertices[dfg.labelDict[node.label]]
289290
neighbors = in_neighbors(vert, dfg.g) #Don't use out_neighbors! It enforces directiveness even if we don't want it
290291
# Additional filtering
291-
neighbors = ready != nothing ? filter(v -> isSolvable(v.dfgNode) >= ready, neighbors) : neighbors
292+
neighbors = solvable != 0 ? filter(v -> solvable <= isSolvable(v.dfgNode), neighbors) : neighbors
292293
neighbors = backendset != nothing ? filter(v -> isSolveInProgress(v.dfgNode) == backendset, neighbors) : neighbors
293294
# Variable sorting (order is important)
294295
if node isa DFGFactor
@@ -302,14 +303,14 @@ end
302303
$(SIGNATURES)
303304
Retrieve a list of labels of the immediate neighbors around a given variable or factor specified by its label.
304305
"""
305-
function getNeighbors(dfg::GraphsDFG, label::Symbol; ready::Union{Nothing, Int}=nothing, backendset::Union{Nothing, Int}=nothing)::Vector{Symbol} where T <: DFGNode
306+
function getNeighbors(dfg::GraphsDFG, label::Symbol; solvable::Int=0, backendset::Union{Nothing, Int}=nothing)::Vector{Symbol} where T <: DFGNode
306307
if !haskey(dfg.labelDict, label)
307308
error("Variable/factor with label '$(label)' does not exist in the factor graph")
308309
end
309310
vert = dfg.g.vertices[dfg.labelDict[label]]
310311
neighbors = in_neighbors(vert, dfg.g) #Don't use out_neighbors! It enforces directiveness even if we don't want it
311312
# Additional filtering
312-
neighbors = ready != nothing ? filter(v -> isSolvable(v.dfgNode) >= ready, neighbors) : neighbors
313+
neighbors = solvable != 0 ? filter(v -> isSolvable(v.dfgNode) >= solvable, neighbors) : neighbors
313314
neighbors = backendset != nothing ? filter(v -> isSolveInProgress(v.dfgNode) == backendset, neighbors) : neighbors
314315
# Variable sorting when using a factor (function order is important)
315316
if vert.dfgNode isa DFGFactor

src/LightDFG/services/LightDFG.jl

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,16 @@ end
194194
List the DFGVariables in the DFG.
195195
Optionally specify a label regular expression to retrieves a subset of the variables.
196196
"""
197-
function getVariables(dfg::LightDFG, regexFilter::Union{Nothing, Regex}=nothing; tags::Vector{Symbol}=Symbol[])::Vector{AbstractDFGVariable}
197+
function getVariables(dfg::LightDFG, regexFilter::Union{Nothing, Regex}=nothing; tags::Vector{Symbol}=Symbol[], solvable::Int=0)::Vector{AbstractDFGVariable}
198198

199199
# variables = map(v -> v.dfgNode, filter(n -> n.dfgNode isa DFGVariable, vertices(dfg.g)))
200200
variables = collect(values(dfg.g.variables))
201201
if regexFilter != nothing
202202
variables = filter(v -> occursin(regexFilter, String(v.label)), variables)
203203
end
204+
if solvable != 0
205+
variables = filter(v -> _isSolvable(dfg, v.label, solvable), variables)
206+
end
204207
if length(tags) > 0
205208
mask = map(v -> length(intersect(v.tags, tags)) > 0, variables )
206209
return variables[mask]
@@ -223,14 +226,15 @@ Related
223226
ls
224227
"""
225228

226-
function getVariableIds(dfg::LightDFG, regexFilter::Union{Nothing, Regex}=nothing; tags::Vector{Symbol}=Symbol[])::Vector{Symbol}
229+
function getVariableIds(dfg::LightDFG, regexFilter::Union{Nothing, Regex}=nothing; tags::Vector{Symbol}=Symbol[], solvable::Int=0)::Vector{Symbol}
227230

228231
# variables = map(v -> v.dfgNode, filter(n -> n.dfgNode isa DFGVariable, vertices(dfg.g)))
229232
if length(tags) > 0
230-
return map(v -> v.label, getVariables(dfg, regexFilter, tags=tags))
233+
return map(v -> v.label, getVariables(dfg, regexFilter, tags=tags, solvable=solvable))
231234
else
232235
variables = collect(keys(dfg.g.variables))
233236
regexFilter != nothing && (variables = filter(v -> occursin(regexFilter, String(v)), variables))
237+
solvable != 0 && (variables = filter(vId -> _isSolvable(dfg, vId, solvable), variables))
234238
return variables
235239
end
236240
end
@@ -240,12 +244,15 @@ end
240244
List the DFGFactors in the DFG.
241245
Optionally specify a label regular expression to retrieves a subset of the factors.
242246
"""
243-
function getFactors(dfg::LightDFG, regexFilter::Union{Nothing, Regex}=nothing)::Vector{AbstractDFGFactor}
247+
function getFactors(dfg::LightDFG, regexFilter::Union{Nothing, Regex}=nothing; solvable::Int=0)::Vector{AbstractDFGFactor}
244248
# factors = map(v -> v.dfgNode, filter(n -> n.dfgNode isa DFGFactor, vertices(dfg.g)))
245249
factors = collect(values(dfg.g.factors))
246250
if regexFilter != nothing
247251
factors = filter(f -> occursin(regexFilter, String(f.label)), factors)
248252
end
253+
if solvable != 0
254+
factors = filter(f -> _isSolvable(dfg, f.label, solvable), factors)
255+
end
249256
return factors
250257
end
251258

@@ -254,12 +261,15 @@ end
254261
Get a list of the IDs of the DFGFactors in the DFG.
255262
Optionally specify a label regular expression to retrieves a subset of the factors.
256263
"""
257-
function getFactorIds(dfg::LightDFG, regexFilter::Union{Nothing, Regex}=nothing)::Vector{Symbol}
264+
function getFactorIds(dfg::LightDFG, regexFilter::Union{Nothing, Regex}=nothing; solvable::Int=0)::Vector{Symbol}
258265
# factors = map(v -> v.dfgNode, filter(n -> n.dfgNode isa DFGFactor, vertices(dfg.g)))
259266
factors = collect(keys(dfg.g.factors))
260267
if regexFilter != nothing
261268
factors = filter(f -> occursin(regexFilter, String(f)), factors)
262269
end
270+
if solvable != 0
271+
factors = filter(fId -> _isSolvable(dfg, fId, solvable), factors)
272+
end
263273
return factors
264274
end
265275

@@ -271,7 +281,7 @@ function isFullyConnected(dfg::LightDFG)::Bool
271281
return length(LightGraphs.connected_components(dfg.g)) == 1
272282
end
273283

274-
function _isready(dfg::LightDFG, label::Symbol, ready::Int)::Bool
284+
function _isSolvable(dfg::LightDFG, label::Symbol, ready::Int)::Bool
275285

276286
haskey(dfg.g.variables, label) && (return dfg.g.variables[label].solvable >= ready)
277287
haskey(dfg.g.factors, label) && (return dfg.g.factors[label].solvable >= ready)
@@ -294,7 +304,7 @@ end
294304
$(SIGNATURES)
295305
Retrieve a list of labels of the immediate neighbors around a given variable or factor.
296306
"""
297-
function getNeighbors(dfg::LightDFG, node::DFGNode; ready::Union{Nothing, Int}=nothing, backendset::Union{Nothing, Int}=nothing)::Vector{Symbol}
307+
function getNeighbors(dfg::LightDFG, node::DFGNode; solvable::Int=0, backendset::Union{Nothing, Int}=nothing)::Vector{Symbol}
298308
label = node.label
299309
if !exists(dfg, label)
300310
error("Variable/factor with label '$(node.label)' does not exist in the factor graph")
@@ -303,7 +313,7 @@ function getNeighbors(dfg::LightDFG, node::DFGNode; ready::Union{Nothing, Int}=n
303313
neighbors_il = FactorGraphs.outneighbors(dfg.g, dfg.g.labels[label])
304314
neighbors_ll = [dfg.g.labels[i] for i in neighbors_il]
305315
# Additional filtering
306-
ready != nothing && filter!(lbl -> _isready(dfg, lbl, ready), neighbors_ll)
316+
solvable != 0 && filter!(lbl -> _isSolvable(dfg, lbl, solvable), neighbors_ll)
307317
backendset != nothing && filter!(lbl -> _isbackendset(dfg, lbl, backendset), neighbors_ll)
308318

309319
# Variable sorting (order is important)
@@ -320,15 +330,15 @@ end
320330
$(SIGNATURES)
321331
Retrieve a list of labels of the immediate neighbors around a given variable or factor specified by its label.
322332
"""
323-
function getNeighbors(dfg::LightDFG, label::Symbol; ready::Union{Nothing, Int}=nothing, backendset::Union{Nothing, Int}=nothing)::Vector{Symbol}
333+
function getNeighbors(dfg::LightDFG, label::Symbol; solvable::Int=0, backendset::Union{Nothing, Int}=nothing)::Vector{Symbol}
324334
if !exists(dfg, label)
325335
error("Variable/factor with label '$(label)' does not exist in the factor graph")
326336
end
327337

328338
neighbors_il = FactorGraphs.outneighbors(dfg.g, dfg.g.labels[label])
329339
neighbors_ll = [dfg.g.labels[i] for i in neighbors_il]
330340
# Additional filtering
331-
ready != nothing && filter!(lbl -> _isready(dfg, lbl, ready), neighbors_ll)
341+
solvable != 0 && filter!(lbl -> _isSolvable(dfg, lbl, solvable), neighbors_ll)
332342
backendset != nothing && filter!(lbl -> _isbackendset(dfg, lbl, backendset), neighbors_ll)
333343

334344
# Variable sorting (order is important)
@@ -426,10 +436,10 @@ Get an adjacency matrix for the DFG, returned as a Matrix{Union{Nothing, Symbol}
426436
Rows are all factors, columns are all variables, and each cell contains either nothing or the symbol of the relating factor.
427437
The first row and first column are factor and variable headings respectively.
428438
"""
429-
function getAdjacencyMatrix(dfg::LightDFG)::Matrix{Union{Nothing, Symbol}}
439+
function getAdjacencyMatrix(dfg::LightDFG; solvable::Int=0)::Matrix{Union{Nothing, Symbol}}
430440
#TODO Why does it need to be sorted?
431-
varLabels = sort(collect(keys(dfg.g.variables)))#ort(map(v->v.label, getVariables(dfg)))
432-
factLabels = sort(collect(keys(dfg.g.factors)))#sort(map(f->f.label, getFactors(dfg)))
441+
varLabels = sort(getVariableIds(dfg, solvable=solvable))#ort(map(v->v.label, getVariables(dfg)))
442+
factLabels = sort(getFactorIds(dfg, solvable=solvable))#sort(map(f->f.label, getFactors(dfg)))
433443
vDict = Dict(varLabels .=> [1:length(varLabels)...].+1)
434444

435445
adjMat = Matrix{Union{Nothing, Symbol}}(nothing, length(factLabels)+1, length(varLabels)+1)
@@ -443,9 +453,9 @@ function getAdjacencyMatrix(dfg::LightDFG)::Matrix{Union{Nothing, Symbol}}
443453
return adjMat
444454
end
445455

446-
function getAdjacencyMatrixSparse(dfg::LightDFG)::Tuple{LightGraphs.SparseMatrixCSC, Vector{Symbol}, Vector{Symbol}}
447-
varLabels = collect(keys(dfg.g.variables))
448-
factLabels = collect(keys(dfg.g.factors))
456+
function getAdjacencyMatrixSparse(dfg::LightDFG; solvable::Int=0)::Tuple{LightGraphs.SparseMatrixCSC, Vector{Symbol}, Vector{Symbol}}
457+
varLabels = getVariableIds(dfg, solvable=solvable)
458+
factLabels = getFactorIds(dfg, solvable=solvable)
449459
varIndex = [dfg.g.labels[s] for s in varLabels]
450460
factIndex = [dfg.g.labels[s] for s in factLabels]
451461

src/SymbolDFG/SymbolDFG.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ using SparseArrays
55
using DocStringExtensions
66

77
import ...DistributedFactorGraphs: AbstractDFG, DFGNode, AbstractParams, NoSolverParams, DFGVariable, DFGFactor
8-
# import DFG functions to exstend
8+
# import DFG functions to extend
99
import ...DistributedFactorGraphs: setSolverParams,
1010
getFactor,
1111
setDescription,

0 commit comments

Comments
 (0)