@@ -11,66 +11,63 @@ import IncrementalInference: getSample
1111
1212# #
1313
14- mutable struct DevelopPrior <: AbstractPrior
15- x:: Distribution
14+ mutable struct DevelopPrior{T <: SamplableBelief } <: AbstractPrior
15+ x:: T
1616end
17- getSample (dpl :: DevelopPrior , N:: Int = 1 ) = (reshape (rand (dpl . x, N),1 ,N), )
17+ getSample (cf :: CalcFactor{<: DevelopPrior} , N:: Int = 1 ) = (reshape (rand (cf . factor . x, N),1 ,N), )
1818
19- mutable struct DevelopLikelihood <: AbstractRelativeRoots
20- x:: Distribution
19+ mutable struct DevelopLikelihood{T <: SamplableBelief } <: AbstractRelativeRoots
20+ x:: T
2121end
22- getSample (dpl:: DevelopLikelihood , N:: Int = 1 ) = (reshape (rand (dpl. x, N),1 ,N), )
23- function (vv:: DevelopLikelihood )(res:: AbstractArray{<:Real} ,
24- userdata:: FactorMetadata ,
25- idx:: Int ,
26- meas:: Tuple ,
27- wXi:: AbstractArray{<:Real,2} ,
28- wXj:: AbstractArray{<:Real,2} ):: Nothing
22+
23+ getSample (cf:: CalcFactor{<:DevelopLikelihood} , N:: Int = 1 ) = (reshape (rand (cf. factor. x, N),1 ,N), )
24+ function (cf:: CalcFactor{<:DevelopLikelihood} )( res:: AbstractVector{<:Real} ,
25+ meas,
26+ wXi,
27+ wXj )
2928 #
30- res[ 1 ] = meas[ 1 ][idx] - (wXj[ 1 ,idx] - wXi[ 1 ,idx] )
29+ res . = meas - (wXj - wXi)
3130 nothing
3231end
3332
3433
35- global N = 100
36- global fg = initfg ()
34+ N = 100
35+ fg = initfg ()
3736
37+ # #
3838
3939@testset " test populate factor graph with a multi-hypothesis factor..." begin
4040
41- global v1 = addVariable! (fg, :x1 , ContinuousScalar, N = N)
41+ # #
4242
43- global pr = DevelopPrior (Normal (10.0 ,1.0 ))
44- global f1 = addFactor! (fg,[:x1 ],pr)
43+ v1 = addVariable! (fg, :x1 , ContinuousScalar, N= N)
4544
45+ pr = DevelopPrior (Normal (10.0 ,1.0 ))
46+ f1 = addFactor! (fg,[:x1 ],pr)
4647
47- ensureAllInitialized! (fg)
4848
49- # Juno.breakpoint("/home/dehann/.julia/v0.5/IncrementalInference/src/ApproxConv.jl",121 )
49+ ensureAllInitialized! (fg )
5050
51- global pts = evalFactor (fg, f1, v1. label, N= N)
51+ pts = evalFactor (fg, f1, v1. label, N= N)
5252
5353@test sum (abs .(pts .- 1.0 ) .< 5 ) < 30
5454@test sum (abs .(pts .- 10.0 ) .< 5 ) > 30
5555
5656
57-
58- global v2 = addVariable! (fg, :x2 , ContinuousScalar, N= N)
59- global pp = DevelopLikelihood (Normal (100.0 ,1.0 ))
60- global f2 = addFactor! (fg, [:x1 ;:x2 ], pp)
57+ v2 = addVariable! (fg, :x2 , ContinuousScalar, N= N)
58+ pp = DevelopLikelihood (Normal (100.0 ,1.0 ))
59+ f2 = addFactor! (fg, [:x1 ;:x2 ], pp)
6160
6261ensureAllInitialized! (fg)
6362
6463@test abs (Statistics. mean (getVal (fg, :x2 ))- 110.0 ) < 10.0
6564
6665
66+ v3 = addVariable! (fg, :x3 , ContinuousScalar, N= N)
67+ v4 = addVariable! (fg, :x4 , ContinuousScalar, N= N)
6768
68-
69- global v3 = addVariable! (fg, :x3 , ContinuousScalar, N= N)
70- global v4 = addVariable! (fg, :x4 , ContinuousScalar, N= N)
71-
72- global ppMH = DevelopLikelihood (Normal (90.0 ,1.0 ))
73- global f3 = addFactor! (fg, [:x2 ;:x3 ;:x4 ], ppMH, multihypo= [1.0 ;0.5 ;0.5 ])
69+ ppMH = DevelopLikelihood (Normal (90.0 ,1.0 ))
70+ f3 = addFactor! (fg, [:x2 ;:x3 ;:x4 ], ppMH, multihypo= [1.0 ;0.5 ;0.5 ])
7471
7572
7673# @test IIIF._getCCW(f3).hypoverts == [:x3, :x4]
@@ -82,27 +79,33 @@ initManual!(fg, :x2, 1*ones(1,100))
8279initManual! (fg, :x3 , 2 * ones (1 ,100 ))
8380initManual! (fg, :x4 , 3 * ones (1 ,100 ))
8481
82+ # #
83+
8584end
8685
8786
8887
8988@testset " Test multi-hypothesis factor convolution exploration" begin
9089
91- global pts = approxConv (fg, :x2x3x4f1 , :x2 , N= N)
90+ # #
91+
92+ pts = approxConv (fg, :x2x3x4f1 , :x2 , N= N)
9293
9394@test 99 < sum (pts .<= - 70.0 )
9495
95- global pts = approxConv (fg, :x2x3x4f1 , :x3 , N= N)
96+ pts = approxConv (fg, :x2x3x4f1 , :x3 , N= N)
9697
9798@test 15 < sum (pts .== 3.0 ) < 75
9899
99- global pts = approxConv (fg, :x2x3x4f1 , :x4 , N= N)
100+ pts = approxConv (fg, :x2x3x4f1 , :x4 , N= N)
100101
101102@test 15 < sum (pts .== 2.0 ) < 75
102103
103- end
104+ # #
104105
106+ end
105107
108+ # #
106109
107110println (" Packing converters" )
108111
@@ -113,10 +116,10 @@ mutable struct PackedDevelopPrior <: PackedInferenceType
113116 PackedDevelopPrior (x) = new (x)
114117end
115118function convert (:: Type{PackedDevelopPrior} , d:: DevelopPrior )
116- PackedDevelopPrior (string ( d. x))
119+ PackedDevelopPrior (convert (PackedSamplableBelief, d. x))
117120end
118121function convert (:: Type{DevelopPrior} , d:: PackedDevelopPrior )
119- DevelopPrior (IncrementalInference . normalfromstring ( d. x))
122+ DevelopPrior (convert (SamplableBelief, d. x))
120123end
121124
122125mutable struct PackedDevelopLikelihood <: PackedInferenceType
@@ -125,80 +128,89 @@ mutable struct PackedDevelopLikelihood <: PackedInferenceType
125128 PackedDevelopLikelihood (x) = new (x)
126129end
127130function convert (:: Type{PackedDevelopLikelihood} , d:: DevelopLikelihood )
128- PackedDevelopLikelihood (string ( d. x))
131+ PackedDevelopLikelihood (convert (PackedSamplableBelief, d. x))
129132end
130133function convert (:: Type{DevelopLikelihood} , d:: PackedDevelopLikelihood )
131- DevelopLikelihood (IncrementalInference . extractdistribution ( d. x))
134+ DevelopLikelihood (convert (SamplableBelief, d. x))
132135end
133136
134137
138+ # #
139+
135140@testset " test packing and unpacking the data structure" begin
136141
137- global topack = getSolverData (f1)
138- global dd = convert (PackedFunctionNodeData{PackedDevelopPrior},topack)
139- global unpacked = convert (FunctionNodeData{CommonConvWrapper{DevelopPrior}},dd)
142+ # #
140143
141- @test abs (IIF. _getCCW (unpacked). usrfnc!. x. μ - 10.0 ) < 1e-10
142- @test abs (IIF. _getCCW (unpacked). usrfnc!. x. σ - 1.0 ) < 1e-10
144+ topack = getSolverData (getFactor (fg,:x1f1 ))
145+ dd = convert (PackedFunctionNodeData{PackedDevelopPrior},topack)
146+ unpacked = convert (FunctionNodeData{CommonConvWrapper{DevelopPrior}},dd)
143147
148+ @test abs (IIF. _getCCW (unpacked). usrfnc!. x. μ - 10.0 ) < 1e-10
149+ @test abs (IIF. _getCCW (unpacked). usrfnc!. x. σ - 1.0 ) < 1e-10
144150
145151
146- global topack = getSolverData (f3)
147- global dd = convert (PackedFunctionNodeData{PackedDevelopLikelihood},topack)
148- global unpacked = convert (FunctionNodeData{CommonConvWrapper{DevelopLikelihood}},dd)
152+ fct = getFactor (fg, :x2x3x4f1 )
153+ @show typeof (fct)
154+ topack = getSolverData (fct) # f3
155+ dd = convert (PackedFunctionNodeData{PackedDevelopLikelihood},topack)
156+ unpacked = convert (FunctionNodeData{CommonConvWrapper{DevelopLikelihood}},dd)
149157
150- # @test IIF._getCCW(unpacked).hypoverts == Symbol[:x3; :x4]
151- @test sum (abs .(IIF. _getCCW (unpacked). hypotheses. p[1 ] .- 0.0 )) < 0.1
152- @test sum (abs .(IIF. _getCCW (unpacked). hypotheses. p[2 : 3 ] .- 0.5 )) < 0.1
153- # str = "Symbol[:x3, :x4];[0.5, 0.5]"
154- # IncrementalInference.parsemultihypostr(str)
158+ # @test IIF._getCCW(unpacked).hypoverts == Symbol[:x3; :x4]
159+ @test sum (abs .(IIF. _getCCW (unpacked). hypotheses. p[1 ] .- 0.0 )) < 0.1
160+ @test sum (abs .(IIF. _getCCW (unpacked). hypotheses. p[2 : 3 ] .- 0.5 )) < 0.1
155161
156- end
157162
163+ # #
158164
165+ end
166+
167+ # #
159168
160169# start a new factor graph
161- global N = 200
162- global fg = initfg ()
170+ N = 200
171+ fg = initfg ()
172+
173+ # #
163174
164175@testset " test tri-modal factor..." begin
165176
177+ # #
166178
167- global v1 = addVariable! (fg, :x1 , ContinuousScalar, N= N)
179+ v1 = addVariable! (fg, :x1 , ContinuousScalar, N= N)
168180
169- global pr = DevelopPrior (Normal (10.0 ,1.0 ))
170- global f1 = addFactor! (fg,[:x1 ],pr)
181+ pr = DevelopPrior (Normal (10.0 ,1.0 ))
182+ f1 = addFactor! (fg,[:x1 ],pr)
171183
172184
173185ensureAllInitialized! (fg)
174186
175187# Juno.breakpoint("/home/dehann/.julia/v0.5/IncrementalInference/src/ApproxConv.jl",121)
176188
177- global pts = approxConv (fg, Symbol (f1. label), :x1 , N= N)
189+ pts = approxConv (fg, Symbol (f1. label), :x1 , N= N)
178190
179191
180192@test sum (abs .(pts .- 1.0 ) .< 5 ) < 30
181193@test sum (abs .(pts .- 10.0 ) .< 5 ) > 30
182194
183195
184196
185- global v2 = addVariable! (fg, :x2 , ContinuousScalar, N= N)
186- global pp = DevelopLikelihood (Normal (100.0 ,1.0 ))
187- global f2 = addFactor! (fg, [:x1 ;:x2 ], pp)
197+ v2 = addVariable! (fg, :x2 , ContinuousScalar, N= N)
198+ pp = DevelopLikelihood (Normal (100.0 ,1.0 ))
199+ f2 = addFactor! (fg, [:x1 ;:x2 ], pp)
188200
189201ensureAllInitialized! (fg)
190202
191203@test abs (Statistics. mean (getVal (fg, :x2 ))- 110.0 ) < 10.0
192204
193205
194206
195- global v3 = addVariable! (fg, :x3 , ContinuousScalar, N= N)
196- global v4 = addVariable! (fg, :x4 , ContinuousScalar, N= N)
197- global v5 = addVariable! (fg, :x5 , ContinuousScalar, N= N)
207+ v3 = addVariable! (fg, :x3 , ContinuousScalar, N= N)
208+ v4 = addVariable! (fg, :x4 , ContinuousScalar, N= N)
209+ v5 = addVariable! (fg, :x5 , ContinuousScalar, N= N)
198210
199211
200- global ppMH = DevelopLikelihood (Normal (90.0 ,1.0 ))
201- global f3 = addFactor! (fg, [:x2 ;:x3 ;:x4 ;:x5 ], ppMH, multihypo= [1.0 ,0.333 ,0.333 ,0.334 ])
212+ ppMH = DevelopLikelihood (Normal (90.0 ,1.0 ))
213+ f3 = addFactor! (fg, [:x2 ;:x3 ;:x4 ;:x5 ], ppMH, multihypo= [1.0 ,0.333 ,0.333 ,0.334 ])
202214
203215
204216
@@ -216,13 +228,13 @@ initManual!(fg, :x5 ,4*ones(1,100))
216228
217229
218230# solve for certain idx
219- global pts = approxConv (fg, :x2x3x4x5f1 , :x2 , N= N)
231+ pts = approxConv (fg, :x2x3x4x5f1 , :x2 , N= N)
220232
221233@test 0.95 * N < sum (pts .<= - 70.0 )
222234
223235
224236# solve for one of uncertain variables
225- global pts = approxConv (fg, :x2x3x4x5f1 , :x3 , N= N)
237+ pts = approxConv (fg, :x2x3x4x5f1 , :x3 , N= N)
226238
227239@test 0.1 * N < sum (80 .< pts .< 100.0 ) < 0.5 * N
228240@test 0.1 * N < sum (pts .== 3.0 ) < 0.5 * N
@@ -233,7 +245,7 @@ global pts = approxConv(fg, :x2x3x4x5f1, :x3, N=N)
233245
234246
235247# solve for one of uncertain variables
236- global pts = approxConv (fg, :x2x3x4x5f1 , :x4 , N= N)
248+ pts = approxConv (fg, :x2x3x4x5f1 , :x4 , N= N)
237249
238250@test 0.1 * N < sum (80 .< pts .< 100.0 ) < 0.5 * N
239251@test 0.1 * N < sum (pts .== 2.0 ) < 0.5 * N
@@ -243,22 +255,23 @@ global pts = approxConv(fg, :x2x3x4x5f1, :x4, N=N)
243255
244256
245257# solve for one of uncertain variables
246- global pts = approxConv (fg, :x2x3x4x5f1 , :x5 , N= N)
258+ pts = approxConv (fg, :x2x3x4x5f1 , :x5 , N= N)
247259
248260@test 0.1 * N < sum (80 .< pts .< 100.0 ) < 0.5 * N
249261@test 0.1 * N < sum (pts .== 2.0 ) < 0.5 * N
250262@test 0.1 * N < sum (pts .== 3.0 ) < 0.5 * N
251263
252264@test 0.5 * N <= sum (80 .< pts .< 100.0 ) + sum (pts .== 2.0 ) + sum (pts .== 3.0 )
253265
254-
266+ # #
255267
256268end
257269
258- # #
259270
260271@testset " test multihypo api numerical tolerance, #1086" begin
261272
273+ # #
274+
262275fg = initfg ()
263276
264277addVariable! (fg, :x0 , ContinuousEuclid{1 })
@@ -268,6 +281,8 @@ addFactor!(fg, [:x0;:x1a;:x1b], LinearRelative(Normal()), multihypo=[1; 0.5;0.49
268281addFactor! (fg, [:x0 ;:x1a ;:x1b ], LinearRelative (Normal ()), multihypo= [1 ; 0.5 ;0.5000000000001 ])
269282
270283
284+ # #
285+
271286end
272287
273288
0 commit comments