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
|
/* +------------------------------------------------------------------------+
| Mobile Robot Programming Toolkit (MRPT) |
| https://www.mrpt.org/ |
| |
| Copyright (c) 2005-2023, Individual contributors, see AUTHORS file |
| See: https://www.mrpt.org/Authors - All rights reserved. |
| Released under BSD License. See: https://www.mrpt.org/License |
+------------------------------------------------------------------------+ */
#include <mrpt/bayes/CParticleFilterCapable.h>
#include <mrpt/io/vector_loadsave.h>
#include <mrpt/math/data_utils.h>
#include <mrpt/math/ops_vectors.h>
#include <mrpt/math/utils.h>
#include <mrpt/random.h>
#include <mrpt/system/filesystem.h>
#include <iostream>
#include <map>
using namespace mrpt::bayes;
using namespace mrpt::math;
using namespace mrpt::random;
using namespace mrpt::system;
using namespace mrpt::io;
using namespace std;
double MIN_LOG_WEIG = -1.0;
unsigned int N_TESTS = 500;
int N_PARTICLES = 100;
// For batch experiment:
CVectorDouble min_log_ws;
map<string, CVectorDouble> results;
// vectorToTextFile( out_indxs, #ALGOR, true, true); /* By rows, append */
#define TEST_RESAMPLING(ALGOR) \
mrpt::system::deleteFile(#ALGOR); \
/*printf(#ALGOR);*/ \
/*printf("\n");*/ \
ERR_MEANs.clear(); \
ERR_STDs.clear(); \
for (size_t i = 0; i < N_TESTS; i++) \
{ \
mrpt::random::getRandomGenerator().drawUniformVector( \
log_ws, MIN_LOG_WEIG, 0.0); \
CParticleFilterCapable::log2linearWeights(log_ws, lin_ws); \
CParticleFilterCapable::computeResampling( \
CParticleFilter::ALGOR, log_ws, out_indxs); \
hist_parts = mrpt::math::histogram(out_indxs, 0, M - 1, M, true); \
vector<double> errs_hist = lin_ws - hist_parts; \
ERR_MEANs.push_back(mrpt::math::mean(errs_hist)); \
ERR_STDs.push_back(mrpt::math::stddev(errs_hist)); \
} \
printf("%s: ERR_MEAN %e\n", #ALGOR, mrpt::math::mean(ERR_MEANs)); \
printf("%s: ERR_STD %f\n", #ALGOR, mrpt::math::mean(ERR_STDs)); \
results[#ALGOR].push_back(mrpt::math::mean(ERR_STDs));
// ------------------------------------------------------
// TestResampling
// ------------------------------------------------------
void TestResampling()
{
vector<double> log_ws;
std::vector<size_t> out_indxs;
const size_t M = N_PARTICLES;
log_ws.resize(M);
// vectorToTextFile( log_ws, "log_ws.txt");
// Compute normalized linear weights:
vector<double> lin_ws;
vector<double> hist_parts;
vector<double> ERR_MEANs;
vector<double> ERR_STDs;
// prMultinomial
TEST_RESAMPLING(prMultinomial)
// prResidual
TEST_RESAMPLING(prResidual)
// prStratified
TEST_RESAMPLING(prStratified)
// prSystematic
TEST_RESAMPLING(prSystematic)
}
void TestBatch()
{
for (double LL = -2; LL <= 2.01; LL += 0.08)
{
double L = pow(10.0, LL);
min_log_ws.push_back(L);
printf("MIN_LOG_W=%f\n", L);
MIN_LOG_WEIG = L;
TestResampling();
}
// Save results to files:
CVectorDouble R;
vectorToTextFile(min_log_ws, "min_log_ws.txt");
R = results["prMultinomial"];
vectorToTextFile(R, "prMultinomial.txt");
R = results["prResidual"];
vectorToTextFile(R, "prResidual.txt");
R = results["prStratified"];
vectorToTextFile(R, "prStratified.txt");
R = results["prSystematic"];
vectorToTextFile(R, "prSystematic.txt");
}
// ------------------------------------------------------
// MAIN
// ------------------------------------------------------
int main(int argc, char** argv)
{
try
{
getRandomGenerator().randomize();
if (argc > 1) N_PARTICLES = atoi(argv[1]);
// TestResampling();
TestBatch();
return 0;
}
catch (exception& e)
{
std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
return -1;
}
catch (...)
{
cerr << "Untyped excepcion!!";
return -1;
}
}
|