Skip to content

Commit

Permalink
Merge pull request #156 from BayAreaMetro/highway-relative-gaps
Browse files Browse the repository at this point in the history
Highway relative gaps by global iterations
  • Loading branch information
i-am-sijia authored Jul 30, 2024
2 parents c1ac021 + 99dbd3c commit d4735bf
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
3 changes: 1 addition & 2 deletions tm2py/acceptance/simulated.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,8 @@ def _make_simulated_maz_data(self):
self._make_taz_district_crosswalk()

return

def _make_taz_district_crosswalk(self):

def _make_taz_district_crosswalk(self):
df = self.simulated_maz_data_df[["TAZ_ORIGINAL", "DistID"]].copy()
df = df.rename(columns={"TAZ_ORIGINAL": "taz", "DistID": "district"})
self.taz_to_district_df = df.drop_duplicates().reset_index(drop=True)
Expand Down
2 changes: 1 addition & 1 deletion tm2py/components/network/create_tod_scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def run(self):
# emme_app = self._emme_manager.project(project_path)
# self._emme_manager.init_modeller(emme_app)
with self._setup():
# self._create_highway_scenarios()
self._create_highway_scenarios()
self._create_transit_scenarios()

@_context
Expand Down
13 changes: 12 additions & 1 deletion tm2py/components/network/highway/highway_assign.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,18 @@ def _get_assignment_spec(
Emme specification for SOLA traffic assignment
"""
relative_gap = self.config.relative_gap
relative_gaps = self.config.relative_gaps
# get the corresponding relative gap for the current iteration
relative_gap = None
if relative_gaps and isinstance(relative_gaps, tuple):
for item in relative_gaps:
if item["global_iteration"] == self.controller.iteration:
relative_gap = item["relative_gap"]
break
if relative_gap is None:
raise ValueError(
f"RelativeGapConfig: Must specifify a value for global iteration {self.controller.iteration}"
)
max_iterations = self.config.max_iterations
# NOTE: mazmazvol as background traffic in link.data1 ("ul1")
base_spec = {
Expand Down
29 changes: 27 additions & 2 deletions tm2py/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,19 @@ class ClassDemandConfig(ConfigItem):
factor: float = Field(default=1.0, gt=0)


@dataclass(frozen=True)
class HighwayRelativeGapConfig(ConfigItem):
"""Highway assignment relative gap parameters.
Properties:
global_iteration: global iteration number
relative_gap: relative gap
"""

global_iteration: int = Field(ge=0)
relative_gap: float = Field(gt=0)


@dataclass(frozen=True)
class HighwayClassConfig(ConfigItem):
"""Highway assignment class definition.
Expand Down Expand Up @@ -906,7 +919,7 @@ class HighwayConfig(ConfigItem):
Properties:
generic_highway_mode_code: single character unique mode ID for entire
highway network (no excluded_links)
relative_gap: target relative gap stopping criteria
relative_gaps: relative gaps for assignment convergence, specific to global iteration, see HighwayRelativeGapConfig
max_iterations: maximum iterations stopping criteria
area_type_buffer_dist_miles: used to in calculation to categorize link @areatype
The area type is determined based on the average density of nearby
Expand All @@ -924,10 +937,12 @@ class HighwayConfig(ConfigItem):
see HighwayClassConfig
capclass_lookup: index cross-reference table from the link @capclass value
to the free-flow speed, capacity, and critical speed values
interchange_nodes_file: relative path to the interchange nodes file, this is
used for calculating highway reliability
"""

generic_highway_mode_code: str = Field(min_length=1, max_length=1)
relative_gap: float = Field(ge=0)
relative_gaps: Tuple[HighwayRelativeGapConfig, ...] = Field()
max_iterations: int = Field(ge=0)
area_type_buffer_dist_miles: float = Field(gt=0)
drive_access_output_skim_path: Optional[str] = Field(default=None)
Expand Down Expand Up @@ -1399,6 +1414,16 @@ def maz_skim_period_exists(cls, value, values):
), "maz_to_maz -> skim_period -> name not found in time_periods list"
return value

@validator("highway", always=True)
def relative_gap_length(cls, value, values):
"""Validate highway.relative_gaps is a list of the same length as global iterations."""
if "run" in values:
assert len(value.relative_gaps) == (
values["run"]["end_iteration"] + 1
), f"'highway.relative_gaps must be the same length as end_iteration+1,\
that includes global iteration 0 to {values['run']['end_iteration']}'"
return value


def _load_toml(path: str) -> dict:
"""Load config from toml file at path."""
Expand Down

0 comments on commit d4735bf

Please sign in to comment.