This repository has been archived by the owner on Apr 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
risk-calculation.cpp
351 lines (296 loc) · 10.5 KB
/
risk-calculation.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include "hash-map.cpp"
#include "graph-matrix.cpp"
#include "rdf-types.h"
#include <algorithm>
#include <vector>
#include <set>
#include <math.h>
/**
* Generate the covid risk for each person
*/
struct RoomRisk
{
RoomRisk(int _n = 0, float _r = 0.0, set<int> _f = {})
{
no = _n;
risk = _r;
from = _f;
};
int no = 0;
float risk = 0.0;
set<int> from = {};
};
struct RoomDimensions
{
RoomDimensions(int _w = 0, int _l = 0)
{
width = _w;
length = _l;
};
int width = 0;
int length = 0;
};
struct IndividualRiskDetails
{
// IndividualRiskDetails(float _risk = 0.0, int _startLocation = 0)
// {
// risk = _risk;
// startLocation = _startLocation;
// };
float risk;
int startLocation;
friend ostream& operator<<(ostream& os, const IndividualRiskDetails& dt)
{
os << dt.risk << "-" << dt.startLocation;
return os;
};
};
struct IndividualAndRisk
{
IndividualAndRisk(string _individual = "", float _risk = 0.0)
{
risk = _risk;
individual = _individual;
};
float risk = 0.0;
string individual;
// Since we are using this to determine those *most likely
// to be exposed to COVID* the most important thing to be
// sorting by here is risk of infection
bool operator<(const IndividualAndRisk& ind)
{
return ((this->risk) < ind.risk);
};
friend ostream& operator<<(ostream& os, const IndividualAndRisk& dt)
{
os << dt.individual << "-" << dt.risk;
return os;
};
};
Map<string, IndividualRiskDetails> personRisks(Triples personTriples, GraphMatrix<string> graph)
{
int s = personTriples.size() / 3;
Map<string, IndividualRiskDetails> individuals(s > 0 ? s : 1);
cout << "inside person risks" << endl;
cout << "there are " << personTriples.size() << " triples" << endl;
cout << "there are " << graph.nodeCount() << " nodes" << endl;
for (Triple t : personTriples)
{
cout << t[0] << " " << t[1] << " " << t[2] << endl;
if (t[1] == "http://example.org/risk")
{
float risk = stof(t[2]);
if (individuals.hasKey(t[0]))
{
individuals.add(t[0], {risk : risk, startLocation : individuals.get(t[0]).startLocation});
}
else
{
individuals.add(t[0], {risk : risk, startLocation : -1});
};
}
else if (t[1] == "http://architecture#located")
{
if (individuals.hasKey(t[0]))
{
individuals.add(t[0], {risk : individuals.get(t[0]).risk, startLocation : graph.nameToId(t[2])});
// individuals.get(t[0]).risk = graph.nameToId(t[2]);
}
else
{
individuals.add(t[0], {risk : 0.0, startLocation : graph.nameToId(t[2])});
};
};
};
return individuals;
};
struct CumulativeRisk {
CumulativeRisk(float _totalRisk = 0.0, int _no = 1)
{
totalRisk = _totalRisk;
no = _no;
};
void operator+(const float &risk)
{
totalRisk += risk;
no += 1;
};
float totalRisk = 0.0;
int no = 1;
};
Map<int, RoomRisk> initRisk(GraphMatrix<string> graph, Map<string, IndividualRiskDetails> &individualRisk)
{
cout << "-------------------------------------------------------------------------init risk called " << endl;
cout << individualRisk.size() << endl;
Map<int, CumulativeRisk> risks(graph.nodeCount());
// Would be equal to the number of people in the file as with the
// current setup we have exactly 3 people in the file. Obviously
// this is not really scalable to a larger application - for instance
// if we wanted to add more data to the persons file - but we don't
// really care about that here anyway.
individualRisk.print();
for (Pair<string, IndividualRiskDetails> r : individualRisk.getPairs())
{
cout << "inside for" << endl;
if (risks.hasKey(r.value.startLocation))
{
CumulativeRisk c = risks.get(r.value.startLocation);
risks.add(r.value.startLocation, CumulativeRisk(c.totalRisk + r.value.risk, c.no + 1));
// risks.get(r.value.startLocation).push_back(r.value.risk);
}
else
{
risks.add(r.value.startLocation, CumulativeRisk(r.value.risk));
};
};
// int s = personTriples.size() / 3;
// Map<string, IndividualRiskDetails> individuals(s > 0 ? s : 1);
// // risks.reserve(graph.size());
// for (Triple t : personTriples)
// {
// if (t[1] == "http://example.org/risk")
// {
// // Get the room that the person is located
// // to start off with
// int located = graph.nameToId(t[0]);
// // Converts string to float
// float risk = stof(t[2]);
// if (risks.hasKey(located))
// {
// risks.get(located).push_back(risk);
// }
// else
// {
// risks.add(located, {risk});
// };
// if (individuals.hasKey(t[0]))
// {
// individuals.get(t[0]).risk = risk;
// }
// else
// {
// individuals.add(t[0], IndividualRiskDetails(risk));
// };
// };
// };
Map<int, RoomRisk> roomRisks;
for (Pair<int, CumulativeRisk> risks : risks.getPairs())
{
// float total = 0.0;
// for (float f : risks.value)
// {
// total += f;
// };
roomRisks.add(risks.key, RoomRisk(risks.value.no, ((float)risks.value.totalRisk) / ((float)(risks.value.no)), {risks.key}));
};
return roomRisks;
};
/**
* This is a *heuristic* used to determine the likely
* risk of catching covid in a room *assuming* that
* given individual does *not already* have covid
*/
float roomRisk(int w, int h, int no, float avg)
{
cout << w << " " << h << " " << no << endl;
float density = (float(no - 1)) / ((float)(w * h));
cout << " density is " << density;
cout << " avergae is" << avg << endl;
return density * avg;
};
// float increasedRisk
/**
* Generates the risk of covid exposure when starting in each room
* (note that this is a dynamic programming solution and we have)
* additionally reduced computation time by calculating risk increase by
* room rather than by individual.
*/
Map<int, float> generateCovidRisk(GraphMatrix<string> &graph, Map<int, RoomRisk> &initRisks, Map<int, int> &nextRoom, Map<int, RoomDimensions> &roomDims)
{
// Can start with these initialised
int timestamp = 0;
cout << endl << endl;
cout << "generate covid risk called" << endl;
cout << "the size of initirisks is" << initRisks.size() << endl;
// Map<int, float> roomRiskAvg;
// Map<int, int> roomCount;
Map<int, Map<int, RoomRisk>> transit;
Map<int, float> exposureRisk;
for (int i : initRisks.keys())
{
exposureRisk.add(i, 0.000000000000000000);
};
// Map<int, int> nextRoom;
// Map<int, int> nextRoomDistance;
transit.add(0, initRisks);
cout << "the size of initirisks is" << initRisks.size() << endl;
while (transit.size() > 0)
{
cout << "the transit size is " << transit.size() << endl;
if (transit.hasKey(timestamp))
{
Map<int, RoomRisk> data = transit.get(timestamp);
cout << "inside haskey" << endl;
for (Pair<int, RoomRisk> p : data.getPairs())
{
cout << "inside pair loop" << endl;
// Note that if this condition is false then we are
// in the pathological case where there is no escape
// path from a particular room
if (nextRoom.hasKey(p.key))
{
int next = nextRoom.get(p.key);
RoomDimensions roomDim = roomDims.get(p.key);
float risk = roomRisk(roomDim.width, roomDim.length, p.value.no, p.value.risk);
cout << "------------------------------------------------------------------------------------next is " << next << endl;
// Updating the risk value
for (int i : p.value.from)
{
float prevRisk = exposureRisk.get(i);
// This works since we have designed the
// hash map to ovveride existing values
exposureRisk.add(i, prevRisk + (1.0 - prevRisk) * risk);
// exposureRisk.add(i, p.value.risk);
cout << "----------------------------------------------------------------- updating exposure risk risk:" << risk << " prevrisk:" << prevRisk << endl;
};
if (next != -1)
{
int nextTime = timestamp + graph._edgeLength(p.key, next);
if (!transit.hasKey(nextTime))
{
Map<int, RoomRisk> temp;
transit.add(nextTime, temp);
};
// if (!transit.get(nextTime).hasKey(next))
// {
// transit.get(nextTime).add(next, RoomRisk());
// };
RoomRisk prev = transit.get(nextTime).hasKey(next) ? transit.get(nextTime).get(next) : RoomRisk();
prev.from.insert(p.value.from.begin(), p.value.from.end());
transit.get(nextTime).add(next, RoomRisk(prev.no + p.value.no,
(prev.risk * ((float(prev.no))) + p.value.risk * (float(p.value.no))) / float(prev.no + p.value.no),
prev.from));
};
};
};
// data[2]
transit.remove(timestamp);
timestamp++;
};
};
return exposureRisk;
// while (true)
// {
// Map<int, float> roomRiskTemp;
// Map<int, int> roomCountTemp;
// for (int i = 0; i < graph.nodeCount(); i++)
// {
// if
// };
// };
// Map<string, int> risk;
// for (int i = 0;)
};
// Map<string, float> generateIndividualrisk(GraphMatrix<string> graph, Map<int, RoomRisk> initRisks, Map<int, int> nextRoom, Map<int, RoomDimensions> roomDims)
// {
// };