-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulatedHouse.cpp
More file actions
123 lines (106 loc) · 4.13 KB
/
SimulatedHouse.cpp
File metadata and controls
123 lines (106 loc) · 4.13 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
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <algorithm>
#include "SimulatedHouse.h"
#include "Validator.h"
void SimulatedHouse::InitSimulatedHouse(map<string, AbstractAlgorithm*>& algorithms, map<string, int>& configurations)
{
if (house.isValidHouse() == false)
{
competition.status = HouseCompetitionStatus::Invalid;
return;
}
competition.winnerNumSteps = simulationSteps = 0;
competition.currentPositon = 1;
competition.status = HouseCompetitionStatus::Running;
for (auto algorithmPair : algorithms)
{
string algorithmName = algorithmPair.first;
AbstractAlgorithm* algorithm = algorithmPair.second;
activeRobots.push_back(new SimulatedRobot(house, algorithmName, *algorithm, configurations, house.getMaxSteps()));
activeRobots.back()->InitSimulation();
validRobots.push_back(algorithmName);
}
}
void SimulatedHouse::StepAllAlgorithms()
{
for (SimulatedRobot* robot : activeRobots)
{
robot->RunStep();
}
simulationSteps++;
UpdateCompetitionStatus();
}
void SimulatedHouse::UpdateCompetitionStatus()
{
bool isWinningRound = false;
for (SimulatedRobot* robot : activeRobots)
{
//algorithm simulation finished
if (robot->getStatus() != SimulationStatus::Running) inactiveRobots.push_back(robot);
//robot cleaned house successfully
if (robot->getStatus() == SimulationStatus::Done)
{
isWinningRound = true;
if (competition.algorithmsPositions.empty())
{
//first robot to finish - winner!
competition.winnerNumSteps = simulationSteps;
//alert all other algorithms
for (SimulatedRobot* algorithm : activeRobots)
{
if (algorithm->getStatus() == SimulationStatus::Running)
algorithm->WeHaveAWinner();
}
}
//set algorithm's position in competition
competition.algorithmsPositions[robot] = competition.currentPositon;
}
}
activeRobots.remove_if(NotRunning);
if (isWinningRound) competition.currentPositon++;
competition.maxPosition = competition.currentPositon;
if (activeRobots.empty())
{ //if winnerNumSteps = 0 => all algorithms failed
competition.winnerNumSteps = competition.winnerNumSteps == 0 ? simulationSteps : competition.winnerNumSteps;
competition.status = HouseCompetitionStatus::Finished;
}
}
//depricated - used for debug
void SimulatedHouse::PrintHouseCompetitionScore(calc_t* scoreFunction)
{
cout << "House filename: " << Validator::getShortFilename(house.getFilenameNoSuffix()) << endl;
cout << "House simulation Steps: " << simulationSteps << endl;
for (SimulatedRobot* simulation : inactiveRobots)
{
string docking = simulation->IsInDocking() ? "Yes" : "No";
int robotSteps = simulation->getStatus() == SimulationStatus::Dead ? simulationSteps : simulation->getNumSteps();
cout << "Algorithm Name " << simulation->getAlgorithmName() << ":" << endl;
cout << "\tHas Finished Successfully: " << HasFinishedSuccessfully(simulation) << endl;
cout << "\tHas Returned To Docking Station: " << docking << endl;
cout << "\tPosition In Competition: " << getAlgorithmPositionInCompetition(simulation) << endl;
cout << "\tDirt Cleaned: " << simulation->getDirtCleaned() << endl;
cout << "\tDirt Left: " << simulation->getDirtLeft() << endl;
cout << "\tNum Robot Steps: " << robotSteps << endl;
cout << "\tTotal Score :" << ComputeAlgorithmScore(simulation->getAlgorithmName(), scoreFunction) << endl << endl;
}
}
int SimulatedHouse::ComputeAlgorithmScore(string algorithmName, calc_t*& scoreFunc)
{
map<string, int> score_params;
SimulatedRobot* robot = inactiveRobots.front();
for (SimulatedRobot* algorithm : inactiveRobots)
{
if (algorithm->getAlgorithmName().compare(algorithmName) == 0)
{
robot = algorithm;
break;
}
}
if (robot->getStatus() == SimulationStatus::Crashed) return 0;
score_params["actual_position_in_competition"] = getActualAlgorithmPositionInCompetition(robot);
score_params["winner_num_steps"] = competition.winnerNumSteps;
score_params["this_num_steps"] = robot->getStatus() == SimulationStatus::Dead ? simulationSteps : robot->getNumSteps();
score_params["sum_dirt_in_house"] = house.getInitalDirt();
score_params["dirt_collected"] = robot->getDirtCleaned();
score_params["is_back_in_docking"] = robot->IsInDocking() ? 1 : 0;
return scoreFunc(score_params);
}