Skip to content

Commit fe5c136

Browse files
committed
addFactors!() keeps previous graph manipulation
1 parent c2adde3 commit fe5c136

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

docs/src/man/graphicalmodel.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,5 @@ The function adds new factor nodes to the existing factor graph.
118118
```julia-repl
119119
addFactors!(gbp; mean = vector, variance = vector, jacobian = matrix)
120120
```
121-
The function allows the addition of the multiple factor nodes using the same input data format as well as the initial formation of the factor graph. The function accepts the following parameters: composite type `GraphicalModel`; the `mean` and `variance` vectors represent new measurement values and variances. The keyword `jacobian` with corresponding coefficients defines the set of equations that define new factor nodes. Also, function initializes messages from variable nodes to a new factor node using results from the last GBP iteration. Note that the function also affects `SystemModel.observation`, `SystemModel.variance`, `SystemModel.jacobian` and `SystemModel.jacobianTranspose` fields.
121+
The function allows the addition of the multiple factor nodes using the same input data format as well as the initial factor graph. The function accepts the following parameters: composite type `GraphicalModel`; the `mean` and `variance` vectors represent new measurement values and variances. The keyword `jacobian` with corresponding coefficients defines the set of equations that define new factor nodes. Also, function initializes messages from variable nodes to a new factor node using results from the last GBP iteration. Note that the function also affects `SystemModel.observation`, `SystemModel.variance`, `SystemModel.jacobian` and `SystemModel.jacobianTranspose` fields.
122122

src/graphmanipulation.jl

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ function defreezeFactor!(gbp::GraphicalModel; factor = 0::Int64)
3333
end
3434

3535
if whereIs != 0
36-
errorInside(whereIs, gbp.graph.iterateFactor, factorLocal; name = "factor node")
36+
errorInside(whereIs, gbp.graph.iterateFactor, factorLocal; name = "factor node", state = "defrozen")
3737
insert!(gbp.graph.iterateFactor, whereIs, factorLocal)
3838
else
39-
errorLast(gbp.graph.iterateFactor, factorLocal; name = " factor node")
39+
errorLast(gbp.graph.iterateFactor, factorLocal; name = " factor node", state = "defrozen")
4040
push!(gbp.graph.iterateFactor, factorLocal)
4141
end
4242
end
@@ -70,10 +70,10 @@ function defreezeVariable!(gbp::GraphicalModel; variable = 0::Int64)
7070
end
7171

