-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulator.js
85 lines (70 loc) · 2 KB
/
simulator.js
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
import Agents from "../agent-manager/index.js";
import Store from "../store/index.js";
import uniqid from "uniqid";
// todo import locations
import Events from "../location/Events/index.js";
export default class{
constructor(params = {}){
let {
speed = "hour",
id = uniqid("sim-"),
save = false,
sync = true
} = params;
this.id = id;
this.agents = new Agents({
simulation:this.id,
speed,
save,
sync
});
this.params = {
_id:this.id,
simulation:this.id,
speed,
save,
sync,
agents: 0
};
if(this.params.save){
try {
this.store = new Store({type: "simulations",id:this.id});
}catch (err){
console.error("store error",err)
}
}
this.eventsHelper = new Events();
}
async init(agents){
console.time("user generation");
await this.agents.init(agents);
console.timeEnd("user generation");
this.params.agents = agents;
await this._record();
}
async run(days){
try {
// generate events
let events = this.eventsHelper.today();
console.time("simulation");
for(let day= 0; day < days; day++){
console.time(`Day ${day+1}`);
let summary = await this.agents.run(events);
console.timeEnd(`Day ${day+1}`);
}
console.timeEnd("simulation");
}catch (err){
console.error(err);
}
}
async closeUp(){
console.time("clean-up");
await this.agents.closeUp();
console.timeEnd("clean-up");
}
async _record(){
if(!this.store){return Promise.resolve("I'm not keeping this in the diary...")}
let r = await this.store.save(this.params);
// console.log("saved?",r);
}
}