-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulator.java
More file actions
69 lines (62 loc) · 2.41 KB
/
Simulator.java
File metadata and controls
69 lines (62 loc) · 2.41 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
import java.util.List;
class Simulator {
private final int numOfServers;
private final int qmax;
private final ImList<Pair<Double,Double>> inputTimes;
Simulator(int numOfServers, int qmax, ImList<Pair<Double,Double>> inputTimes) {
this.numOfServers = numOfServers;
this.qmax = qmax;
this.inputTimes = inputTimes;
}
private ImList<Server> populateShop() {
ImList<Server> serverList = new ImList<Server>();
for (int i = 1; i <= numOfServers; i++) {
Server server = new Server(i, 0.0);
serverList = serverList.add(server);
}
return serverList;
}
private ImList<ImList<Customer>> populateQueue() {
ImList<ImList<Customer>> allQueues = new ImList<ImList<Customer>>();
for (int i = 1; i <= numOfServers; i++) {
ImList<Customer> queue = new ImList<Customer>();
allQueues = allQueues.add(queue);
}
return allQueues;
}
public String simulate() {
String s = "";
Shop shop = new Shop(populateShop(), populateQueue(), qmax);
PQ<Event> pq = new PQ<Event>(new EventComp());
ImList<Double> statistics = new ImList<Double>(List.of(0.0,0.0,0.0));
int index = 1;
//populate pq with arrive event
for (Pair<Double, Double> input : inputTimes) {
Customer customer = new Customer(input.first(), input.second(), index);
Event newArrive = new Arrive(shop, customer, customer.getArrivalTime());
index++;
pq = pq.add(newArrive);
}
while (!pq.isEmpty()) {
Pair<Event, PQ<Event>> pr = pq.poll();
Event event = pr.first();
pq = pr.second();
s += (event.toString() + "\n");
Event nextEvent = event.getNextEvent(shop);
boolean shouldAdd = false;
if (event != nextEvent) {
shouldAdd = true;
statistics = nextEvent.updateStatistics(statistics);
shop = nextEvent.getShop();
}
if (shouldAdd) {
pq = pq.add(nextEvent);
}
}
double averageTime = statistics.get(0) / statistics.get(1);
int servedTotal = statistics.get(1).intValue();
int leftTotal = statistics.get(2).intValue();
s += String.format("[%.3f %d %d]", averageTime, servedTotal, leftTotal);
return s;
}
}