-
Notifications
You must be signed in to change notification settings - Fork 14
/
MTCTM2TripTables.java
890 lines (699 loc) · 29.2 KB
/
MTCTM2TripTables.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
package com.pb.mtctm2.abm.application;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Properties;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;
import com.pb.mtctm2.abm.ctramp.TapDataManager;
import com.pb.mtctm2.abm.ctramp.TazDataManager;
import com.pb.common.calculator.MatrixDataServerIf;
import com.pb.common.datafile.NEW_CSVFileReader;
import com.pb.common.datafile.OLD_CSVFileReader;
import com.pb.common.datafile.CSVFileWriter;
import com.pb.common.datafile.TableDataSet;
import com.pb.common.matrix.Matrix;
import com.pb.common.matrix.MatrixType;
import com.pb.common.matrix.MatrixWriter;
import com.pb.common.util.ResourceUtil;
import com.pb.mtctm2.abm.ctramp.CtrampApplication;
import com.pb.mtctm2.abm.ctramp.MatrixDataServer;
import com.pb.mtctm2.abm.ctramp.MatrixDataServerRmi;
import com.pb.mtctm2.abm.ctramp.MgraDataManager;
import com.pb.mtctm2.abm.ctramp.Util;
import com.pb.mtctm2.abm.maas.ResimulateTransitPathModel;
import com.pb.mtctm2.abm.reports.SkimBuilder;
public class MTCTM2TripTables {
private static Logger logger = Logger.getLogger(MTCTM2TripTables.class);
private TableDataSet indivTripData;
private TableDataSet jointTripData;
//Some parameters
private int[] modeIndex; // an index array, dimensioned by number of total modes, returns 0=auto modes, 1=non-motor, 2=transit, 3= other, 4 AV auto modes
private int[] matrixIndex; // an index array, dimensioned by number of modes, returns the element of the matrix array to store value
//array modes: AUTO, NON-MOTORIZED, TRANSIT, OTHER
private int autoModes=0;
private int tranModes=0;
private int nmotModes=0;
private int othrModes=0;
private int avModes = 0;
//one file per time period
private int numberOfPeriods;
private String purposeName[] = {"Work","University","School","Escort","Shop","Maintenance","EatingOut","Visiting","Discretionary","WorkBased"};
// matrices are indexed by modes
private Matrix[][] matrix;
private Properties properties;
private HashMap<String, String> rbMap;
private MgraDataManager mgraManager;
private TazDataManager tazManager;
private TapDataManager tapManager;
private SandagModelStructure modelStructure;
private float[][] CBDVehicles; // an array of parked vehicles in MGRAS by period
private float[][] PNRVehicles; // an array of parked vehicles at TAPs by period
private int iteration;
private String directory;
private String matrixFileExtension = "omx";
private MatrixDataServerIf ms;
SkimBuilder skimBuilder;
private HashMap<String, Float> averageOcc3Plus; //a HashMap of average occupancies for 3+ vehicles by tour purpose
public MazSets mazSets;
public int numSkimSets;
public boolean transitResim;
public MTCTM2TripTables(String resourceBundleName, int iteration, boolean transitResim){
this.transitResim = transitResim;
rbMap = ResourceUtil.getResourceBundleAsHashMap(resourceBundleName);
properties = new Properties();
for (String key : rbMap.keySet())
properties.put(key,rbMap.get(key));
directory = properties.getProperty("Project.Directory");
numSkimSets = Integer.valueOf(properties.getProperty("utility.bestTransitPath.skim.sets"));
tazManager = TazDataManager.getInstance(rbMap);
tapManager = TapDataManager.getInstance(rbMap);
mgraManager = MgraDataManager.getInstance(rbMap);
modelStructure = new SandagModelStructure();
//Time period limits
numberOfPeriods = modelStructure.getNumberModelPeriods();
//number of modes
modeIndex = new int[modelStructure.MAXIMUM_TOUR_MODE_ALT_INDEX+1];
matrixIndex = new int[modeIndex.length];
//set the mode arrays
for(int i=1;i<modeIndex.length;++i){
if(modelStructure.getTourModeIsSovOrHov(i)){
modeIndex[i] = 0;
matrixIndex[i]= autoModes;
++autoModes;
++avModes;
}else if(modelStructure.getTourModeIsNonMotorized(i)){
modeIndex[i] = 1;
matrixIndex[i]= nmotModes;
++nmotModes;
}else if(modelStructure.getTourModeIsWalkTransit(i)|| modelStructure.getTourModeIsDriveTransit(i)){
modeIndex[i] = 2;
matrixIndex[i]= tranModes;
++tranModes;
}else{
modeIndex[i] = 3;
matrixIndex[i]= othrModes;
++othrModes;
}
}
readOccupancies();
//Initialize arrays (need for all periods, so initialize here)
CBDVehicles = new float[mgraManager.getMaxMgra()+1][numberOfPeriods];
PNRVehicles = new float[tapManager.getMaxTap()+1][numberOfPeriods];
this.iteration = iteration;
//create mazSets
mazSets = new MazSets();
//setup skim builder class
skimBuilder = new SkimBuilder(properties);
}
/**
* Read occupancies from the properties file and store in the averageOcc3Plus HashMap
*/
public void readOccupancies(){
averageOcc3Plus = new HashMap<String,Float>();
for(int i=0;i<purposeName.length;++i){
String searchString = "occ3plus.purpose." + purposeName[i];
float occupancy = new Float(properties.getProperty(searchString));
averageOcc3Plus.put(purposeName[i], occupancy);
}
}
/**
* Initialize all the matrices for the given time period.
*
* @param periodName The name of the time period.
*/
public void initializeMatrices(String periodName){
//get the taz and tap matrix sizes
int tazs = tazManager.getMaxTaz();
int taps = tapManager.getMaxTap();
//Initialize matrices; one for each mode group (auto, non-mot, tran, other, AVs)
int numberOfModes = 5;
matrix = new Matrix[numberOfModes][];
for(int i = 0; i < numberOfModes; ++ i){
String modeName;
if(i==0){
//dim TAZ to TAZ auto matrices + MAZ to MAZ matrices
matrix[i] = new Matrix[autoModes + mazSets.numSets];
for(int j=0;j<autoModes;++j){
modeName = modelStructure.getModeName(j+1);
matrix[i][j] = new Matrix(modeName+"_"+periodName,"",tazs,tazs);
mazSets.autoMatOffset = j;
}
//dim MAZ to MAZ auto matrices
for(int k=0;k<mazSets.numSets;k++){
matrix[i][mazSets.autoMatOffset + k+1] = new Matrix("MAZ_AUTO_"+(k+1)+"_"+periodName,"",mazSets.numZones[k],mazSets.numZones[k]);
}
}else if(i==1){
matrix[i] = new Matrix[nmotModes];
for(int j=0;j<nmotModes;++j){
modeName = modelStructure.getModeName(j+1+autoModes);
matrix[i][j] = new Matrix(modeName+"_"+periodName,"",tazs,tazs);
}
}else if(i==2){
matrix[i] = new Matrix[tranModes*numSkimSets];
for(int j=0;j<tranModes;++j){
for(int k=0;k<numSkimSets;++k){
modeName = modelStructure.getModeName(j+1+autoModes+nmotModes);
String setName = String.valueOf(k+1);
matrix[i][(j*numSkimSets)+k] = new Matrix(modeName+"_set"+setName+"_"+periodName,"",taps,taps);
}
}
}else if(i==3){
matrix[i] = new Matrix[othrModes];
for(int j=0;j<othrModes;++j){
modeName = modelStructure.getModeName(j+1+autoModes+nmotModes+tranModes);
matrix[i][j] = new Matrix(modeName+"_"+periodName,"",tazs,tazs);
}
}else{ //AVs
//dim TAZ to TAZ auto matrices + MAZ to MAZ matrices
matrix[i] = new Matrix[autoModes + mazSets.numSets];
for(int j=0;j<autoModes;++j){
modeName = modelStructure.getModeName(j+1); //the auto mode name
matrix[i][j] = new Matrix(modeName+"_AV_"+periodName,"",tazs,tazs);
mazSets.autoMatOffset = j;
}
//dim MAZ to MAZ auto matrices
for(int k=0;k<mazSets.numSets;k++){
matrix[i][mazSets.autoMatOffset + k+1] = new Matrix("MAZ_AUTO_AV_"+(k+1)+"_"+periodName,"",mazSets.numZones[k],mazSets.numZones[k]);
}
}
}
}
/**
* Create trip tables for all time periods and modes.
* This is the main entry point into the class; it should be called
* after instantiating the SandagTripTables object.
*
*/
public void createTripTables(){
String indivTripFile = null;
String jointTripFile = null;
//Open the individual trip file
if(transitResim){
indivTripFile = properties.getProperty(ResimulateTransitPathModel.ResimulateTransitPathIndividualOutputFileProperty);
jointTripFile = properties.getProperty(ResimulateTransitPathModel.ResimulateTransitPathJointOutputFileProperty);
}else{
indivTripFile = properties.getProperty("Results.IndivTripDataFile");
jointTripFile = properties.getProperty("Results.JointTripDataFile");
}
indivTripFile = formFileName(directory + indivTripFile, iteration);
indivTripData = openTripFile(indivTripFile);
//Open the joint trip file
jointTripFile = formFileName(directory + jointTripFile, iteration);
jointTripData = openTripFile(jointTripFile);
// connect to matrix server
connectToMatrixServer();
// add time, distance, and cost to trip files
addTripFields(indivTripData);
addTripFields(jointTripData);
// write out updated trip tables
writeTripFile(indivTripData, indivTripFile);
writeTripFile(jointTripData, jointTripFile);
//Iterate through periods so that we don't have to keep
//trip tables for all periods in memory.
for(int i=0;i<numberOfPeriods;++i){
//Initialize the matrices
initializeMatrices(modelStructure.getModelPeriodLabel(i));
//process trips
processTrips(i, indivTripData);
processTrips(i, jointTripData);
//write matrices
writeTrips(i);
}
//write the vehicles by parking-constrained MGRA
if(! transitResim){
String CBDFile = properties.getProperty("Results.CBDFile");
writeCBDFile(directory+CBDFile);
//write the vehicles by PNR lot TAP
String PNRFile = properties.getProperty("Results.PNRFile");
writePNRFile(directory+PNRFile);
}
}
/**
* Open a trip file and return the Tabledataset.
*
* @fileName The name of the trip file
* @return The tabledataset
*/
public TableDataSet openTripFile(String fileName){
logger.info("Begin reading the data in file " + fileName);
TableDataSet tripData;
try {
NEW_CSVFileReader csvFile = new NEW_CSVFileReader();
tripData = csvFile.readFile(new File(fileName));
} catch (IOException e) {
throw new RuntimeException(e);
}
logger.info("End reading the data in file " + fileName);
return tripData;
}
/**
* This is the main workhorse method in this class. It iterates over records in the trip file.
* Attributes for the trip record are read, and the trip record is accumulated in the relevant
* matrix.
*
* @param timePeriod The time period to process
* @param tripData The trip data file to process
*/
public void processTrips(int timePeriod, TableDataSet tripData){
logger.info("Begin processing trips for period "+ timePeriod);
boolean jointTour = tripData.containsColumn("num_participants");
int participantsCol = 0;
if(jointTour){
participantsCol = tripData.getColumnPosition("num_participants");
}
//iterate through the trip data and save trips in arrays
for(int i = 1; i <= tripData.getRowCount(); ++i){
int departTime = (int) tripData.getValueAt(i,"stop_period");
int period = SandagModelStructure.getModelPeriodIndex(departTime);
if(period!=timePeriod)
continue;
int originMGRA = (int) tripData.getValueAt(i, "orig_mgra");
int destinationMGRA = (int) tripData.getValueAt(i, "dest_mgra");
int tripMode = (int) tripData.getValueAt(i,"trip_mode");
int originTAZ = mgraManager.getTaz(originMGRA);
int destinationTAZ = mgraManager.getTaz(destinationMGRA);
int inbound = (int) tripData.getValueAt(i,"inbound");
//get trip distance for taz/maz level matrix decision
float tripdist = (int) tripData.getValueAt(i,"TRIP_DISTANCE");
float sampleRate = tripData.getValueAt(i,"sampleRate");
int avAvailable = (int) tripData.getValueAt(i, "avAvailable");
//transit trip - get boarding and alighting tap
int boardTap=0;
int alightTap=0;
int parkingTaz=0;
int parkingMGRA=0;
int set=0;
if(modelStructure.getTourModeIsWalkTransit(tripMode)||modelStructure.getTourModeIsDriveTransit(tripMode)){
boardTap=(int) tripData.getValueAt(i,"trip_board_tap");
alightTap = (int) tripData.getValueAt(i,"trip_alight_tap");
set = (int) tripData.getValueAt(i,"set");
}else{
parkingMGRA = (int) tripData.getValueAt(i,"parking_mgra");
}
//scale individual person trips by occupancy for vehicle trips (auto modes only)
float vehicleTrips=1;
if(modelStructure.getTourModeIsS2(tripMode) && !jointTour){
vehicleTrips = 0.5f;
}else if(modelStructure.getTourModeIsS3(tripMode) && !jointTour){
String tourPurpose = tripData.getStringValueAt(i, "tour_purpose");
tourPurpose = tourPurpose.replace(" ","");
tourPurpose = tourPurpose.replace("-", "");
float occ = averageOcc3Plus.get(tourPurpose);
vehicleTrips = 1/occ;
}
//calculate person trips for all other modes
float personTrips=1;
if(jointTour){
personTrips = (int) tripData.getValueAt(i, participantsCol);
}
//apply sample rate
vehicleTrips = vehicleTrips * 1/sampleRate;
personTrips = personTrips * 1/sampleRate;
//Store in matrix
int mode = modeIndex[tripMode];
int mat = matrixIndex[tripMode];
if(mode==0){
//look up what taz the parking mgra is in, and re-assign the trip destination to the parking taz
if(parkingMGRA>0 ){
parkingTaz = mgraManager.getTaz(parkingMGRA);
destinationTAZ = parkingTaz;
destinationMGRA = parkingMGRA; //update dest maz as well for maz to maz assignment if needed
CBDVehicles[parkingMGRA][period] = CBDVehicles[parkingMGRA][period] + vehicleTrips;
}
//is auto trip maz level or taz level
if(mazSets.isMazSetTrip(tripdist, originMGRA, destinationMGRA)) {
//maz level
int mazSet = mazSets.getZoneSet(originMGRA, destinationMGRA);
int omaz = mazSets.getNewZoneSetNum(originMGRA);
int dmaz = mazSets.getNewZoneSetNum(destinationMGRA);
if(avAvailable==0){
float value = matrix[mode][mazSets.autoMatOffset + mazSet].getValueAt(omaz, dmaz);
matrix[mode][mazSets.autoMatOffset + mazSet].setValueAt(omaz, dmaz, (value + vehicleTrips));
}else{
float value = matrix[4][mazSets.autoMatOffset + mazSet].getValueAt(omaz, dmaz);
matrix[4][mazSets.autoMatOffset + mazSet].setValueAt(omaz, dmaz, (value + vehicleTrips));
}
} else {
//taz level
if(avAvailable==0){
float value = matrix[mode][mat].getValueAt(originTAZ, destinationTAZ);
matrix[mode][mat].setValueAt(originTAZ, destinationTAZ, (value + vehicleTrips));
}else{
float value = matrix[4][mat].getValueAt(originTAZ, destinationTAZ);
matrix[4][mat].setValueAt(originTAZ, destinationTAZ, (value + vehicleTrips));
}
}
} else if (mode==1){
float value = matrix[mode][mat].getValueAt(originTAZ, destinationTAZ);
matrix[mode][mat].setValueAt(originTAZ, destinationTAZ, (value + personTrips));
} else if (mode==2){
if(boardTap==0||alightTap==0)
continue;
//store transit trips in matrices
mat = (matrixIndex[tripMode]*numSkimSets)+set;
float value = matrix[mode][mat].getValueAt(boardTap, alightTap);
matrix[mode][mat].setValueAt(boardTap, alightTap, (value + personTrips));
//Store PNR transit trips in SOV free mode skim (mode 0 mat 0)
if(modelStructure.getTourModeIsDriveTransit(tripMode)){
// add the vehicle trip portion to the trip table
if(inbound==0){ //from origin to lot (boarding tap)
int PNRTAZ = tapManager.getTazForTap(boardTap);
value = matrix[0][0].getValueAt(originTAZ, PNRTAZ);
matrix[0][0].setValueAt(originTAZ,PNRTAZ,(value+vehicleTrips));
//and increment up the array of parked vehicles at the lot
++PNRVehicles[boardTap][period];
}else{ // from lot (alighting tap) to destination
int PNRTAZ = tapManager.getTazForTap(alightTap);
value = matrix[0][0].getValueAt(PNRTAZ, destinationTAZ);
matrix[0][0].setValueAt(PNRTAZ, destinationTAZ,(value+vehicleTrips));
}
}
} else {
float value = matrix[mode][mat].getValueAt(originTAZ, destinationTAZ);
matrix[mode][mat].setValueAt(originTAZ, destinationTAZ, (value + personTrips));
}
}
logger.info("End creating trip tables for period "+ timePeriod);
}
/**
* Get the output trip table file names from the properties file,
* and write trip tables for all modes for the given time period.
*
* @param period Time period, which will be used to find the
* period time string to append to each trip table matrix file
*/
public void writeTrips(int period){
String per = modelStructure.getModelPeriodLabel(period);
String end = "_" + per;
String[] fileName = new String[5];
fileName[0] = directory + properties.getProperty("Results.AutoTripMatrix") + end;
fileName[1] = directory + properties.getProperty("Results.NMotTripMatrix") + end;
fileName[2] = directory + properties.getProperty("Results.TranTripMatrix") + end;
fileName[3] = directory + properties.getProperty("Results.OthrTripMatrix") + end;
fileName[4] = directory + properties.getProperty("Results.AutoAVTripMatrix") + end;
for(int i=0;i<fileName.length;++i) {
//don't write non-transit matrices if in transit resim mode
if(transitResim && i!=2)
continue;
writeMatricesToFile(fileName[i], matrix[i]);
/*
try {
//Delete the file if it exists
File f = new File(fileName[i]);
if(f.exists()){
logger.info("Deleting existing trip file: "+fileName[i]);
f.delete();
}
if (ms != null) ms.writeMatrixFile(fileName[i], matrix[i], mt);
else writeMatrixFile(fileName[i], matrix[i]);
} catch (Exception e)
{
logger.error("Exception caught writing " + mt.toString() + " matrix file = "
+ fileName[i] + ", for mode index = " + i, e);
throw new RuntimeException();
}
*/
}
}
/**
* Utility method to write a set of matrices to disk.
*
* @param fileName The file name to write to.
* @param m An array of matrices
*/
public void writeMatricesToFile(String fileName, Matrix[] m){
//todo - currently write one matrix to each file since there is a problem
//calling RemoteMatrixDataServer.writeMatrices from RMI if the matrix array has more than 1 matrix
for (int i=0; i<m.length; i++) {
String matFileName = fileName + "_" + m[i].getName() + "." + matrixFileExtension;
Matrix[] temp = new Matrix[1];
temp[0] = m[i];
logger.info( m[i].getName() + " has " + m[i].getRowCount() + " rows, " + m[i].getColumnCount() + " cols, and a total of " + m[i].getSum() );
logger.info("Writing " + matFileName);
ms.writeMatrixFile(matFileName, temp);
}
}
/**
* Connect to matrix server
*/
private void connectToMatrixServer()
{
// get matrix server address and port
String matrixServerAddress = properties.getProperty("RunModel.MatrixServerAddress");
int serverPort = Integer.parseInt(properties.getProperty("RunModel.MatrixServerPort"));
ms = new MatrixDataServerRmi(matrixServerAddress, serverPort, MatrixDataServer.MATRIX_DATA_SERVER_NAME);
ms.testRemote(Thread.currentThread().getName());
logger.info("connected to matrix data server");
}
/**
* Write a file of vehicles parking in parking-constrained areas by MGRA.
*
* @param fileName The name of the csv file to write to.
*/
public void writeCBDFile(String fileName){
try
{
FileWriter writer = new FileWriter(fileName);
//write header
writer.append("MGRA,");
for(int j=0;j<numberOfPeriods;++j)
writer.append(modelStructure.getModelPeriodLabel(j)+",");
writer.append("Total\n");
//iterate through mgras
for(int i =0; i< CBDVehicles.length;++i){
float totalVehicles=0;
for(int j=0;j<numberOfPeriods;++j){
totalVehicles += CBDVehicles[i][j];
}
//only write the mgra if there are vehicles parked there
if(totalVehicles>0){
writer.append(Integer.toString(i));
//iterate through periods
for(int j=0;j<numberOfPeriods;++j)
writer.append(","+Float.toString(CBDVehicles[i][j]));
writer.append(","+Float.toString(totalVehicles)+"\n");
writer.flush();
}
}
writer.flush();
writer.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
/**
* Write a file of vehicles parking in PNR lots by TAP.
*
* @param fileName The name of the csv file to write to.
*/
public void writePNRFile(String fileName){
try
{
FileWriter writer = new FileWriter(fileName);
//write header
writer.append("TAP,");
for(int j=0;j<numberOfPeriods;++j)
writer.append(modelStructure.getModelPeriodLabel(j)+",");
writer.append("Total\n");
//iterate through taps
for(int i =0; i< PNRVehicles.length;++i){
float totalVehicles=0;
for(int j=0;j<numberOfPeriods;++j){
totalVehicles += PNRVehicles[i][j];
}
//only write the tap if there are vehicles parked there
if(totalVehicles>0){
writer.append(Integer.toString(i));
//iterate through periods
for(int j=0;j<numberOfPeriods;++j)
writer.append(","+Float.toString(PNRVehicles[i][j]));
writer.append(","+Float.toString(totalVehicles)+"\n");
writer.flush();
}
}
writer.flush();
writer.close();
}catch(IOException e)
{
e.printStackTrace();
}
}
private String formFileName( String originalFileName, int iteration ) {
int lastDot = originalFileName.lastIndexOf('.');
String returnString = "";
if ( lastDot > 0 ) {
String base = originalFileName.substring( 0, lastDot );
String ext = originalFileName.substring( lastDot );
returnString = String.format( "%s_%d%s", base, iteration, ext );
}
else {
returnString = String.format( "%s_%d.csv", originalFileName, iteration );
}
return returnString;
}
/**
* @param args
*/
public static void main(String[] args) {
//command line arguments
String propertiesName = null;
int iteration=0;
boolean transitResim=false;
if (args.length == 0)
{
logger.error(String
.format("no properties file base name (without .properties extension) was specified as an argument."));
return;
} else {
propertiesName = args[0];
for (int i = 1; i < args.length; ++i)
{
if (args[i].equalsIgnoreCase("-iteration"))
{
iteration = Integer.valueOf(args[i + 1]);
}
if (args[i].equalsIgnoreCase("-transitResim"))
{
transitResim = args[i + 1].equalsIgnoreCase("TRUE");
}
}
}
//run trip table generator
MTCTM2TripTables tripTables = new MTCTM2TripTables(propertiesName, iteration, transitResim);
tripTables.createTripTables();
}
public void writeTripFile(TableDataSet table, String fileName){
logger.info("Write the trip file " + fileName);
try {
CSVFileWriter csvWriter = new CSVFileWriter();
csvWriter.writeFile(table, new File(fileName));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void addTripFields(TableDataSet table) {
//columns to add: trip_time, trip_distance, trip_cost
int rowCount = table.getRowCount();
float[] tripTime = new float[rowCount];
float[] tripDistance = new float[rowCount];
float[] tripCost = new float[rowCount];
//loop through trips and get trip attributes
for (int i = 0; i < rowCount; i++) {
int row = i+1;
SkimBuilder.TripAttributes attributes = skimBuilder.getTripAttributes(
(int) table.getValueAt(row,"orig_mgra"),
(int) table.getValueAt(row,"dest_mgra"),
(int) table.getValueAt(row,"trip_mode"),
(int) table.getValueAt(row,"trip_board_tap"),
(int) table.getValueAt(row,"trip_alight_tap"),
(int) table.getValueAt(row,"stop_period"),
((int) table.getValueAt(row,"inbound"))==1,
(int) table.getValueAt(row,"set"));
tripTime[i] = attributes.getTripTime();
tripDistance[i] = attributes.getTripDistance();
tripCost[i] = attributes.getTripCost();
}
//append data
table.appendColumn(tripTime,"TRIP_TIME");
table.appendColumn(tripDistance,"TRIP_DISTANCE");
table.appendColumn(tripCost,"TRIP_COST");
}
public class MazSets implements Serializable
{
private float maxMazAutoTripDistance;
private int[] maxMaz = new int[3];
public int[] numZones = new int[maxMaz.length];
public int numSets = maxMaz.length;
public int autoMatOffset;
public boolean intrazonalOnly = false;
public MazSets() {
//get max distance for maz to maz trips
maxMazAutoTripDistance = Float.parseFloat(properties.getProperty("Results.MAZAutoTripMatrix.TripMaxDistance"));
intrazonalOnly = Util.getBooleanValueFromPropertyMap(rbMap, "Results.MAZAutoTripMatrix.IntrazonalOnly");
TableDataSet mgraDataTable = mgraManager.getMgraTableDataSet();
int[] countiesSet1 = Util.getIntegerArrayFromPropertyMap(rbMap, "Results.MAZAutoTripMatrix.CountySet1");
int[] countiesSet2 = Util.getIntegerArrayFromPropertyMap(rbMap, "Results.MAZAutoTripMatrix.CountySet2");
int[] countiesSet3 = Util.getIntegerArrayFromPropertyMap(rbMap, "Results.MAZAutoTripMatrix.CountySet3");
//figure out the max maz value in each set
int maxMazSet1 = 0;
int maxMazSet2 = 0;
int maxMazSet3 = 0;
for(int row = 1; row< mgraDataTable.getRowCount();++row){
int mgra = (int) mgraDataTable.getValueAt(row, "MAZ");
int county = (int) mgraDataTable.getValueAt(row,mgraManager.MGRA_COUNTY_FIELD_NAME);
for(int i=0;i<countiesSet1.length;++i)
if(county == countiesSet1[i] ) //found county in set
if(mgra>maxMazSet1)
maxMazSet1=mgra;
for(int i=0;i<countiesSet2.length;++i)
if(county == countiesSet2[i] ) //found county in set
if(mgra>maxMazSet2)
maxMazSet2=mgra;
for(int i=0;i<countiesSet3.length;++i)
if(county == countiesSet3[i] ) //found county in set
if(mgra>maxMazSet3)
maxMazSet3=mgra;
}
//get the near maz to maz assignment sets
maxMaz[0] = maxMazSet1;
maxMaz[1] = maxMazSet2;
maxMaz[2] = maxMazSet3;
numZones[0] = maxMaz[0];
numZones[1] = maxMaz[1] - maxMaz[0];
numZones[2] = maxMaz[2] - maxMaz[1];
}
public int getZoneSet(int omaz, int dmaz) {
//determine if trip is in a near maz set
int oZoneSet = 0;
for(int j=0;j<maxMaz.length;++j){
if(omaz <= maxMaz[j]) {
oZoneSet = j + 1;
break;
}
}
int dZoneSet = 0;
for(int j=0;j<maxMaz.length;++j){
if(dmaz <= maxMaz[j]) {
dZoneSet = j + 1;
break;
}
}
if(oZoneSet == dZoneSet) {
return(oZoneSet);
} else {
return(0); //not an intrazonal maz set trip
}
}
//get new maz offset from the start of the zone set
public int getNewZoneSetNum(int maz) {
int zoneSet = 0;
for(int j=0;j<maxMaz.length;++j){
if(maz <= maxMaz[j]) {
zoneSet = j + 1;
break;
}
}
if (zoneSet > 1) {
return(maz - maxMaz[zoneSet-2]);
} else {
return(maz);
}
}
public boolean isMazSetTrip(float tripdist, int originMGRA, int destinationMGRA) {
int oTaz = mgraManager.getTaz(originMGRA);
int dTaz = mgraManager.getTaz(destinationMGRA);
boolean intrazonal = (oTaz==dTaz) ? true : false;
if(intrazonalOnly && intrazonal)
return true;
else if(((tripdist < maxMazAutoTripDistance)||intrazonal) & (getZoneSet(originMGRA, destinationMGRA) > 0))
return(true);
else
return(false);
}
}
}