-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStation.java
455 lines (376 loc) · 13.3 KB
/
Station.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import java.util.*;
public class Station {
int cx; // xPixel around which station is formed
int cy; // yPixel around which station is formed
List<String> pixels;
TreeSet<Taxi> dTaxis; // taxis that depart from this station, and must be returned at the end of the day
TreeSet<Taxi> aTaxis; // taxis that arrive to this station at any point in time
Map<String, List<Double>> arrivals;
Map<String, List<Double>> departures;
double emptyMiles;
public Station(int i, int j) {
cx = i;
cy = j;
dTaxis = new TreeSet<Taxi>();
aTaxis = new TreeSet<Taxi>();
pixels = new ArrayList<String>();
pixels.add(new String(i + ", " + j));
arrivals = new HashMap<String, List<Double>>();
departures = new HashMap<String, List<Double>>();
}
public void initEmpty() {
for (Taxi d : dTaxis) {
emptyMiles += d.vehicleMiles();
}
}
private TreeSet<Taxi> cycleDepartures() {
TreeMap<Double, TreeSet<Taxi>> returnTimes = new TreeMap<Double, TreeSet<Taxi>>();
TreeSet<Taxi> fTaxis = new TreeSet<Taxi>();
// at the beginning, each taxi only has one trip, so update all the return times and add them to list
for (Taxi d : dTaxis) {
// if taxi is already being used by another county on its return
if (d.returnTime() > Double.NEGATIVE_INFINITY) {
TreeSet<Taxi> returns = returnTimes.get(d.returnTime());
if (returns == null) {
returns = new TreeSet<Taxi>();
}
returns.add(d);
returnTimes.put(d.returnTime(), returns);
// System.out.println("checkpoint");
}
// if taxis return hasnt been accounted for yet
else {
List<Trip> trips = d.trips();
Trip trip = trips.get(0);
// return time = arrival time + the time it takes to make the trip back
double returnTime = d.endTime() + (60 * 1.2 * trip.tripMiles() / 30);
// update the departure taxi
d.updateReturn(returnTime);
// d.updateEmptyMiles(trip.tripMiles());
// update list of return times
TreeSet<Taxi> returns = returnTimes.get(d.returnTime());
if (returns == null) {
returns = new TreeSet<Taxi>();
}
returns.add(d);
returnTimes.put(d.returnTime(), returns);
}
}
// go through departing taxis in ascending order
for (Taxi d : dTaxis) {
// find the times <= this taxis departure time
NavigableMap<Double, TreeSet<Taxi>> times = returnTimes.headMap(d.startTime(), true);
Set<Double> retTimes = times.descendingKeySet();
Taxi current = null; // marker variable
// go through each time in descending order
for (double retTime : retTimes) {
// find the taxi associated with return time
NavigableSet<Taxi> retTaxis = returnTimes.get(retTime);
retTaxis = retTaxis.descendingSet();
for (Taxi t : retTaxis) {
// find a taxi that is the proper size
if (t.size() >= d.size()) {
d.combine(t); // combine the taxis
current = t; // mark this taxi
fTaxis.remove(t); // remove combined taxi from final list of departing taxis
break;
}
}
if (current != null) break;
}
// if a returning taxi was combined
if (current != null) {
TreeSet<Taxi> currentTaxis = returnTimes.get(current.returnTime());
currentTaxis.remove(current);
if (currentTaxis.size() == 0) returnTimes.remove(current.returnTime());
}
// add this taxi to the final list of departing taxis
fTaxis.add(d);
// update list of return times
TreeSet<Taxi> rTaxis;
if (returnTimes.containsKey(d.returnTime()))
rTaxis = returnTimes.get(d.returnTime());
else
rTaxis = new TreeSet<Taxi>();
rTaxis.add(d);
returnTimes.put(d.returnTime(), rTaxis);
}
return fTaxis;
}
public List<Station> cycleDepartures(List<Station> countyStations) {
dTaxis = cycleDepartures();
return countyStations;
}
private Double getAcceptableArrival(String pixel, double dTime, double offset) {
List<Double> aTimes = arrivals.get(pixel);
for (double time : aTimes) {
// if within the acceptable range
if (time <= dTime && time >= (dTime - offset)) {
return time;
}
}
return null;
}
private Double getAcceptableNearbyArrival(Station nStation, String pixel, double dTime, double offset) {
List<Double> aTimes = nStation.arrivals.get(pixel);
// System.out.println(pixel);
// System.out.println(dTime);
// System.out.println("Size: "+ aTimes.size());
// System.out.println("offset: " + offset);
for (double time : aTimes) {
// System.out.println("dTime: " + dTime + " vs aTime: " + time);
// if within the acceptable range
if (time <= dTime && time >= (dTime - offset)) {
// System.out.println();
return time;
}
}
// System.out.println();
return null;
}
public List<Station> findNearby(List<Station> countyStations) {
Set<String> dPixels = departures.keySet();
// System.out.println(dPixels.size());
// for each pixel to which we depart
for (String pixel : dPixels) {
// check the nearby pixels / get the nearby station
for (String nPixel : pixels) {
String[] coord = nPixel.split(", ");
int i = Integer.parseInt(coord[0]);
int j = Integer.parseInt(coord[1]);
Station nStation = null;
if (countyStations.contains(new Station(i, j))) {
// find endpoint station
nStation = countyStations.get(countyStations.indexOf(new Station(i, j)));
}
else continue;
// System.out.println("checkpoint: "+ pixel + " vs " + nPixel);
// System.out.println("Times:");
// System.out.println("=======");
// if a nearby station has an arrival from the same pixel
if (nStation.arrivals.containsKey(pixel)) {
// System.out.println("contains pixel as arrival");
// compare the departure and arrival times to see if any can be deleted
for (double dTime : departures.get(pixel)) {
Double time = getAcceptableNearbyArrival(nStation, pixel, dTime, 600-timeTo(nPixel));
// if an acceptable arrival has been found
if (time != null) {
// System.out.println("checkpoint2");
//find departing taxi to be deleted
Taxi d = null;
for (Taxi taxi : nStation.dTaxis) {
if (taxi.startTime() == dTime) {
d = taxi;
break;
}
}
if (d == null) continue;
// delete the arrival time from nearby station, so that it won't be found again
List<Double> aTimes = nStation.arrivals.get(pixel);
aTimes.remove(time);
nStation.arrivals.put(pixel, aTimes);
// if intracounty trip
coord = pixel.split(", ");
if (countyStations.contains(new Station(Integer.parseInt(coord[0]), Integer.parseInt(coord[1])))) {
// find endpoint station
Station dest = countyStations.get(countyStations.indexOf(new Station(Integer.parseInt(coord[0]), Integer.parseInt(coord[1]))));
// delete the previously expected arrival time in the endpoint station (may or may not delete this)
List<Double> times = dest.arrivals.get(new String(cx + ", " + cy));
//if (times != null) {
times.remove(d.endTime());
dest.arrivals.put(new String(cx + ", " + cy), times);
//}
// delete previously expected arrival taxi in endpoint station
Taxi a = null;
for (Taxi aTaxi : dest.aTaxis) {
if (aTaxi.startTime() == dTime) {
a = aTaxi;
break;
}
}
dest.aTaxis.remove(a);
// find the dTaxi that corresponds with arrival to this station
a = null;
for (Taxi dTaxi : dest.dTaxis) {
if (dTaxi.endTime() == time) {
a = dTaxi;
break;
}
}
// update the taxis return time and empty miles
if (a != null) {
a.updateReturn(d.endTime());
a.updateEmptyMiles(distanceTo(nStation)); //taxi is empty between the two stations before making the trip
// update object
dest.dTaxis.remove(a);
dest.dTaxis.add(a);
}
// update empty miles in other station
dest.emptyMiles = dest.emptyMiles - d.vehicleMiles(); // works because each taxi still only has one trip
}
// delete the departure in nearby station
nStation.dTaxis.remove(d);
nStation.emptyMiles = nStation.emptyMiles - d.vehicleMiles();
}
}
}
}
}
return countyStations;
}
public List<Station> minimizeDepartures(List<Station> countyStations) {
// offset = 600 - timeTo station
Set<String> dPixels = departures.keySet();
for (String pixel : dPixels) {
if (arrivals.containsKey(pixel)) {
for (double dTime : departures.get(pixel)) {
Double time = getAcceptableArrival(pixel, dTime, 600);
// if an acceptable arrival has been found
if (time != null) {
//find departing taxi to be deleted
Taxi d = null;
for (Taxi taxi : dTaxis) {
if (taxi.startTime() == dTime) {
d = taxi;
break;
}
}
if (d == null) continue;
// delete the arrival time from this station, so that it won't be found again
List<Double> aTimes = arrivals.get(pixel);
aTimes.remove(time);
arrivals.put(pixel, aTimes);
// if intracounty trip
String[] coord = pixel.split(", ");
if (countyStations.contains(new Station(Integer.parseInt(coord[0]), Integer.parseInt(coord[1])))) {
// find endpoint station
Station dest = countyStations.get(countyStations.indexOf(new Station(Integer.parseInt(coord[0]), Integer.parseInt(coord[1]))));
// delete the previously expected arrival time in the endpoint station (may or may not delete this)
List<Double> times = dest.arrivals.get(new String(cx + ", " + cy));
//if (times != null) {
times.remove(d.endTime());
dest.arrivals.put(new String(cx + ", " + cy), times);
//}
// delete previously expected arrival taxi in endpoint station
Taxi a = null;
for (Taxi aTaxi : dest.aTaxis) {
if (aTaxi.startTime() == dTime) {
a = aTaxi;
break;
}
}
if (a != null) dest.aTaxis.remove(a);
// find the dTaxi that corresponds with arrival to this station
a = null;
for (Taxi dTaxi : dest.dTaxis) {
if (dTaxi.endTime() == time) {
a = dTaxi;
break;
}
}
// update the taxis return time
if (a != null) {
a.updateReturn(d.endTime());
// System.out.println("checkpoint");
// update object
dest.dTaxis.remove(a);
dest.dTaxis.add(a);
}
// update empty miles in other station
dest.emptyMiles = dest.emptyMiles - d.vehicleMiles(); // works because every taxi only has one trip
}
// delete the departure in this station
dTaxis.remove(d);
emptyMiles = emptyMiles - d.vehicleMiles();
}
}
}
//System.out.println("checkpoint");
}
return countyStations;
}
public double timeTo(String pixel) {
String[] coord = pixel.split(", ");
int i = Integer.parseInt(coord[0]);
int j = Integer.parseInt(coord[1]);
double cartesianDistance = Math.sqrt(Math.pow(cx-i, 2) + Math.pow(cy-j, 2));
double time = 60 * 1.2 * cartesianDistance / 30; // returns time in minutes
return time*60; // return time in seconds
}
public double distanceTo(Station nStation) {
double cartesianDistance = Math.sqrt(Math.pow(cx-nStation.cx, 2) + Math.pow(cy-nStation.cy, 2));
return cartesianDistance * 1.2;
}
public boolean withinRange(int i, int j) {
// if (i < 0 || j < 0) throw new OutOfBoundsException();
double cartesianDistance = Math.sqrt(Math.pow(cx-i, 2) + Math.pow(cy-j, 2));
double time = 60 * 1.2 * cartesianDistance / 30;
return (time <= 10); // radius of circle = 2.5min, so that diameter = 5min
}
public boolean withinRange(Station that) {
return withinRange(that.cx, that.cy);
}
public boolean contains(int i, int j) {
String pixel = i + ", " + j;
return pixels.contains(pixel);
}
public boolean equals(Object obj) {
if (obj instanceof Station) {
Station that = (Station) obj;
return (cx == that.cx && cy == that.cy);
}
return false;
}
// make sure to check that it is within range, before pixel gets added to group
public void add(int i, int j) {
String pixel = i + ", " + j;
pixels.add(pixel);
}
public void add(String pixel) {
pixels.add(pixel);
}
public void addDeparture(Taxi taxi, String pixel) {
dTaxis.add(taxi);
List<Double> dTimes;
if (departures.containsKey(pixel))
dTimes = departures.get(pixel);
else
dTimes = new ArrayList<Double>();
dTimes.add(taxi.startTime());
departures.put(pixel, dTimes);
}
public void addArrival(Taxi taxi, String pixel) {
aTaxis.add(taxi);
List<Double> aTimes;
if (arrivals.containsKey(pixel))
aTimes = arrivals.get(pixel);
else
aTimes = new ArrayList<Double>();
aTimes.add(taxi.endTime());
arrivals.put(pixel, aTimes);
}
public int size() {
return pixels.size();
}
public String center() {
return new String(cx + ", " + cy);
}
public double totalVehicleMiles() {
double vehicleMiles = 0;
for (Taxi t : dTaxis) {
vehicleMiles += t.vehicleMiles();
}
return vehicleMiles;
}
public double totalEmptyMiles() {
return emptyMiles;
}
public static void main(String[] args) {
Station test = new Station(3, 4);
System.out.println(test.withinRange(Integer.parseInt(args[0]), Integer.parseInt(args[1])));
System.out.println(test.withinRange(3+1, 4));
System.out.println(test.withinRange(3, 4+1));
System.out.println(test.withinRange(3, 4-1));
System.out.println(test.withinRange(3-1, 4));
}
}