Skip to content

prepare() and resume() perform duplicate computation #150

Closed

Description

@NONONOexe

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().

  def resume(self, precompute_data: PrecomputeData) -> RoadDetector:
    super().resume(precompute_data)
    if self.get_count_resume() >= 2:
      return self

    self._target_areas: set[EntityID] = set()
    entities = self._world_info.get_entities_of_types([Refuge, Building, GasStation])
    for entity in entities:
      if not isinstance(entity, Building):
        continue
      for entity_id in entity.get_neighbors():
        neighbor = self._world_info.get_entity(entity_id)
        if isinstance(neighbor, Road):
          self._target_areas.add(entity_id)

    self._priority_roads = set()
    for entity in self._world_info.get_entities_of_types([Refuge]):
      if not isinstance(entity, Building):
        continue
      for entity_id in entity.get_neighbors():
        neighbor = self._world_info.get_entity(entity_id)
        if isinstance(neighbor, Road):
          self._priority_roads.add(entity_id)

    return self

  def prepare(self) -> RoadDetector:
    super().prepare()
    if self.get_count_prepare() >= 2:
      return self

    self._target_areas = set()
    entities = self._world_info.get_entities_of_types([Refuge, Building, GasStation])
    for entity in entities:
      building: Building = cast(Building, entity)
      for entity_id in building.get_neighbors():
        neighbor = self._world_info.get_entity(entity_id)
        if isinstance(neighbor, Road):
          self._target_areas.add(entity_id)

    self._priority_roads = set()
    for entity in self._world_info.get_entities_of_types([Refuge]):
      refuge: Refuge = cast(Refuge, entity)
      for entity_id in refuge.get_neighbors():
        neighbor = self._world_info.get_entity(entity_id)
        if isinstance(neighbor, Road):
          self._priority_roads.add(entity_id)

    return self

Activity

  1. NONONOexe commented on Apr 21, 2026

    @NONONOexe
    ContributorAuthor

    I had mistakenly written precompute() and prepare() in the the explanation, so I have rewrited them.

  2. NONONOexe commented on Apr 22, 2026

    @NONONOexe
    ContributorAuthor

    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)
        )
    
    ...
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions