Skip to content

Commit 4693b16

Browse files
authored
Merge pull request #47 from adf-python/adf/impl/extaction
adf/impl/extaction/のambulanceで使う部分の実装
2 parents 5a11e26 + 5b70068 commit 4693b16

File tree

4 files changed

+434
-36
lines changed

4 files changed

+434
-36
lines changed

adf_core_python/core/agent/info/world_info.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from typing import Any
1+
from typing import Any, Optional
22

3+
from rcrs_core.entities.entity import Entity
34
from rcrs_core.worldmodel.changeSet import ChangeSet
45
from rcrs_core.worldmodel.entityID import EntityID
56
from rcrs_core.worldmodel.worldmodel import WorldModel
@@ -35,3 +36,40 @@ def set_change_set(self, change_set: ChangeSet) -> None:
3536
Change set
3637
"""
3738
self._change_set = change_set
39+
40+
def get_entity(self, entity_id: EntityID) -> Optional[Entity]:
41+
"""
42+
Get the entity
43+
44+
Parameters
45+
----------
46+
entity_id : EntityID
47+
Entity ID
48+
49+
Returns
50+
-------
51+
Optional[Entity]
52+
Entity
53+
"""
54+
return self._world_model.get_entity(entity_id)
55+
56+
def get_entity_ids_of_type(self, entity_type: type[Entity]) -> list[EntityID]:
57+
"""
58+
Get the entity IDs of the specified type
59+
60+
Parameters
61+
----------
62+
entity_type : type[Entity]
63+
Entity type
64+
65+
Returns
66+
-------
67+
list[EntityID]
68+
Entity IDs
69+
"""
70+
entity_ids: list[EntityID] = []
71+
for entity in self._world_model.get_entities():
72+
if isinstance(entity, entity_type):
73+
entity_ids.append(entity.get_id())
74+
75+
return entity_ids
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
from typing import Optional, cast
2+
3+
from rcrs_core.entities.area import Area
4+
from rcrs_core.entities.blockade import Blockade
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.action.common.action_move import ActionMove
10+
from adf_core_python.core.agent.communication.message_manager import MessageManager
11+
from adf_core_python.core.agent.develop.develop_data import DevelopData
12+
from adf_core_python.core.agent.info.agent_info import AgentInfo
13+
from adf_core_python.core.agent.info.scenario_info import Mode, ScenarioInfo
14+
from adf_core_python.core.agent.info.world_info import WorldInfo
15+
from adf_core_python.core.agent.module.module_manager import ModuleManager
16+
from adf_core_python.core.agent.precompute.precompute_data import PrecomputeData
17+
from adf_core_python.core.component.extaction.ext_action import ExtAction
18+
from adf_core_python.core.component.module.algorithm.path_planning import PathPlanning
19+
20+
21+
class DefaultExtendActionMove(ExtAction):
22+
def __init__(
23+
self,
24+
agent_info: AgentInfo,
25+
world_info: WorldInfo,
26+
scenario_info: ScenarioInfo,
27+
module_manager: ModuleManager,
28+
develop_data: DevelopData,
29+
) -> None:
30+
super().__init__(
31+
agent_info, world_info, scenario_info, module_manager, develop_data
32+
)
33+
self._target_entity_id: Optional[EntityID] = None
34+
self._threshold_to_rest: int = develop_data.get_value("threshold_to_rest", 100)
35+
36+
match self.scenario_info.get_mode():
37+
case Mode.NON_PRECOMPUTE:
38+
self._path_planning: PathPlanning = cast(
39+
PathPlanning,
40+
self.module_manager.get_module(
41+
"DefaultExtendActionMove.PathPlanning",
42+
"adf_core_python.implement.module.astar_path_planning.AStarPathPlanning",
43+
),
44+
)
45+
case Mode.PRECOMPUTATION:
46+
pass
47+
case Mode.PRECOMPUTED:
48+
pass
49+
50+
def precompute(self, precompute_data: PrecomputeData) -> ExtAction:
51+
super().precompute(precompute_data)
52+
if self.get_count_precompute() > 1:
53+
return self
54+
self._path_planning.precompute(precompute_data)
55+
return self
56+
57+
def resume(self, precompute_data: PrecomputeData) -> ExtAction:
58+
super().resume(precompute_data)
59+
if self.get_count_resume() > 1:
60+
return self
61+
self._path_planning.resume(precompute_data)
62+
return self
63+
64+
def prepare(self) -> ExtAction:
65+
super().prepare()
66+
if self.get_count_prepare() > 1:
67+
return self
68+
self._path_planning.prepare()
69+
return self
70+
71+
def update_info(self, message_manager: MessageManager) -> ExtAction:
72+
super().update_info(message_manager)
73+
if self.get_count_update_info() > 1:
74+
return self
75+
self._path_planning.update_info(message_manager)
76+
return self
77+
78+
def set_target_entity_id(self, target_entity_id: EntityID) -> ExtAction:
79+
entity: Optional[Entity] = self.world_info.get_entity(target_entity_id)
80+
self._target_entity_id = None
81+
82+
if entity is None:
83+
return self
84+
85+
if isinstance(entity, Blockade):
86+
entity = self.world_info.get_entity(cast(Blockade, entity).get_position())
87+
elif isinstance(entity, Human):
88+
entity = entity.get_position()
89+
90+
if entity is None and isinstance(entity, Area):
91+
self._target_entity_id = None
92+
93+
return self
94+
95+
def calc(self) -> ExtAction:
96+
self._result = None
97+
agent: Human = cast(Human, self.agent_info.get_myself())
98+
99+
path: list[EntityID] = self._path_planning.get_path(
100+
agent.get_position(), self._target_entity_id
101+
)
102+
103+
if path is not None or len(path) != 0:
104+
self.result = ActionMove(path)
105+
106+
return self

0 commit comments

Comments
 (0)