From 01ccbced0a17f8e1cb528d8f17de4fbb4a34d7a9 Mon Sep 17 00:00:00 2001 From: West Jens Date: Fri, 4 Feb 2022 16:15:13 +0200 Subject: [PATCH 1/3] Refactor matrix getting --- Scripts/assignment/assignment_period.py | 40 ++++++++++++------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/Scripts/assignment/assignment_period.py b/Scripts/assignment/assignment_period.py index ad406f63..2efff002 100644 --- a/Scripts/assignment/assignment_period.py +++ b/Scripts/assignment/assignment_period.py @@ -142,20 +142,7 @@ def assign(self, matrices, iteration): path_not_found = mtxs["time"][mtx_class] > 999999 mtxs[mtx_type][mtx_class][path_not_found] = 999999 # adjust impedance - mtxs["time"]["bike"] = mtxs["time"]["bike"].clip(None, 9999.) - for ass_class in ("car_work", "car_leisure"): - mtxs["time"][ass_class] = self._extract_timecost_from_gcost( - ass_class) - mtxs["time"]["transit_work"] = self._damp( - mtxs["time"]["transit_work"], - self._get_matrix("trip_part_transit_work", "fw_time")) - if iteration=="last": - mtxs["time"]["transit_leisure"] = self._damp( - mtxs["time"]["transit_leisure"], - self._get_matrix("trip_part_transit_leisure", "fw_time")) - else: - for mtx_type in mtxs: - mtxs[mtx_type]["transit_leisure"] = mtxs[mtx_type]["transit_work"] + if iteration != "last": for ass_cl in ("car_work", "car_leisure"): mtxs["cost"][ass_cl] += self.dist_unit_cost * mtxs["dist"][ass_cl] return mtxs @@ -437,12 +424,24 @@ def _get_emmebank_matrices(self, mtx_type, is_last_iteration=False): Subtype (car_work/truck/inv_time/...) : numpy 2-d matrix Matrix of the specified type """ - matrices = dict.fromkeys(self.result_mtx[mtx_type].keys()) + matrices = {} + for subtype in self.result_mtx[mtx_type]: + if is_last_iteration or subtype not in param.freight_classes: + mtx = self._get_matrix(mtx_type, subtype) + if mtx_type == "time" and subtype == "bike": + mtx = mtx.clip(None, 9999.) + if mtx_type == "time" and subtype == "transit_work": + mtx = self._damp(mtx, "transit_work") + car_modes = ("car_work", "car_leisure") + if is_last_iteration: + if mtx_type == "time" and subtype == "transit_leisure": + mtx = self._damp(mtx, "transit_leisure") + car_modes += param.freight_classes + if mtx_type == "time" and subtype in car_modes: + mtx = self._extract_timecost_from_gcost(subtype) + matrices[subtype] = mtx if not is_last_iteration: - for key in param.freight_classes: - del matrices[key] - for subtype in matrices: - matrices[subtype] = self._get_matrix(mtx_type, subtype) + matrices["transit_leisure"] = matrices["transit_work"] return matrices def _get_matrix(self, assignment_result_type, subtype): @@ -463,8 +462,9 @@ def _get_matrix(self, assignment_result_type, subtype): emme_id = self.result_mtx[assignment_result_type][subtype]["id"] return self.emme_project.modeller.emmebank.matrix(emme_id).get_numpy_data() - def _damp(self, travel_time, fw_time): + def _damp(self, travel_time, demand_type): """Reduce the impact from first waiting time on total travel time.""" + fw_time = self._get_matrix("trip_part_"+demand_type, "fw_time") wt_weight = param.waiting_time_perception_factor return travel_time + wt_weight*((5./3.*fw_time)**0.8 - fw_time) From d776f39957a60624986cbd58cbfcd936ffe6db95 Mon Sep 17 00:00:00 2001 From: West Jens Date: Mon, 7 Feb 2022 11:57:03 +0200 Subject: [PATCH 2/3] Refactor further --- Scripts/assignment/assignment_period.py | 37 +++++++++++++------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/Scripts/assignment/assignment_period.py b/Scripts/assignment/assignment_period.py index 2efff002..a540f030 100644 --- a/Scripts/assignment/assignment_period.py +++ b/Scripts/assignment/assignment_period.py @@ -133,7 +133,7 @@ def assign(self, matrices, iteration): else: raise ValueError("Iteration number not valid") - mtxs = {imp_type: self._get_emmebank_matrices(imp_type, iteration=="last") + mtxs = {imp_type: self._get_matrices(imp_type, iteration=="last") for imp_type in ("time", "cost", "dist")} # fix the emme path analysis results # (dist and cost zero if path not found) @@ -407,7 +407,7 @@ def _set_matrix(self, mtx_label, matrix, result_type=None): self.emme_project.modeller.emmebank.matrix( self.result_mtx[result_type][mtx_label]["id"]).set_numpy_data(matrix) - def _get_emmebank_matrices(self, mtx_type, is_last_iteration=False): + def _get_matrices(self, mtx_type, is_last_iteration=False): """Get all matrices of specified type. Parameters @@ -424,21 +424,18 @@ def _get_emmebank_matrices(self, mtx_type, is_last_iteration=False): Subtype (car_work/truck/inv_time/...) : numpy 2-d matrix Matrix of the specified type """ + last_iteration_classes = param.freight_classes + ("transit_leisure",) matrices = {} for subtype in self.result_mtx[mtx_type]: - if is_last_iteration or subtype not in param.freight_classes: - mtx = self._get_matrix(mtx_type, subtype) + if is_last_iteration or subtype not in last_iteration_classes: + if mtx_type == "time" and subtype in param.assignment_modes: + mtx = self._extract_timecost_from_gcost(subtype) + elif mtx_type == "time" and subtype in param.transit_classes: + mtx = self._damp(subtype) + else: + mtx = self._get_matrix(mtx_type, subtype) if mtx_type == "time" and subtype == "bike": mtx = mtx.clip(None, 9999.) - if mtx_type == "time" and subtype == "transit_work": - mtx = self._damp(mtx, "transit_work") - car_modes = ("car_work", "car_leisure") - if is_last_iteration: - if mtx_type == "time" and subtype == "transit_leisure": - mtx = self._damp(mtx, "transit_leisure") - car_modes += param.freight_classes - if mtx_type == "time" and subtype in car_modes: - mtx = self._extract_timecost_from_gcost(subtype) matrices[subtype] = mtx if not is_last_iteration: matrices["transit_leisure"] = matrices["transit_work"] @@ -460,18 +457,22 @@ def _get_matrix(self, assignment_result_type, subtype): Matrix of the specified type """ emme_id = self.result_mtx[assignment_result_type][subtype]["id"] - return self.emme_project.modeller.emmebank.matrix(emme_id).get_numpy_data() + return (self.emme_project.modeller.emmebank.matrix(emme_id) + .get_numpy_data()) - def _damp(self, travel_time, demand_type): + def _damp(self, demand_type): """Reduce the impact from first waiting time on total travel time.""" + travel_time = self._get_matrix("time", demand_type) fw_time = self._get_matrix("trip_part_"+demand_type, "fw_time") wt_weight = param.waiting_time_perception_factor return travel_time + wt_weight*((5./3.*fw_time)**0.8 - fw_time) def _extract_timecost_from_gcost(self, ass_class): - """Remove monetary cost from generalized cost.""" - # Traffic assignment produces a generalized cost matrix. - # To get travel time, monetary cost is removed from generalized cost. + """Remove monetary cost from generalized cost. + + Traffic assignment produces a generalized cost matrix. + To get travel time, monetary cost is removed from generalized cost. + """ vot_inv = param.vot_inv[param.vot_classes[ass_class]] gcost = self._get_matrix("gen_cost", ass_class) cost = self._get_matrix("cost", ass_class) From be42620e29d93dcb8fb5538841e791d86b0143de Mon Sep 17 00:00:00 2001 From: West Jens Date: Mon, 7 Feb 2022 12:12:32 +0200 Subject: [PATCH 3/3] Change function name --- Scripts/assignment/assignment_period.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Scripts/assignment/assignment_period.py b/Scripts/assignment/assignment_period.py index a540f030..7fcad91a 100644 --- a/Scripts/assignment/assignment_period.py +++ b/Scripts/assignment/assignment_period.py @@ -431,7 +431,7 @@ def _get_matrices(self, mtx_type, is_last_iteration=False): if mtx_type == "time" and subtype in param.assignment_modes: mtx = self._extract_timecost_from_gcost(subtype) elif mtx_type == "time" and subtype in param.transit_classes: - mtx = self._damp(subtype) + mtx = self._damp_travel_time(subtype) else: mtx = self._get_matrix(mtx_type, subtype) if mtx_type == "time" and subtype == "bike": @@ -460,7 +460,7 @@ def _get_matrix(self, assignment_result_type, subtype): return (self.emme_project.modeller.emmebank.matrix(emme_id) .get_numpy_data()) - def _damp(self, demand_type): + def _damp_travel_time(self, demand_type): """Reduce the impact from first waiting time on total travel time.""" travel_time = self._get_matrix("time", demand_type) fw_time = self._get_matrix("trip_part_"+demand_type, "fw_time")