-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalysis4.java
287 lines (232 loc) · 7.98 KB
/
Analysis4.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
import java.util.*;
import java.io.*;
public class Analysis4 {
private static List<Station> countyStations = new ArrayList<Station>();
private static int count = 0;
private static boolean[][] visited;
private static int X;
private static int Y;
public static void main(String[] args) {
for (int i = 0; i < args.length/2; i++) {
getDepartures(args[i]);
}
for (int i = args.length/2; i < args.length; i++) {
getArrivals(args[i]);
}
// System.out.println(countyStations.size());
// Station first = countyStations.get(0);
// System.out.println(first.center());
// //System.out.println(first.departures.size());
// System.out.println(first.dTaxis.size());
// System.out.println(first.aTaxis.size());
// Set<String> dPixels = first.departures.keySet();
// Set<String> aPixels = first.arrivals.keySet();
// System.out.println("departures");
// for (String pixel : dPixels) System.out.println(pixel);
// System.out.println();
// System.out.println("arrivals");
// for (String pixel : aPixels) System.out.println(pixel);
// System.out.println();
// make a copy of all the stations
List<Station> stations = new ArrayList<Station>();
stations.addAll(countyStations);
int count = 1;
int totalTaxis = 0;
int totalTrips = 0;
for (Station station : stations) {
// System.out.println("checkpoint #" + count);
int taxis = station.dTaxis.size();
totalTrips += taxis;
station.initEmpty();
// System.out.println("Before: " + taxis);
countyStations = station.minimizeDepartures(countyStations);
taxis = station.dTaxis.size();
// System.out.println("After: " + taxis);
totalTaxis += taxis;
count++;
// System.out.println();
}
// System.out.println("Total # taxis: " + totalTaxis);
System.out.println("Taxis if every trip had its own taxi: " + totalTrips);
// RESET
count = 1;
totalTaxis = 0;
for (Station station : stations) {
// System.out.println("checkpoint #" + count + ".2");
int taxis = station.dTaxis.size();
// System.out.println("Before: " + taxis);
countyStations = station.findNearby(countyStations);
taxis = station.dTaxis.size();
// System.out.println("After: " + taxis);
totalTaxis += taxis;
count++;
// System.out.println();
}
// System.out.println("Total # taxis: " + totalTaxis);
// RESET
count = 1;
totalTaxis = 0;
int totalVehMiles = 0;
int totalEmpMiles = 0;
for (Station station : countyStations) {
// TreeSet<Taxi> finalTaxis = new TreeSet<Taxi>();
countyStations = station.cycleDepartures(countyStations);
// System.out.println("checkpoint #" + count + ".3");
// System.out.println(finalTaxis.size());
// System.out.println(station.dTaxis.size());
// totalTaxis += finalTaxis.size();
totalTaxis += station.dTaxis.size();
totalVehMiles += station.totalVehicleMiles();
totalEmpMiles += station.totalEmptyMiles();
count++;
}
System.out.println("Final # taxis: " + totalTaxis);
System.out.println("Total Vehicle Miles: " + totalVehMiles);
System.out.println("Total Empty Vehicle Miles: " + totalEmpMiles);
// System.out.println("Total # taxis: " + totalTaxis);
//countyStations = first.minimizeDepartures(countyStations);
//System.out.println(first.dTaxis.size());
}
public static void getDepartures(String fileName) {
BufferedReader reader = null;
try {
String line;
reader = new BufferedReader(new FileReader(fileName));
line = reader.readLine(); // read the header line
while ((line = reader.readLine()) != null) {
String[] rawData = line.split(","); //split the data
// get the size of the trip
int riders = Integer.parseInt(rawData[17]);
int size = 0; // determine the size of the taxi
if (riders <= 2)
size = 4;
else if (riders > 2 && riders <= 6)
size = 6;
else if (riders > 6)
size = Integer.MAX_VALUE;
// if 2/6 passenger trip, save the trip info
if (size <= 6) {
// get the departing pixel
int i = Integer.parseInt(rawData[1]);
int j = Integer.parseInt(rawData[2]);
String pixel = i + ", " + j;
Trip trip = new Trip(pixel, Double.parseDouble(rawData[3]), rawData[22] + ", " + rawData[23], Double.parseDouble(rawData[24]), Double.parseDouble(rawData[18]), size);
Taxi taxi;
Taxi taxi2 = null;
if (size == 6) {
taxi = new Taxi(4);
taxi2 = new Taxi(4);
}
else taxi = new Taxi(size);
// create a station if not already created
Station station = new Station(i, j);
if (!countyStations.contains(station))
countyStations.add(station);
else {
Station s = countyStations.get(countyStations.indexOf(station));
station = s;
}
// each trip gets assigned its own taxi, and each taxi has an origin station
taxi.assignTo(station);
taxi.addTrip(trip);
// make sure to add taxi to saved station, and save where trip departs to
//station = countyStations.get(countyStations.indexOf(new Station(i, j)));
station.addDeparture(taxi, trip.destinationPixel());
if (taxi2 != null) {
taxi2.assignTo(station);
taxi2.addTrip(trip);
station.addDeparture(taxi2, trip.destinationPixel());
}
// check and see if this station is within range of any of the other stations
for (Station s : countyStations) {
if (s.withinRange(station)) {
if (!station.pixels.contains(s.center()))
station.add(s.center());
if (!s.pixels.contains(station.center()))
s.add(station.center());
}
}
}
}
}
catch(Exception e) {
e.printStackTrace();
}
finally {
if (reader != null){
try {reader.close();} catch (Exception e) {}
}
}
// System.out.println("checkpoint1");
}
public static void getArrivals(String fileName) {
BufferedReader reader = null;
try {
String line;
reader = new BufferedReader(new FileReader(fileName));
line = reader.readLine(); // read the header line
while ((line = reader.readLine()) != null) {
String[] rawData = line.split(","); //split the data
// get the size of the trip
int riders = Integer.parseInt(rawData[17]);
int size = 0; // determine the size of the taxi
if (riders <= 2)
size = 4;
else if (riders > 2 && riders <= 6)
size = 6;
else if (riders > 6)
size = Integer.MAX_VALUE;
// if 2/6 passenger trip, save the trip info
if (size <= 6) {
// get the arrival pixel
int i = Integer.parseInt(rawData[22]);
int j = Integer.parseInt(rawData[23]);
//arrival trip, taxi, station
String pixel = i + ", " + j;
Trip trip = new Trip(rawData[1] + ", " + rawData[2], Double.parseDouble(rawData[3]), pixel, Double.parseDouble(rawData[24]), Double.parseDouble(rawData[18]), size);
Taxi taxi;
Taxi taxi2 = null;
if (size == 6) {
taxi = new Taxi(4);
taxi2 = new Taxi(4);
}
else taxi = new Taxi(size);
// create a station if not already created
Station station = new Station(i, j);
if (!countyStations.contains(station))
countyStations.add(station);
else {
Station s = countyStations.get(countyStations.indexOf(station));
station = s;
}
// each trip gets assigned its own taxi, and each taxi has an origin station
taxi.addTrip(trip);
// make sure to add taxi to saved station, and save where trip arrives from
station.addArrival(taxi, trip.originPixel());
if (taxi2 != null) {
taxi2.addTrip(trip);
station.addArrival(taxi2, trip.originPixel());
}
// check and see if this station is within range of any of the other stations
for (Station s : countyStations) {
if (s.withinRange(station)) {
if (!station.pixels.contains(s.center()))
station.add(s.center());
if (!s.pixels.contains(station.center()))
s.add(station.center());
}
}
}
}
}
catch(Exception e) {
e.printStackTrace();
}
finally {
if (reader != null) {
try {reader.close();} catch (Exception e) {}
}
}
// System.out.println("checkpoint2");
}
}