-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMarkovLocalization2D.cpp
More file actions
110 lines (88 loc) · 2.46 KB
/
MarkovLocalization2D.cpp
File metadata and controls
110 lines (88 loc) · 2.46 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
#include<iostream>
#include<Eigen/Dense>
using namespace std;
double sensor_right;
double sensor_wrong;
double p_move;
double p_stay;
int mod(int n, int l){
if(n >= 0) return n % l;
else return l + (n % l);
}
Eigen::MatrixXd sense(Eigen::MatrixXd &p, char Z, Eigen::MatrixXi &world){
Eigen::MatrixXd q(p.rows(), p.cols());
double sum = 0.0;
for(int i = 0; i < p.rows(); i++){
for(int j = 0; j < p.cols(); j++){
double hit = (Z == world(i,j));
q(i,j) = p(i,j) * (hit*sensor_right + (1-hit)*sensor_wrong);
sum += q(i,j);
}
}
for(int i = 0; i < q.rows(); i++){
for(int j = 0; j < q.cols(); j++){
q(i,j) = q(i,j)/sum;
}
}
return q;
}
Eigen::MatrixXd move(Eigen::MatrixXd &p, Eigen::MatrixXi &U, int t){
Eigen::MatrixXd q(p.rows(), p.cols());
for(int i = 0; i < p.rows(); i++){
for(int j = 0; j < p.cols(); j++){
q(i,j) = (p_move*p(mod(i-U(0,t), p.rows()), mod(j-U(1,t),p.cols()))) + (p_stay*p(i,j));
}
}
return q;
}
void localize(Eigen::MatrixXi &world, Eigen::VectorXi &measurements, Eigen::MatrixXi &motions, Eigen::MatrixXd &p){
for(int i = 0; i < measurements.rows(); i++){
p = move(p, motions, i);
p = sense(p, measurements(i), world);
}
}
void Run2D(){
//--configurations
pHit = 0.6;
pMiss = 0.2;
pExact = 0.8;
pOvershoot = 0.1;
pUndershoot = 0.1;
//--probability sensor is right and move probability
sensor_right = 0.7;
p_move = 0.8;
sensor_wrong = 1.0 - sensor_right;
p_stay = 1.0 - p_move;
//--map
Eigen::MatrixXi world(4,5);
world << 'R', 'G', 'G', 'R', 'R',
'R', 'R', 'G', 'R', 'R',
'R', 'R', 'G', 'G', 'R',
'R', 'R', 'R', 'R', 'R';
//-- measurements
Eigen::VectorXi measurements(5);
measurements << 'G', 'G', 'G', 'G', 'G';
//--motions
// [ 0, 0] no move
// [ 0, 1] right
// [ 0,-1] left
// [ 1, 0] down
// [-1, 0] up
Eigen::MatrixXi motions(2, 5);
motions << 0, 0, 1, 1, 0,
0, 1, 0, 0, 1;
//--probabilities
Eigen::MatrixXd p(world.rows(), world.cols());
for(int i = 0; i < p.rows(); i++){
for(int j = 0; j < p.cols(); j++){
p(i,j) = (double)1.0/double(p.rows()*p.cols());
}
}
//--localization
localize(world, measurements, motions, p);
//--print
cout<<p<<endl;
}
int main(){
Run2D();
}