-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulationwindow.cpp
More file actions
454 lines (401 loc) · 17.1 KB
/
simulationwindow.cpp
File metadata and controls
454 lines (401 loc) · 17.1 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
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
#include "simulationwindow.h"
#include "ui_simulationwindow.h"
#include <iostream>
#include "fox.h"
#include "rabbit.h"
#include "plant.h"
#include <QTimer>
#include <QDebug>
SimulationWindow::SimulationWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::SimulationWindow)
{
ui->setupUi(this);
this->initilizeEnviroment();
ui->simulationView->updateEnviroment(&enviroment);
timer = new QTimer(this);
QObject::connect(timer,SIGNAL(timeout()),this,SLOT(updateEnviroment()));
connect(ui->StartButton_2,SIGNAL(clicked()),this,SLOT(on_StartButton_clicked()));
connect(ui->PauseButton_2,SIGNAL(clicked()),this,SLOT(on_PauseButton_clicked()));
connect(ui->ResetButton_2,SIGNAL(clicked()),this,SLOT(on_ResetButton_clicked()));
//Init Charts
foxPopulation = new QChart();
foxPopulationS = new QLineSeries(foxPopulation);
foxPopulation->legend()->hide();
foxPopulation->addSeries(foxPopulationS);
foxPopulation->setTitle("Fox Population over Time");
foxPopulation->createDefaultAxes();
foxPopulation->axisX()->setTitleText("Time");
foxPopulation->axisY()->setTitleText("Number of Foxes");
ui->FoxPopulation->setChart(foxPopulation);
foxSpeed = new QChart();
foxSpeedS = new QLineSeries(foxSpeed);
foxSpeed->legend()->hide();
foxSpeed->addSeries(foxSpeedS);
foxSpeed->setTitle("Fox Average Speed over Time");
foxSpeed->createDefaultAxes();
foxSpeed->axisX()->setTitleText("Time");
foxSpeed->axisY()->setTitleText("Average Speed");
ui->FoxSpeed->setChart(foxSpeed);
foxSenseRadius = new QChart();
foxSenseRadiusS = new QLineSeries(foxSenseRadius);
foxSenseRadius->legend()->hide();
foxSenseRadius->addSeries(foxSenseRadiusS);
foxSenseRadius->setTitle("Fox Average Sense Radius over Time");
foxSenseRadius->createDefaultAxes();
foxSenseRadius->axisX()->setTitleText("Time");
foxSenseRadius->axisY()->setTitleText("Average Sense Radius");
ui->FoxSenseRadius->setChart(foxSenseRadius);
foxEnergyEficiency = new QChart();
foxEnergyEficiencyS = new QLineSeries(foxEnergyEficiency);
foxEnergyEficiency->legend()->hide();
foxEnergyEficiency->addSeries(foxEnergyEficiencyS);
foxEnergyEficiency->setTitle("Fox Average Energy Efficiency over Time");
foxEnergyEficiency->createDefaultAxes();
foxEnergyEficiency->axisX()->setTitleText("Time");
foxEnergyEficiency->axisY()->setTitleText("Average Energy Efficiency");
ui->FoxEnergyEfficiency->setChart(foxEnergyEficiency);
rabbitPopulation = new QChart();
rabbitPopulationS = new QLineSeries(rabbitPopulation);
rabbitPopulation->legend()->hide();
rabbitPopulation->addSeries(rabbitPopulationS);
rabbitPopulation->setTitle("Rabbit Population over Time");
rabbitPopulation->createDefaultAxes();
rabbitPopulation->axisX()->setTitleText("Time");
rabbitPopulation->axisY()->setTitleText("Number of Rabbits");
ui->RabbitPopulation->setChart(rabbitPopulation);
rabbitSpeed = new QChart();
rabbitSpeedS = new QLineSeries(rabbitSpeed);
rabbitSpeed->legend()->hide();
rabbitSpeed->addSeries(rabbitSpeedS);
rabbitSpeed->setTitle("Rabbit Average Speed over Time");
rabbitSpeed->createDefaultAxes();
rabbitSpeed->axisX()->setTitleText("Time");
rabbitSpeed->axisY()->setTitleText("Average Speed");
ui->RabbitSpeed->setChart(rabbitSpeed);
rabbitSenseRadius = new QChart();
rabbitSenseRadiusS = new QLineSeries(rabbitSenseRadius);
rabbitSenseRadius->legend()->hide();
rabbitSenseRadius->addSeries(rabbitSenseRadiusS);
rabbitSenseRadius->setTitle("Rabbit Average Sense Radius over Time");
rabbitSenseRadius->createDefaultAxes();
rabbitSenseRadius->axisX()->setTitleText("Time");
rabbitSenseRadius->axisY()->setTitleText("Average Sense Radius");
ui->RabbitSenseRadius->setChart(rabbitSenseRadius);
rabbitEnergyEfficiency = new QChart();
rabbitEnergyEfficiencyS = new QLineSeries(rabbitEnergyEfficiency);
rabbitEnergyEfficiency->legend()->hide();
rabbitEnergyEfficiency->addSeries(rabbitEnergyEfficiencyS);
rabbitEnergyEfficiency->setTitle("Rabbit Average Energy Efficiency over Time");
rabbitEnergyEfficiency->createDefaultAxes();
rabbitEnergyEfficiency->axisX()->setTitleText("Time");
rabbitEnergyEfficiency->axisY()->setTitleText("Average Energy Efficiency");
ui->RabbitEnergyEfficiency->setChart(rabbitEnergyEfficiency);
totalPlants = new QChart();
totalPlantsS = new QLineSeries();
totalPlants->legend()->hide();
totalPlants->addSeries(totalPlantsS);
totalPlants->setTitle("Number of Plants over Time");
totalPlants->createDefaultAxes();
totalPlants->axisX()->setTitleText("Time");
totalPlants->axisY()->setTitleText("Number of Plants");
ui->TotalPlants->setChart(totalPlants);
newPlantsPerSteep = new QChart();
newPlantsPerSteepS = new QLineSeries();
newPlantsPerSteep->legend()->hide();
newPlantsPerSteep->addSeries(newPlantsPerSteepS);
newPlantsPerSteep->setTitle("Number of new Plants per Steep over Time");
newPlantsPerSteep->createDefaultAxes();
newPlantsPerSteep->axisX()->setTitleText("Time");
newPlantsPerSteep->axisY()->setTitleText("Number of new Plants");
ui->NewPlantsPerSteep->setChart(newPlantsPerSteep);
for(uint i = 0 ; i<threadCount;i++){
threadPoolFinished.push_back(false);
startExec.push_back(false);
}
//initThreadPool();
threadFun = [&](const uint id){
while(!stopExec){
while(!startExec[id] && !stopExec){}
for(const auto entity : enviroment.Entitys){
entity->accessUpdated.lock();
if(!entity->updated){
entity->updated = true;
entity->accessUpdated.unlock();
entity->update();
}
else{
entity->accessUpdated.unlock();
}
}
std::lock_guard<std::mutex> l(accessBoolVecs);
threadPoolFinished[id] = true;
startExec[id] = false;
}
};
}
SimulationWindow::~SimulationWindow()
{
delete foxPopulation;
delete foxSpeed;
delete foxSenseRadius;
delete foxEnergyEficiency;
delete foxPopulationS;
delete foxSpeedS;
delete foxSenseRadiusS;
delete foxEnergyEficiencyS;
delete rabbitPopulation;
delete rabbitSpeed;
delete rabbitSenseRadius;
delete rabbitEnergyEfficiency;
delete rabbitPopulationS;
delete rabbitSpeedS;
delete rabbitSenseRadiusS;
delete rabbitEnergyEfficiencyS;
delete totalPlants;
delete totalPlantsS;
delete timer;
delete ui;
stopExec = true;
for(std::thread &t : threadPool){
t.join();
}
}
void SimulationWindow::initilizeEnviroment(){
//Start Gens
//Amount of Start Fox, Rabbits, Plants
//Mutate PRobability
enviroment.Entitys.clear();
enviroment.minCoordinates = Vector2(0,0);
enviroment.maxCoordinates = Vector2(ui->MaxX->value(),ui->MaxY->value());
int plants = ui->NumberPlants->value();
enviroment.newPlants(plants);
int foxes = ui->NumberFoxes->value();
float foxMutate = ui->FoxGensMutateProbability->value();
float rabbitMutate = ui->RabbitGensMutatePropability->value();
int rabbits = ui->NumberRabbits->value();
Gens foxGens;
foxGens.speed = ui->FoxGensSpeed->value();
foxGens.senseRadius = ui->FoxGensSenseRadius->value();
foxGens.energyEfficiency = ui->FoxGensEnergyEfficiency->value();
Gens rabbitGens;
rabbitGens.speed = ui->RabbitGensSpeed->value();
rabbitGens.senseRadius = ui->RabbitGensSenseRadius->value();
rabbitGens.energyEfficiency = ui->RabbitGensEnergyEfficiency->value();
for(int i = 0;i<foxes;i++){
Fox * fox = new Fox(Vector2::randomVec(enviroment.minCoordinates,
enviroment.maxCoordinates),&enviroment,foxMutate);
fox->gens = foxGens;
enviroment.Entitys.push_back(std::shared_ptr<Fox>(fox));
}
for(int i = 0;i<rabbits;i++){
Rabbit * rabbit = new Rabbit(Vector2::randomVec(enviroment.minCoordinates,
enviroment.maxCoordinates),&enviroment,rabbitMutate);
rabbit->gens = rabbitGens;
enviroment.Entitys.push_back(std::shared_ptr<Rabbit>(rabbit));
}
}
const std::map<EntityType,QString> typeNames = {
{EntityType::Rabbit, QString("Rabbit")},
{EntityType::Fox , QString("Fox")}
};
const std::map<Animal::Gender, QString> genderNames = {
{Animal::Gender::Male,QString("Male")},
{Animal::Gender::Female, QString("Female")},
{Animal::Gender::Whatever, QString("Whatever")}
};
const std::map<Animal::Activity,QString> activityNames = {
{Animal::Activity::Fleeing, QString("Fleeing")},
{Animal::Activity::Hunting,QString("Hunting")},
{Animal::Activity::SearchingForFood,QString("Searching for Food")},
{Animal::Activity::SearchingForMate,QString("Searching for Mate")}
};
const std::map<Animal::MostNeed,QString> mostNeedNames = {
{Animal::MostNeed::Foot,QString("Food")},
{Animal::MostNeed::Water, QString("Water")},
{Animal::MostNeed::Reproduce, QString("Reproduce")}
};
bool SimulationWindow::allThreadsFinished()const{
for(const bool b: threadPoolFinished){
if(!b){
return false;
}
}
return true;
}
void SimulationWindow::updateEnviroment(){
static char count = 10;
if(count == 10){
count = 0;
updateStatistics(enviroment.calcEnviromentStatistics(),timeStepCount);
}
count++;
timeStepCount++;
//if(enviroment.Entitys.size()<1000){
int spawn = (ui->PlantsDecreaseBase->value()-
(timeStepCount/ui->PlantsRateOfDecrease->value()));
enviroment.newPlants(spawn);
enviroment.newPlants(ui->PlantsSpawnedPerSteep->value());
//}
Enviroment newEnviroment;
for(const auto entity : enviroment.Entitys){
entity->setnextEnviroment(&newEnviroment);
}
for(auto entity : enviroment.Entitys){
entity->updated = false;
}
newEnviroment = enviroment;
for(uint i = 0 ; i<threadCount;i++){
threadPoolFinished[i] = false;
startExec[i] = true;
}
while (!allThreadsFinished()) {}
enviroment = newEnviroment;
std::shared_ptr<Animal> cAnimal = ui->simulationView->lastClickedAnimal;
ui->CAnimalType->setText(typeNames.at(cAnimal->eType));
ui->CAnimalSpeed->setText(QString("Speed: %1").arg(cAnimal->gens.speed));
ui->CAnimalSenseRadius->setText(QString("Sense Radius: %1").arg(cAnimal->gens.senseRadius));
ui->CAnimalEnergyEfficiency->setText(QString("Energy Efficiency: %1").arg(cAnimal->gens.energyEfficiency));
ui->CAnimalMutatePropability->setText(QString("Mutate Propability: %1").arg(cAnimal->getMutateProbability()));
ui->CAnimalID->setText(QString("ID: %1").arg(cAnimal->id));
ui->CAnimalGender->setText(QString("Gender: %1").arg(genderNames.at(cAnimal->sex)));
ui->CAnimalHunger->setText(QString("Hunger: %1").arg(cAnimal->getHunger()));
ui->CAnimalPosition->setText(QString("X: %1 Y: %2").arg(cAnimal->pos.x).arg(cAnimal->pos.y));
ui->CAnimalActivity->setText(QString("Activity: %1").arg(activityNames.at(cAnimal->activity)));
ui->CAnimalMostNeed->setText(QString("Most Need: %1").arg(mostNeedNames.at(cAnimal->mostNeed)));
ui->CAnimalUrgeToReproduce->setText(QString("Urge to Reproduce: %1").arg(cAnimal->getUrgeToReproduce()));
ui->simulationView->update();
//qDebug() << "Called ones";
}
#define SET_MAX_X(chart) chart->axisX()->setMax(timeStep)
#define SET_MAX_Y(chart,cmax,value) if(value > cmax){cmax = value; chart->axisY()->setMax(value+1);}
void SimulationWindow::updateStatistics(const EnviromentStatistics &stats,const unsigned int timeStep){
if(!std::isnan(stats.averageFoxGens.speed)){
averageFoxGens = stats.averageFoxGens;
foxPopulationS->append(timeStep,stats.typeCounts.at(EntityType::Fox));
SET_MAX_X(foxPopulation);
SET_MAX_Y(foxPopulation,foxPopulationMax,stats.typeCounts.at(EntityType::Fox));
foxSpeedS->append(timeStep,stats.averageFoxGens.speed);
SET_MAX_X(foxSpeed);
SET_MAX_Y(foxSpeed,foxSpeedMax,stats.averageFoxGens.speed);
foxSenseRadiusS->append(timeStep,stats.averageFoxGens.senseRadius);
SET_MAX_X(foxSenseRadius);
SET_MAX_Y(foxSenseRadius,foxSenseRadiusMax,stats.averageFoxGens.senseRadius);
foxEnergyEficiencyS->append(timeStep,stats.averageFoxGens.energyEfficiency);
SET_MAX_X(foxEnergyEficiency);
SET_MAX_Y(foxEnergyEficiency,foxEnergyEficiencyMax,stats.averageFoxGens.energyEfficiency);
}
if(!std::isnan(stats.averageRabbitGens.speed)){
averageRabbitGens = stats.averageRabbitGens;
rabbitPopulationS->append(timeStep,stats.typeCounts.at(EntityType::Rabbit));
SET_MAX_X(rabbitPopulation);
SET_MAX_Y(rabbitPopulation,rabbitPopulationMax,stats.typeCounts.at(EntityType::Rabbit));
rabbitSpeedS->append(timeStep,stats.averageRabbitGens.speed);
SET_MAX_X(rabbitSpeed);
SET_MAX_Y(rabbitSpeed,rabbitSpeedMax,stats.averageRabbitGens.speed);
rabbitSenseRadiusS->append(timeStep,stats.averageRabbitGens.senseRadius);
SET_MAX_X(rabbitSenseRadius);
SET_MAX_Y(rabbitSenseRadius,rabbitSenseRadiusMax,stats.averageRabbitGens.senseRadius);
rabbitEnergyEfficiencyS->append(timeStep,stats.averageRabbitGens.energyEfficiency);
SET_MAX_X(rabbitEnergyEfficiency);
SET_MAX_Y(rabbitEnergyEfficiency,rabbitEnergyEfficiencyMax,stats.averageRabbitGens.energyEfficiency);
}
totalPlantsS->append(timeStep,stats.typeCounts.at(EntityType::Plant));
SET_MAX_X(totalPlants);
SET_MAX_Y(totalPlants,totalPlantsMax,stats.typeCounts.at(EntityType::Plant));
int plantSpawnBase = ui->PlantsSpawnedPerSteep->value();
int spawnedPlants = (ui->PlantsDecreaseBase->value()-
(timeStep/ui->PlantsRateOfDecrease->value()))+plantSpawnBase;
if(spawnedPlants < plantSpawnBase){
spawnedPlants = plantSpawnBase;
}
newPlantsPerSteepS->append(timeStep,spawnedPlants);
SET_MAX_X(newPlantsPerSteep);
SET_MAX_Y(newPlantsPerSteep,newPlantsPerSteepMax,spawnedPlants);
}
void SimulationWindow::on_addFox_clicked()
{
std::shared_ptr<Fox> newFox(new Fox(
Vector2::randomVec(enviroment.minCoordinates,enviroment.maxCoordinates),
&enviroment,ui->mutatePropability->value()));
newFox->gens.speed = ui->Speed->value();
newFox->gens.senseRadius = ui->SenseRadius->value();
newFox->gens.energyEfficiency = ui->EnergyEfficeincy->value();
enviroment.Entitys.push_back(newFox);
}
void SimulationWindow::on_addRabbit_clicked()
{
std::shared_ptr<Rabbit> newRabbit(new Rabbit(
Vector2::randomVec(enviroment.minCoordinates,enviroment.maxCoordinates),
&enviroment,ui->mutatePropability->value()));
newRabbit->gens.speed = ui->Speed->value();
newRabbit->gens.senseRadius = ui->SenseRadius->value();
newRabbit->gens.energyEfficiency = ui->EnergyEfficeincy->value();
enviroment.Entitys.push_back(newRabbit);
}
void SimulationWindow::on_addAverageFox_clicked()
{
std::shared_ptr<Fox> newFox(new Fox(
Vector2::randomVec(enviroment.minCoordinates,enviroment.maxCoordinates),
&enviroment,ui->mutatePropability->value()));
newFox->gens = averageFoxGens;
enviroment.Entitys.push_back(newFox);
}
void SimulationWindow::on_addAverageRabbit_clicked()
{
std::shared_ptr<Rabbit> newRabbit(new Rabbit(
Vector2::randomVec(enviroment.minCoordinates,enviroment.maxCoordinates),
&enviroment,ui->mutatePropability->value()));
newRabbit->gens = averageRabbitGens;
enviroment.Entitys.push_back(newRabbit);
}
void SimulationWindow::on_StartButton_clicked()
{
stopExec = false;
if(!isPause){
initilizeEnviroment();
}
for(uint i = 0; i <threadCount;i++){
threadPool.push_back(std::thread(threadFun,i));
}
timer->start(ui->SimulationSpeed->value());
}
void SimulationWindow::on_PauseButton_clicked()
{
isPause = true;
stopExec = true;
for(std::thread &t : threadPool){
t.join();
}
threadPool.clear();
timer->stop();
}
void SimulationWindow::on_ResetButton_clicked()
{
initilizeEnviroment();
foxPopulationS->clear();
foxSpeedS->clear();
foxSenseRadiusS->clear();
foxEnergyEficiencyS->clear();
foxPopulationMax = 0;
foxSpeedMax = 0;
foxSenseRadiusMax = 0;
foxEnergyEficiencyMax = 0;
rabbitPopulationS->clear();
rabbitSpeedS->clear();
rabbitSenseRadiusS->clear();
rabbitEnergyEfficiencyS->clear();
rabbitPopulationMax = 0;
rabbitSpeedMax = 0;
rabbitSenseRadiusMax = 0;
rabbitEnergyEfficiencyMax = 0;
totalPlantsS->clear();
newPlantsPerSteepS->clear();
totalPlantsMax = 0;
newPlantsPerSteepMax = 0;
timeStepCount = 0;
}
void SimulationWindow::on_SimulationSpeed_valueChanged(int value)
{
timer->setInterval(value);
}