-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulation.cpp
More file actions
123 lines (103 loc) · 3.06 KB
/
Simulation.cpp
File metadata and controls
123 lines (103 loc) · 3.06 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
/*---simulation.cpp-----------------------------------------------------------
* Thomas Matlak
*----------------------------------------------------------------------------*/
#include "Simulation.h"
#include <random>
#include <iostream>
#include <climits> // INT_MAX
#include <deque>
Simulation::Simulation(int setNumPlayers, int setNumNodes, int setMaxEdgeLength)
{
numPlayers = setNumPlayers;
simMap = new Graph(setNumNodes, setMaxEdgeLength);
std::random_device rd;
std::mt19937 eng(rd());
std::uniform_int_distribution<> distr(0, simMap->getNumNodes() - 1);
for (int k = 0; k < numPlayers; k++)
{
int playerStart;
int playerEnd;
do
{
playerStart = distr(eng);
playerEnd = distr(eng);
}
/* ensure a player's destination is possible and the player is not
* starting and ending at the same place */
while ((simMap->traversalDistance(playerStart, playerEnd) == INT_MAX)
|| (playerStart == playerEnd));
Player* player = new Player(playerStart, playerEnd);
players.push_back(player);
}
}
Simulation::~Simulation()
{
for (size_t i = 0; i < players.size(); i++)
{
delete players[i];
}
delete simMap;
}
float Simulation::runSim(WhichAlgorithm greedyOrOptimal)
{
int totalTravelTime = 0;
for (int i = 0; i < numPlayers; i++)
{
int start = players[i]->getStart();
int dest = players[i]->getDestination();
if (greedyOrOptimal == SELFISH)
{
simMap->traverseSelfish(start, dest);
totalTravelTime += simMap->traversalDistance(start, dest);
}
else if (greedyOrOptimal == OPTIMAL)
{
simMap->traverseOptimal(start, dest);
totalTravelTime += simMap->traversalDistance(start, dest);
}
}
return (float)totalTravelTime / (float)numPlayers;
}
void Simulation::displayMap()
{
simMap->printGraph();
}
void Simulation::displayPlayers()
{
/*// for debugging
std::cout << "PLAYERS:" << std::endl;
for (int i = 0; i < numPlayers; i++)
{
std::cout << i << ": ";
players[i]->printInfo();
}
*/
std::cout << "PLAYER TRAVEL DISTANCES:" << std::endl;
for (int j = 0; j < numPlayers; j++)
{
int start = players[j]->getStart();
int dest = players[j]->getDestination();
int distance = simMap->traversalDistance(start, dest);
std::cout << j << ": " << distance << std::endl;
/*// for debugging
if (distance == INT_MAX)
std::cout << j << ": " << "This player cannot go here" << std::endl;
else
{
std::cout << j << ": " << distance << "|";
std::deque<int> path = simMap.traverseSelfish(start, dest);
while (!path.empty())
{
std::cout << path.front();
path.pop_front();
if (!path.empty()) std::cout << "->";
}
std::cout << std::endl;
}
*/
}
}
void Simulation::resetGraph()
{
simMap->resetGraph();
}