-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mouse.hpp
552 lines (496 loc) · 20.5 KB
/
Mouse.hpp
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*******************************************************************************
Jose Jorge Jimenez-Olivas
Brandon Cramer
Chris Robles
Srinivas Venkatraman
University of California, San Diego
IEEE Micromouse Team 2019
File Name: Mouse.h
Description: Abstract Mouse interface with dependent hardware functions.
*******************************************************************************/
#ifndef MOUSE_HPP
#define MOUSE_HPP
/* stm32 architecture or non arduino */
#if defined( ARDUINO_ARCH_STM32 ) || !defined( ARDUINO )
#include <iostream>
#include <utility>
#include <vector>
#include <deque>
#include <climits>
#include <Maze.h>
#include "utility/Orientation.hpp"
#else
#error "board not supported."
#endif
class Mouse {
public:
Maze & maze;
int row = 0;
int column = 0;
Orientation orientation = NORTH;
std::vector<std::vector<bool>> visited;
/*****************************************************************************
% Constructor: Abstract Mouse with starting location - row, column
% File: Mouse.hpp
% Parameters: maze - maze data structure.
% row - starting row of mouse.
% column - starting column of mouse.
*****************************************************************************/
Mouse( Maze & maze, int row, int column ) : maze( maze ),
visited( maze.height, std::vector<bool>(maze.width, false) ) {
if( maze.outOfBounds(row, column) ) {
#ifndef ARDUINO
std::cerr << "Mouse position: (" << row << ", " << column << ")"
<< "out of bounds." << std::endl;
#endif
row = column = 0;
}
if( maze.height != maze.width ) {
#ifndef ARDUINO
std::cerr << "unsupported: non-square dimension of maze: height x width"
<< ": " << maze.height << " x " << maze.width << std::endl;
#endif
}
this->row = row;
this->column = column;
origin = std::make_pair( row, column );
start_position = std::make_pair( row, column );
start();
}
/*****************************************************************************
% Routine Name: markNeighborWalls
% File: Mouse.hpp
% Parameters: cell - current cell.
% orientation - current orientation of mouse.
% Description: Adds walls to the maze given if the mouse detects walls on it
% surroundings.
% Return: Nothing.
*****************************************************************************/
virtual void markNeighborWalls( MazeCell * cell, Orientation orientation) = 0;
/*****************************************************************************
% Routine Name: rotateTo
% File: Mouse.hpp
% Parameters: cell - adjacent cell that mouse should be pointing forward to.
% Description: Rotates the mouse to have its front face point towards cell.
% Return: Nothing.
*****************************************************************************/
virtual void rotateTo( MazeCell * cell ) = 0;
/*****************************************************************************
% Routine Name: moveTo
% File: Mouse.hpp
% Parameters: cell - adjacent cell to move to.
% Description: Moves the mouse to an adjacent cell.
% Return: Nothing.
*****************************************************************************/
virtual void moveTo( MazeCell * cell ) = 0;
/*****************************************************************************
% Routine Name: exploreNextCell
% File: Mouse.hpp
% Parameters: None.
% Description: Flood Fill Algorithm iteration.
% Return: true if mouse is in progress to get to target; false if mouse
% is at target.
*****************************************************************************/
virtual bool exploreNextCell() final {
if( explore_stack.empty() ) {
/* mouse is at target. */
done = true;
trackSteps();
if( mousePath.size() == previousPath.size() && isCompletePath(mousePath)){
/* An optimal path was discovered */
return false;
}
/* otherwise continue traversing maze */
done = false;
retreat();
previousPath = mousePath;
return false;
}
MazeCell * cell = explore_stack.back();
explore_stack.pop_back();
/* sensor surroundings */
rotateTo( cell );
moveTo( cell );
setVisited( cell, true );
markNeighborWalls( cell, orientation );
/* notify other cells of new walls */
callibrateDistances( cell );
for( MazeCell * openNeighbor : cell->getNeighborList() ) {
/* choose best adjacent open cell */
if( openNeighbor->distance == cell->distance - 1 ) {
/* hueristic to move closer to the target */
explore_stack.push_back( openNeighbor );
if( openNeighbor->distance == 0 ) {
explore_stack.push_back( openNeighbor );
}
return true;
}
else if( openNeighbor->distance == 0 && !hasVisited( openNeighbor ) ) {
/* visit all target nodes in quad-cell solution */
explore_stack.push_back( openNeighbor );
return true;
}
}
return true;
}
/*****************************************************************************
% Routine Name: start
% File: Mouse.hpp
% Parameters: None.
% Description: Clears the mouse memory then restarts the mouse.
% Return: Nothing.
*****************************************************************************/
virtual void start() final {
clearMemory();
restart();
}
/*****************************************************************************
% Routine Name: restart
% File: Mouse.hpp
% Parameters: None.
% Description: Resets the mouse's position the origin position that was
% passed to the constructor.
% Return: Nothing.
*****************************************************************************/
virtual void restart() final {
/* restart starting position */
start_position = std::make_pair( origin.first, origin.second );
explore_stack.push_back( maze.at(origin.first, origin.second) );
setMousePosition( origin );
orientation = NORTH;
}
/*****************************************************************************
% Routine Name: updateMazeCellDistances
% File: Mouse.hpp
% Parameters: target - cell that will have a distance of 0.
% Description: Update distance values for each cell in the maze given the target.
% Return: Nothing.
*****************************************************************************/
virtual void updateMazeCellDistances( MazeCell * target ) final {
if( target == nullptr ) return;
std::deque<MazeCell *> q;
for( MazeCell * cell : maze ) {
/* reset visited values of all cells in the maze */
cell->setVisited( false );
}
q.push_back( target );
target->setVisited( true );
target->distance = 0;
while( !q.empty() ) {
/* BFS traversal */
MazeCell * cell = q.front();
q.pop_front();
for( MazeCell * openNeighbor : cell->getNeighborList() ) {
/* update distance only to open neighbor of cell */
if( openNeighbor->visited ) continue;
q.push_back( openNeighbor );
openNeighbor->setVisited( true );
openNeighbor->distance = cell->distance + 1;
}
}
}
/*****************************************************************************
% Routine Name: getMousePath
% File: Mouse.hpp
% Parameters: None.
% Description: Getter for the most optimal path the mouse has found so far.
% Return: List of cells.
*****************************************************************************/
virtual std::vector<MazeCell *> getMousePath() final {
return mousePath;
}
/*****************************************************************************
% Routine Name: getNumberOfRuns
% File: Mouse.hpp
% Parameters: None.
% Description: Getter for the number of times the mouse reached the target.
% Return: The number of runs made thus far.
*****************************************************************************/
virtual int getNumberOfRuns() final {
return ( isDone() ) ? num_of_runs + 1 : num_of_runs;
}
/*****************************************************************************
% Routine Name: isDone
% File: Mouse.hpp
% Parameters: Nothing.
% Description: Checks if the mouse has found the most optimal path to the
% target.
% Return: true if the mouse has found the optimal path to the target,
% false otherwise.
*****************************************************************************/
virtual bool isDone() final {
return done;
}
private:
std::vector<MazeCell *> explore_stack;
std::vector<MazeCell *> mousePath;
std::vector<MazeCell *> previousPath;
std::pair<int, int> start_position;
std::pair<int, int> origin;
int num_of_runs = 0;
bool done = false;
/*****************************************************************************
% Routine Name: callibrateDistances
% File: Mouse.hpp
% Parameters: cell - current cell.
% Description: Floods the current cell and its adjacent cell distance value
% towards the target; This fuction delegates to callibrate.
% Return: Nothing.
*****************************************************************************/
virtual void callibrateDistances( MazeCell * cell ) final {
if( cell == nullptr ) return;
callibrate( cell );
for( MazeCell * globalNeighbor : maze.getAdjacentCellList( cell ) ) {
if( globalNeighbor->distance == 0 ) continue;
callibrate( globalNeighbor );
}
}
/*****************************************************************************
% Routine Name: calibrate
% File: Mouse.hpp
% Parameters: cell - cell in need of distance validation.
% Description: Floods currenct cell such that there exist an "open" neighbor
% with a distance of cell->distance - 1.
% Return: Nothing.
*****************************************************************************/
virtual void callibrate( MazeCell * cell ) final {
if( cell == nullptr ) return;
int minDistance = INT_MAX;
for( MazeCell * openNeighbor : cell->getNeighborList() ) {
/* validate cell's need for callibration */
if( openNeighbor->distance == cell->distance - 1 ) return;
if( openNeighbor->distance < minDistance ) {
minDistance = openNeighbor->distance;
}
}
/* update non target cell to a higher elevation */
if( cell->distance != 0 ) cell->distance = minDistance + 1;
for( MazeCell * globalNeighbor : maze.getAdjacentCellList( cell ) ) {
/* callibrate all global neighbors except for the target cells */
if( globalNeighbor->distance == 0 ) continue;
callibrate( globalNeighbor );
}
}
/*****************************************************************************
% Routine Name: retreat
% File: Mouse.hpp
% Parameters: None.
% Description: Continue exploring maze by returning to the starting position.
% Return: Nothing.
*****************************************************************************/
virtual void retreat() final {
MazeCell * newTargetCell = maze.at( start_position );
start_position = std::make_pair( row, column );
/* updates all cell distances with respect to new taget cell */
updateMazeCellDistances( newTargetCell );
/* current location pushed and ready */
explore_stack.push_back( maze.at(row, column) );
num_of_runs++;
}
/*****************************************************************************
% Routine Name: trackSteps
% File: Mouse.hpp
% Parameters: Nothing.
% Description: Tracks the mouse's next maze traversal from starting point to
% target point.
% Return: Nothing.
*****************************************************************************/
virtual void trackSteps() final {
mousePath.clear();
updateMousePath( maze.at(start_position), maze.at(row, column) );
}
/*****************************************************************************
% Routine Name: updateMousePath
% File: Mouse.hpp
% Parameters: start - starting cell of path.
% end - terminating cell of path.
% Description: Appends path traversal to mousePath linked list.
% Return: Nothing.
*****************************************************************************/
virtual void updateMousePath( MazeCell * start, MazeCell * end ) final {
if( start == nullptr || end == nullptr ) return;
mousePath.push_back( start );
/* current node is at destination */
if( *start == *end ) return;
/* move to the next least expensive cell */
for( MazeCell * neighbor : start->getNeighborList() ) {
/* if mouse did not visit neighbor do not consider it */
if( this->hasVisited( neighbor ) == false ) continue;
/* otherwise append mouse path from neighbor to end */
if( neighbor->distance == start->distance - 1 ) {
updateMousePath( neighbor, end );
return;
}
}
}
/*****************************************************************************
% Routine Name: clearMemory
% File: Mouse.hpp
% Parameters: None.
% Description: Erases all memory about the maze configuration.
% Return: Nothing.
*****************************************************************************/
virtual void clearMemory() final {
/* erase all walls mouse created in maze */
maze.clearWalls();
/* erase memeory of expoloring maze */
explore_stack.clear();
mousePath.clear();
previousPath.clear();
num_of_runs = 0;
done = false;
for( MazeCell * cell : maze ) {
MazeCell * center = getClosestCenter( cell );
/* manhattan distance of cell to center */
int dr = abs(center->row - cell->row);
int dc = abs(center->column - cell->column);
cell->setDistance( dr + dc );
cell->setVisited( false );
setVisited( cell, false );
}
}
/*****************************************************************************
% Routine Name: moveTo
% File: Mouse.hpp
% Parameters: coordinate - row, column pair.
% Description: Moves mouse to the given coordinate.
% Return: Nothing.
*****************************************************************************/
virtual void moveTo( std::pair<int, int> & coordinate ) final {
moveTo( maze.at(coordinate) );
}
/*****************************************************************************
% Routine Name: moveTo
% File: Mouse.hpp
% Parameters: row - row to move to.
% column - column to move to.
% Description: Moves mouse to (row, column).
% Return: Nothing.
*****************************************************************************/
virtual void moveTo( int row, int column ) final {
moveTo( maze.at(row, column) );
}
/*****************************************************************************
% Routine Name: setVisited
% File: Mouse.hpp
% Parameters: cell - cell of interest.
% truthValue - visited value to set for cell.
% Description: Sets cell to the given truthValue.
% Return: Nothing.
*****************************************************************************/
virtual void setVisited( MazeCell * cell, bool truthValue ) final {
if( cell == nullptr ) return;
visited[ cell->row ][ cell->column ] = truthValue;
}
/*****************************************************************************
% Routine Name: setMousePosition
% File: Mouse.hpp
% Parameters: cell - cell to set the mouse to.
% Description: Updates the row and column of mouse to the given cell.
% Return: Nothing.
*****************************************************************************/
virtual void setMousePosition( MazeCell * cell ) final {
// also check range
if( cell == nullptr ) {
#ifndef ARDUINO
std::cerr << "Mouse.h:setMousePosition parameter invalid - defaulting "
<< "to (" << origin.first << ", " << origin.second << ")"
<< std::endl;
#endif
cell = maze.at( origin );
}
row = cell->row;
column = cell->column;
}
/*****************************************************************************
% Routine Name: setMousePosition
% File: Mouse.hpp
% Parameters: coordinate - (row, column) pair.
% Description: Updates the mouse position to the given coordinate.
% Return: Nothing.
*****************************************************************************/
virtual void setMousePosition( std::pair<int, int> & coordinate ) final {
setMousePosition( maze.at(coordinate) );
}
/*****************************************************************************
% Routine Name: setMousePosition
% File: Mouse.hpp
% Parameters: row - row in maze.
% column - column in maze.
% Description: Updates the mouse position to the given row, column.
% Return: Nothing.
*****************************************************************************/
virtual void setMousePosition( int row, int column ) final {
setMousePosition( maze.at(row, column) );
}
/*****************************************************************************
% Routine Name: hasVisited
% File: Mouse.hpp
% Parameters: cell - cell of interest.
% Description: Evaluates if the given cell has been visited by the mouse.
% Return: true if the mouse visited the cell, false otherwise.
*****************************************************************************/
virtual bool hasVisited( MazeCell * cell ) final {
if( cell == nullptr ) return false;
return visited[ cell->row ][ cell->column ];
}
/*****************************************************************************
% Routine Name: isCompletePath
% File: Mouse.hpp
% Parameters: path - path of interest.
% Description: Evaluates if the given path contains both the start and end
% cell.
% Return: True if the path is complete, false otherwise.
*****************************************************************************/
virtual bool isCompletePath( std::vector<MazeCell *> & path ) final {
if( path.size() == 0 ) {
/* invlaid argument */
#ifndef ARDUINO
std::cerr << "Mouse.h:isCompletePath parameter is invalid: path size is"
<< " 0" << std::endl;
#endif
return false;
}
MazeCell * mouse_cell = maze.at( row, column );
MazeCell * start_cell = maze.at( start_position );
bool path_contains_start = *path.front() == *start_cell
|| *path.back() == *start_cell;
bool path_contains_mouse = *path.front() == *mouse_cell
|| *path.back() == *mouse_cell;
if( path_contains_start && path_contains_mouse ) {
/* path contains both end points */
return true;
}
return false;
}
/*****************************************************************************
% Routine Name: getClosestCenter
% File: Mouse.hpp
% Parameters: cell - current cell.
% Description: Finds the center most cell with respect to the given cell.
% Return: The closest centered cell.
*****************************************************************************/
virtual MazeCell * getClosestCenter( MazeCell * cell ) final {
const int EVEN = 2;
if( maze.height != maze.width || cell == nullptr ) {
/* non-square maze environment - no center or cell defined */
return maze.at( 0, 0 );
}
const int maze_dimension = maze.height;
int center_row = maze_dimension / EVEN;
int center_column = maze_dimension / EVEN;
/* singular solution cell */
if( maze_dimension % EVEN == 1 ) {
return maze.at( center_row, center_column );
}
/* quad-cell solution */
if( cell->row < maze_dimension / EVEN ) {
center_row = maze_dimension / EVEN - 1;
}
if( cell->column < maze_dimension / EVEN ) {
center_column = maze_dimension / EVEN - 1;
}
return maze.at( center_row, center_column );
}
};
#endif /* MOUSE_HPP */