Skip to content

Commit 5a61af2

Browse files
committed
docs: add search module documentation and update index
1 parent 021deb0 commit 5a61af2

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed

docs/source/hands-on/search.md

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
# サーチモジュール
2+
3+
## サーチモジュールの概要
4+
5+
今回開発するモジュールは、`KMeansPPClustering` モジュールを用いた情報探索対象決定 (`Search`) モジュールです。 クラスタリングモジュールによってエージェント間で担当地域の分割をおこない、 担当地域内からランダムに探索対象として選択します。
6+
7+
## サーチモジュールの実装の準備
8+
9+
```{note}
10+
以降の作業では、カレントディレクトリがプロジェクトのルートディレクトリであることを前提としています。
11+
```
12+
13+
まず、サーチモジュールを記述するためのファイルを作成します。
14+
15+
```bash
16+
mkdir -p src/<your_team_name>/module/search
17+
touch src/<your_team_name>/module/complex/k_means_pp_search.py
18+
```
19+
20+
次に、サーチモジュールの実装を行います。 以下のコードを `k_means_pp_search.py` に記述してください。
21+
これが今回実装するサーチモジュールの雛形になります。
22+
23+
```python
24+
import random
25+
from typing import Optional, cast
26+
27+
from rcrs_core.entities.building import Building
28+
from rcrs_core.entities.entity import Entity
29+
from rcrs_core.entities.refuge import Refuge
30+
from rcrs_core.worldmodel.entityID import EntityID
31+
32+
from adf_core_python.core.agent.communication.message_manager import MessageManager
33+
from adf_core_python.core.agent.develop.develop_data import DevelopData
34+
from adf_core_python.core.agent.info.agent_info import AgentInfo
35+
from adf_core_python.core.agent.info.scenario_info import ScenarioInfo
36+
from adf_core_python.core.agent.info.world_info import WorldInfo
37+
from adf_core_python.core.agent.module.module_manager import ModuleManager
38+
from adf_core_python.core.component.module.algorithm.clustering import Clustering
39+
from adf_core_python.core.component.module.algorithm.path_planning import PathPlanning
40+
from adf_core_python.core.component.module.complex.search import Search
41+
from adf_core_python.core.logger.logger import get_agent_logger
42+
43+
44+
class KMeansPPSearch(Search):
45+
def __init__(
46+
self,
47+
agent_info: AgentInfo,
48+
world_info: WorldInfo,
49+
scenario_info: ScenarioInfo,
50+
module_manager: ModuleManager,
51+
develop_data: DevelopData,
52+
) -> None:
53+
super().__init__(
54+
agent_info, world_info, scenario_info, module_manager, develop_data
55+
)
56+
self._result: Optional[EntityID] = None
57+
self._logger = get_agent_logger(
58+
f"{self.__class__.__module__}.{self.__class__.__qualname__}",
59+
self._agent_info,
60+
)
61+
62+
def calculate(self) -> Search:
63+
return self
64+
65+
def get_target_entity_id(self) -> Optional[EntityID]:
66+
return self._result
67+
```
68+
69+
## モジュールの登録
70+
71+
次に、作成したモジュールを登録します。
72+
以下のように`config/module.yaml`の該当箇所を変更してください
73+
74+
```yaml
75+
DefaultTacticsAmbulanceTeam:
76+
Search: src.<team-name>.module.complex.k_means_pp_search.KMeansPPSearch
77+
78+
DefaultTacticsFireBrigade:
79+
Search: src.<team-name>.module.complex.k_means_pp_search.KMeansPPSearch
80+
81+
DefaultTacticsPoliceForce:
82+
Search: src.<team-name>.module.complex.k_means_pp_search.KMeansPPSearch
83+
```
84+
85+
## モジュールの実装
86+
87+
まず、`KMeansPPClustering` モジュールを呼び出せるようにします。
88+
89+
以下のコードを`config/module.yaml`に追記してください。
90+
91+
```yaml
92+
KMeansPPSearch:
93+
Clustering: src.<team-name>.module.algorithm.k_means_pp_clustering.KMeansPPClustering
94+
```
95+
96+
次に、`KMeansPPSearch` モジュールで `KMeansPPClustering` モジュールを呼び出せるようにします。
97+
98+
以下のコードを `k_means_pp_search.py` に追記してください。
99+
100+
```python
101+
class KMeansPPSearch(Search):
102+
def __init__(
103+
self,
104+
agent_info: AgentInfo,
105+
world_info: WorldInfo,
106+
scenario_info: ScenarioInfo,
107+
module_manager: ModuleManager,
108+
develop_data: DevelopData,
109+
) -> None:
110+
super().__init__(
111+
agent_info, world_info, scenario_info, module_manager, develop_data
112+
)
113+
self._result: Optional[EntityID] = None
114+
115+
self._logger = get_agent_logger(
116+
f"{self.__class__.__module__}.{self.__class__.__qualname__}",
117+
self._agent_info,
118+
)
119+
120+
self._clustering: Clustering = cast(
121+
Clustering,
122+
module_manager.get_module(
123+
"KMeansPPSearch.Clustering",
124+
"adf_core_python.implement.module.algorithm.k_means_clustering.KMeansClustering",
125+
),
126+
)
127+
128+
self.register_sub_module(self._clustering)
129+
```
130+
131+
そして、`calculate` メソッドでクラスタリングモジュールを呼び出し、探索対象を決定します。
132+
133+
```python
134+
def calculate(self) -> Search:
135+
# 自エージェントのエンティティIDを取得
136+
me: EntityID = self._agent_info.get_entity_id()
137+
# 自エージェントが所属するクラスターのインデックスを取得
138+
allocated_cluster_index: int = self._clustering.get_cluster_index(me)
139+
# クラスター内のエンティティIDを取得
140+
cluster_entity_ids: list[EntityID] = self._clustering.get_cluster_entity_ids(
141+
allocated_cluster_index
142+
)
143+
# 乱数で選択
144+
index = random.randint(0, len(cluster_entity_ids) - 1)
145+
# 選択したエンティティIDを結果として設定
146+
self._result = cluster_entity_ids[index]
147+
148+
return self
149+
```
150+
151+
以上で、`KMeansPPClustering` モジュールを用いた `KMeansPPSearch` モジュールの実装が完了しました。
152+
153+
実行すると、各エージェントが担当地域内からランダムに探索対象を選択し、探索を行います。
154+
155+
## モジュールの改善
156+
157+
`KMeansPPSearch` モジュールは、クラスタリングモジュールを用いて担当地域内からランダムに探索対象を選択しています。
158+
そのため、以下のような問題があります。
159+
160+
- 探索対象がステップごとに変わってしまう
161+
- 目標にたどり着く前に探索対象が変わってしまうため、なかなか目標にたどり着けない
162+
- 色んなところにランダムに探索対象を選択することで、効率的な探索ができない
163+
- すでに探索したエンティティを再度探索対象として選択してしまうため、効率的な探索ができない
164+
165+
などの問題があります。
166+
167+
## 課題
168+
169+
`KMeansPPSearch` モジュールを改善し、より効率的な探索を行うモジュールを実装して見てください。
170+
171+
### 探索対象がステップごとに変わってしまう問題
172+
173+
### すでに探索したエンティティを再度探索対象として選択してしまう問題

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ ADF Core Python を始めるには、インストール手順に従い、この
4747
:caption: ハンズオン:
4848

4949
hands-on/clustering
50+
hands-on/search
5051

5152
.. toctree::
5253
:maxdepth: 1

0 commit comments

Comments
 (0)