-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
test_resamplers.cpp
602 lines (477 loc) · 17.3 KB
/
test_resamplers.cpp
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
#include <catch2/catch_all.hpp>
#include <pf/resamplers.h>
#include <pf/cf_filters.h>
#define NUMPARTICLES 20
#define DIMSTATE 3
#define DIMINNERMOD 2
#define DIMOBS 1
using namespace pf::resamplers;
using namespace pf::filters;
using Catch::Approx;
class MRFixture
{
public:
// types
using ssv = Eigen::Matrix<double,DIMSTATE,1>;
using innerVec = Eigen::Matrix<double,DIMINNERMOD,1>;
using transMat = Eigen::Matrix<double,DIMINNERMOD,DIMINNERMOD>;
using arrayVec = std::array<ssv,NUMPARTICLES>;
using arrayDouble = std::array<double,NUMPARTICLES>;
using arrayHMMMods = std::array<hmm<DIMINNERMOD,DIMOBS,double>,NUMPARTICLES>;
using arrayInnerVec = std::array<innerVec,NUMPARTICLES>;
// make the resampling object(s)
mn_resampler<NUMPARTICLES, DIMSTATE,double> m_mr;
mn_resampler_rbpf<NUMPARTICLES,DIMSTATE,hmm<DIMINNERMOD,DIMOBS,double>,double> m_mr_rbpf_hmm;
resid_resampler<NUMPARTICLES,DIMSTATE,double> m_residr;
stratif_resampler<NUMPARTICLES,DIMSTATE,double> m_stratifr;
systematic_resampler<NUMPARTICLES,DIMSTATE,double> m_systematicr;
// for Test_resampLogWts and systematic, residual, stratified, etc.
double m_good_value;
arrayVec m_vparts;
arrayDouble m_vw;
arrayVec m_vparts2;
arrayDouble m_vw2;
arrayVec m_vparts3;
arrayDouble m_vw3;
arrayVec m_vparts4;
arrayDouble m_vw4;
// for Test_resampLogWts_RBPF
innerVec m_initLogProbDistn1;
innerVec m_initLogProbDistn2;
transMat m_initLogTransMat1;
transMat m_initLogTransMat2;
arrayHMMMods m_hmms;
arrayVec m_rbpf_samps;
arrayDouble m_rbpf_logwts;
MRFixture() : m_initLogTransMat1(transMat::Zero()), m_initLogTransMat2(transMat::Zero())
{
// make the first particle have good value for everything (samples and weights)
// make all other particles have 0 for samples and -INF for weights
m_good_value = 42.42;
for(size_t i = 0; i < NUMPARTICLES; ++i){
if ( i == 0){
m_vparts[i] = ssv::Constant(m_good_value);
m_vw[i] = 0.0;
m_vparts2[i] = ssv::Constant(m_good_value);
m_vw2[i] = 0.0;
m_vparts3[i] = ssv::Constant(m_good_value);
m_vw3[i] = 0.0;
m_vparts4[i] = ssv::Constant(m_good_value);
m_vw4[i] = 0.0;
}else{
m_vparts[i] = ssv::Constant(0.0);
m_vw[i] = -std::numeric_limits<float_t>::infinity();
m_vparts2[i] = ssv::Constant(0.0);
m_vw2[i] = -std::numeric_limits<float_t>::infinity();
m_vparts3[i] = ssv::Constant(0.0);
m_vw3[i] = -std::numeric_limits<float_t>::infinity();
m_vparts4[i] = ssv::Constant(0.0);
m_vw4[i] = -std::numeric_limits<float_t>::infinity();;
}
}
// for Test_resampLogWts_RBPF
// make the first particle have 0 for samples and weights,
// and will have a model with all weight in the first spot,
// and a transition matrix that keeps everything in that first spot
// make everything BUT the first particle have 1 for samples and -INF for weights
// as well as models that have models with all weight in the last spot
// and transition matrices that keep it in that last spot
for(size_t i = 0; i < DIMINNERMOD; ++i){
m_initLogTransMat1(i,0) = std::log(1.0);
m_initLogTransMat2(i,DIMINNERMOD-1) = std::log(1.0);
m_initLogTransMat1.block(i,1,1,DIMINNERMOD-1).fill(std::log(0.0));// = Eigen::Matrix<double,1,DIMINNERMOD-1>::Zero();
m_initLogTransMat2.block(i,0,1,DIMINNERMOD-1).fill(std::log(0.0));// = Eigen::Matrix<double,1,DIMINNERMOD-1>::Zero();
if(i==0){
m_initLogProbDistn1(i) = std::log(1.0);
m_initLogProbDistn2(i) = std::log(0.0);
}else if(i == DIMINNERMOD - 1){
m_initLogProbDistn1(i) = std::log(0.0);
m_initLogProbDistn2(i) = std::log(1.0);
}else{
m_initLogProbDistn1(i) = std::log(0.0);
m_initLogProbDistn2(i) = std::log(0.0);
}
}
for(size_t i = 0; i < NUMPARTICLES; ++i){
if(i == 0){
m_hmms[i] = hmm<DIMINNERMOD,DIMOBS,double>(m_initLogProbDistn1, m_initLogTransMat1);
m_rbpf_samps[i] = ssv::Constant(0.0);
m_rbpf_logwts[i] = 0.0;
}else{
m_hmms[i] = hmm<DIMINNERMOD,DIMOBS,double>(m_initLogProbDistn2, m_initLogTransMat2);
m_rbpf_samps[i] = ssv::Constant(1.0);
m_rbpf_logwts[i] = -std::numeric_limits<float_t>::infinity();
}
}
}
};
TEST_CASE_METHOD(MRFixture, "test resampLogWts", "[resamplers]")
{
m_mr.resampLogWts(m_vparts, m_vw);
for(unsigned int p = 0; p < NUMPARTICLES; ++p){
REQUIRE(m_vw[p] == 0.0);
for(unsigned int i = 0; i < DIMSTATE; ++i){
REQUIRE(m_vparts[p](i) == m_good_value);
}
}
}
TEST_CASE_METHOD(MRFixture, "test resampLogWts 2", "[resamplers]")
{
arrayVec oneGoodSamp;
arrayDouble oneGoodWeight;
for(size_t i =0; i < NUMPARTICLES; ++i){
oneGoodWeight[i] = -std::numeric_limits<float_t>::infinity();
ssv xt;
xt(0) = i;
oneGoodSamp[i] = xt;
}
oneGoodWeight[3] = 0.0;
m_mr.resampLogWts(oneGoodSamp, oneGoodWeight);
for(unsigned int p = 0; p < NUMPARTICLES; ++p){
REQUIRE(oneGoodSamp[p](0) == Approx(3.0));
REQUIRE(oneGoodWeight[p] == 0.0);
}
}
TEST_CASE_METHOD(MRFixture, "test resampLogWts_RBPF", "[resamplers]")
{
// resample... you should only have the first set of things repeated a bunch of times
m_mr_rbpf_hmm.resampLogWts(m_hmms, m_rbpf_samps, m_rbpf_logwts);
// check all the particles
for(unsigned int p = 0; p < NUMPARTICLES; ++p){
// log 1 = 0
REQUIRE(m_rbpf_logwts[p] == 0.0);
// everything should be 0
for(unsigned int i = 0; i < DIMSTATE; ++i){
REQUIRE(m_rbpf_samps[p](i) == 0.0);
}
// the model that keeps everything in the first spot should remain
innerVec before = m_hmms[p].getFilterVecLogProbs();
for(unsigned int i = 0; i < DIMINNERMOD; ++i){
if(i == 0){
REQUIRE(before(i) == std::log(1.0)); // all weight in the first spot
}else{
REQUIRE(before(i) == std::log(0.0)); // all weight in the first spot
}
}
m_hmms[p].update(innerVec::Constant(1.0));
innerVec after = m_hmms[p].getFilterVecLogProbs();
for(unsigned int i = 0; i < DIMINNERMOD; ++i){
if(i == 0){
REQUIRE(after(i) == std::log(1.0)); // all weight in the first spot
}else{
REQUIRE(after(i) == std::log(0.0)); // all weight in the first spot
}
}
}
/**
* @TODO complete Kalman testing as well
*/
}
TEST_CASE_METHOD(MRFixture, "test resampLogWts_resid" "[resamplers]")
{
m_residr.resampLogWts(m_vparts2, m_vw2);
for(unsigned int p = 0; p < NUMPARTICLES; ++p){
REQUIRE(m_vw2[p] == Approx(0.0));
for(unsigned int i = 0; i < DIMSTATE; ++i){
REQUIRE(m_vparts2[p](i) == m_good_value);
}
}
}
TEST_CASE_METHOD(MRFixture, "test resampLogWts_resid2" "[resamplers]")
{
arrayVec oneGoodSamp;
arrayDouble oneGoodWeight;
for(size_t i =0; i < NUMPARTICLES; ++i){
oneGoodWeight[i] = -std::numeric_limits<float_t>::infinity();
ssv xt;
xt(0) = i;
oneGoodSamp[i] = xt;
}
oneGoodWeight[3] = 0.0;
m_residr.resampLogWts(oneGoodSamp, oneGoodWeight);
for(unsigned int p = 0; p < NUMPARTICLES; ++p){
REQUIRE(oneGoodSamp[p](0) == Approx(3.0));
REQUIRE(oneGoodWeight[p] == 0.0);
}
}
TEST_CASE_METHOD(MRFixture, "test resampLogWts_stratif", "[resamplers]")
{
m_stratifr.resampLogWts(m_vparts3, m_vw3);
for(unsigned int p = 0; p < NUMPARTICLES; ++p){
REQUIRE(m_vw3[p] == 0.0);
for(unsigned int i = 0; i < DIMSTATE; ++i){
REQUIRE(m_vparts3[p](i) == m_good_value);
}
}
}
TEST_CASE_METHOD(MRFixture, "test resampLogWts_stratif2", "[resamplers]")
{
arrayVec oneGoodSamp;
arrayDouble oneGoodWeight;
for(size_t i =0; i < NUMPARTICLES; ++i){
oneGoodWeight[i] = -std::numeric_limits<float_t>::infinity();
ssv xt;
xt(0) = i;
oneGoodSamp[i] = xt;
}
oneGoodWeight[3] = 0.0;
m_stratifr.resampLogWts(oneGoodSamp, oneGoodWeight);
for(unsigned int p = 0; p < NUMPARTICLES; ++p){
REQUIRE(oneGoodSamp[p](0) == Approx(3.0));
REQUIRE(oneGoodWeight[p] == 0.0);
}
}
TEST_CASE_METHOD(MRFixture, "test resampLogWts_systematic", "[resamplers]")
{
m_systematicr.resampLogWts(m_vparts4, m_vw4);
for(unsigned int p = 0; p < NUMPARTICLES; ++p){
REQUIRE(m_vw4[p] == 0.0);
for(unsigned int i = 0; i < DIMSTATE; ++i){
REQUIRE(m_vparts4[p](i) == m_good_value);
}
}
}
TEST_CASE_METHOD(MRFixture, "test resampLogWts_systematic2", "[resamplers]")
{
arrayVec oneGoodSamp;
arrayDouble oneGoodWeight;
for(size_t i =0; i < NUMPARTICLES; ++i){
oneGoodWeight[i] = -std::numeric_limits<float_t>::infinity();
ssv xt;
xt(0) = i;
oneGoodSamp[i] = xt;
}
oneGoodWeight[3] = 0.0;
m_systematicr.resampLogWts(oneGoodSamp, oneGoodWeight);
for(unsigned int p = 0; p < NUMPARTICLES; ++p){
REQUIRE(oneGoodSamp[p](0) == Approx(3.0));
REQUIRE(oneGoodWeight[p] == 0.0);
}
}
TEST_CASE_METHOD(MRFixture, "test auxiliary hilbert functions", "[resamplers]")
{
using namespace pf::resamplers;
constexpr unsigned nb = 3;
constexpr unsigned nd = 2;
unsigned total_matches = 0;
unsigned recov_H;
for(unsigned H = 0; H < pow(2,nb*nd); ++H){
recov_H = makeH<nb,nd>(makeHTranspose<nb,nd>(H));
if(recov_H == H)
total_matches++;
}
REQUIRE(total_matches == pow(2, nb*nd));
}
TEST_CASE_METHOD(MRFixture, "test auxiliary hilbert functions 2", "[resamplers]")
{
using namespace pf::resamplers;
constexpr unsigned nb = 2;
constexpr unsigned nd = 2;
unsigned total_matches = 0;
unsigned recov_H;
for(unsigned H = 0; H < pow(2,nb*nd); ++H){
recov_H = makeH<nb,nd>(makeHTranspose<nb,nd>(H));
if(recov_H == H)
total_matches++;
}
REQUIRE(total_matches == pow(2, nb*nd));
}
TEST_CASE_METHOD(MRFixture, "test auxiliary hilbert functions 3", "[resamplers]")
{
using namespace pf::resamplers;
constexpr unsigned nb = 1;
constexpr unsigned nd = 2;
unsigned total_matches = 0;
unsigned recov_H;
for(unsigned H = 0; H < pow(2,nb*nd); ++H){
recov_H = makeH<nb,nd>(makeHTranspose<nb,nd>(H));
if(recov_H == H)
total_matches++;
}
REQUIRE(total_matches == pow(2, nb*nd));
}
TEST_CASE_METHOD(MRFixture, "test auxiliary hilbert functions 4", "[resamplers]")
{
using namespace pf::resamplers;
constexpr unsigned nb = 3;
constexpr unsigned nd = 3;
unsigned total_matches = 0;
unsigned recov_H;
for(unsigned H = 0; H < pow(2,nb*nd); ++H){
recov_H = makeH<nb,nd>(makeHTranspose<nb,nd>(H));
if(recov_H == H)
total_matches++;
}
REQUIRE(total_matches == pow(2, nb*nd));
}
TEST_CASE_METHOD(MRFixture, "test auxiliary hilbert functions 5", "[resamplers]")
{
using namespace pf::resamplers;
constexpr unsigned nb = 2;
constexpr unsigned nd = 3;
unsigned total_matches = 0;
unsigned recov_H;
for(unsigned H = 0; H < pow(2,nb*nd); ++H){
recov_H = makeH<nb,nd>(makeHTranspose<nb,nd>(H));
if(recov_H == H)
total_matches++;
}
REQUIRE(total_matches == pow(2, nb*nd));
}
TEST_CASE_METHOD(MRFixture, "test auxiliary hilbert functions 6", "[resamplers]")
{
using namespace pf::resamplers;
constexpr unsigned nb = 1;
constexpr unsigned nd = 3;
unsigned total_matches = 0;
unsigned recov_H;
for(unsigned H = 0; H < pow(2,nb*nd); ++H){
recov_H = makeH<nb,nd>(makeHTranspose<nb,nd>(H));
if(recov_H == H)
total_matches++;
}
REQUIRE(total_matches == pow(2, nb*nd));
}
TEST_CASE_METHOD(MRFixture, "test hilbert inverses 1", "[resamplers]")
{
constexpr unsigned nb = 1;
constexpr unsigned nd = 2;
using namespace pf::resamplers;
unsigned total_matches = 0;
unsigned recov_H;
for(unsigned H = 0; H < pow(2,nb*nd); ++H){
recov_H = makeH<nb,nd>(
AxesToTranspose<nb,nd>(
TransposeToAxes<nb,nd>(
makeHTranspose<nb,nd>(H))));
if(recov_H == H)
total_matches++;
//std::cout << H << ", " << recov_H << "\n";
// this is just for printing if you want it
//auto H_image = TransposeToAxes<nb,nd>(makeHTranspose<nb,nd>(H));
//std::cout << H << ", ";
//for(size_t dim = 0; dim < nd; ++dim)
// std::cout << H_image[dim].to_ulong() << ", ";
//std::cout << "\n";
}
REQUIRE(total_matches == pow(2, nb*nd));
}
TEST_CASE_METHOD(MRFixture, "test hilbert inverses 2", "[resamplers]")
{
constexpr unsigned nb = 2;
constexpr unsigned nd = 2;
using namespace pf::resamplers;
unsigned total_matches = 0;
unsigned recov_H;
for(unsigned H = 0; H < pow(2,nb*nd); ++H){
recov_H = makeH<nb,nd>(
AxesToTranspose<nb,nd>(
TransposeToAxes<nb,nd>(
makeHTranspose<nb,nd>(H))));
if(recov_H == H)
total_matches++;
}
REQUIRE(total_matches == pow(2, nb*nd));
}
TEST_CASE_METHOD(MRFixture, "test hilbert inverses 3", "[resamplers]")
{
constexpr unsigned nb = 3;
constexpr unsigned nd = 2;
using namespace pf::resamplers;
unsigned total_matches = 0;
unsigned recov_H;
for(unsigned H = 0; H < pow(2,nb*nd); ++H){
recov_H = makeH<nb,nd>(
AxesToTranspose<nb,nd>(
TransposeToAxes<nb,nd>(
makeHTranspose<nb,nd>(H))));
if(recov_H == H)
total_matches++;
}
REQUIRE(total_matches == pow(2, nb*nd));
}
TEST_CASE_METHOD(MRFixture, "test hilbert inverses 4", "[resamplers]")
{
constexpr unsigned nb = 4;
constexpr unsigned nd = 2;
using namespace pf::resamplers;
unsigned total_matches = 0;
unsigned recov_H;
for(unsigned H = 0; H < pow(2,nb*nd); ++H){
recov_H = makeH<nb,nd>(
AxesToTranspose<nb,nd>(
TransposeToAxes<nb,nd>(
makeHTranspose<nb,nd>(H))));
if(recov_H == H)
total_matches++;
}
REQUIRE(total_matches == pow(2, nb*nd));
}
TEST_CASE_METHOD(MRFixture, "test hilbert inverses 5", "[resamplers]")
{
constexpr unsigned nb = 1;
constexpr unsigned nd = 3;
using namespace pf::resamplers;
unsigned total_matches = 0;
unsigned recov_H;
for(unsigned H = 0; H < pow(2,nb*nd); ++H){
recov_H = makeH<nb,nd>(
AxesToTranspose<nb,nd>(
TransposeToAxes<nb,nd>(
makeHTranspose<nb,nd>(H))));
if(recov_H == H)
total_matches++;
}
REQUIRE(total_matches == pow(2, nb*nd));
}
TEST_CASE_METHOD(MRFixture, "test hilbert inverses 6", "[resamplers]")
{
constexpr unsigned nb = 2;
constexpr unsigned nd = 3;
using namespace pf::resamplers;
unsigned total_matches = 0;
unsigned recov_H;
for(unsigned H = 0; H < pow(2,nb*nd); ++H){
recov_H = makeH<nb,nd>(
AxesToTranspose<nb,nd>(
TransposeToAxes<nb,nd>(
makeHTranspose<nb,nd>(H))));
if(recov_H == H)
total_matches++;
}
REQUIRE(total_matches == pow(2, nb*nd));
}
TEST_CASE_METHOD(MRFixture, "test hilbert inverses 7", "[resamplers]")
{
constexpr unsigned nb = 3;
constexpr unsigned nd = 3;
using namespace pf::resamplers;
unsigned total_matches = 0;
unsigned recov_H;
for(unsigned H = 0; H < pow(2,nb*nd); ++H){
recov_H = makeH<nb,nd>(
AxesToTranspose<nb,nd>(
TransposeToAxes<nb,nd>(
makeHTranspose<nb,nd>(H))));
if(recov_H == H)
total_matches++;
}
REQUIRE(total_matches == pow(2, nb*nd));
}
TEST_CASE_METHOD(MRFixture, "test hilbert inverses 8", "[resamplers]")
{
constexpr unsigned nb = 4;
constexpr unsigned nd = 3;
using namespace pf::resamplers;
unsigned total_matches = 0;
unsigned recov_H;
for(unsigned H = 0; H < pow(2,nb*nd); ++H){
recov_H = makeH<nb,nd>(
AxesToTranspose<nb,nd>(
TransposeToAxes<nb,nd>(
makeHTranspose<nb,nd>(H))));
if(recov_H == H)
total_matches++;
}
REQUIRE(total_matches == pow(2, nb*nd));
}