-
Notifications
You must be signed in to change notification settings - Fork 3
/
gpfex1.cpp
173 lines (144 loc) · 4.36 KB
/
gpfex1.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
#include <iostream>
#include <fstream>
#include "gpf.h"
using namespace std;
using namespace PF;
//#define FILEIO
const double proc_noise_sd = sqrt(10.0);
const double obsv_noise_sd = 1.0;
const uint T = 100;
const uint Nsamples = 1000;
const uint Nx = 1;
const uint Nz = 1;
uint k = 0;
//----------------------------
// Process Equation
// xn : process noise
void process(std::vector<double> &xk, const std::vector<double> &xkm1, void* data)
{
gsl_rng *r = (gsl_rng*)data;
double un = gsl_ran_gaussian(r, proc_noise_sd);
xk[0] = 0.5*xkm1[0] + 25*xkm1[0]/(1+xkm1[0]*xkm1[0]) + 8*cos(1.2*k) + un ;
}
//-------------------------
// Observation Equation
// vn: measurement noise
void observation(std::vector<double> &zk, const std::vector<double> &xk, void* data)
{
gsl_rng *r = (gsl_rng*)data;
double vn = gsl_ran_gaussian(r, obsv_noise_sd);
zk[0] = xk[0]*xk[0]/20.0 + vn;
}
//-------------------------------------------
// Likelihood: P(zk | xk)
// ----------------------------------------
double likelihood(const std::vector<double> &z, const std::vector<double> &zhat, void* data)
{
double prod = 1.0;
for(uint i = 0; i < Nz; ++i)
{
double e = z[i] - zhat[i];
prod = prod * gsl_ran_gaussian_pdf(e, obsv_noise_sd);
}
return prod;
}
//-----------------------------------------------------
int main()
{
gsl_rng *rg;
long seed = time(NULL)*getpid();
rg = gsl_rng_alloc(gsl_rng_rand48);
gsl_rng_set(rg,seed);
//Re-sample criterion
float resample_percentage = 0.5;
uint Nt = ceil(resample_percentage * Nsamples);
// Initial variance in the state estimate
double V = 2.0;
std::vector<double> x(1);
std::vector<double> z(1);
std::vector<double> xf(1);
// Initial value
x[0] = 0.1;
// Create a pointCloud
PF::pf pointCloud(Nsamples, Nx, Nz, SYSTEMATIC);
ofstream f1("data.txt");
for(k = 0; k < T; ++k)
{
if(k == 0)
{
pointCloud.initialize(k, x[0], sqrt(V));
observation(z, x, (void*)rg);
#ifdef FILEIO
std::vector<double> xp(Nx);
std::vector<double> zp(Nz);
std::vector<double> w(Nsamples);
cout << "Initial Distribution of states" << endl;
ofstream f1("init_x.txt");
for(uint i = 0; i < Nsamples; ++i)
{
w[i] = pointCloud.getParticleState(xp, zp, i);
f1 << xp[0] << endl;
}
f1.close();
cout << "Use following command to see plot:" << endl;
cout << "octave plot1.m" << endl;
getchar();
#endif
}
else // k > 0
{
//Actual values
process(x, x, (void*)rg); // p(xk | xkm1)
observation(z, x, (void*)rg); // p(yk | xk)
// Note that for us, only measurement is available
// Estimate the states using particle Filter
pointCloud.particleFilterUpdate(process, observation,likelihood,z, 0); // Don't resample here
#ifdef FILEIO
std::vector<double> xp(Nx);
std::vector<double> zp(Nz);
std::vector<double> w(Nsamples);
cout << "Distribution before sampling" << endl;
ofstream f2("before_sampling.txt");
f2 << x[0] << "\t" << z[0] << "\t" << k << endl << endl;
for(uint i = 0; i < Nsamples; ++i)
{
w[i] = pointCloud.getParticleState(xp, zp, i);
f2 << xp[0] << "\t" << zp[0] << "\t" << w[i] << endl;
}
f2.close();
cout << "Use following command to see plot:" << endl;
cout << "octave plot2.m" << endl;
getchar();
#endif
float neff = pointCloud.getEffectivePopulation();
cout << "k = " << pointCloud.getItnNum() << "\t Neff = " << neff << "\t Nt = " << Nt << "\t";
pointCloud.filterOutput(xf);
if(ceil(neff) < Nt)
{
cout << "Resampling ..." ;
pointCloud.resample();
#ifdef FILEIO
std::vector<double> xp(Nx);
std::vector<double> zp(Nz);
std::vector<double> w(Nsamples);
ofstream f3("after_sampling.txt");
f3 << xf[0] << "\t" << k << endl ;
for(uint i = 0; i < Nsamples; ++i)
{
w[i] = pointCloud.getParticleState(xp,zp, i);
f3 << xp[0] << "\t" << w[i] << endl;
}
cout << "Use following command to see plot:" << endl;
cout << "octave plot3.m" << endl;
f3.close();
getchar();
#endif
}
cout << endl;
f1 << k << "\t" << x[0] << "\t" << z[0] << "\t" << xf[0] << endl;
}
}
f1.close();
gsl_rng_free(rg);
return 0;
}