-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathGraphProductOperations.jl
218 lines (173 loc) · 7.29 KB
/
GraphProductOperations.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
"""
$(SIGNATURES)
Multiply various full and partial dimension proposal densities.
DevNotes
- FIXME consolidate partial and full product AMP API, relates to #1010
- TODO better consolidate with full dimension product
- TODO -- reuse memory rather than rand here
"""
function prodmultiplefullpartials(dens::Vector{BallTreeDensity},
partials::Dict{Int, Vector{BallTreeDensity}},
Ndims::Int,
N::Int,
manis::Tuple )
#
# calculate products over all dimensions, legacy proposals held in `dens` vector
pGM = AMP.manifoldProduct(dens, manis, Niter=1) |> getPoints
# whats up with this?
for (dimnum,pp) in partials
push!(pp, AMP.manikde!(pGM[dimnum:dimnum,:], (manis[dimnum],) ))
end
# do each partial dimension individually
for (dimnum,pp) in partials
pGM[dimnum,:] = AMP.manifoldProduct(pp, (manis[dimnum],), Niter=1) |> getPoints
end
return pGM
end
"""
$SIGNATURES
Take product of `dens` accompanied by optional `partials` proposal belief densities.
Notes
-----
- `d` dimensional product approximation
- `partials` are treated as one dimensional
- Incorporate ApproxManifoldProducts to process variables in individual batches.
"""
function productbelief( dfg::AbstractDFG,
vertlabel::Symbol,
dens::Vector{<:BallTreeDensity},
partials::Dict{Int, <:AbstractVector{<:BallTreeDensity}},
N::Int;
dbg::Bool=false,
logger=ConsoleLogger() )
#
vert = DFG.getVariable(dfg, vertlabel)
manis = getVariableType(vert) |> getManifolds
pGM = Array{Float64,2}(undef, 0,0)
lennonp, lenpart = length(dens), length(partials)
denspts = getPoints(getBelief(vert))
Ndims = size(denspts,1)
with_logger(logger) do
@info "[$(lennonp)x$(lenpart)p,d$(Ndims),N$(N)],"
end
dens_ = 0 < lennonp ? dens : [deepcopy(getBelief(vert));]
if 0 < lennonp # || (lennonp == 0 && 0 < lenpart)
# multiple non-partials
pGM = prodmultiplefullpartials(dens_, partials, Ndims, N, manis)
elseif lennonp == 0 && 0 < lenpart
# only partials, must get other existing values for vertlabel from dfg
pGM = deepcopy(denspts)
# do each partial dimension individually
for (dimnum,pp) in partials
pGM[dimnum,:] = AMP.manifoldProduct(pp, (manis[dimnum],), Niter=1) |> getPoints
end
else
with_logger(logger) do
@warn "Unknown density product on variable=$(vert.label), lennonp=$(lennonp), lenpart=$(lenpart)"
end
pGM = Array{Float64,2}(undef, 0,1)
end
return pGM
end
"""
$SIGNATURES
Calculate the proposals and products on `destvert` using `factors` in factor graph `dfg`.
Notes
- Returns tuple of product and whether full dimensional (=true) or partial (=false).
"""
function predictbelief( dfg::AbstractDFG,
destvert::DFGVariable,
factors::Vector{<:DFGFactor};
solveKey::Symbol=:defaul,
needFreshMeasurements::Bool=true,
N::Int=0,
dbg::Bool=false,
logger=ConsoleLogger(),
dens = Array{BallTreeDensity,1}(),
partials = Dict{Int, Vector{BallTreeDensity}}() )
#
# determine number of particles to draw from the marginal
nn = N != 0 ? N : size(getVal(destvert),2)
# get proposal beliefs
destvertlabel = destvert.label
inferdim = proposalbeliefs!(dfg, destvertlabel, factors, dens, partials, N=nn, dbg=dbg)
# take the product
pGM = productbelief(dfg, destvertlabel, dens, partials, nn, dbg=dbg, logger=logger )
return pGM, sum(inferdim)
end
function predictbelief( dfg::AbstractDFG,
destvertsym::Symbol,
factorsyms::AbstractVector{Symbol};
solveKey::Symbol=:default,
needFreshMeasurements::Bool=true,
N::Int=0,
dbg::Bool=false,
logger=ConsoleLogger(),
dens = Array{BallTreeDensity,1}(),
partials = Dict{Int, Vector{BallTreeDensity}}() )
#
factors = getFactor.(dfg, factorsyms)
vert = getVariable(dfg, destvertsym)
# determine the number of particles to draw from the marginal
nn = N != 0 ? N : size(getVal(vert, solveKey=solveKey),2)
# do the belief prediction
predictbelief(dfg, vert, factors, solveKey=solveKey, needFreshMeasurements=needFreshMeasurements, N=nn, dbg=dbg, logger=logger, dens=dens, partials=partials)
end
function predictbelief( dfg::AbstractDFG,
destvertsym::Symbol,
::Colon;
solveKey::Symbol=:default,
needFreshMeasurements::Bool=true,
N::Int=0,
dbg::Bool=false,
logger=ConsoleLogger(),
dens = Array{BallTreeDensity,1}(),
partials = Dict{Int, Vector{BallTreeDensity}}() )
#
predictbelief(dfg, destvertsym, getNeighbors(dfg, destvertsym), solveKey=solveKey, needFreshMeasurements=needFreshMeasurements, N=N, dbg=dbg, logger=logger, dens=dens, partials=partials )
end
"""
$(SIGNATURES)
Using factor graph object `dfg`, project belief through connected factors
(convolution with likelihood) to variable `sym` followed by a approximate functional product.
Return: product belief, full proposals, partial dimension proposals, labels
"""
function localProduct(dfg::AbstractDFG,
sym::Symbol;
solveKey::Symbol=:default,
N::Int=100,
dbg::Bool=false,
logger=ConsoleLogger() )
#
# vector of all neighbors as Symbols
lb = getNeighbors(dfg, sym)
# # get proposal beliefs
dens = Array{BallTreeDensity,1}()
partials = Dict{Int, Vector{BallTreeDensity}}()
pGM, sinfd = predictbelief(dfg, sym, lb, solveKey=solveKey, logger=logger, dens=dens, partials=partials)
# make manifold belief from product
vari = getVariable(dfg, sym)
pp = AMP.manikde!(pGM, getVariableType(vari) |> getManifolds )
return pp, dens, partials, lb, sinfd
end
localProduct(dfg::AbstractDFG, lbl::AbstractString; solveKey::Symbol=:default, N::Int=100, dbg::Bool=false) = localProduct(dfg, Symbol(lbl), solveKey=solveKey, N=N, dbg=dbg)
"""
$SIGNATURES
Basic wrapper to take local product and then set the value of `sym` in `dfg`.
Notes
- returns `::Tuple{BallTreeDensity, Float64, Vector{Symbol}}`
DevNotes:
- Unknown issue first occurred here near IIF v0.8.4 tag, recorded case at 2020-01-17T15:26:17.673
"""
function localProductAndUpdate!(dfg::AbstractDFG,
sym::Symbol,
setkde::Bool=true,
logger=ConsoleLogger() )
#
# calculate new points for sym using existing structure around sym in dfg
newPts, dens, parts, lbl, infdim = localProduct(dfg, sym, N=getSolverParams(dfg).N, logger=logger)
# maybe update dfg sym with newly calculated points
setkde && 0 < size(getPoints(newPts),2) ? setValKDE!(dfg, sym, newPts, false, infdim) : nothing
return newPts, infdim, lbl
end
#