7272
if whereIs != 0
73-
errorInside(whereIs, gbp.graph.iterateVariable, variable; name = "variable node")
73+
errorInside(whereIs, gbp.graph.iterateVariable, variable; name = "variable node", state = "defrozen")
7474
insert!(gbp.graph.iterateVariable, whereIs, variable)
7575
else
76-
errorLast(gbp.graph.iterateVariable, variable; name = "variable node")
76+
errorLast(gbp.graph.iterateVariable, variable; name = "variable node", state = "defrozen")
7777
push!(gbp.graph.iterateVariable, variable)
7878
end
7979
end
@@ -94,6 +94,7 @@ function freezeVariableFactor!(gbp::GraphicalModel; variable = 0::Int64, factor
9494
errorWhereIs(whereIs; name = "edge", state = "frozen")
9595

9696
deleteat!(gbp.graph.colptr[variable], whereIs)
97+
gbp.graph.toFactor[factor, variable] = 0
9798
end
9899

99100
######### Defreeze Egdge: From variable to factor node ##########
@@ -114,6 +115,13 @@ function defreezeVariableFactor!(gbp::GraphicalModel; variable = 0::Int64, facto
114115
break
115116
end
116117
end
118+
119+
@inbounds for i = 1:gbp.graph.Nlink
120+
if gbp.inference.fromVariable[i] == variable && gbp.inference.toFactor[i] == factor
121+
gbp.graph.toFactor[factor, variable] = i
122+
break
123+
end
124+
end
117125
sort!(gbp.graph.colptr[variable])
118126
end
119127

@@ -135,6 +143,7 @@ function freezeFactorVariable!(gbp::GraphicalModel; factor = 0::Int64, variable
135143
errorWhereIs(whereIs; name = "edge", state = "frozen")
136144

137145
deleteat!(gbp.graph.rowptr[factorLocal], whereIs)
146+
gbp.graph.toVariable[variable, factor] = 0
138147
end
139148

140149
######### Defreeze Egdge: From factor to variable node ##########
@@ -157,6 +166,13 @@ function defreezeFactorVariable!(gbp::GraphicalModel; factor = 0::Int64, variabl
157166
break
158167
end
159168
end
169+
170+
@inbounds for i = 1:gbp.graph.Nlink
171+
if gbp.inference.fromFactor[i] == factor && gbp.inference.toVariable[i] == variable
172+
gbp.graph.toVariable[variable, factor] = i
173+
break
174+
end
175+
end
160176
sort!(gbp.graph.rowptr[factorLocal])
161177
end
162178

@@ -214,6 +230,7 @@ function hideFactor!(gbp::GraphicalModel; factor = 0::Int64)
214230
col = gbp.system.jacobianTranspose.rowval[i]
215231
gbp.system.jacobianTranspose.nzval[i] = 0.0
216232
gbp.system.jacobian[factor, col] = 0.0
233+
gbp.graph.toFactor[factor, col] = 0
217234
end
218235
end
219236

@@ -314,7 +331,7 @@ function addFactors!(gbp::GraphicalModel; mean = 0.0, variance = 0.0, jacobian =
314331
### Update vectors related with variable nodes
315332
append!(gbp.inference.toVariable, toFactorLocal)
316333
append!(gbp.inference.fromFactor, toFactorLocal)
317-
dropzeros!(gbp.system.jacobian)
334+
gbp.graph.toVariable = [gbp.graph.toVariable transpose(sendToFactor)]
318335
idxi = 1; prev = 1
319336
@inbounds for col = 1:gbp.graph.Nvariable
320337
for i = gbp.system.jacobian.colptr[col]:(gbp.system.jacobian.colptr[col + 1] - 1)
@@ -326,18 +343,20 @@ function addFactors!(gbp::GraphicalModel; mean = 0.0, variance = 0.0, jacobian =
326343
end
327344
prev = col + 1
328345

329-
push!(gbp.graph.colptr[col], idxi)
330-
push!(gbp.graph.colptrMarginal[col], idxi)
346+
if gbp.graph.toFactor[row, col] != 0
347+
push!(gbp.graph.colptr[col], idxi)
348+
push!(gbp.graph.colptrMarginal[col], idxi)
349+
end
331350
gbp.inference.toVariable[idxi] = col
332351
gbp.inference.fromFactor[idxi] = row
352+
353+
if gbp.graph.toVariable[col, row] != 0
354+
gbp.graph.toVariable[col, row] = idxi
355+
end
333356
idxi += 1
334357
end
335358
end
336359
end
337-
338-
links = collect(1:idxi - 1)
339-
gbp.graph.toVariable = sparse(gbp.inference.toVariable, gbp.inference.fromFactor, links, gbp.graph.Nvariable, gbp.graph.Nfactor + Nfactor)
340-
341360
append!(gbp.inference.meanFactorVariable, temp)
342361
append!(gbp.inference.varianceFactorVariable, temp)
343362
append!(gbp.graph.iterateFactor, collect(gbp.graph.Nindirect + 1:gbp.graph.Nindirect + Nindirect))
@@ -371,16 +390,16 @@ end
371390
end
372391

373392
######### Error inside ##########
374-
@inline function errorInside(whereIs, iterate, node; name = "")
393+
@inline function errorInside(whereIs, iterate, node; name = "", state = "")
375394
if whereIs != 1 && iterate[whereIs - 1] == node || iterate[1] == node
376-
error("The $name is already defrozen.")
395+
error("The $name is already $state.")
377396
end
378397
end
379398

380399
######### Error last ##########
381-
@inline function errorLast(iterate, node; name = "")
400+
@inline function errorLast(iterate, node; name = "", state = "")
382401
if iterate[end] == node
383-
error("The $name is already defrozen.")
402+
error("The $name is already $state.")
384403
end
385404
end
386405

0 commit comments

Comments
 (0)