-
Notifications
You must be signed in to change notification settings - Fork 2
/
localizer.cpp
172 lines (131 loc) · 4.52 KB
/
localizer.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
/**
localizer.cpp
Purpose: implements a 2-dimensional histogram filter
for a robot living on a colored cyclical grid by
correctly implementing the "initialize_beliefs",
"sense", and "move" functions.
This file is incomplete! Your job is to make these
functions work. Feel free to look at localizer.py
for working implementations which are written in python.
*/
#include "helpers.cpp"
#include <stdlib.h>
#include "debugging_helpers.cpp"
using namespace std;
/**
TODO - implement this function
Initializes a grid of beliefs to a uniform distribution.
@param grid - a two dimensional grid map (vector of vectors
of chars) representing the robot's world. For example:
g g g
g r g
g g g
would be a 3x3 world where every cell is green except
for the center, which is red.
@return - a normalized two dimensional grid of floats. For
a 2x2 grid, for example, this would be:
0.25 0.25
0.25 0.25
*/
fGrid_t initialize_beliefs(cGrid_t grid) {
int height = grid.size();
int width = grid[0].size();
int area = height * width;
float belief_per_cell = 1.0/area;
// vector initialization
// http://www.cplusplus.com/reference/vector/vector/vector/
fGrid_t newGrid(height, std::vector<float> (width, belief_per_cell));
return newGrid;
}
/**
TODO - implement this function
Implements robot sensing by updating beliefs based on the
color of a sensor measurement
@param color - the color the robot has sensed at its location
@param grid - the current map of the world, stored as a grid
(vector of vectors of chars) where each char represents a
color. For example:
g g g
g r g
g g g
@param beliefs - a two dimensional grid of floats representing
the robot's beliefs for each cell before sensing. For
example, a robot which has almost certainly localized
itself in a 2D world might have the following beliefs:
0.01 0.98
0.00 0.01
@param p_hit - the RELATIVE probability that any "sense" is
correct. The ratio of p_hit / p_miss indicates how many
times MORE likely it is to have a correct "sense" than
an incorrect one.
@param p_miss - the RELATIVE probability that any "sense" is
incorrect. The ratio of p_hit / p_miss indicates how many
times MORE likely it is to have a correct "sense" than
an incorrect one.
@return - a normalized two dimensional grid of floats
representing the updated beliefs for the robot.
*/
fGrid_t sense(char color,
cGrid_t grid,
fGrid_t beliefs,
float p_hit,
float p_miss)
{
int height = grid.size();
int width = grid[0].size();
fGrid_t newGrid = zeros(height, width);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
newGrid[i][j] = beliefs[i][j]; // prior belief
if (grid[i][j] == color) {
newGrid[i][j] *= p_hit;
}
else {
newGrid[i][j] *= p_miss;
}
}
}
return normalize(newGrid);
}
/**
TODO - implement this function
Implements robot motion by updating beliefs based on the
intended dx and dy of the robot.
For example, if a localized robot with the following beliefs
0.00 0.00 0.00
0.00 1.00 0.00
0.00 0.00 0.00
and dx and dy are both 1 and blurring is 0 (noiseless motion),
than after calling this function the returned beliefs would be
0.00 0.00 0.00
0.00 0.00 0.00
0.00 0.00 1.00
@param dy - the intended change in y position of the robot
@param dx - the intended change in x position of the robot
@param beliefs - a two dimensional grid of floats representing
the robot's beliefs for each cell before sensing. For
example, a robot which has almost certainly localized
itself in a 2D world might have the following beliefs:
0.01 0.98
0.00 0.01
@param blurring - A number representing how noisy robot motion
is. If blurring = 0.0 then motion is noiseless.
@return - a normalized two dimensional grid of floats
representing the updated beliefs for the robot.
*/
fGrid_t move(int dy, int dx,
fGrid_t beliefs,
float blurring)
{
int height = beliefs.size();
int width = beliefs[0].size();
fGrid_t newGrid = zeros(height, width);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int new_i = (i + dy + height) % height;
int new_j = (j + dx + width) % width;
newGrid[new_i][new_j] = beliefs[i][j];
}
}
return blur(newGrid, blurring);
}