-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
303 lines (265 loc) · 7.63 KB
/
main.cpp
File metadata and controls
303 lines (265 loc) · 7.63 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
#include <memory>
#include <vector>
#include <fstream>
#include <time.h>
#define OLC_PGE_APPLICATION
#include "olcPixelGameEngine.h"
#undef min
const char* NODEHEADER = "id,current pos x,y,start pos x,y,infectable,infected,infected for,max travel,maxSpeed";
const double PI = 3.141592;
double drand() {
return rand() / (double)RAND_MAX;
}
class Node;
bool infectTest (Node&, Node&);
std::ostream& operator<<(std::ostream& os, const Node& node);
struct WorldParams {
std::ostream& log;
int numNodes;
int minInfTime;
int maxInfTime;
double suvRate;
bool reInfect;
double minMaxTravel;
double maxMaxTravel;
olc::vd2d maxPos;
olc::vd2d minPos;
olc::vi2d res;
olc::vi2d pixSize;
double maxSpeed;
int randSeed;
bool origin;
bool showRange;
bool outputNodeData = true;
};
class World {
private:
std::vector<Node> nodes;
public:
int minInfTime;
int maxInfTime;
double suvRate;
double maxSpeed;
std::ostream& stream;
int nextSeed;
bool outputNodeData;
World(WorldParams wParams) : World(wParams.log, wParams.numNodes, wParams.minInfTime, wParams.maxInfTime, wParams.suvRate, wParams.reInfect,
wParams.minMaxTravel, wParams.maxMaxTravel, wParams.maxPos, wParams.minPos, wParams.maxSpeed, wParams.outputNodeData, wParams.randSeed) {}
World(std::ostream& log, int numNodes, int minInfTime, int maxInfTime, double suvRate, bool reInfect, double minMaxTravel,
double maxMaxTravel, olc::vd2d maxPos, olc::vd2d minPos, double maxSpeed, bool outputNodeData, int randSeed = 0);
World(World&) = delete;
void update();
std::vector<Node>& getNodes() {return nodes;}
};
class Node {
private:
public:
int id;
olc::vd2d cPos;
olc::vd2d sPos;
bool reinfectable;
bool infectable;
bool infected;
int infectedFor;
double maxTravel;
double maxSpeed;
Node(std::ostream& log, int id, olc::vf2d pos, double maxTravel, float maxSpeed, bool reinfect) :
id(id), cPos(pos), sPos(pos), reinfectable(reinfect), infectable(true), infected(false),
infectedFor(0), maxTravel(maxTravel), maxSpeed(maxSpeed) {
log << *this;
}
void move() {
double dir = drand() * PI * 2;
double dist = drand() * maxSpeed;
olc::vd2d posOff = olc::vd2d(sin(dir), cos(dir)) * dist;
double ndist = (posOff + cPos - sPos).mag();
cPos = sPos;
if (ndist != 0) {
cPos += (posOff + cPos - sPos).norm()*std::min(ndist, maxTravel);
}
}
void update(World& world) {
for (Node& node : world.getNodes()) {
if (node.id != id && !infected) {
infected = infectTest(*this, node);
}
}
}
friend std::ostream& operator<<(std::ostream& os, const Node& dt);
};
World::World(std::ostream& log, int numNodes, int minInfTime, int maxInfTime, double suvRate, bool reInfect, double minMaxTravel,
double maxMaxTravel, olc::vd2d maxPos, olc::vd2d minPos, double maxSpeed, bool outputNodeData, int randSeed /*= 0*/)
: nodes(), minInfTime(minInfTime), maxInfTime(maxInfTime), suvRate(suvRate), maxSpeed(maxSpeed), outputNodeData(outputNodeData), stream(log)
{
log << "seed," << randSeed << "\n";
log << "Node count," << numNodes << "\n";
log << "min infected time," << minInfTime << "\n";
log << "max infected time," << maxInfTime << "\n";
log << "survival rate," << suvRate << "\n";
log << "reinfect," << reInfect << "\n";
log << "maxSpeed," << maxSpeed << "\n";
log << NODEHEADER << "\n";
srand(randSeed);
olc::vd2d posDif = maxPos - minPos;
for (int i = 0; i < numNodes; ++i) {
olc::vd2d nodePos = minPos + olc::vd2d(drand(), drand()) * posDif;
double nodeMaxTravel = minMaxTravel + drand() * (maxMaxTravel - minMaxTravel);
nodes.emplace_back(log, i, nodePos, nodeMaxTravel, maxSpeed, reInfect);
}
nextSeed = rand();
}
void World::update(){
srand(nextSeed);
for (auto& node : nodes) {
node.move();
}
for (auto& node : nodes) {
node.update(*this);
}
nextSeed = rand();
if (outputNodeData) {
stream << NODEHEADER << "\n";
for (auto& node : nodes) {
stream << node;
}
}
}
bool infectTest (Node& n1, Node& n2) {
return (n1.cPos-n2.cPos).mag() < 4 ? n2.infected : false;
}
std::ostream& operator<<(std::ostream& os, const Node& node)
{
os << node.id << "," << node.cPos << "," << node.sPos << "," << node.infectable << "," << node.infected << "," <<
node.infectedFor << "," << node.maxTravel << "," << node.maxSpeed << "\n";
return os;
}
class Stats {
public:
void collect(World& world);
};
class DSim : public olc::PixelGameEngine {
private:
double scale;
public:
World world;
bool running;
WorldParams& wParams;
DSim(WorldParams& wParams) : world(wParams), wParams(wParams),
running(false)
{
sAppName = "dSim";
scale = 32;
auto& nodes = world.getNodes();
nodes[0].infected = true;
}
public:
bool OnUserCreate() override
{
return true;
}
bool OnUserUpdate(float fElapsedTime) override
{
if (GetKey(olc::SPACE).bPressed) {
running = !running;
}
if (running || GetKey(olc::R).bPressed) {
world.update();
}
Clear(olc::BLACK);
for (Node& node : world.getNodes()) {
if (wParams.origin) DrawCircle(node.sPos, 2, olc::GREEN);
DrawCircle(node.cPos, 2, node.infected ? olc::RED : olc::WHITE);
if (wParams.showRange) DrawCircle(node.sPos, node.maxTravel, olc::VERY_DARK_GREY);
}
return true;
}
};
int main(int argc, char** argv)
{
WorldParams wp{std::cout, 3000, 5, 20, 0.8, false, 3, 10, olc::vd2d(500, 500), olc::vd2d(0,0), olc::vi2d(512, 512), olc::vi2d(2, 2), 1, (int)time(0), false, false};
for (int i = 1; i < argc; ++i) {
int falls = 0;
if (argc - i <= 1) {//no args
if (strcmp(argv[i], "--reInf") == 0) {
wp.reInfect = true;
}
else if (strcmp(argv[i], "--showRange") == 0) {
wp.showRange = true;
}
else if (strcmp(argv[i], "--showOrigins") == 0) {
wp.origin = true;
}
else {
++falls;
}
}
if (argc - i >= 2) { //1 arg
if (strcmp(argv[i], "--file") == 0) {
++i;
}
else if (strcmp(argv[i], "--nodeCount") == 0) {
wp.numNodes = std::stoi(argv[++i]);
}
else if (strcmp(argv[i], "--minMaxTravel") == 0) {
wp.minMaxTravel = std::stoi(argv[++i]);
}
else if (strcmp(argv[i], "--maxMaxTravel") == 0) {
wp.maxMaxTravel = std::stoi(argv[++i]);
}
else if (strcmp(argv[i], "--suvRate") == 0) {
wp.suvRate = std::stod(argv[++i]);
}
else if (strcmp(argv[i], "--minInfTime") == 0) {
wp.minInfTime = std::stod(argv[++i]);
}
else if (strcmp(argv[i], "--maxInfTime") == 0) {
wp.maxInfTime = std::stod(argv[++i]);
}
else if (strcmp(argv[i], "--minPosX") == 0) {
wp.minPos.x = std::stod(argv[++i]);
}
else if (strcmp(argv[i], "--minPosY") == 0) {
wp.minPos.y = std::stod(argv[++i]);
}
else if (strcmp(argv[i], "--maxPosX") == 0) {
wp.maxPos.x = std::stod(argv[++i]);
}
else if (strcmp(argv[i], "--maxPosY") == 0) {
wp.maxPos.y = std::stod(argv[++i]);
}
else if (strcmp(argv[i], "--resX") == 0) {
wp.res.x = std::stoi(argv[++i]);
}
else if (strcmp(argv[i], "--resY") == 0) {
wp.res.y = std::stoi(argv[++i]);
}
else if (strcmp(argv[i], "--pixX") == 0) {
wp.pixSize.x = std::stoi(argv[++i]);
}
else if (strcmp(argv[i], "--pixY") == 0) {
wp.pixSize.y = std::stoi(argv[++i]);
}
else if(strcmp(argv[i], "--maxSpeed") == 0) {
wp.maxSpeed = std::stod(argv[++i]);
}
else if(strcmp(argv[i], "--seed") == 0) {
wp.randSeed = std::stoi(argv[++i]);
}
else if(strcmp(argv[i], "--outputNodeData") == 0) {
++i;
wp.outputNodeData = (strcmp(argv[i], "on") == 0 || strcmp(argv[i], "true") == 0) ? true : false;
}
else {
++falls;
}
}
if (falls == 2) {
std::cout << "unknown argument: " << argv[i] << std::endl;
return 1;
}
}
DSim demo{wp};
if (demo.Construct(wp.res.x, wp.res.y, wp.pixSize.x, wp.pixSize.y, false, false))
demo.Start();
return 0;
}