-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAIMC.java
More file actions
338 lines (276 loc) · 11.5 KB
/
AIMC.java
File metadata and controls
338 lines (276 loc) · 11.5 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import java.util.*;
/**
* CSCI 6341/ Final Project: Automatic Intersection Management and Control
* Author: Ismayil Hasanov, Yubo Tsai, Han Wang
* Date : Apr 25 2017
* Description: Queue controller and algorithms for AIMC
*/
@SuppressWarnings("unchecked")
public class AIMC extends Observable {
// Number of lanes
int k = 4;
// Flag used in the GUI departure drawing
public boolean[] departFlag = new boolean[k];
// Avg time between arrivals = 1.0 (lambda)
double arrivalRate = 4.0;
double serviceRate = 1.0;
private PriorityQueue<Event> eventList;
private Event currentEvent = null;
private PriorityQueue<Car>[] carList = new PriorityQueue[k];
private double clock;
// Statistics.
int numArrivals; // Number of arrivals
int numDepartures; // Number of departures
int[] numDeparturesFromLane = new int[k]; // Departures per lane
double[] sysTime = new double[k]; // Time spent per lane
double[] avgSysTime = new double[k]; // Average time spent per lane
double totalWaitTime, avgWaitTime; // For time spent in queue
double totalSystemTime, avgSystemTime; // For time spent in system
double prevEventTime;
// Event direction
int left = Event.LEFT;
int right = Event.RIGHT;
int straight = Event.STRAIGHT;
int carID = 1;
public AIMC() {
reset();
}
void reset() {
eventList = new PriorityQueue<Event>();
Event currentEvent = null;
for (int i = 0; i < k; i++) {
carList[i] = new PriorityQueue<Car>();
departFlag[i] = false;
// Stats reset
sysTime[i] = 0;
avgSysTime[i] = 0;
numDeparturesFromLane[i] = 0;
}
// Stats reset
carID = 1;
numArrivals = 0;
numDepartures = 0;
avgWaitTime = totalWaitTime = 0;
avgSystemTime = totalSystemTime = 0;
prevEventTime = 0;
// Need to have at least one event in event list
clock = 0;
scheduleArrival();
}
void nextStep() {
if (currentEvent != null) {
prevEventTime = currentEvent.eventTime;
}
// Event list empty?
if (eventList.isEmpty()) {
System.out.println ("ERROR: nextStep(): EventList empty");
return;
}
// Extract the next event and set the time to that event.
currentEvent = eventList.poll();
clock = currentEvent.eventTime;
// Handle each type separately.
if (currentEvent.type == Event.ARRIVAL) {
handleArrival(currentEvent);
}
else if (currentEvent.type == Event.DEPARTURE) {
handleDeparture(currentEvent);
// Let GUI know the departure occurred
setChanged();
notifyObservers();
}
// Do stats after event is processed.
stats(currentEvent);
}
private void handleArrival(Event e) {
// Debugging
// System.out.println("**********************************************************");
// System.out.println("Car " + e.carID + " arrives at lane " + e.fromLane + " and turns to " + e.direction);
// System.out.println("The size of this lane: " + carList[e.fromLane].size());
numArrivals++;
carList[e.fromLane].add(new Car(e.eventTime, e.direction, e.fromLane, e.carID));
scheduleDeparture(e);
// Schedule the next arrival
scheduleArrival();
}
private void handleDeparture(Event e) {
numDepartures++;
numDeparturesFromLane[e.fromLane]++;
//remove it from carList, set departFlag for that lane
Car car = carList[e.fromLane].poll();
departFlag[e.fromLane] = true;
sysTime[e.fromLane] += clock - car.arrivalTime;
// Debugging
// System.out.println("****************");
// System.out.println("Car " + e.carID + " departs from lane " + e.fromLane + " to " + e.direction);
// System.out.println("Departure #: " + numDepartures);
// System.out.println("Waiting cars: 0=" + carList[0].size() + ", 1=" + carList[1].size() + ", 2=" + carList[2].size() + ", 3=" + carList[3].size());
}
private void scheduleArrival() {
int fromLane = RandTool.uniform(0,k-1);
double nextArrivalTime = clock + randomInterarrivalTime(fromLane);
int direction = RandTool.uniform(0,2);
eventList.add(new Event(nextArrivalTime, Event.ARRIVAL, fromLane, carID, direction));
// Debugging
// System.out.println("Car " + carID + " will arrive at " + nextArrivalTime);
carID ++;
}
// Schedule departure for the main car
private void scheduleDeparture(Event e) {
double nextDepartureTime = clock + randomServiceTime();
Event eventDeparture = new Event(nextDepartureTime, Event.DEPARTURE, e.fromLane, e.carID, e.direction);
eventList.add(eventDeparture);
// Debugging
// System.out.println("Main departure scheduled ~> " + eventDeparture);
// System.out.println("Car " + e.carID + " will depart at " + nextDepartureTime);
// To check if any car can be scheduled/go simultaneously
allowTraffic(eventDeparture);
}
// Schedule departure for cars from other lanes simultaneously
private void scheduleDeparture(Event e, Car car) {
double nextDepartureTime = e.eventTime;
Event eventDeparture = new Event(nextDepartureTime, Event.DEPARTURE, car.fromLane, car.carID, car.direction);
eventList.add(eventDeparture);
// Debugging
// System.out.println("Simultaneous departure scheduled -> " + eventDeparture);
// System.out.println("================ allowTraffic is scheduling: ==========");
// System.out.println("Event's CarID, fromLane, time: " + e.carID+", " +e.fromLane + ", "+e.direction+", "+e.eventTime);
// System.out.println("Other Cars "+car.carID+" will depart at " + nextDepartureTime);
}
// Simultaneous going strategy
private void allowTraffic(Event e) {
int direction = e.direction;
int fromLane = e.fromLane;
// Allowed cars queue
PriorityQueue<Car> allowedCars = new PriorityQueue<Car>();
// Get the first three cars from other lanes
for (int i = 0; i < k; i++) {
if (carList[i].size() == 0) {
continue;
}
if (i != fromLane) {
allowedCars.add(carList[i].peek());
}
}
if (allowedCars.size() == 0) {
return;
}
Iterator<Car> it = allowedCars.iterator();
while (it.hasNext()) {
Car car = /*(Car)*/ it.next();
// Debugging
// System.out.println(" CarID: " + car.carID+", " +car.fromLane + ", "+car.direction);
// Left car
if (car.fromLane == (fromLane + 1) % 4 && car.direction == right) {
// System.out.println("Checking left...");
if (removeDuplicateEvent(car.carID, Event.DEPARTURE, e.eventTime)) {
scheduleDeparture(e, car);
}
// Remove this car from carlist and eventlist
// Car carToBeRemoved = carList[fromLane].peek();
// allowedCars.remove(carToBeRemoved);
it.remove();
continue;
}
// Opposite car
if (Math.abs(car.fromLane - fromLane) == 2) {
if (fromLane != left && car.direction != left) {
// System.out.println("Checking opposite...");
if (removeDuplicateEvent(car.carID, Event.DEPARTURE, e.eventTime)) {
scheduleDeparture(e, car);
}
// System.out.println(" CarID: " + car.carID+ " is gone at " + e.eventTime);
// Remove this car from carlist and eventlist
// Car carToBeRemoved = carList[fromLane].peek();
// allowedCars.remove(carToBeRemoved);
it.remove();
continue;
}
}
// Right car
if (fromLane == (car.fromLane + 1) % 4) {
if (direction == right && car.direction == right) {
// System.out.println("Checking right...");
if (removeDuplicateEvent(car.carID, Event.DEPARTURE, e.eventTime)) {
scheduleDeparture(e, car);
}
// System.out.println(" CarID: " + car.carID+ " is gone at " + e.eventTime);
// Remove this car from carlist and eventlist
// Car carToBeRemoved = carList[fromLane].peek();
// allowedCars.remove(carToBeRemoved);
it.remove();
continue;
}
}
}
}
private boolean removeDuplicateEvent(int carID, int type, double departureTime) {
Event event;
Iterator<Event> it = eventList.iterator();
while (it.hasNext()) {
event = it.next();
if (event.carID == carID && event.type == type && departureTime < event.eventTime) {
it.remove();
// Debugging
// System.out.println("Duplicate removed => " + event);
return true;
}
}
return false;
}
///////////////////////////////////////////////////////////////////
// utility methods
private double randomInterarrivalTime(int i) {
return exponential(arrivalRate);
}
private double randomServiceTime() {
return exponential(serviceRate);
}
private double exponential(double lambda) {
return (1.0 / lambda) * (- Math.log(1.0 - RandTool.uniform()));
}
private void stats(Event e) {
if (numDepartures == 0) {
return;
}
// avgWaitTime = totalWaitTime / numDepartures[e.fromLane];
avgSysTime[e.fromLane] = sysTime[e.fromLane] / numDeparturesFromLane[e.fromLane];
}
// getter for carList
public PriorityQueue<Car>[] getCarList() {
return carList;
}
// getter for currentEvent
public Event getCurrentEvent() {
return currentEvent;
}
// end utility methods
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
// main
public static void main(String[] argv) {
int k = 4;
Function[] F = new Function[k];
for (int j = 0; j < k; j++) {
F[j] = new Function("Avg system time vs lambda, lane " + j);
}
double epsilon = 1;
double endLambda = 100;
for(double lambda = epsilon; lambda < endLambda; lambda += epsilon) {
AIMC aimc = new AIMC();
aimc.arrivalRate = lambda;
int maxDepartures = 10000;
while (aimc.numDepartures < maxDepartures) {
aimc.nextStep();
}
System.out.println("Arrival rate = " + lambda);
for (int i = 0; i < aimc.k; i++) {
// System.out.println("Average system time for lane " + i + ": " + aimc.avgSysTime[i]);
F[i].add(lambda, aimc.avgSysTime[i]);
}
}
Function.show(F[0], F[1], F[2], F[3]);
}
// end main
///////////////////////////////////////////////////////////////////
}