-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulatedb.js
More file actions
527 lines (487 loc) · 13.8 KB
/
Copy pathpopulatedb.js
File metadata and controls
527 lines (487 loc) · 13.8 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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
//Script to populate the database with some elements
const state_boundaries = require("./state_boundaries.json");
//Total number of vehicle to be generated
let NB_VEHICLE_GENERATED = 10; //default value
// Get arguments passed on command line
var userArgs = process.argv.slice(2);
var async = require("async");
var {
Battery,
Vehicle,
Path,
Cluster,
Parameter
} = require("./database/models/index");
var mongoose = require("mongoose");
var mongoDB = userArgs[0];
NB_VEHICLE_GENERATED = userArgs[1];
mongoose.connect(mongoDB, { useUnifiedTopology: true, useNewUrlParser: true });
mongoose.Promise = global.Promise;
var db = mongoose.connection;
db.on("error", console.error.bind(console, "MongoDB connection error:"));
var paths = [];
var parameters = [];
var clusters = [];
var batteries = [];
var vehicles = [];
function pathCreate(time, start_lat, start_lng, end_lat, end_lng, cb) {
pathdetail = {
time: time,
start_lat: start_lat,
start_lng: start_lng,
end_lat: end_lat,
end_lng: end_lng
};
var path = new Path(pathdetail);
path.save(function(err) {
if (err) {
cb(err, null);
return;
}
//console.log("New Path: " + path);
paths.push(path);
cb(null, path);
});
}
function parameterCreate(time, performance, battery_charge, cost_value, cb) {
parameterDetail = {
time: time,
performance: performance,
battery_charge: battery_charge,
cost_value: cost_value
};
var parameter = new Parameter(parameterDetail);
parameter.save(function(err) {
if (err) {
//cb(err, null)
//console.log("error " + err);
return;
}
//console.log("New Parameter: " + parameter);
parameters.push(parameter);
});
}
function clusterCreate(name, center_lat, center_lng, radius, gen_health, tco_savings, cb) {
clusterDetail = {
name: name,
center_lat: center_lat,
center_lng: center_lng,
radius: radius,
gen_health,
tco_savings
};
var cluster = new Cluster(clusterDetail);
cluster.save(function(err) {
if (err) {
//cb(err, null)
console.log(err);
return;
}
//console.log("New Cluster: " + cluster);
clusters.push(cluster);
//cb(null, cluster)
});
}
function batteryCreate(name, charge, life_span, status, date_of_creation, cb) {
batterydetail = {
name: name,
charge: charge,
life_span: life_span,
status: status,
date_of_creation: date_of_creation
};
var battery = new Battery(batterydetail);
battery.save(function(err) {
if (err) {
cb(err, null);
return;
}
//console.log("New Battery: " + battery);
batteries.push(battery);
cb(null, battery);
});
}
function vehicleCreate(
id,
position_lat,
position_lng,
model,
temperature,
cluster,
parameters,
_battery_id,
date_of_creation,
_path_id,
cb
) {
vehicledetail = {
_id: id,
position_lat: position_lat,
position_lng: position_lng,
model: model,
temperature: temperature,
cluster: cluster,
parameters: parameters,
_battery_id: _battery_id,
date_of_creation: date_of_creation,
_path_id: _path_id
};
var vehicle = new Vehicle(vehicledetail);
vehicle.save(function(err) {
if (err) {
//cb(err, null)
console.log(err);
return;
}
//console.log("New Vehicle: " + vehicle);
vehicles.push(vehicle);
//cb(null, vehicle)
});
}
function createPaths(cb) {
async.series(
[
function(callback) {
pathCreate(120, 40.732461, 42.032461, -73.865242, -73.065242, callback);
},
function(callback) {
pathCreate(90, 40.77061, 40.3461, -71.935242, -72.865242, callback);
},
function(callback) {
pathCreate(103, 41.01061, 39.71061, -73.125242, -74.035242, callback);
},
function(callback) {
pathCreate(64, 40.74361, 41.074361, -73.935242, -72.865242, callback);
}
],
// optional callback
cb
);
}
async function createParams(date_creation, random_vehicle_index, vehicle_id, cb) {
price_list = [8795, 8797, 8794, 8792, 8793, 8798, 8791];
random_vehicle_index = Math.floor(random_vehicle_index);
let today = new Date();
let now = new Date(today.setMonth(today.getMonth()+1));
let projection = new Date(today.setFullYear(today.getFullYear()+2));
let performance = 100;
let newperformance = 99.8 ;
let battery_charge = 100;
let params = [];
let month = 0;
let coef = Math.random();
let cost_value = price_list[random_vehicle_index];
for (var d = new Date(date_creation); d <= projection; d.setMonth(d.getMonth() + 1)) {
date = new Date(d);
table = [0, 1];
maxtab = [50, 100]
selector = Math.round(Math.random());
let baseline_value = -Math.pow(month / 12, 1.7 ) + 100;
let baseline_value_max = -Math.pow(month / 12, 1.4 ) + 100;
let baseline_value_min = -Math.pow(month / 12, 1.95) + 100;
//newperformance = baseline_value + table[selector] * ((100 - baseline_value)/10)*(month/12 ) - table[1-selector] * (baseline_value/10)*(month/12 );
let newperformance = coef*(baseline_value_max - baseline_value_min) + baseline_value_min;
// while(newperformance > performance){
// //console.log(newperformance);
// newperformance = Math.random()*(baseline_value_max - baseline_value_min) + baseline_value_max;
// }
// // // other_selector = Math.round(Math.random());
// newperformance = newperformance - Math.random()*(0.2) * month/12;
// }
performance = newperformance.toFixed(3);
month ++;
if (battery_charge < 8) {
battery_charge = battery_charge + Math.random() * 90;
} else {
battery_charge = Math.random() * battery_charge;
}
cost_value = cost_value - cost_value * (0.4 / 365) * (performance/100);
cost_value = cost_value.toPrecision(5);
parameterDetail = {
vehicle: vehicle_id,
time: date,
performance: performance,
battery_charge: battery_charge,
cost_value: cost_value
};
params.push(new Parameter(parameterDetail));
}
async.parallel({
fun: function(callback) {
Parameter.insertMany(params, function(err, docs) {
if (err) {
return console.log(err);
} else {
//console.log("Inserted in collection: " + docs);
return docs;
}
});
callback(null, params);
}
}, function(err, results) {
//console.log("RESULTS ", results.fun);
});
return params;
}
function createClusters(cb) {
let cluster_center_lat;
let cluster_center_lng;
let cluster_name;
let cluster_radius;
async.series(
[
function(callback) {
for (let element of Object.keys(state_boundaries)) {
cluster_center_lat =
(state_boundaries[element].min_lat +
state_boundaries[element].max_lat) /
2;
cluster_center_lng =
(state_boundaries[element].min_lng +
state_boundaries[element].max_lng) /
2;
cluster_name = state_boundaries[element].name;
clust_size = getDistanceFromLatLonInKm(
state_boundaries[element].max_lat,
state_boundaries[element].max_lng,
cluster_center_lat,
cluster_center_lng
);
cluster_radius = 500000;/*getDistanceFromLatLonInKm(
state_boundaries[element].max_lat,
state_boundaries[element].max_lng,
cluster_center_lat,
cluster_center_lng
);*/
tco_savings = Math.random() * (clust_size - 100000) + 100000;
clusterCreate(
cluster_name,
cluster_center_lat,
cluster_center_lng,
cluster_radius,
Math.random()*50+45,
tco_savings
);
}
callback();
}
],
cb
);
}
function getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2 - lat1); // deg2rad below
var dLon = deg2rad(lon2 - lon1);
var a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(deg2rad(lat1)) *
Math.cos(deg2rad(lat2)) *
Math.sin(dLon / 2) *
Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c; // Distance in km
return d * 1000;
}
function deg2rad(deg) {
return deg * (Math.PI / 180);
}
function createBatteries(cb) {
async.parallel(
[
function(callback) {
batteryCreate("lithium-ion", 86, 74, "Working", "2017,08,07", callback);
},
function(callback) {
batteryCreate(
"li-ion polymer",
76,
94,
"Working",
"2018,10,04",
callback
);
},
function(callback) {
batteryCreate(
"nickel-metal hydride",
56,
21,
"Damaged",
"2015,01,11",
callback
);
},
function(callback) {
batteryCreate("lead acid", 21, 29, "Working", "2016,04,06", callback);
},
function(callback) {
batteryCreate("lead acid", 47, 18, "Working", "2016,04,06", callback);
},
function(callback) {
batteryCreate("lead acid", 32, 58, "Working", "2016,04,06", callback);
},
function(callback) {
batteryCreate("lead acid", 78, 64, "Working", "2016,04,06", callback);
},
function(callback) {
batteryCreate("lead acid", 64, 78, "Working", "2016,04,06", callback);
},
function(callback) {
batteryCreate("lead acid", 58, 32, "Working", "2016,04,06", callback);
},
function(callback) {
batteryCreate("lead acid", 18, 47, "Working", "2016,04,06", callback);
},
function(callback) {
batteryCreate("lead acid", 68, 21, "Working", "2016,04,06", callback);
},
function(callback) {
batteryCreate("lead acid", 29, 68, "Working", "2016,04,06", callback);
}
],
// optional callback
cb
);
}
async function genVehicleParams() {
// General parameters
const usa_lat_max = 43.5;
const usa_lat_min = 30.35;
const usa_lng_max = -74;
const usa_lng_min = -120.5;
let random_lat;
let random_lng;
vehicle_list = [
"Tesla Model 3",
"MG ZS EV",
"Mercedes-Benz EQC",
"Nissan Leaf",
"Tesla Model X",
"Hyundai Kona Electric",
"Hyundai Ionic Electric"
];
// Generation of time-dependent data
let random_vehicle_index = Math.random() * vehicle_list.length;
random_vehicle_index = Math.floor(random_vehicle_index);
let chosen_model = vehicle_list[random_vehicle_index];
let cluster;
const start = new Date(2012, 01, 01);
const end = new Date();
const date_creation_vehicle = randomDate(start, end);
let param_array = [];
let id = mongoose.Types.ObjectId(); //id of the vehicle
param_array = await createParams(date_creation_vehicle,random_vehicle_index, id);
const temperature = (Math.random()*(60)+32).toPrecision(2);
// Generation of random coordinates and cluster position
while (!cluster) {
random_lat = Math.random() * (usa_lat_max - usa_lat_min) + usa_lat_min;
for (let element of Object.keys(state_boundaries)) {
if (
random_lat >= state_boundaries[element].min_lat &&
random_lat < state_boundaries[element].max_lat
) {
random_lng = Math.random() * (usa_lng_max - usa_lng_min) + usa_lng_min;
if (
random_lng >= state_boundaries[element].min_lng &&
random_lng < state_boundaries[element].max_lng
) {
cluster = state_boundaries[element].name;
let name = "FLE-"+ date_creation_vehicle.getTime() +"-"+ (random_lat*100).toPrecision(4);
let vehicle_prop;
thisCluster = await Cluster.findOne(
{ name: cluster },
(err, matchingCluster) => {
thisCluster = matchingCluster;
}
);
vehicle_prop = {
id: id,
lat: random_lat,
lng: random_lng,
model: name,
temperature: temperature,
date_creation: date_creation_vehicle,
cluster: thisCluster,
parameters: param_array
};
return vehicle_prop;
}
}
}
}
}
function createVehicles(cb) {
vehicles = [];
async.parallel(
[
async function(callback) {
for (let i = 0; i < NB_VEHICLE_GENERATED; i++) {
vehicle_prop = await genVehicleParams();
await vehicleCreate(
vehicle_prop.id,
vehicle_prop.lat,
vehicle_prop.lng,
vehicle_prop.model,
vehicle_prop.temperature,
vehicle_prop.cluster,
vehicle_prop.parameters,
batteries[Math.floor(Math.random()*(12))],
vehicle_prop.date_creation,
paths[0]
);
}
callback();
}
],
cb
);
}
function randomDate(start, end) {
var date = new Date(+start + Math.random() * (end - start));
return date;
}
function cleanDb(cb) {
// Delete all items in current database
async.parallel(
[
function(callback) {
Vehicle.deleteMany({}, callback);
},
function(callback) {
Parameter.deleteMany({}, callback);
},
function(callback) {
Battery.deleteMany({}, callback);
},
function(callback) {
Cluster.deleteMany({}, callback);
},
function(callback) {
Path.deleteMany({}, callback);
}
],
// optional callback
cb
);
}
async.series(
[
cleanDb, //delete this line if you do not want to remove the current database collections
//createParams,
createClusters,
createPaths,
createBatteries,
createVehicles
],
// Optional callback
function(err, results) {
if (err) {
//console.log('FINAL ERR: ' + err);
} else {
console.log("Vehicles: " + vehicles);
}
// All done, disconnect from database
mongoose.connection.close();
}
);