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