-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathParticles.h
More file actions
272 lines (229 loc) · 7.63 KB
/
Copy pathParticles.h
File metadata and controls
272 lines (229 loc) · 7.63 KB
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
#pragma once
#include "Robot.h"
#include "Ray.h"
#include <iostream>
#include <random>
#include <chrono>
#include <cstdlib>
class Particles {
private:
int N;
double Xmin, Xmax, Ymin, Ymax;
double * meanParticle;
int bestParticle;
double **particles;
Scene* scene;
Robot r1;
public:
Particles(int n, double Xmi, double Xma, double Ymi, double Yma, std::vector<std::shared_ptr<Ray>> &v, cv::Vec2f dir, Scene *s):N(n), Xmin(Xmi), Ymin(Ymi),
Xmax(Xma), Ymax(Yma){
for(auto rays:v)
r1.Rays.push_back(rays);
scene = s;
r1.dir = dir;
}
void initialize() {
meanParticle = new double[6];
std::srand(std::time(0));
particles = new double*[7];
for(int i = 0; i<7; i++)
particles[i] = new double[N];
for(int i=0;i<N;i++){
//std::srand(std::time(0));
particles[2][i]=0;
particles[3][i]=0;
particles[4][i]=0;
particles[5][i]=0;
particles[0][i]=Xmin + ((double)rand()/(RAND_MAX))*(Xmax-Xmin);
particles[1][i]=Ymin + (double)rand()/(RAND_MAX)*(Ymax-Ymin);
particles[6][i]=1.0/N;
}
}
void updateWeights(const double *realPing){
for(int i=0;i<N;i++){
r1.pos = cv::Vec2f(particles[0][i], particles[1][i]);
double* simulatedPing = scene->Trace(r1);
double sumProb=0;
int nScanWithHit=0;
for (int k=0;k<r1.Rays.size();k++){
if(simulatedPing[k]==0.0)
simulatedPing[k]+=0.0001; //?
double probK = 1/(0.5 * 1.772)*exp(-0.5*pow((simulatedPing[k]-realPing[k])/0.5,2)); //todo: replacement by constants
sumProb = sumProb + probK;
nScanWithHit++;
}
if(nScanWithHit != 0){
particles[6][i]=sumProb/ nScanWithHit;
}
else
particles[6][i]=0.3;
}
std::cout<<"update weight finished"<<std::endl;
}
void normalize(){
double sumWeights=0;
for(int i=0;i<N;i++){
sumWeights+=particles[6][i];
}
for(int i=0;i<N;i++){
double previousWeight=particles[6][i];
particles[6][i]=previousWeight/sumWeights;
}
}
void updateDir(cv::Vec2f v){
r1.dir = v;
}
void resample(){
std::srand(std::time(0));
std::cout<<"starting resampling..."<<std::endl;
//if(Const.SLEEP_IN_STEPS) Thread.sleep(3000);
double** newGeneration;
newGeneration = particles;
double* cumprob= new double[N];
cumprob[0]=particles[6][0];
for(int i=1;i<N;i++){
cumprob[i]=cumprob[i-1]+particles[6][i];
}
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator(seed);
std::normal_distribution<double> distribution(0.0,0.5);
for(int k=0;k<N;k++){
double q = (double)rand()/(RAND_MAX);
int index = -1;
//std::cout
for(int i=0;i<N;i++){
if(cumprob[i]>q){
index=i;
break;
}
}
newGeneration[0][k]=(particles[0][index] + distribution(generator));
distribution.reset();
newGeneration[1][k]=(particles[1][index] + distribution(generator));
distribution.reset();
newGeneration[2][k]=(particles[2][index] + distribution(generator));
distribution.reset();
newGeneration[3][k]=(particles[3][index] + distribution(generator));
distribution.reset();
newGeneration[4][k]=(particles[4][index] + distribution(generator));
distribution.reset();
newGeneration[5][k]=(particles[5][index] + distribution(generator));
newGeneration[6][k]=(1.0/N);
}
particles=newGeneration;
}
void updateState(double dX,double dY,double dZ,double dRoll,double dPitch,double dYaw){
for(int i=0;i<N;i++){
//todo: add noise (?) check if already noisy
particles[0][i] += dX;
if(particles[0][i]<Xmin)
particles[0][i] = Xmax;
if(particles[0][i]>Xmax)
particles[0][i] = Xmin;
particles[1][i] += dY;
if(particles[1][i]<Ymin)
particles[1][i] = Ymax;
if(particles[1][i]>Ymax)
particles[1][i] = Ymin;
particles[2][i] += dZ;
particles[3][i] += dRoll;
particles[4][i] += dPitch;
particles[5][i] += dYaw;
}
}
void reinitializeMean(){
for(int i = 0; i<sizeof(meanParticle); i++)
meanParticle[i] = 0;
}
void printExpectedPosition(){
//int indexMax=-1;
//double weightMax=-1;
double sumWeight=0;
//reinitializeMean();
bestParticle = 0;
for(int i=0;i<N;i++){
//std::cout<<"Best Particle"<<particles[6][i]<<std::endl;
sumWeight+=particles[6][i];
if(particles[6][i]>particles[6][bestParticle]){
bestParticle=i;
}
// meanParticle[0] += particles[0][i]*particles[6][i];
// meanParticle[1] += particles[1][i]*particles[6][i];
// meanParticle[2] += particles[2][i]*particles[6][i];
// meanParticle[3] += particles[3][i]*particles[6][i];
// meanParticle[4] += particles[4][i]*particles[6][i];
// meanParticle[5] += particles[5][i]*particles[6][i];
}
// meanParticle[0] /= sumWeight;
// meanParticle[1] /= sumWeight;
// meanParticle[2] /= sumWeight;
// meanParticle[3] /= sumWeight;
// meanParticle[4] /= sumWeight;
// meanParticle[5] /= sumWeight;
//
std::cout<<"Best Particle (weight: "<<particles[6][bestParticle]<<"):"<<std::endl;
std::cout<<"X: "<<particles[0][bestParticle]<<std::endl;
std::cout<<"Y: "<<particles[1][bestParticle]<<std::endl;
std::cout<<"Index: "<<bestParticle<<std::endl;
// std::cout<<"------------------------------\nMean Particle:";
// std::cout<<"X: "<<meanParticle[0]<<std::endl;
// std::cout<<"Y: "<<meanParticle[1]<<std::endl;
}
double* getMeanParticle() {
return meanParticle;
}
/* public void setMeanParticle(Particle meanParticle) {
this.meanParticle = meanParticle;
}
*/
int getBestParticle(){
return bestParticle;
}
~Particles(){
for (int i = 0; i<7; i++)
delete [] particles[i];
delete [] particles;
delete [] meanParticle;
}
};
/*
public void setBestParticle(Particle bestParticle) {
this.bestParticle = bestParticle;
}
*/
/*
public void setEnvironment(){
// coefficient describing the test setup (U environment) - paramenters got with Matlab
double[][] lines = {{100,0,-50,0,50,0,-100,0},{0,-30,0,-40,0,-30,0,100},{0,3000,1500,2000,-3500,3000,10000,0},{0,0,30,30,70,70,100,0},{0,30,30,70,70,100,100,100},{0,100,50,50,50,100,0,0},{100,100,100,50,100,100,100,0}};
simulatedSonar.setLines(lines);
}
public static void setEnvironment(SynteticSonar simulatedSonar){
// coefficient describing the test setup (U environment) - paramenters got with Matlab
double[][] lines = {{100,0,-50,0,50,0,-100,0},{0,-30,0,-40,0,-30,0,100},{0,3000,1500,2000,-3500,3000,10000,0},{0,0,30,30,70,70,100,0},{0,30,30,70,70,100,100,100},{0,100,50,50,50,100,0,0},{100,100,100,50,100,100,100,0}};
simulatedSonar.setLines(lines);
}
public int getNParticles(){
return particles[0].length;
}
public int getDimState(){
return particles.length;
}
public double[][] getPositions(){
double[][] positions = new double[Const.N][2];
for(int i=0;i<particles[0].length;i++){
positions[i][0]=particles[0][i];
positions[i][1]=particles[1][i];
}
return positions;
}
@Override
protected Object clone(){
Particles clone = new Particles(new double[particles.length][particles[0].length]);
for(int i=0;i<particles.length;i++){
for(int j=0;j<particles[0].length;j++){
clone.particles[i][j]= particles[i][j]; //copy the same state of particles
}
}
return clone;
}
*/