-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulatedRobot.cpp
More file actions
102 lines (85 loc) · 2.69 KB
/
SimulatedRobot.cpp
File metadata and controls
102 lines (85 loc) · 2.69 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
#include <algorithm>
#include "SimulatedRobot.h"
SimulatedRobot::SimulatedRobot(House& house, string algorithmName, AbstractAlgorithm& algorithm, const map<string, int> configurations, int maxSteps) {
this->algorithmName = algorithmName;
this->house = house;
this->algorithm = &algorithm;
this->config = configurations;
this->maxSteps = maxSteps;
battery = Battery(config["BatteryCapacity"], config["BatteryConsumptionRate"], config["BatteryRechargeRate"]);
}
void SimulatedRobot::InitSimulation() {
pos = house.getDockPosition();
stepsDid = dirtCleaned = 0;
stepsLeft = maxSteps;
algorithm->setSensor(sensor);
algorithm->setConfiguration(config);
prevStep = Direction::Stay;
status = SimulationStatus::Running;
}
void SimulatedRobot::RunStep() {
UpdateSensor();
prevStep = algorithm->step(prevStep);
MoveRobot(prevStep);
UpdateSimulationStatus();
}
void SimulatedRobot::MoveRobot(Direction dir) {
pos.Move(dir);
battery.UpdateBatteryLife(IsInDocking());
stepsDid++;
stepsLeft--;
}
void SimulatedRobot::UpdateSimulationStatus() {
//simulation finished - crashed into a wall
if (IsWall(pos)) {
status = SimulationStatus::Crashed;
crashDetails << "Algorithm " << algorithmName << " when running on House " << house.getFilenameNoSuffix() << " went on a wall in step " << stepsDid << endl;
return;
}
//simulation finished - battery is dead
if (battery.IsBatteryDead()) {
status = SimulationStatus::Dead;
return;
}
//simulation finished - success
if (dirtCleaned == house.getInitalDirt() && IsInDocking())
{
status = SimulationStatus::Done;
return;
}
//simulation finished - out of steps
if (stepsLeft == 0)
{
status = SimulationStatus::Stopped;
// house.Print();
return;
}
//passed on dirty floor and cleaned it
if (house.isDirtyCell(house.getPositionValue(pos))) {
house.CleanDirt(pos);
dirtCleaned++;
}
//let the algorithm know it has few steps left
if (stepsLeft == config["MaxStepsAfterWinner"]) algorithm->aboutToFinish(stepsLeft);
//Finally, if -video is set, print house to file:
if (video) { house.PrintVideo(stepsDid, algorithmName, pos, prevStep); }
}
void SimulatedRobot::UpdateSensor()
{
bool isWall[4];
char posValue = house.getPositionValue(pos);
int dirtLevel = house.isDirtyCell(posValue) ? posValue - '0' : 0;
for (int i = 0; i < 4; i++)
{
int dir = i % 2 == 0 ? 1 : -1;
int row = i < 2 ? pos.getRow() : pos.getRow() + dir;
int col = i >= 2 ? pos.getCol() : pos.getCol() + dir;
isWall[i] = IsWall(Position(row, col));
}
sensor.UpdateSensor(dirtLevel, isWall);
}
void SimulatedRobot::WeHaveAWinner()
{
stepsLeft = min(stepsLeft, config["MaxStepsAfterWinner"]);
if (stepsLeft == config["MaxStepsAfterWinner"]) algorithm->aboutToFinish(stepsLeft);
}