@@ -20,6 +20,7 @@ public class InfrastructureSystem extends ArrayList<Facility> { |
//setting up parameters |
int FacilityLayers = 3; |
int FacilityNumber; |
+ boolean estimate_flag; // whether has conduct the estimation of the capacity or not |
// HashMap<Integer, ArrayList<Integer>> functioinalTree; // record all the functional support, and forms a tree |
HashMap<Integer, ArrayList<Integer>> resource_sup_downward_relationship; // illustrate the relationship between each type of resource |
HashMap<Integer, HashMap<Integer,ArrayList<Integer>>> resource_sup_upward_relationship; |
@@ -34,6 +35,7 @@ public class InfrastructureSystem extends ArrayList<Facility> { |
// read in facility data |
GeneralParameterNEncoding GE = new GeneralParameterNEncoding(); |
int total_entities = 0; |
+ this.estimate_flag = false; |
// this.functioinalTree = new HashMap<>(); |
this.totalFacilities = new ArrayList<Integer>(); |
this.resource_sup_downward_relationship = new HashMap(); |
@@ -474,17 +476,37 @@ public class InfrastructureSystem extends ArrayList<Facility> { |
|
private int findNearestGivenListofFacilities(int[] destination_type_list, int[] loc){ |
int nearest_facility = -1; |
- double nearest_dist = 1000000000.0; |
+ double min_cost = Double.POSITIVE_INFINITY; |
+ double current_cost; |
double current_dist; |
- for (int destination_type : destination_type_list) { |
- Integer[] destination_list = this.getFacilitybyType(destination_type); |
- for (Integer i : destination_list) { |
- Facility candidate = (Facility) this.getFacility(i); |
- int[] cand_loc = candidate.Location; |
- current_dist = computeDistance(loc, cand_loc); |
- if ((nearest_dist > current_dist) && (candidate.Status != 0)) { |
- nearest_dist = current_dist; |
- nearest_facility = candidate.ID; |
+ if (this.estimate_flag){ |
+ for (int destination_type : destination_type_list) { |
+ Integer[] destination_list = this.getFacilitybyType(destination_type); |
+ for (Integer i : destination_list) { |
+ Facility candidate = this.getFacility(i); |
+ int[] cand_loc = candidate.Location; |
+ current_dist = computeDistance(loc, cand_loc); |
+ double rate = (candidate.ProductionInventory)/(candidate.ProductionCapacity + candidate.unfulfilled_demand); |
+ current_cost = current_dist * rate + 2 * current_dist * (1 - rate); |
+ if ((min_cost > current_cost) && (candidate.Status != 0)) { |
+ min_cost = current_cost; |
+ nearest_facility = candidate.ID; |
+ } |
+ } |
+ } |
+ }else{ |
+ for (int destination_type : destination_type_list) { |
+ Integer[] destination_list = this.getFacilitybyType(destination_type); |
+ for (Integer i : destination_list) { |
+ Facility candidate = this.getFacility(i); |
+ int[] cand_loc = candidate.Location; |
+ current_dist = computeDistance(loc, cand_loc); |
+ double rate = 1; |
+ current_cost = current_dist * rate + 2 * current_dist * (1 - rate); |
+ if ((min_cost > current_cost) && (candidate.Status != 0)) { |
+ min_cost = current_cost; |
+ nearest_facility = candidate.ID; |
+ } |
} |
} |
} |
@@ -720,6 +742,7 @@ public class InfrastructureSystem extends ArrayList<Facility> { |
public InfrastructureSystem(String function_fac, String function_hh, String resource_sup, String fac_id, String nearby_al, String details) throws IOException { |
GeneralParameterNEncoding GE = new GeneralParameterNEncoding(); |
this.FacilityNumber = 0; |
+ this.estimate_flag = true; |
this.totalFacilities = new ArrayList<Integer>(); |
this.resource_sup_downward_relationship = new HashMap(); |
this.resource_sup_upward_relationship = new HashMap(); |
@@ -56,6 +56,8 @@ public class UrbanSystem implements Serializable { |
AggregatedResourceSecurityOutput AggFuelOutput; |
FacilityStatusOutput FacilityStatus; |
AggregatedFacilityStatusOutput AggFacilityStatus; |
+// AggregatedFacilityStatusOutput AggWaterFacilityStatus; |
+// AggregatedFacilityStatusOutput AggFuelFacilityStatus; |
|
public UrbanSystem(InfrastructureSystem I, Population P, RoadNetwork N, ActivityLocation AL, int time, int period) { |
WaterRsrOutput = new ResourceSecurityOutput("Water"); |
@@ -64,21 +66,25 @@ public class UrbanSystem implements Serializable { |
AggFuelOutput = new AggregatedResourceSecurityOutput("Fuel"); |
FacilityStatus = new FacilityStatusOutput(); |
AggFacilityStatus = new AggregatedFacilityStatusOutput(); |
+// AggWaterFacilityStatus = new AggregatedFacilityStatusOutput("Water"); |
+// AggFuelFacilityStatus = new AggregatedFacilityStatusOutput("Fuel"); |
this.RoadNetwork = N; |
this.Infrastructure = I; |
this.Population = P; |
this.AL = AL; |
this.SystemTime = time; |
this.AnalysisPeriod = period; |
+ this.startTime = time; |
this.VehicleOwnerNResource = new HashMap<>(); |
this.HHIDReference = new HashMap<>(); |
this.FakeHouseholdInfor = new HashMap<>(); |
assignALDir(AL, N); |
assignHHIDandVHID(); |
- buildFunctionalSupportToHousehold(); |
estimateInfrastructureServiceCapacity(); |
+ buildFunctionalSupportToHousehold(); |
initHouseholdState(AggWaterOutput, AggFuelOutput); |
initFacilityState(AggFacilityStatus); |
+// initFacilityState(AggWaterFacilityStatus, AggFuelFacilityStatus); |
} |
|
public UrbanSystem(InfrastructureSystem I, Population P, RoadNetwork N, ActivityLocation AL, int time, int period, int i) { |
@@ -88,13 +94,15 @@ public class UrbanSystem implements Serializable { |
AggFuelOutput = new AggregatedResourceSecurityOutput("Fuel"); |
FacilityStatus = new FacilityStatusOutput(); |
AggFacilityStatus = new AggregatedFacilityStatusOutput(); |
+// AggWaterFacilityStatus = new AggregatedFacilityStatusOutput("Water"); |
+// AggFuelFacilityStatus = new AggregatedFacilityStatusOutput("Fuel"); |
this.RoadNetwork = N; |
this.Infrastructure = I; |
this.Population = P; |
this.AL = AL; |
this.SystemTime = time; |
this.AnalysisPeriod = period; |
- this.startTime = this.SystemTime; |
+ this.startTime = time; |
this.VehicleOwnerNResource = new HashMap<>(); |
this.HHIDReference = new HashMap<>(); |
this.FakeHouseholdInfor = new HashMap<>(); |
@@ -377,6 +385,7 @@ public class UrbanSystem implements Serializable { |
public void PostTransimsSimulation(RoutingPlan RP, ActivityLocation AL, String loc) throws IOException { |
|
int t; |
+ HashMap<Integer, Double> unfulfilled_demand = new HashMap<>(); |
boolean[] trip_success = new boolean[RP.TripNumber]; |
double[] supply_amount = new double[RP.TripNumber]; |
GeneralParameterNEncoding GE = new GeneralParameterNEncoding(); |
@@ -392,7 +401,7 @@ public class UrbanSystem implements Serializable { |
if (t % GE.PopulationResourceSimulationTimestep == 0) { |
// this.Population.resourceDepletion(GE.PopulationResourceSimulationTimestep, this.SystemTime, this.WaterRsrOutput, this.FuelRsrOutput); |
this.Population.aggregatedResourceDepletion(GE.PopulationResourceSimulationTimestep, this.SystemTime, this.AggWaterOutput, this.AggFuelOutput); |
- this.Infrastructure.recordAggregatedFacilityStatus(this.SystemTime, this.AggFacilityStatus); |
+// this.Infrastructure.recordAggregatedFacilityStatus(this.SystemTime, this.AggFacilityStatus); |
// this.Infrastructure.recordFacilityStatus(this.SystemTime, this.FacilityStatus); |
// new_failed = new ArrayList<Integer>(); |
// System.out.println(TF.Seconds2HMS(this.SystemTime)); |
@@ -440,6 +449,11 @@ public class UrbanSystem implements Serializable { |
trip_success[trip] = false; |
supply_amount[trip] = 0; |
} |
+ if (unfulfilled_demand.containsKey(resrc_dest)){ |
+ unfulfilled_demand.put(resrc_dest, unfulfilled_demand.get(resrc_dest) + demand - supply_amount[trip]); |
+ }else{ |
+ unfulfilled_demand.put(resrc_dest, demand - supply_amount[trip]); |
+ } |
} else { |
// resource replenishment at origin |
if (trip_success[trip]) { |
@@ -461,7 +475,54 @@ public class UrbanSystem implements Serializable { |
} |
} |
} |
+ for (String s : this.AggFacilityStatus.keySet()){ |
+ int id = Integer.parseInt(s); |
+ int[] temp = this.AggFacilityStatus.get(s); |
+ int amount = 0; |
+ if (unfulfilled_demand.containsKey(id)){ |
+ amount = (int) (1000*unfulfilled_demand.get(id)); |
+ } |
+ temp[3] = this.SystemTime; |
+ temp[4] = temp[4] + amount; |
+ this.AggFacilityStatus.put(s, temp); |
+ } |
+// for (String s : this.AggFuelFacilityStatus.keySet()){ |
+// int id = Integer.parseInt(s); |
+// int[] temp = this.AggFuelFacilityStatus.get(s); |
+// int amount = 0; |
+// if (unfulfilled_demand.containsKey(id)){ |
+// amount = (int) (1000*unfulfilled_demand.get(id)); |
+// } |
+// temp[3] = this.SystemTime; |
+// temp[4] = temp[4] + amount; |
+// this.AggFuelFacilityStatus.put(s, temp); |
+// } |
+// for (String s : this.AggWaterFacilityStatus.keySet()){ |
+// int id = Integer.parseInt(s); |
+// int[] temp = this.AggWaterFacilityStatus.get(s); |
+// int amount = 0; |
+// if (unfulfilled_demand.containsKey(id)){ |
+// amount = (int) (1000*unfulfilled_demand.get(id)); |
+// } |
+// temp[3] = this.SystemTime; |
+// temp[4] = temp[4] + amount; |
+// this.AggWaterFacilityStatus.put(s, temp); |
+// } |
+ for (String s : this.AggWaterOutput.keySet()){ |
+ int[] infor_water = this.AggWaterOutput.get(s); |
+ int[] infor_fuel = this.AggFuelOutput.get(s); |
+ if (infor_water[2] == 0){ |
+ infor_water[4] += this.SystemTime - infor_water[3]; |
+ } |
+ infor_water[3] = this.SystemTime; |
+ this.AggWaterOutput.put(s, infor_water); |
|
+ if (infor_fuel[2] == 0){ |
+ infor_fuel[4] += this.SystemTime - infor_fuel[3]; |
+ } |
+ infor_fuel[3] = this.SystemTime; |
+ this.AggFuelOutput.put(s, infor_fuel); |
+ } |
System.out.println("Post TRANSIMS simulation complete."); |
} |
|
@@ -547,7 +608,7 @@ public class UrbanSystem implements Serializable { |
if (f.Demand[r] > 0) { |
int travel_time_estimate = (int) this.AL.computeDistance(this.AL.Location[f.ODAL[r][0]], |
this.AL.Location[f.ODAL[r][1]]) / GE.AverageTravelSpeed; |
- int start_time = f.VehicleDispatchTime[r] + this.SystemTime; |
+ int start_time = f.VehicleDispatchTime[r] + this.startTime; |
String start_departure = CT.Seconds2HMS(start_time); |
String arrive_departure = CT.Seconds2HMS(start_time + travel_time_estimate); |
String start_return = CT.Seconds2HMS(start_time |
@@ -599,7 +660,7 @@ public class UrbanSystem implements Serializable { |
if (h.Demand[r] > 0) { |
int travel_time_estimate = (int) this.AL.computeDistance(this.AL.Location[h.ODAL[r][0]], |
this.AL.Location[h.ODAL[r][1]]) / GE.AverageTravelSpeed; |
- int start_time = h.VehicleDispatchTime[r] + this.SystemTime; |
+ int start_time = h.VehicleDispatchTime[r] + this.startTime; |
String start_departure = CT.Seconds2HMS(start_time); |
String arrive_departure = CT.Seconds2HMS(start_time + travel_time_estimate); |
String start_return = CT.Seconds2HMS(start_time |
@@ -637,7 +698,7 @@ public class UrbanSystem implements Serializable { |
// + (h.Demand.length + 1) + "\t" + (h.Demand.length + 1) + "\t" + "\n"), household_file_loc); |
hh_file_records.add(hhid_fake_trips + "\t" + h.ActivityLocation + "\t" + (h.Demand.length + 1) + "\t" |
+ (h.Demand.length + 1) + "\t" + (h.Demand.length + 1) + "\t" + "\n"); |
- start_time = this.SystemTime + (int) (Math.random()* this.AnalysisPeriod); |
+ start_time = this.startTime + (int) (Math.random()* this.AnalysisPeriod); |
|
start_departure = CT.Seconds2HMS(start_time); |
arrive_departure = CT.Seconds2HMS(start_time + travel_time_estimate); |
@@ -679,7 +740,7 @@ public class UrbanSystem implements Serializable { |
if (h.Demand[r] > 0) { |
int travel_time_estimate = (int) this.AL.computeDistance(this.AL.Location[h.ODAL[r][0]], |
this.AL.Location[h.ODAL[r][1]]) / GE.AverageTravelSpeed; |
- int start_time = h.VehicleDispatchTime[r] + this.SystemTime; |
+ int start_time = h.VehicleDispatchTime[r] + this.startTime; |
String start_departure = CT.Seconds2HMS(start_time); |
String arrive_departure = CT.Seconds2HMS(start_time + travel_time_estimate); |
String start_return = CT.Seconds2HMS(start_time |
@@ -718,7 +779,7 @@ public class UrbanSystem implements Serializable { |
hh_file_records.add(hhid_fake_trips + "\t" + h.ActivityLocation + "\t" + (h.Demand.length + 1) + "\t" |
+ (h.Demand.length + 1) + "\t" + (h.Demand.length + 1) + "\t" + "\n"); |
|
- start_time = this.SystemTime + (int) (Math.random() * this.AnalysisPeriod); |
+ start_time = this.startTime + (int) (Math.random() * this.AnalysisPeriod); |
|
start_departure = CT.Seconds2HMS(start_time); |
arrive_departure = CT.Seconds2HMS(start_time + travel_time_estimate); |
@@ -797,31 +858,30 @@ public class UrbanSystem implements Serializable { |
// fake work trips |
for (int j = 0; j < number_of_fake_trips - 1; j++) { |
int start_time = (int) (Math.random() * 1800 - 900) + trip_info[6]; |
- start_time = Math.max(0, start_time); |
- start_time = Math.min(this.AnalysisPeriod, start_time); |
- |
- departure_time = CT.Seconds2HMS(start_time); |
- arrival_time = CT.Seconds2HMS(start_time + travel_time_estimate); |
+ if ((start_time > this.startTime) && (start_time < (this.startTime + this.AnalysisPeriod))){ |
+ departure_time = CT.Seconds2HMS(start_time); |
+ arrival_time = CT.Seconds2HMS(start_time + travel_time_estimate); |
|
// TW.write((hhid_fake_trips + "\t" + al + "\t" + 4 + "\t" |
// + 4 + "\t" + 4 + "\t" + "\n"), household_file_loc); |
- hh_file_records.add(hhid_fake_trips + "\t" + al + "\t" + 4 + "\t" |
- + 4 + "\t" + 4 + "\t" + "\n"); |
+ hh_file_records.add(hhid_fake_trips + "\t" + al + "\t" + 4 + "\t" |
+ + 4 + "\t" + 4 + "\t" + "\n"); |
// TW.write((vhid_fake_trips + "\t" + hhid_fake_trips + "\t" + al + "\t1\t0" + "\n"), vehicle_file_loc); |
- vh_file_records.add(vhid_fake_trips + "\t" + hhid_fake_trips + "\t" + al + "\t1\t0" + "\n"); |
+ vh_file_records.add(vhid_fake_trips + "\t" + hhid_fake_trips + "\t" + al + "\t1\t0" + "\n"); |
// TW.write((hhid_fake_trips + "\t4" + "\t1" + "\t1" + "\t2" + "\t" + vhid_fake_trips + "\t" |
// + departure_time + "\t" + origin + "\t" + arrival_time + "\t" |
// + dest + "\t0\n"), trip_file_loc); |
- trp_file_records.add(hhid_fake_trips + "\t4" + "\t1" + "\t1" + "\t2" + "\t" + vhid_fake_trips + "\t" |
- + departure_time + "\t" + origin + "\t" + arrival_time + "\t" |
- + dest + "\t0\n"); |
- count++; |
+ trp_file_records.add(hhid_fake_trips + "\t4" + "\t1" + "\t1" + "\t2" + "\t" + vhid_fake_trips + "\t" |
+ + departure_time + "\t" + origin + "\t" + arrival_time + "\t" |
+ + dest + "\t0\n"); |
+ count++; |
|
- // Record the information of the fake trips |
- this.FakeHouseholdInfor.put(vhid_fake_trips, new int[]{al, origin, dest}); |
+ // Record the information of the fake trips |
+ this.FakeHouseholdInfor.put(vhid_fake_trips, new int[]{al, origin, dest}); |
|
- vhid_fake_trips++; |
- hhid_fake_trips++; |
+ vhid_fake_trips++; |
+ hhid_fake_trips++; |
+ } |
} |
} |
} |
@@ -997,18 +1057,37 @@ public class UrbanSystem implements Serializable { |
} |
|
private void initFacilityState(AggregatedFacilityStatusOutput fac_output) { |
- int[] fac_statues; |
- |
+// GeneralParameterNEncoding GE = new GeneralParameterNEncoding(); |
+ int[] fac_statues; |
+// int type_1, type_2; |
+// if (fac_output.ResourceType == "Water"){ |
+// type_1 = GE.WATERCODE; |
+// type_2 = GE.FUELCODE; |
+// }else{ |
+// type_1 = GE.FUELCODE; |
+// type_2 = GE.WATERCODE; |
+// } |
+ |
Iterator<Facility> I = this.Infrastructure.iterator(); |
while(I.hasNext()) { |
Facility f = I.next(); |
- fac_statues = new int[5]; |
- fac_statues[0] = f.Location[0]; |
- fac_statues[1] = f.Location[1]; |
- fac_statues[2] = f.Status; |
- fac_statues[3] = 0; |
- fac_statues[4] = 0; |
- fac_output.put(Integer.toString(f.ID), fac_statues); |
+// if (f.Type/100 == type_1){ |
+ fac_statues = new int[5]; |
+ fac_statues[0] = f.Location[0]; |
+ fac_statues[1] = f.Location[1]; |
+ fac_statues[2] = f.Status; |
+ fac_statues[3] = 0; |
+ fac_statues[4] = 0; |
+ fac_output.put(Integer.toString(f.ID), fac_statues); |
+// }else if (f.Type/100 == type_2){ |
+// fac_statues = new int[5]; |
+// fac_statues[0] = f.Location[0]; |
+// fac_statues[1] = f.Location[1]; |
+// fac_statues[2] = f.Status; |
+// fac_statues[3] = 0; |
+// fac_statues[4] = 0; |
+// fac_output_2.put(Integer.toString(f.ID), fac_statues); |
+// } |
} |
} |
|
@@ -1170,14 +1249,21 @@ public class UrbanSystem implements Serializable { |
while((line = br.readLine()) != null){ |
String[] splitline = line.split("\\s"); |
String id = splitline[0]; |
+ int Id = Integer.parseInt(id); |
+ Facility f = this.Infrastructure.get(Id); |
int[] temp = new int[5]; |
for (int i = 0; i < 5; i++){ |
temp[i] = Integer.parseInt(splitline[i + 1]); |
} |
+ f.unfulfilled_demand = temp[4]; |
A.put(id, temp); |
} |
} |
|
+ public void update_Aggreagted_Results(){ |
+ this.Population.aggregatedResourceDepletion(0, this.startTime, AggWaterOutput, AggFuelOutput); |
+ } |
+ |
|
public void WriteUrbanStates(String loc) throws IOException { |
FileWriter writer = new FileWriter(loc + "Urban_Vechile_Owner.txt"); |
@@ -1220,16 +1306,23 @@ public class UrbanSystem implements Serializable { |
FileWriter vf_writer2 = new FileWriter(vehicle_file2); |
List<String> vh_file_records2 = new ArrayList<String>(); |
|
+// String casename2 = "Output_Water_Facility.txt"; |
+// String results_file_loc2 = loc + casename2; |
+// File vehicle_file2 = new File(results_file_loc2); |
+// FileWriter vf_writer2 = new FileWriter(vehicle_file2); |
+// List<String> vh_file_records2 = new ArrayList<String>(); |
+ |
+// String casename3 = "Output_Fuel_Facility.txt"; |
+// String results_file_loc3 = loc + casename3; |
+// File vehicle_file3 = new File(results_file_loc3); |
+// FileWriter vf_writer3 = new FileWriter(vehicle_file3); |
+// List<String> vh_file_records3 = new ArrayList<String>(); |
+ |
vh_file_records.add("ID\tLocation_X\tLocation_Y\tCurrentState\tResourceLostTime\tCurrentTime\n"); |
Iterator<String> I = this.AggWaterOutput.keySet().iterator(); |
while(I.hasNext()){ |
String id = I.next(); |
int[] infor = this.AggWaterOutput.get(id); |
- if (infor[2] == 0){ |
- infor[4] += t - infor[3]; |
- } |
- infor[3] = t; |
- this.AggWaterOutput.put(id, infor); |
vh_file_records.add(id + "\t" + infor[0] + "\t" + infor[1] + "\t" + infor[2] + "\t" + infor[3] + "\t" + infor[4] + "\n"); |
} |
for (String record: vh_file_records) { |
@@ -1242,11 +1335,6 @@ public class UrbanSystem implements Serializable { |
while(II.hasNext()){ |
String id = II.next(); |
int[] infor = this.AggFuelOutput.get(id); |
- if (infor[2] == 0){ |
- infor[4] += t - infor[3]; |
- } |
- infor[3] = t; |
- this.AggFuelOutput.put(id, infor); |
vh_file_records1.add(id + "\t" + infor[0] + "\t" + infor[1] + "\t" + infor[2] + "\t" + infor[3] + "\t" + infor[4] + "\n"); |
} |
for (String record: vh_file_records1) { |
@@ -1259,11 +1347,6 @@ public class UrbanSystem implements Serializable { |
while(III.hasNext()){ |
String id = III.next(); |
int[] infor = this.AggFacilityStatus.get(id); |
- if (infor[2] == 0){ |
- infor[4] += t - infor[3]; |
- } |
- infor[3] = t; |
- this.AggFacilityStatus.put(id, infor); |
vh_file_records2.add(id + "\t" + infor[0] + "\t" + infor[1] + "\t" + infor[2] + "\t" + infor[3] + "\t" + infor[4] + "\n"); |
} |
for (String record: vh_file_records2) { |
@@ -1271,6 +1354,30 @@ public class UrbanSystem implements Serializable { |
} |
vf_writer2.close(); |
|
+// vh_file_records2.add("ID\tLocation_X\tLocation_Y\tCurrentState\tResourceLostTime\tCurrentTime\n"); |
+// Iterator<String> III = this.AggWaterFacilityStatus.keySet().iterator(); |
+// while(III.hasNext()){ |
+// String id = III.next(); |
+// int[] infor = this.AggWaterFacilityStatus.get(id); |
+// vh_file_records2.add(id + "\t" + infor[0] + "\t" + infor[1] + "\t" + infor[2] + "\t" + infor[3] + "\t" + infor[4] + "\n"); |
+// } |
+// for (String record: vh_file_records2) { |
+// vf_writer2.write(record); |
+// } |
+// vf_writer2.close(); |
+// |
+// vh_file_records3.add("ID\tLocation_X\tLocation_Y\tCurrentState\tResourceLostTime\tCurrentTime\n"); |
+// Iterator<String> IV = this.AggFuelFacilityStatus.keySet().iterator(); |
+// while(IV.hasNext()){ |
+// String id = IV.next(); |
+// int[] infor = this.AggFuelFacilityStatus.get(id); |
+// vh_file_records3.add(id + "\t" + infor[0] + "\t" + infor[1] + "\t" + infor[2] + "\t" + infor[3] + "\t" + infor[4] + "\n"); |
+// } |
+// for (String record: vh_file_records3) { |
+// vf_writer3.write(record); |
+// } |
+// vf_writer3.close(); |
+ |
System.out.println("Write resource security output files FINISHED."); |
} |
|