|
| 1 | +from typing import Optional, cast |
| 2 | + |
| 3 | +from rcrs_core.connection.URN import Entity as EntityURN |
| 4 | +from rcrs_core.entities.civilian import Civilian |
| 5 | +from rcrs_core.entities.entity import Entity |
| 6 | +from rcrs_core.entities.human import Human |
| 7 | +from rcrs_core.worldmodel.entityID import EntityID |
| 8 | + |
| 9 | +from adf_core_python.core.agent.develop.develop_data import DevelopData |
| 10 | +from adf_core_python.core.agent.info.agent_info import AgentInfo |
| 11 | +from adf_core_python.core.agent.info.scenario_info import ScenarioInfo |
| 12 | +from adf_core_python.core.agent.info.world_info import WorldInfo |
| 13 | +from adf_core_python.core.agent.module.module_manager import ModuleManager |
| 14 | +from adf_core_python.core.component.module.algorithm.clustering import Clustering |
| 15 | +from adf_core_python.core.component.module.complex.human_detector import HumanDetector |
| 16 | +from adf_core_python.core.logger.logger import get_agent_logger |
| 17 | + |
| 18 | + |
| 19 | +class SampleHumanDetector(HumanDetector): |
| 20 | + def __init__( |
| 21 | + self, |
| 22 | + agent_info: AgentInfo, |
| 23 | + world_info: WorldInfo, |
| 24 | + scenario_info: ScenarioInfo, |
| 25 | + module_manager: ModuleManager, |
| 26 | + develop_data: DevelopData, |
| 27 | + ) -> None: |
| 28 | + super().__init__( |
| 29 | + agent_info, world_info, scenario_info, module_manager, develop_data |
| 30 | + ) |
| 31 | + self._clustering: Clustering = cast( |
| 32 | + Clustering, |
| 33 | + module_manager.get_module( |
| 34 | + "DefaultHumanDetector.Clustering", |
| 35 | + "adf_core_python.implement.module.algorithm.k_means_clustering.KMeansClustering", |
| 36 | + ), |
| 37 | + ) |
| 38 | + self.register_sub_module(self._clustering) |
| 39 | + |
| 40 | + self._result: Optional[EntityID] = None |
| 41 | + self._logger = get_agent_logger( |
| 42 | + f"{self.__class__.__module__}.{self.__class__.__qualname__}", |
| 43 | + self._agent_info, |
| 44 | + ) |
| 45 | + |
| 46 | + def calculate(self) -> HumanDetector: |
| 47 | + transport_human: Optional[Human] = self._agent_info.some_one_on_board() |
| 48 | + if transport_human is not None: |
| 49 | + self._result = transport_human.get_id() |
| 50 | + return self |
| 51 | + |
| 52 | + if self._result is not None: |
| 53 | + if not self._is_valid_human(self._result): |
| 54 | + self._result = None |
| 55 | + |
| 56 | + if self._result is None: |
| 57 | + self._result = self._select_target() |
| 58 | + |
| 59 | + return self |
| 60 | + |
| 61 | + def _select_target(self) -> Optional[EntityID]: |
| 62 | + if self._result is not None and self._is_valid_human(self._result): |
| 63 | + return self._result |
| 64 | + |
| 65 | + cluster_index: int = self._clustering.get_cluster_index( |
| 66 | + self._agent_info.get_entity_id() |
| 67 | + ) |
| 68 | + cluster_entities: list[Entity] = self._clustering.get_cluster_entities( |
| 69 | + cluster_index |
| 70 | + ) |
| 71 | + |
| 72 | + cluster_valid_human_entities: list[Entity] = [ |
| 73 | + entity |
| 74 | + for entity in cluster_entities |
| 75 | + if self._is_valid_human(entity.get_id()) and isinstance(entity, Civilian) |
| 76 | + ] |
| 77 | + if len(cluster_valid_human_entities) != 0: |
| 78 | + nearest_human_entity = cluster_valid_human_entities[0] |
| 79 | + nearest_distance = self._world_info.get_distance( |
| 80 | + self._agent_info.get_entity_id(), |
| 81 | + nearest_human_entity.get_id(), |
| 82 | + ) |
| 83 | + for entity in cluster_valid_human_entities: |
| 84 | + distance = self._world_info.get_distance( |
| 85 | + self._agent_info.get_entity_id(), |
| 86 | + entity.get_id(), |
| 87 | + ) |
| 88 | + if distance < nearest_distance: |
| 89 | + nearest_distance = distance |
| 90 | + nearest_human_entity = entity |
| 91 | + return nearest_human_entity.get_id() |
| 92 | + |
| 93 | + world_valid_human_entities: list[Entity] = [ |
| 94 | + entity |
| 95 | + for entity in self._world_info.get_entities_of_types([Civilian]) |
| 96 | + if self._is_valid_human(entity.get_id()) |
| 97 | + ] |
| 98 | + if len(world_valid_human_entities) != 0: |
| 99 | + nearest_human_entity = world_valid_human_entities[0] |
| 100 | + nearest_distance = self._world_info.get_distance( |
| 101 | + self._agent_info.get_entity_id(), |
| 102 | + nearest_human_entity.get_id(), |
| 103 | + ) |
| 104 | + for entity in world_valid_human_entities: |
| 105 | + distance = self._world_info.get_distance( |
| 106 | + self._agent_info.get_entity_id(), |
| 107 | + entity.get_id(), |
| 108 | + ) |
| 109 | + if distance < nearest_distance: |
| 110 | + nearest_distance = distance |
| 111 | + nearest_human_entity = entity |
| 112 | + return nearest_human_entity.get_id() |
| 113 | + |
| 114 | + return None |
| 115 | + |
| 116 | + def _is_valid_human(self, target_entity_id: EntityID) -> bool: |
| 117 | + target: Optional[Entity] = self._world_info.get_entity(target_entity_id) |
| 118 | + if target is None: |
| 119 | + return False |
| 120 | + if not isinstance(target, Human): |
| 121 | + return False |
| 122 | + hp: Optional[int] = target.get_hp() |
| 123 | + if hp is None or hp <= 0: |
| 124 | + return False |
| 125 | + buriedness: Optional[int] = target.get_buriedness() |
| 126 | + if buriedness is None: |
| 127 | + return False |
| 128 | + myself = self._agent_info.get_myself() |
| 129 | + if myself.get_urn() == EntityURN.FIRE_BRIGADE and buriedness == 0: |
| 130 | + return False |
| 131 | + if myself.get_urn() == EntityURN.AMBULANCE_TEAM and buriedness > 0: |
| 132 | + return False |
| 133 | + damage: Optional[int] = target.get_damage() |
| 134 | + if damage is None or damage == 0: |
| 135 | + return False |
| 136 | + position_entity_id: Optional[EntityID] = target.get_position() |
| 137 | + if position_entity_id is None: |
| 138 | + return False |
| 139 | + position: Optional[Entity] = self._world_info.get_entity(position_entity_id) |
| 140 | + if position is None: |
| 141 | + return False |
| 142 | + urn: EntityURN = position.get_urn() |
| 143 | + if urn == EntityURN.REFUGE or urn == EntityURN.AMBULANCE_TEAM: |
| 144 | + return False |
| 145 | + |
| 146 | + return True |
| 147 | + |
| 148 | + def get_target_entity_id(self) -> Optional[EntityID]: |
| 149 | + return self._result |
0 commit comments