diff --git a/Scripts/modelsystem.py b/Scripts/modelsystem.py index 4b8599b6..93384501 100644 --- a/Scripts/modelsystem.py +++ b/Scripts/modelsystem.py @@ -200,6 +200,7 @@ def assign_base_demand(self, use_fixed_transit_cost=False, is_end_assignment=Fal self._calculate_noise_areas() self.resultdata.flush() self.dtm.init_demand() + self.imptrans.average_car_impedance(impedance) return impedance def run_iteration(self, previous_iter_impedance, iteration=None): @@ -311,6 +312,7 @@ def run_iteration(self, previous_iter_impedance, iteration=None): if iteration=="last": self.ass_model.aggregate_results(self.resultdata) self._calculate_noise_areas() + self.imptrans.average_car_impedance(impedance) # Reset time-period specific demand matrices (DTM), and empty result buffer self.dtm.init_demand() diff --git a/Scripts/transform/impedance_transformer.py b/Scripts/transform/impedance_transformer.py index 1180cf62..599905d0 100644 --- a/Scripts/transform/impedance_transformer.py +++ b/Scripts/transform/impedance_transformer.py @@ -6,7 +6,47 @@ class ImpedanceTransformer: def __init__(self): - pass + self.iteration_counter = 0 + self.averaged_impedance = dict.fromkeys( + ["aht", "pt", "iht"]) + for time_period in self.averaged_impedance: + self.averaged_impedance[time_period] = dict.fromkeys( + ["time", "cost", "dist"]) + for mtx_type in self.averaged_impedance[time_period]: + self.averaged_impedance[time_period][mtx_type] = dict.fromkeys( + ["car_work", "car_leisure"]) + + def average_car_impedance(self, impedance): + """Perform averaging of car impedance matrices. + Parameters + ---------- + impedance: dict + Time period (aht/pt/iht) : dict + Type (time/cost/dist) : dict + Assignment class (car_work/transit/...) : numpy 2d matrix + Return + ------ + dict + Time period (aht/pt/iht) : dict + Type (time/cost/dist) : dict + Assignment class (car_work/transit/...) : numpy 2d matrix + """ + self.iteration_counter += 1 + n = self.iteration_counter + for time_period in impedance: + for mtx_type in impedance[time_period]: + average_imp = self.averaged_impedance[time_period][mtx_type] + for ass_class in average_imp: + imp = impedance[time_period][mtx_type][ass_class] + if n == 1: + average_imp[ass_class] = imp + else: + old_average = average_imp[ass_class] + # Method of successive averages + new_average = ((n-1)*old_average + imp) / n + average_imp[ass_class] = new_average + impedance[time_period][mtx_type][ass_class] = new_average + return impedance def transform(self, purpose, impedance): """Perform transformation from time period dependent matrices