Skip to content

Commit 7f8a6b4

Browse files
committed
fix: Improve documentation with additional comments and examples for clustering and search modules
1 parent f32e24c commit 7f8a6b4

File tree

5 files changed

+52
-17
lines changed

5 files changed

+52
-17
lines changed

docs/source/hands-on/clustering.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,13 @@ class KMeansPPClustering(Clustering):
6969
super().__init__(
7070
agent_info, world_info, scenario_info, module_manager, develop_data
7171
)
72+
# ロガーの取得
7273
self._logger = get_logger(f"{self.__class__.__name__}")
7374

7475
# クラスター数の設定
7576
self._cluster_number: int = 1
7677
match agent_info.get_myself().get_urn():
78+
# エージェントのクラスに応じてクラスター数を設定
7779
case EntityURN.AMBULANCE_TEAM:
7880
self._cluster_number = scenario_info.get_value(
7981
ScenarioInfoKeys.SCENARIO_AGENTS_AT,
@@ -271,6 +273,7 @@ class KMeansPPClustering(Clustering):
271273
np.ndarray
272274
エージェントとクラスターの対応付け結果
273275
"""
276+
# エージェントの位置のリストを取得
274277
agent_positions = np.array(
275278
[
276279
[agent.get_x(), agent.get_y()]
@@ -279,10 +282,12 @@ class KMeansPPClustering(Clustering):
279282
]
280283
)
281284

285+
# エージェントとクラスターの距離行列を計算
282286
agent_positions = agent_positions.reshape(-1, 2)
283287
cost_matrix = np.linalg.norm(
284288
agent_positions[:, np.newaxis] - cluster_positions, axis=2
285289
)
290+
# ハンガリアンアルゴリズムによりエージェントとクラスターの対応付けを行う
286291
_, col_ind = linear_sum_assignment(cost_matrix)
287292
return col_ind
288293
```
@@ -329,7 +334,7 @@ python main.py
329334

330335
このままだと、クラスタリング結果がわかりにくいので、クラスタリング結果を地図上に表示してみましょう。
331336

332-
{download}`クラスターの可視化用スクリプト <./../download/cluster_plot.zip>`をダウンロードして解凍し、`main.py`の以下の部分に
337+
{download}`クラスターの可視化用スクリプト <./../download/cluster_plot.zip>`をダウンロードして解凍し、中の`main.py`の以下の部分に
333338

334339
```python
335340
# クラスタリング結果
@@ -348,6 +353,7 @@ clusters = [[257, 259, 262, 263, 270, 278, 280, 297, 336, 913, 914, 915, 916, 91
348353
貼り付けたら、以下のコマンドを実行してください。
349354

350355
```bash
356+
pip install -r requirements.txt
351357
python main.py
352358
```
353359

docs/source/hands-on/search.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ class KMeansPPSearch(Search):
125125
self.register_sub_module(self._clustering)
126126
```
127127

128-
そして、`calculate` メソッドでクラスタリングモジュールを呼び出し、探索対象を決定します。
128+
そして、`calculate` メソッドでクラスタリングモジュールを呼び出し、探索対象を決定するように変更します。
129+
130+
以下のコードを `k_means_pp_search.py` に追記してください。
129131

130132
```python
131133
def calculate(self) -> Search:
@@ -141,6 +143,9 @@ class KMeansPPSearch(Search):
141143
if cluster_entity_ids:
142144
self._result = random.choice(cluster_entity_ids)
143145
146+
# ログ出力
147+
self._logger.info(f"Target entity ID: {self._result}")
148+
144149
return self
145150
```
146151

@@ -185,6 +190,10 @@ python main.py
185190
ここに上げた問題以外にも、改善すべき点が存在すると思うので、それを改善していただいても構いません。
186191
```
187192

193+
```{warning}
194+
プログラム例のプログラムにも一部問題があるので、余裕があったら修正してみてください。
195+
```
196+
188197
### 探索対象がステップごとに変わってしまう問題
189198

190199
```{admonition} 方針のヒント

docs/source/tutorial/agent/agent_control.md

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -235,20 +235,19 @@ my_entity_id: EntityID = self._agent_info.get_entity_id()
235235
```python
236236
from typing import Optional
237237

238-
from rcrs_core.worldmodel.entityID import EntityID
239-
from rcrs_core.entities.civilian import Civilian
240-
from rcrs_core.entities.entity import Entity
241-
242238
from adf_core_python.core.agent.develop.develop_data import DevelopData
243239
from adf_core_python.core.agent.info.agent_info import AgentInfo
244240
from adf_core_python.core.agent.info.scenario_info import ScenarioInfo
245241
from adf_core_python.core.agent.info.world_info import WorldInfo
246242
from adf_core_python.core.agent.module.module_manager import ModuleManager
247243
from adf_core_python.core.component.module.complex.human_detector import HumanDetector
248244
from adf_core_python.core.logger.logger import get_agent_logger
245+
from rcrs_core.entities.civilian import Civilian
246+
from rcrs_core.entities.entity import Entity
247+
from rcrs_core.worldmodel.entityID import EntityID
249248

250249

251-
class SampleHumanDetector(HumanDetector):
250+
class FireBrigadeHumanDetector(HumanDetector):
252251
def __init__(
253252
self,
254253
agent_info: AgentInfo,
@@ -271,44 +270,56 @@ class SampleHumanDetector(HumanDetector):
271270
def calculate(self) -> HumanDetector:
272271
"""
273272
行動対象を決定する
274-
273+
275274
Returns
276275
-------
277276
HumanDetector: 自身のインスタンス
278277
"""
278+
# 自分自身のEntityIDを取得
279279
me: EntityID = self._agent_info.get_entity_id()
280+
# すべてのCivilianを取得
280281
civilians: list[Entity] = self._world_info.get_entities_of_types(
281282
[
282283
Civilian,
283284
]
284285
)
285-
286+
287+
# 最も近いCivilianを探す
286288
nearest_civilian: Optional[EntityID] = None
287289
nearest_distance: Optional[float] = None
288290
for civilian in civilians:
291+
# civilianがCivilianクラスのインスタンスでない場合はスキップ
289292
if not isinstance(civilian, Civilian):
290293
continue
291-
294+
295+
# civilianのHPが0以下の場合はすでに死んでしまっているのでスキップ
292296
if civilian.get_hp() <= 0:
293297
continue
294-
298+
299+
# civilianの埋没度が0以下の場合は掘り起こす必要がないのでスキップ
295300
if civilian.get_buriedness() <= 0:
296301
continue
297-
302+
303+
# 自分自身との距離を計算
298304
distance: float = self._world_info.get_distance(me, civilian.get_id())
299-
305+
306+
# 最も近いCivilianを更新
300307
if nearest_distance is None or distance < nearest_distance:
301308
nearest_civilian = civilian.get_id()
302309
nearest_distance = distance
303-
310+
311+
# 計算結果を格納
304312
self._result = nearest_civilian
305313

314+
# ロガーに出力
315+
self._logger.info(f"Target: {self._result}")
316+
306317
return self
307318

308319
def get_target_entity_id(self) -> Optional[EntityID]:
309320
"""
310321
行動対象のEntityIDを取得する
311-
322+
312323
Returns
313324
-------
314325
Optional[EntityID]: 行動対象のEntityID

docs/source/tutorial/config/config.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@
2424
読み込むモジュールのパスなどが記述されています。
2525
パスを指定する際は、プロジェクトの`main.py`からの相対パスで指定してください。
2626

27+
```{admonition}
28+
:class: note
29+
30+
```yaml
31+
DefaultSearch:
32+
PathPlanning: src.<your_team_name>.module.complex.sample_search.SampleSearch
33+
Clustering: src.<your_team_name>.module.complex.sample_search.SampleClustering
34+
```
35+
2736
### development.json
2837

2938
`development.json` は、開発時に使用する設定ファイルです。

docs/source/tutorial/module/module.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
```yaml
1010
SampleSearch:
11-
PathPlanning: src.my-agent.module.complex.sample_search.SampleSearch
12-
Clustering: src.my-agent.module.complex.sample_search.SampleClustering
11+
PathPlanning: src.<your_team_name>.module.complex.sample_search.SampleSearch
12+
Clustering: src.<your_team_name>.module.complex.sample_search.SampleClustering
1313
```
1414
1515
この場合、`SampleSearch` というモジュールで使用される、`PathPlanning` と `Clustering` というモジュールを指定しています。

0 commit comments

Comments
 (0)