Skip to content

Commit 88b0a09

Browse files
committed
deploy: 35b40a2
1 parent 5151395 commit 88b0a09

File tree

59 files changed

+1059
-8
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1059
-8
lines changed

.buildinfo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Sphinx build info version 1
22
# This file records the configuration used when building these files. When it is not found, a full rebuild will be done.
3-
config: 69b24e5c59e722fa6695f22570d6998c
3+
config: 846736bf1b29d86c0b1e9055afab4737
44
tags: 645f666f9bcd5a90fca523b33c5a78b7

_sources/hands-on/search.md

Lines changed: 169 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,8 @@ class KMeansPPSearch(Search):
138138
allocated_cluster_index
139139
)
140140
# 乱数で選択
141-
index = random.randint(0, len(cluster_entity_ids) - 1)
142-
# 選択したエンティティIDを結果として設定
143-
self._result = cluster_entity_ids[index]
141+
if cluster_entity_ids:
142+
self._result = random.choice(cluster_entity_ids)
144143
145144
return self
146145
```
@@ -158,13 +157,180 @@ class KMeansPPSearch(Search):
158157
- 目標にたどり着く前に探索対象が変わってしまうため、なかなか目標にたどり着けない
159158
- 色んなところにランダムに探索対象を選択することで、効率的な探索ができない
160159
- すでに探索したエンティティを再度探索対象として選択してしまうため、効率的な探索ができない
160+
- 近くに未探索のエンティティがあるのに、遠くのエンティティを探索対象として選択してしまう
161161

162162
などの問題があります。
163163

164164
## 課題
165165

166166
`KMeansPPSearch` モジュールを改善し、より効率的な探索を行うモジュールを実装して見てください。
167167

168+
```{warning}
169+
ここに上げた問題以外にも、改善すべき点が存在すると思うので、それを改善していただいても構いません。
170+
```
171+
168172
### 探索対象がステップごとに変わってしまう問題
169173

174+
```{admonition} 方針のヒント
175+
:class: tip dropdown
176+
177+
一度選択した探索対象に到達するまで、探索対象を変更しないようにする
178+
```
179+
180+
```{admonition} プログラム例
181+
:class: tip dropdown
182+
183+
````python
184+
def calculate(self) -> Search:
185+
# 自エージェントのエンティティIDを取得
186+
me: EntityID = self._agent_info.get_entity_id()
187+
# 自エージェントが所属するクラスターのインデックスを取得
188+
allocated_cluster_index: int = self._clustering.get_cluster_index(me)
189+
# クラスター内のエンティティIDを取得
190+
cluster_entity_ids: list[EntityID] = self._clustering.get_cluster_entity_ids(
191+
allocated_cluster_index
192+
)
193+
194+
# 探索対象をすでに選んでいる場合
195+
if self._result:
196+
# 自エージェントのいる場所のエンティティIDを取得
197+
my_position = self._agent_info.get_position_entity_id()
198+
# 探索対象の場所のエンティティIDを取得
199+
target_position = self._world_info.get_entity_position(self._result)
200+
# 自エージェントのいる場所と探索対象の場所が一致している場合、探索対象をリセット
201+
if my_position == target_position:
202+
# 探索対象をリセット
203+
self._result = None
204+
205+
# 探索対象が未選択の場合
206+
if not self._result and cluster_entity_ids:
207+
self._result = random.choice(cluster_entity_ids)
208+
209+
return self
210+
```
211+
170212
### すでに探索したエンティティを再度探索対象として選択してしまう問題
213+
214+
```{admonition} 方針のヒント
215+
:class: tip dropdown
216+
217+
すでに探索したエンティティを何かしらの方法で記録し、再度探索対象として選択しないようにする
218+
```
219+
220+
```{admonition} プログラム例
221+
:class: tip dropdown
222+
223+
````python
224+
def __init__(
225+
self,
226+
agent_info: AgentInfo,
227+
world_info: WorldInfo,
228+
scenario_info: ScenarioInfo,
229+
module_manager: ModuleManager,
230+
develop_data: DevelopData,
231+
) -> None:
232+
super().__init__(
233+
agent_info, world_info, scenario_info, module_manager, develop_data
234+
)
235+
self._result: Optional[EntityID] = None
236+
237+
self._logger = get_agent_logger(
238+
f"{self.__class__.__module__}.{self.__class__.__qualname__}",
239+
self._agent_info,
240+
)
241+
242+
self._clustering: Clustering = cast(
243+
Clustering,
244+
module_manager.get_module(
245+
"KMeansPPSearch.Clustering",
246+
"adf_core_python.implement.module.algorithm.k_means_clustering.KMeansClustering",
247+
),
248+
)
249+
250+
self.register_sub_module(self._clustering)
251+
252+
# 探索したいエンティティIDのリスト(追加)
253+
self._search_entity_ids: list[EntityID] = []
254+
255+
def calculate(self) -> Search:
256+
# 探索したいエンティティIDのリストが空の場合
257+
if not self._search_entity_ids:
258+
# 自エージェントのエンティティIDを取得
259+
me: EntityID = self._agent_info.get_entity_id()
260+
# 自エージェントが所属するクラスターのインデックスを取得
261+
allocated_cluster_index: int = self._clustering.get_cluster_index(me)
262+
# クラスター内のエンティティIDを取得(変更)
263+
self._search_entity_ids: list[EntityID] = (
264+
self._clustering.get_cluster_entity_ids(allocated_cluster_index)
265+
)
266+
267+
# 探索対象をすでに選んでいる場合
268+
if self._result:
269+
# 自エージェントのいる場所のエンティティIDを取得
270+
my_position = self._agent_info.get_position_entity_id()
271+
# 探索対象の場所のエンティティIDを取得
272+
target_position = self._world_info.get_entity_position(self._result)
273+
# 自エージェントのいる場所と探索対象の場所が一致している場合、探索対象をリセット
274+
if my_position == target_position:
275+
# 探索したいエンティティIDのリストから探索対象を削除
276+
self._search_entity_ids.remove(self._result)
277+
# 探索対象をリセット
278+
self._result = None
279+
280+
# 探索対象が未選択の場合(変更)
281+
if not self._result and self._search_entity_ids:
282+
self._result = random.choice(self._search_entity_ids)
283+
284+
return self
285+
```
286+
287+
### 近くに未探索のエンティティがあるのに、遠くのエンティティを探索対象として選択してしまう
288+
289+
```{admonition} 方針のヒント
290+
:class: tip dropdown
291+
292+
エンティティ間の距離を計算し、もっとも近いエンティティを探索対象として選択する
293+
```
294+
295+
```{admonition} プログラム例
296+
:class: tip dropdown
297+
298+
````python
299+
def calculate(self) -> Search:
300+
# 探索したいエンティティIDのリストが空の場合
301+
if not self._search_entity_ids:
302+
# 自エージェントのエンティティIDを取得
303+
me: EntityID = self._agent_info.get_entity_id()
304+
# 自エージェントが所属するクラスターのインデックスを取得
305+
allocated_cluster_index: int = self._clustering.get_cluster_index(me)
306+
# クラスター内のエンティティIDを取得
307+
self._search_entity_ids: list[EntityID] = (
308+
self._clustering.get_cluster_entity_ids(allocated_cluster_index)
309+
)
310+
311+
# 探索対象をすでに選んでいる場合
312+
if self._result:
313+
# 自エージェントのいる場所のエンティティIDを取得
314+
my_position = self._agent_info.get_position_entity_id()
315+
# 探索対象の場所のエンティティIDを取得
316+
target_position = self._world_info.get_entity_position(self._result)
317+
# 自エージェントのいる場所と探索対象の場所が一致している場合、探索対象をリセット
318+
if my_position == target_position:
319+
# 探索したいエンティティIDのリストから探索対象を削除
320+
self._search_entity_ids.remove(self._result)
321+
# 探索対象をリセット
322+
self._result = None
323+
324+
# 探索対象が未選択の場合
325+
if not self._result and self._search_entity_ids:
326+
nearest_entity_id: Optional[EntityID] = None
327+
nearest_distance: float = float("inf")
328+
me: EntityID = self._agent_info.get_entity_id()
329+
# 探索対象の中で自エージェントに最も近いエンティティIDを選択(変更)
330+
for entity_id in self._search_entity_ids:
331+
distance = self._world_info.get_distance(me, entity_id)
332+
if distance < nearest_distance:
333+
nearest_entity_id = entity_id
334+
nearest_distance = distance
335+
self._result = nearest_entity_id
336+
```

_static/togglebutton.css

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/**
2+
* Admonition-based toggles
3+
*/
4+
5+
/* Visibility of the target */
6+
.admonition.toggle .admonition-title ~ * {
7+
transition: opacity .3s, height .3s;
8+
}
9+
10+
/* Toggle buttons inside admonitions so we see the title */
11+
.admonition.toggle {
12+
position: relative;
13+
}
14+
15+
/* Titles should cut off earlier to avoid overlapping w/ button */
16+
.admonition.toggle .admonition-title {
17+
padding-right: 25%;
18+
cursor: pointer;
19+
}
20+
21+
/* Hovering will cause a slight shift in color to make it feel interactive */
22+
.admonition.toggle .admonition-title:hover {
23+
box-shadow: inset 0 0 0px 20px rgb(0 0 0 / 1%);
24+
}
25+
26+
/* Hovering will cause a slight shift in color to make it feel interactive */
27+
.admonition.toggle .admonition-title:active {
28+
box-shadow: inset 0 0 0px 20px rgb(0 0 0 / 3%);
29+
}
30+
31+
/* Remove extra whitespace below the admonition title when hidden */
32+
.admonition.toggle-hidden {
33+
padding-bottom: 0;
34+
}
35+
36+
.admonition.toggle-hidden .admonition-title {
37+
margin-bottom: 0;
38+
}
39+
40+
/* hides all the content of a page until de-toggled */
41+
.admonition.toggle-hidden .admonition-title ~ * {
42+
height: 0;
43+
margin: 0;
44+
opacity: 0;
45+
visibility: hidden;
46+
}
47+
48+
/* General button style and position*/
49+
button.toggle-button {
50+
/**
51+
* Background and shape. By default there's no background
52+
* but users can style as they wish
53+
*/
54+
background: none;
55+
border: none;
56+
outline: none;
57+
58+
/* Positioning just inside the admonition title */
59+
position: absolute;
60+
right: 0.5em;
61+
padding: 0px;
62+
border: none;
63+
outline: none;
64+
}
65+
66+
/* Display the toggle hint on wide screens */
67+
@media (min-width: 768px) {
68+
button.toggle-button.toggle-button-hidden:before {
69+
content: attr(data-toggle-hint); /* This will be filled in by JS */
70+
font-size: .8em;
71+
align-self: center;
72+
}
73+
}
74+
75+
/* Icon behavior */
76+
.tb-icon {
77+
transition: transform .2s ease-out;
78+
height: 1.5em;
79+
width: 1.5em;
80+
stroke: currentColor; /* So that we inherit the color of other text */
81+
}
82+
83+
/* The icon should point right when closed, down when open. */
84+
/* Open */
85+
.admonition.toggle button .tb-icon {
86+
transform: rotate(90deg);
87+
}
88+
89+
/* Closed */
90+
.admonition.toggle button.toggle-button-hidden .tb-icon {
91+
transform: rotate(0deg);
92+
}
93+
94+
/* With details toggles, we don't rotate the icon so it points right */
95+
details.toggle-details .tb-icon {
96+
height: 1.4em;
97+
width: 1.4em;
98+
margin-top: 0.1em; /* To center the button vertically */
99+
}
100+
101+
102+
/**
103+
* Details-based toggles.
104+
* In this case, we wrap elements with `.toggle` in a details block.
105+
*/
106+
107+
/* Details blocks */
108+
details.toggle-details {
109+
margin: 1em 0;
110+
}
111+
112+
113+
details.toggle-details summary {
114+
display: flex;
115+
align-items: center;
116+
cursor: pointer;
117+
list-style: none;
118+
border-radius: .2em;
119+
border-left: 3px solid #1976d2;
120+
background-color: rgb(204 204 204 / 10%);
121+
padding: 0.2em 0.7em 0.3em 0.5em; /* Less padding on left because the SVG has left margin */
122+
font-size: 0.9em;
123+
}
124+
125+
details.toggle-details summary:hover {
126+
background-color: rgb(204 204 204 / 20%);
127+
}
128+
129+
details.toggle-details summary:active {
130+
background: rgb(204 204 204 / 28%);
131+
}
132+
133+
.toggle-details__summary-text {
134+
margin-left: 0.2em;
135+
}
136+
137+
details.toggle-details[open] summary {
138+
margin-bottom: .5em;
139+
}
140+
141+
details.toggle-details[open] summary .tb-icon {
142+
transform: rotate(90deg);
143+
}
144+
145+
details.toggle-details[open] summary ~ * {
146+
animation: toggle-fade-in .3s ease-out;
147+
}
148+
149+
@keyframes toggle-fade-in {
150+
from {opacity: 0%;}
151+
to {opacity: 100%;}
152+
}
153+
154+
/* Print rules - we hide all toggle button elements at print */
155+
@media print {
156+
/* Always hide the summary so the button doesn't show up */
157+
details.toggle-details summary {
158+
display: none;
159+
}
160+
}

0 commit comments

Comments
 (0)