You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
prepare() and resume() perform duplicate computation #150
While developing based on sample_road_detector.py, we noticed that prepare() and resume() perform the same computation. We believe this calculation should be performed by precompute(), not resume().
I think there may be a cleaner way to express this, but here is my attempt at organizing it. This is what I believe that the code should look like when precompute() is used correctly.
...
def precompute(self, precompute_data: PrecomputeData) -> RoadDetector:
super().precompute(precompute_data)
target_areas = self._init_target_areas()
priority_roads = self._build_priority_roads()
precompute_data.write_json_data(
{
"target_areas": list(target_areas),
"priority_roads": list(priority_roads)
},
"RoadDetector"
)
return self
def resume(self, precompute_data: PrecomputeData) -> RoadDetector:
super().resume(precompute_data)
if 1 < self.get_count_resume():
return self
data = precompute_data.read_json_data("RoadDetector")
self._target_areas = data["target_areas"]
self._priority_roads = frozenset(data["priority_roads"])
return self
def prepare(self) -> RoadDetector:
super().prepare()
if 1 < self.get_count_prepare():
return self
self._target_areas = self._init_target_areas()
self._priority_roads = self._build_priority_roads()
return self
def _init_target_areas(self) -> set[EntityID]:
"""Initialize a set of roads adjacent to buildings, refuges, and gas stations."""
return set(
entity_id
for entity in self._world_info.get_entities_of_types([Refuge, Building, GasStation])
if isinstance(entity, Building)
for entity_id in entity.get_neighbors()
if isinstance(self._world_info.get_entity(entity_id), Road)
)
def _build_priority_roads(self) -> frozenset[EntityID]:
"""Build an immutable set of roads adjacent to refuges."""
return frozenset(
entity_id
for entity in self._world_info.get_entities_of_types([Refuge])
if isinstance(entity, Refuge)
for entity_id in entity.get_neighbors()
if isinstance(self._world_info.get_entity(entity_id), Road)
)
...
Activity
NONONOexe commentedon Apr 21, 2026
I had mistakenly written
precompute()andprepare()in the the explanation, so I have rewrited them.NONONOexe commentedon Apr 22, 2026
I think there may be a cleaner way to express this, but here is my attempt at organizing it. This is what I believe that the code should look like when
precompute()is used correctly.