-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpTimeEventDriven.java
112 lines (88 loc) · 3.24 KB
/
ExpTimeEventDriven.java
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
import java.math.*;
import java.util.*;
public class ExpTimeEventDriven {
private static Random r;
private enum Event {
DEPOSIT, WITHDRAWAL, THEFT
}
/* Given expected number of events in a given unit of time,
returns time interval to next event (exponential distribution). */
private static double timeToEvent(double mean) {
return (-1.0 * Math.log(r.nextDouble()) / mean);
}
/* Given times to next deposit, withdrawal, and hot wallet theft,
returns most imminent Event. */
private static Event nextEvent(double nextD, double nextW, double nextT) {
if (nextD <= nextW && nextD <= nextT) {
return Event.DEPOSIT;
}
else if (nextW <= nextD && nextW <= nextT) {
return Event.WITHDRAWAL;
}
else {
return Event.THEFT;
}
}
public static void main(String[] args) {
if (args.length != 3 && args.length != 6) {
System.out.printf("Usage: java ExpTimeEventDriven mean_d mean_w mean_t_h [iterations mu_low mu_high]\n" +
" e.g. java ExpBalanceEventDriven 80.0 78.0 0.001 1000 5 500\n");
return;
}
r = new Random();
final double mD = Double.parseDouble(args[0]);
final double mW = Double.parseDouble(args[1]);
final double mTh = Double.parseDouble(args[2]);
final int iterations;
final int muLow;
final int muHigh;
if (args.length == 3) {
iterations = 1000;
muLow = 5;
muHigh = 500;
}
else {
iterations = Integer.parseInt(args[3]);
muLow = Integer.parseInt(args[4]);
muHigh = Integer.parseInt(args[5]);
}
System.out.println("\n mu time");
System.out.println("----------------");
for (int mu = muLow; mu <= muHigh; mu++) {
double cumulativeTime = 0.0;
for (int i = 0; i < iterations; i++) {
/* Times in seconds */
double time = 0; // simulation time
double timeToD = timeToEvent(mD/3600.0); // time to next deposit
double timeToW = timeToEvent(mW/3600.0); // time to next withdrawal
double timeToT = timeToEvent(mTh/3600.0); // time to next hot wallet theft
int balance = mu;
while (balance > 0) {
Event nextEvent = nextEvent(timeToD, timeToW, timeToT);
switch (nextEvent) {
case DEPOSIT:
if (balance < mu) balance++;
time = timeToD;
timeToD += timeToEvent(mD/3600.0);
break;
case WITHDRAWAL:
balance--;
time = timeToW;
timeToW += timeToEvent(mW/3600.0);
break;
case THEFT:
balance = 0;
time = timeToT;
timeToT += timeToEvent(mTh/3600.0);
break;
default:
break;
}
}
cumulativeTime += (double)time/3600.0;
}
/* Print stats for current value of mu */
System.out.printf("%d %5.1f\n", mu, cumulativeTime/(double)iterations);
}
}
}