Skip to content

Commit 40501b1

Browse files
committed
refactor: Update AgentInfo class to use Civilian instead of Human for entity comparison
1 parent 7bf2896 commit 40501b1

File tree

5 files changed

+76
-26
lines changed

5 files changed

+76
-26
lines changed

adf_core_python/core/agent/info/agent_info.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Any
33

44
from rcrs_core.agents.agent import Agent
5+
from rcrs_core.entities.civilian import Civilian
56
from rcrs_core.entities.entity import Entity
67
from rcrs_core.entities.human import Human
78
from rcrs_core.worldmodel.changeSet import ChangeSet
@@ -137,8 +138,8 @@ def some_one_on_board(self) -> Human | None:
137138
"""
138139
entity_id: EntityID = self.get_entity_id()
139140
for entity in self._world_model.get_entities():
140-
if isinstance(entity, Human):
141-
if entity.get_position_property() == entity_id:
141+
if isinstance(entity, Civilian):
142+
if entity.get_position() == entity_id:
142143
return entity
143144
return None
144145

adf_core_python/implement/extend_action/default_extend_action_transport.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from adf_core_python.core.agent.precompute.precompute_data import PrecomputeData
2222
from adf_core_python.core.component.extaction.ext_action import ExtAction
2323
from adf_core_python.core.component.module.algorithm.path_planning import PathPlanning
24+
from adf_core_python.core.logger.logger import get_agent_logger
2425

2526

2627
# TODO: refactor this class
@@ -38,6 +39,10 @@ def __init__(
3839
)
3940
self._target_entity_id: Optional[EntityID] = None
4041
self._threshold_to_rest: int = develop_data.get_value("threshold_to_rest", 100)
42+
self._logger = get_agent_logger(
43+
f"{self.__class__.__module__}.{self.__class__.__qualname__}",
44+
self.agent_info,
45+
)
4146

4247
match self.scenario_info.get_mode():
4348
case Mode.NON_PRECOMPUTE:
@@ -102,6 +107,7 @@ def calc(self) -> ExtAction:
102107
)
103108
transport_human: Optional[Human] = self.agent_info.some_one_on_board()
104109
if transport_human is not None:
110+
self._logger.debug(f"transport_human: {transport_human.get_id()}")
105111
self.result = self.calc_unload(
106112
agent, self._path_planning, transport_human, self._target_entity_id
107113
)
@@ -135,7 +141,7 @@ def calc_rescue(
135141

136142
target_position = human.get_position()
137143
if agent_position == target_position:
138-
if isinstance(human, Civilian) and ((human.get_buriedness() or 0) > 0):
144+
if isinstance(human, Civilian) and ((human.get_buriedness() or 0) == 0):
139145
return ActionLoad(human.get_id())
140146
else:
141147
path = path_planning.get_path(agent_position, target_position)

adf_core_python/implement/module/complex/default_human_detector.py

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Optional, cast
22

33
from rcrs_core.connection.URN import Entity as EntityURN
4+
from rcrs_core.entities.civilian import Civilian
45
from rcrs_core.entities.entity import Entity
56
from rcrs_core.entities.human import Human
67
from rcrs_core.worldmodel.entityID import EntityID
@@ -12,6 +13,7 @@
1213
from adf_core_python.core.agent.module.module_manager import ModuleManager
1314
from adf_core_python.core.component.module.algorithm.clustering import Clustering
1415
from adf_core_python.core.component.module.complex.human_detector import HumanDetector
16+
from adf_core_python.core.logger.logger import get_agent_logger
1517

1618

1719
class DefaultHumanDetector(HumanDetector):
@@ -36,6 +38,10 @@ def __init__(
3638
self.register_sub_module(self._clustering)
3739

3840
self._result: Optional[EntityID] = None
41+
self._logger = get_agent_logger(
42+
f"{self.__class__.__module__}.{self.__class__.__qualname__}",
43+
self._agent_info,
44+
)
3945

4046
def calculate(self) -> HumanDetector:
4147
transport_human: Optional[Human] = self._agent_info.some_one_on_board()
@@ -44,8 +50,11 @@ def calculate(self) -> HumanDetector:
4450
return self
4551

4652
if self._result is not None:
47-
if self._is_valid_human(self._result):
48-
self._result = self._select_target()
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()
4958

5059
return self
5160

@@ -59,28 +68,54 @@ def _select_target(self) -> Optional[EntityID]:
5968
cluster_entities: list[Entity] = self._clustering.get_cluster_entities(
6069
cluster_index
6170
)
71+
72+
self._logger.debug(
73+
f"cluster_entities: {[str(entity.get_id()) for entity in cluster_entities]}"
74+
)
75+
6276
cluster_valid_human_entities: list[Entity] = [
6377
entity
6478
for entity in cluster_entities
65-
if self._is_valid_human(entity.get_id())
79+
if self._is_valid_human(entity.get_id()) and isinstance(entity, Civilian)
6680
]
67-
if len(cluster_valid_human_entities) == 0:
68-
return None
81+
if len(cluster_valid_human_entities) != 0:
82+
nearest_human_entity: Optional[Entity] = cluster_valid_human_entities[0]
83+
nearest_distance: float = self._world_info.get_distance(
84+
self._agent_info.get_entity_id(),
85+
nearest_human_entity.get_id(),
86+
)
87+
for entity in cluster_valid_human_entities:
88+
distance: float = self._world_info.get_distance(
89+
self._agent_info.get_entity_id(),
90+
entity.get_id(),
91+
)
92+
if distance < nearest_distance:
93+
nearest_distance = distance
94+
nearest_human_entity = entity
95+
return nearest_human_entity.get_id()
6996

70-
nearest_human_entity: Optional[Entity] = None
71-
nearest_distance: float = 10**10
72-
for entity in cluster_valid_human_entities:
73-
distance: float = self._world_info.get_distance(
97+
world_valid_human_entities: list[Entity] = [
98+
entity
99+
for entity in self._world_info.get_entities_of_types([Civilian])
100+
if self._is_valid_human(entity.get_id())
101+
]
102+
if len(world_valid_human_entities) != 0:
103+
nearest_human_entity: Optional[Entity] = world_valid_human_entities[0]
104+
nearest_distance: float = self._world_info.get_distance(
74105
self._agent_info.get_entity_id(),
75-
entity.get_id(),
106+
nearest_human_entity.get_id(),
76107
)
77-
if distance < nearest_distance:
78-
nearest_distance = distance
79-
nearest_human_entity = entity
108+
for entity in world_valid_human_entities:
109+
distance: float = self._world_info.get_distance(
110+
self._agent_info.get_entity_id(),
111+
entity.get_id(),
112+
)
113+
if distance < nearest_distance:
114+
nearest_distance = distance
115+
nearest_human_entity = entity
116+
return nearest_human_entity.get_id()
80117

81-
return (
82-
nearest_human_entity.get_id() if nearest_human_entity is not None else None
83-
)
118+
return None
84119

85120
def _is_valid_human(self, target_entity_id: EntityID) -> bool:
86121
target: Optional[Entity] = self._world_info.get_entity(target_entity_id)
@@ -94,6 +129,9 @@ def _is_valid_human(self, target_entity_id: EntityID) -> bool:
94129
buriedness: Optional[int] = target.get_buriedness()
95130
if buriedness is None or buriedness > 0:
96131
return False
132+
damage: Optional[int] = target.get_damage()
133+
if damage is None or damage == 0:
134+
return False
97135
position_entity_id: Optional[EntityID] = target.get_position()
98136
if position_entity_id is None:
99137
return False

adf_core_python/implement/module/complex/default_search.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from typing import Optional, cast
22

3-
from rcrs_core.connection.URN import Entity as EntityURN
3+
from rcrs_core.entities.building import Building
44
from rcrs_core.entities.entity import Entity
5+
from rcrs_core.entities.refuge import Refuge
56
from rcrs_core.worldmodel.entityID import EntityID
67

78
from adf_core_python.core.agent.communication.message_manager import MessageManager
@@ -55,10 +56,8 @@ def update_info(self, message_manager: MessageManager) -> Search:
5556
if self.get_count_update_info() > 1:
5657
return self
5758

58-
searched_building_ids = self._world_info.get_change_set().get_changed_entities()
59-
self._unsearched_building_ids = self._unsearched_building_ids.difference(
60-
searched_building_ids
61-
)
59+
searched_building_id = self._agent_info.get_position_entity_id()
60+
self._unsearched_building_ids.discard(searched_building_id)
6261

6362
if len(self._unsearched_building_ids) == 0:
6463
self._unsearched_building_ids = self._get_search_targets()
@@ -91,8 +90,7 @@ def _get_search_targets(self) -> set[EntityID]:
9190
building_entity_ids: list[EntityID] = [
9291
entity.get_id()
9392
for entity in cluster_entities
94-
if entity.get_urn() == EntityURN.BUILDING
95-
and entity.get_urn() != EntityURN.REFUGE
93+
if isinstance(entity, Building) and not isinstance(entity, Refuge)
9694
]
9795

9896
return set(building_entity_ids)

adf_core_python/implement/tactics/default_tactics_ambulance_team.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ def think(
120120
entity_id = agent_info.get_entity_id() # noqa: F841
121121

122122
target_entity_id = self._human_detector.calculate().get_target_entity_id()
123+
self._logger.debug(
124+
f"human detector target_entity_id: {target_entity_id}",
125+
time=agent_info.get_time(),
126+
)
123127
if target_entity_id is not None:
124128
action = (
125129
self._action_transport.set_target_entity_id(target_entity_id)
@@ -131,6 +135,9 @@ def think(
131135
return action
132136

133137
target_entity_id = self._search.calculate().get_target_entity_id()
138+
self._logger.debug(
139+
f"search target_entity_id: {target_entity_id}", time=agent_info.get_time()
140+
)
134141
if target_entity_id is not None:
135142
action = (
136143
self._action_ext_move.set_target_entity_id(target_entity_id)

0 commit comments

Comments
 (0)