-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator.cpp
More file actions
89 lines (75 loc) · 2.77 KB
/
Copy pathsimulator.cpp
File metadata and controls
89 lines (75 loc) · 2.77 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
#include <algorithm>
#include <atomic>
#include <iostream>
#include <map>
#include <string>
#include <thread>
#include <vector>
#include <utility>
#include "sim_globals.h"
#include "evtol.h"
#include "simulator.h"
Simulator::Simulator() {}
Simulator * Simulator::getInstance() {
if (!instance)
instance = new Simulator;
return instance;
}
Simulator * Simulator::instance = 0;
void Simulator::start() {
int random_number = 0;
srand(time(NULL));
// Spawn a thread for each vehicle
for (int i = 0; i < 20; i++) {
// Generates a random number from 1 to 5, where 1 indicates alpha, 2 indicates beta, etc. This number determines which company's eVTOL object will be created.
random_number = rand() % 5 + 1;
// Create threads for the eVTOL objects
switch(random_number) {
case 1: {
evtol_threads_.push_back(std::thread([]() {
eVTOL vehicle {"alpha", 120, 320, (int)(0.6 * 60), 1.6, 4, 0.25};
}));
sim_globals::vehicles_per_company["alpha"]++;
break;
}
case 2: {
evtol_threads_.push_back(std::thread([]() {
eVTOL vehicle {"beta", 100, 100, (int)(0.2 * 60), 1.5, 5, 0.10};
}));
sim_globals::vehicles_per_company["beta"]++;
break;
}
case 3: {
evtol_threads_.push_back(std::thread([]() {
eVTOL vehicle {"charlie", 160, 220, (int)(0.8 * 60), 2.2, 3, 0.05};
}));
sim_globals::vehicles_per_company["charlie"]++;
break;
}
case 4: {
evtol_threads_.push_back(std::thread([]() {
eVTOL vehicle {"delta", 90, 120, (int)(0.62 * 60), 0.8, 2, 0.22};
}));
sim_globals::vehicles_per_company["delta"]++;
break;
}
case 5: {
evtol_threads_.push_back(std::thread([]() {
eVTOL vehicle {"echo", 30, 150, (int)(0.3 * 60), 5.8, 2, 0.61};
}));
sim_globals::vehicles_per_company["echo"]++;
}
}
}
// Displays the number of vehicles generated for each company
std::cout << "\n=== Number of vehicles per company ===\n";
for (auto & vehicle : sim_globals::vehicles_per_company) {
std::cout << vehicle.first << " company has " << vehicle.second << " vehicles.\n";
}
std::thread t_timer(sim_globals::simTimer);
t_timer.join();
std::for_each(evtol_threads_.begin(), evtol_threads_.end(), [](std::thread &t) {
t.join();
});
sim_globals::presentData(); // display calculated averages and other data to the console
}