-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathlocalization.js
173 lines (154 loc) · 4.66 KB
/
localization.js
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
class MazeMap {
constructor() {
this.rows = 4;
this.cols = 16;
this.maze = [
[1,1,1,1,0,1,1,1,1,1,0,1,1,1,0,1],
[0,0,1,1,0,1,0,0,1,0,1,0,1,0,0,0],
[0,1,1,1,0,1,0,0,1,1,1,1,1,0,0,1],
[1,1,0,1,1,1,0,1,1,1,1,0,1,1,1,1]
];
this.robot = [0,0];
}
getShape() {
return [this.rows,this.cols];
}
inBound(coords) {
if(coords[0] < 0 || coords[0] >= this.rows || coords[1] < 0 || coords[1] >= this.cols) {
return false;
}
return true;
}
isBlocked(coords) {
return (!this.inBound(coords) || this.maze[coords[0]][coords[1]] == 0);
}
getPercept(coords) {
let walls = {
'N' : false,
'S' : false,
'E' : false,
'W' : false
};
let up = [coords[0]-1,coords[1]],
down = [coords[0]+1,coords[1]],
left = [coords[0],coords[1]-1],
right = [coords[0],coords[1]+1];
if(this.isBlocked(up)) {
walls['N'] = true;
}
if(this.isBlocked(down)) {
walls['S'] = true;
}
if(this.isBlocked(left)) {
walls['W'] = true;
}
if(this.isBlocked(right)) {
walls['E'] = true;
}
return walls;
}
stringifyPercept(percept) {
if(typeof percept == "string"){
return percept
}
let str = '';
if(percept.N) str += 'N';
if(percept.S) str += 'S';
if(percept.W) str += 'W';
if(percept.E) str += 'E';
return str;
}
getCellsFromPercept(percept) {
let perceptText = this.stringifyPercept(percept);
let cells = [];
for(let i = 0; i < this.rows; i++) {
for(let j = 0; j < this.cols; j++) {
if(this.maze[i][j] && perceptText == this.stringifyPercept(this.getPercept([i,j]))) {
cells.push([i,j])
}
}
}
return cells;
}
getRandomLocation() {
let found = false;
while(!found) {
let i = Math.floor(Math.random()*(this.rows));
let j = Math.floor(Math.random()*(this.cols));
if(!this.isBlocked([i,j])) {
return [i,j];
}
}
}
//Receives a list of robot positions and a percept.
//Returns an obj containing list of robots that do not match the percept
//and a list of locations that match the percept but do not have a robot
getWrongRobots(robots,percept) {
let correctPositions = this.getCellsFromPercept(percept);
let obj = {
wrongRobotsIndices : Array.from(Array(robots.length).keys()),
unoccupiedPositions: correctPositions.slice()
};
obj.wrongRobotsIndices = obj.wrongRobotsIndices.filter((index) => {
let pos = robots[index].currLocation;
for(let i = 0; i < correctPositions.length; i++) {
let correctPosition = correctPositions[i];
if(correctPosition[0] == pos[0] && correctPosition[1] == pos[1]) {
return false;
}
}
return true;
})
obj.unoccupiedPositions = obj.unoccupiedPositions.filter((x) => {
for(let i = 0; i < robots.length; i++) {
let pos = robots[i].currLocation;
if(x[0] == pos[0] && x[1] == pos[1]) {
return false;
}
}
return true;
})
return obj;
}
}
class MazeBot {
constructor(start,map) {
this.currLocation = start;
this.map = map;
this.percept = this.map.getPercept(this.currLocation);
}
move(direction) {
switch(direction) {
case 'left' : let left = [this.currLocation[0],this.currLocation[1]-1];
if(this.map.isBlocked(left)) {
return false;
}
this.currLocation = left;
this.percept = this.map.getPercept(this.currLocation);
return true;
case 'right' : let right = [this.currLocation[0],this.currLocation[1]+1];
if(this.map.isBlocked(right)) {
return false;
}
this.currLocation = right;
this.percept = this.map.getPercept(this.currLocation);
return true;
case 'up' : let up = [this.currLocation[0]-1,this.currLocation[1]];
if(this.map.isBlocked(up)) {
return false;
}
this.currLocation = up;
this.percept = this.map.getPercept(this.currLocation);
return true;
case 'down' : let down = [this.currLocation[0]+1,this.currLocation[1]];
if(this.map.isBlocked(down)) {
return false;
}
this.currLocation = down;
this.percept = this.map.getPercept(this.currLocation);
return true;
}
}
}
let map = new MazeMap();
let bot = new MazeBot([0,0],map);