-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulator.java
More file actions
71 lines (65 loc) · 2.65 KB
/
Simulator.java
File metadata and controls
71 lines (65 loc) · 2.65 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
import java.util.function.Supplier;
class Simulator {
private final int numOfServers;
private final int numOfSelfChecks;
private final int qmax;
private final ImList<Double> arrivalTimes;
private final Supplier<Double> serviceTime;
private final Supplier<Double> restTimes;
Simulator(int numOfServers, int numOfSelfChecks, int qmax, ImList<Double> arrivalTimes,
Supplier<Double> serviceTime, Supplier<Double> restTimes) {
this.numOfServers = numOfServers;
this.numOfSelfChecks = numOfSelfChecks;
this.qmax = qmax;
this.arrivalTimes = arrivalTimes;
this.serviceTime = serviceTime;
this.restTimes = restTimes;
}
public String simulate() {
int numServed = 0;
int numLeft = 0;
double totalWaitingTime = 0.0;
double avgWaitingTime;
String output = new String();
ImList<Server> serverList = new ImList<Server>();
PQ<Event> pq = new PQ<Event>(new EventComp());
for (int i = 1; i <= numOfServers; i++) {
serverList = serverList.add(new HumanServer(i, qmax, 0,
0.0, restTimes));
}
if (numOfSelfChecks > 0) {
ImList<Double> selfchecksAT = new ImList<Double>();
for (int i = 1; i <= numOfSelfChecks; i++) {
selfchecksAT = selfchecksAT.add(0.0);
}
serverList = serverList.add(new Selfchecks(numOfServers + 1, qmax, 0,
selfchecksAT));
}
for (int i = 0; i < arrivalTimes.size(); i++) {
pq = pq.add(new ArriveEvent(this.arrivalTimes.get(i), new Customer(i + 1,
this.arrivalTimes.get(i), 0.0, serviceTime)));
}
Pair<Event, PQ<Event>> currentPair;
Event currentEvent;
while (!pq.isEmpty()) {
currentPair = pq.poll();
currentEvent = currentPair.first();
pq = currentPair.second();
output += currentEvent;
numServed = currentEvent.updateNumServed(numServed);
numLeft = currentEvent.updateNumLeft(numLeft);
totalWaitingTime = currentEvent.updateTotalWaitingTime(totalWaitingTime);
serverList = currentEvent.updateServerList(serverList);
if (currentEvent.hasNextEvent()) {
pq = pq.add(currentEvent.nextEvent(serverList));
}
}
if (numServed != 0) {
avgWaitingTime = totalWaitingTime / numServed;
} else {
avgWaitingTime = 0;
}
output += String.format("[%.3f %s %s]", avgWaitingTime, numServed, numLeft);
return output;
}
}