-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.cpp
331 lines (302 loc) · 9.65 KB
/
grid.cpp
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
#include <algorithm>
#include "grid.h"
Grid::Grid(double threshold, int seed) {
happinessThreshold = threshold;
allHappy = false;
rng.setseed(seed);
nMoves = 0;
totalSleep = 0;
// for timing purposes
gettimeofday(&t0, 0);
}
Grid::~Grid() {
gettimeofday(&t1, 0);
std::cout << "Computation time [in milliseconds]: " << timedifference_msec(t0, t1) - totalSleep << " plus a total waiting time of " << totalSleep << ".\n";
}
void Grid::initiate(unsigned int gridSize){
this->gridSize = gridSize;
std::cout << "Creating a grid of size " << gridSize << "x" << gridSize << "\n";
// 1. create the grid
GridEl* lastEl;
GridEl* el;
GridEl* upperEl = nullptr;
unsigned int row = 0, col = 0;
// first iteration
el = new GridEl(row, col, 0, nullptr, nullptr, nullptr, nullptr);
head = el;
lastEl = el;
++col;
while (col < gridSize) {
// iterate left to right and link vertically
el = new GridEl(row, col, 0, nullptr, nullptr, nullptr, nullptr);
el->W = lastEl;
lastEl->E = el;
lastEl = el;
++col;
}
upperEl = lastEl;
++row;
// loop the body
while(row < gridSize) {
col = 0;
if (row % 2 != 0) {
el = new GridEl(row, gridSize - col - 1, 0, nullptr, nullptr, nullptr, nullptr);
// even row (starts at 0...), iterate right to left
el->E = nullptr; // right is nothing
el->N = upperEl;
upperEl->S = el;
lastEl = el;
upperEl = upperEl->W; // move to the left
++col;
while (col < gridSize) {
el = new GridEl(row, gridSize - col - 1, 0, nullptr, nullptr, nullptr, nullptr);
el->E = lastEl;
lastEl->W = el;
el->N = upperEl;
upperEl->S = el;
upperEl = upperEl->W;
lastEl = el;
++col;
}
} else {
el = new GridEl(row, col, 0, nullptr, nullptr, nullptr, nullptr);
// uneven rows (starts at 0...), iterate left to right
el->W = nullptr; // left is nothing
el->N = upperEl;
upperEl->S = el;
lastEl = el;
upperEl = upperEl->E;
col++;
while (col < gridSize) {
el = new GridEl(row, col, 0, nullptr, nullptr, nullptr, nullptr);
el->W = lastEl;
lastEl->E = el;
el->N = upperEl;
upperEl->S = el;
upperEl = upperEl->E;
lastEl = el;
++col;
}
}
++row;
upperEl = el;
}
}
void Grid::countElements(){
unsigned int n = 0;
GridEl* el = head;
while (el != nullptr) {
el = next(el);
++n;
}
std::cout << "Total number of elements: " << n << "\n";
}
Grid::GridEl* Grid::next(Grid::GridEl* reference) {
unsigned int row = reference->row;
GridEl* returnReference;
// if the row is even, move left to right, else move right to left,
// if the next is empty (end), move down)
// if down is empty, return nullptr! (finish)
if (row % 2 != 0) {
// even row, right to left
if (reference->W != nullptr) { // not left = empty!
returnReference = reference->W;
} else { // right = empty
if (reference->S != nullptr) {
returnReference = reference->S;
} else {
returnReference = nullptr;
}
}
} else {
// uneven row, left to right
if (reference->E != nullptr) { // not right = empty!
returnReference = reference->E;
} else { // right = empty
if (reference->S != nullptr) {
returnReference = reference->S;
} else {
returnReference = nullptr;
}
}
}
return returnReference;
}
void Grid::populate(double percentEmpty, unsigned int nRaces) {
// gridSize
if (nRaces > 26) {
std::cout << "Too many races, currently maximal 26 allowed! I cropped them to 26\n";
nRaces = 26;
}
this->nRaces = nRaces;
GridEl* reference = head;
while (reference != nullptr) {
double rnd = rng.runif(0.0, 1.0);
if (rnd < percentEmpty) {
// then empty
reference->race = 0;
emptyPlaces.push_back(reference);
} else {
unsigned int race = (unsigned int) rng.runif(1, (int) nRaces);
reference->race = race;
}
reference = next(reference);
}
//std::reverse(emptyPlaces.begin(), emptyPlaces.end());
}
void Grid::drawPlot() {
GridEl* firstEl = head;
GridEl* reference = head;
std::string num;
while (firstEl != nullptr) {
while (reference != nullptr) {
num = raceChars[reference->race];
std::cout << num << "|";
reference = reference->E;
}
std::cout << "\n";
firstEl = firstEl->S;
reference = firstEl;
}
}
void Grid::updateHappiness() {
GridEl* reference = head;
allHappy = true;
while (reference != nullptr) {
reference->happy = findRatio(reference) >= happinessThreshold;
if (!reference->happy && reference->race != 0) {
allHappy = false;
}
reference->movable = true;
reference = next(reference);
}
}
double Grid::findRatio(Grid::GridEl* reference){
double totalNeighbors = 0;
double sameNeighbors = 0;
// North
if (reference->N != nullptr) {
if (reference->N->race != 0) {
++totalNeighbors;
if (reference->N->race == reference->race) {
++sameNeighbors;
}
}
// North East
if (reference->N->E != nullptr) {
if (reference->N->E->race != 0) {
++totalNeighbors;
if (reference->N->E->race == reference->race) {
++sameNeighbors;
}
}
}
// North West
if (reference->N->W != nullptr) {
if (reference->N->W->race != 0) {
++totalNeighbors;
if (reference->N->W->race == reference->race) {
++sameNeighbors;
}
}
}
}
// West
if (reference->W != nullptr) {
if (reference->W->race != 0) {
++totalNeighbors;
if (reference->W->race == reference->race) {
++sameNeighbors;
}
}
}
// East
if (reference->E != nullptr) {
if (reference->E->race != 0) {
++totalNeighbors;
if (reference->E->race == reference->race) {
++sameNeighbors;
}
}
}
// South
if (reference->S != nullptr) {
if (reference->S->race != 0) {
++totalNeighbors;
if (reference->S->race == reference->race) {
++sameNeighbors;
}
}
// South East
if (reference->S->E != nullptr) {
if (reference->S->E->race != 0) {
++totalNeighbors;
if (reference->S->E->race == reference->race) {
++sameNeighbors;
}
}
}
// South West
if (reference->S->W != nullptr) {
if (reference->S->W->race != 0) {
++totalNeighbors;
if (reference->S->W->race == reference->race) {
++sameNeighbors;
}
}
}
}
return sameNeighbors / totalNeighbors;
}
void Grid::move() {
moves = 0;
GridEl* reference = head;
int rndHouse;
int nEmpty = (int) emptyPlaces.size() - 1;
if (nEmpty > 0) {
while (reference != nullptr) {
if (!reference->happy && reference->movable && reference->race != 0) {
// move to random
reference->movable = false;
rndHouse = rng.runif(0, nEmpty);
//std::cout << "r: " << rndHouse << "\n";
emptyPlaces[rndHouse]->movable = false;
emptyPlaces[rndHouse]->race = reference->race;
reference->race = 0;
emptyPlaces[rndHouse] = reference;
//std::cout << "moving to house #" << rndHouse << " in the range of [0, " << (int) emptyPlaces.size() - 1 << "] !\n";
++moves;
}
reference = next(reference);
}
}
nMoves += moves;
}
void Grid::simulate(unsigned int nMax, int plotAfter, int sleep) {
unsigned int nSim = 0;
while (!allHappy && nSim <= nMax) {
if (nSim % plotAfter == 0) {
std::cout << "\033[2J\033[1;1H"; // clear the screen
std::cout << "---- Grid after " << nSim << " Iterations, with " << moves << " moves in this round and a total of " << nMoves << " moves. ---- \n";
}
updateHappiness();
move();
// clears the screen
if (nSim % plotAfter == 0) {
drawPlot();
totalSleep += (float) sleep/1000; // to milliseconds
usleep(sleep); // in microseconds
}
++n;
++nSim;
}
if (nSim > nMax) {
std::cout << "Aborted after " << nSim - 1 << " rounds!\n";
}
std::cout << "\033[2J\033[1;1H";
std::cout << "---- Grid after " << nSim << " Iterations, with " << moves << " moves in this round and a total of " << nMoves << " moves. ----\n";
drawPlot();
}
float Grid::timedifference_msec(struct timeval t0, struct timeval t1) {
return (t1.tv_sec - t0.tv_sec) * 1000.0f + (t1.tv_usec - t0.tv_usec) / 1000.0f;
}