-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulator.java
More file actions
88 lines (73 loc) · 2.92 KB
/
Simulator.java
File metadata and controls
88 lines (73 loc) · 2.92 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
import java.util.function.Supplier;
import java.util.Random;
class Simulator {
private final int numOfServers;
private final int numOfSelfChecks;
private final int qmax;
private final ImList<Pair<Double, Supplier<Double>>> inputTimes;
private final Supplier<Double> restTime;
Simulator(int numOfServers, int numOfSelfChecks, int qmax,
ImList<Pair<Double, Supplier<Double>>> inputTimes,
Supplier<Double> restTime) {
this.numOfServers = numOfServers;
this.qmax = qmax;
this.inputTimes = inputTimes;
this.restTime = restTime;
this.numOfSelfChecks = numOfSelfChecks;
}
String simulate() {
String output = "";
int cusServed = 0;
double waitTime = 0;
int cusLeave = 0;
int count = 1;
PQ<Event> pq = new PQ<Event>(new EventComparator());
ImList<Servable> serverList = new ImList<Servable>();
for (Pair<Double, Supplier<Double>> curr : inputTimes) {
Customer currCustomer = new Customer(curr.first(), curr.second(), count++);
Arrive arrive = new Arrive(currCustomer);
pq = pq.add(arrive);
}
for (int i = 0; i < this.numOfServers; i++) {
serverList = serverList.add(new Server(
0, i + 1, this.qmax, this.restTime));
}
if (this.numOfSelfChecks > 0) {
ImList<Servable> q = new ImList<>();
for (int i = 0; i < this.numOfSelfChecks; i++) {
q = q.add(new SelfCheckCounter(this.numOfServers + 1 + i, 0, this.numOfServers));
}
serverList = serverList.add(
new SelfServeCounterManager(q, this.qmax, 0, this.numOfServers));
}
while (!pq.isEmpty()) {
Pair<Event, PQ<Event>> currItem = pq.poll();
Event event = currItem.first();
output += event.canPrint() ? event.toString() + "\n" : "";
Event currEvent = event;
Pair<Event, ImList<Servable>> nextItem = currEvent.process(serverList);
Event nextEvent = nextItem.first();
ImList<Servable> nextServer = nextItem.second();
switch (currEvent.getType().value()) {
case "Wait":
waitTime += nextEvent.getCustomer().getTime() -
currEvent.getCustomer().getTime();
break;
case "Served":
cusServed++;
break;
case "Leave":
cusLeave++;
break;
default:
break;
}
pq = nextEvent == event ? currItem.second()
: currItem.second().add(nextEvent);
serverList = nextServer;
}
output += String.format("[%.3f %d %d]", waitTime / cusServed,
cusServed, cusLeave);
return output;
}
}