GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
graph_search_evaluator_test.h
[詳解]
1
3
4// Copyright(c) 2023-2025 Design Engineering Laboratory, Saitama University
5// Released under the MIT license
6// https://opensource.org/licenses/mit-license.php
7
8#ifndef DESIGNLAB_GRAPH_SEARCH_EVALUATOR_TEST_H_
9#define DESIGNLAB_GRAPH_SEARCH_EVALUATOR_TEST_H_
10
11#include <doctest.h>
12
14
15
16TEST_SUITE("GraphSearchEvaluator")
17{
18 using GraphSearchEvaluator = designlab::GraphSearchEvaluator;
19 using GraphSearchEvaluationValue = designlab::GraphSearchEvaluationValue;
20 using Tag = GraphSearchEvaluationValue::Tag;
21
22 constexpr Tag kFirstTag = 10; // 1つ目の評価値のタグ.
23 constexpr Tag kSecondTag = 1; // 2つ目の評価値のタグ.
24
25
26 TEST_CASE("LeftIsBetterTest_WhenLeftIsBetter_ShouldReturnTrue")
27 {
28 SUBCASE("LowerValueIsBetter")
29 {
30 // 評価値が小さいほど良い場合.
31 const GraphSearchEvaluator::EvaluationMethod evaluation_method =
32 {
33 .is_lower_better = true,
34 .margin = 0.0f,
35 };
36
37 GraphSearchEvaluator evaluator({ { kFirstTag, evaluation_method }, { kSecondTag, evaluation_method } },
38 { kFirstTag, kSecondTag });
39
40
41 GraphSearchEvaluationValue left = evaluator.InitializeEvaluationValue();
42 left.value.at(kFirstTag) = -1.0f;
43 left.value.at(kSecondTag) = -1.0f;
44 GraphSearchEvaluationValue right = evaluator.InitializeEvaluationValue();
45 right.value.at(kFirstTag) = 1.0f;
46 right.value.at(kSecondTag) = 1.0f;
47
48 const bool actual = evaluator.LeftIsBetter(left, right);
49
50 const bool expected = true;
51
52 CHECK(actual == expected);
53 }
54
55 SUBCASE("HigherValueIsBetter")
56 {
57 // 評価値が大きいほど良い場合.
58 const GraphSearchEvaluator::EvaluationMethod evaluation_method =
59 {
60 .is_lower_better = false,
61 .margin = 0.0f,
62 };
63
64 GraphSearchEvaluator evaluator({ { kFirstTag, evaluation_method }, { kSecondTag, evaluation_method } },
65 { kFirstTag, kSecondTag });
66
67 GraphSearchEvaluationValue left = evaluator.InitializeEvaluationValue();
68 left.value.at(kFirstTag) = 1.0f;
69 left.value.at(kSecondTag) = 1.0f;
70 GraphSearchEvaluationValue right = evaluator.InitializeEvaluationValue();
71 right.value.at(kFirstTag) = -1.0f;
72 right.value.at(kSecondTag) = -1.0f;
73
74 const bool actual = evaluator.LeftIsBetter(left, right);
75
76 const bool expected = true;
77
78 CHECK(actual == expected);
79 }
80
81 // 2つ目の評価値が低い評価だとしても,1つ目の評価値が高い評価ならば,そちらを優先する.
82 SUBCASE("WhenFirstTagIsBetter")
83 {
84 // 評価値が小さいほど良い場合.
85 const GraphSearchEvaluator::EvaluationMethod evaluation_method =
86 {
87 .is_lower_better = false,
88 .margin = 0.0f,
89 };
90
91 GraphSearchEvaluator evaluator({ { kFirstTag, evaluation_method }, { kSecondTag, evaluation_method } },
92 { kFirstTag, kSecondTag });
93
94 GraphSearchEvaluationValue left = evaluator.InitializeEvaluationValue();
95 left.value.at(kFirstTag) = 1.0f;
96 left.value.at(kSecondTag) = 0.0f;
97 GraphSearchEvaluationValue right = evaluator.InitializeEvaluationValue();
98 right.value.at(kFirstTag) = 0.0f;
99 right.value.at(kSecondTag) = -1.0f;
100
101 const bool actual = evaluator.LeftIsBetter(left, right);
102
103 const bool expected = true;
104
105 CHECK(actual == expected);
106 }
107 }
108
109 TEST_CASE("LeftIsBetterTest_WhenRightIsBetter_ShouldReturnFalse")
110 {
111 SUBCASE("LowerValueIsBetter")
112 {
113 const GraphSearchEvaluator::EvaluationMethod evaluation_method =
114 {
115 .is_lower_better = true,
116 .margin = 0.0f,
117 };
118
119 GraphSearchEvaluator evaluator({ { kFirstTag, evaluation_method }, { kSecondTag, evaluation_method } },
120 { kFirstTag, kSecondTag });
121
122 GraphSearchEvaluationValue left = evaluator.InitializeEvaluationValue();
123 left.value.at(kFirstTag) = 1.0f;
124 left.value.at(kSecondTag) = 1.0f;
125 GraphSearchEvaluationValue right = evaluator.InitializeEvaluationValue();
126 right.value.at(kFirstTag) = -1.0f;
127 right.value.at(kSecondTag) = -1.0f;
128
129 const bool actual = evaluator.LeftIsBetter(left, right);
130
131 const bool expected = false;
132
133 CHECK(actual == expected);
134 }
135
136 SUBCASE("HigherValueIsBetter")
137 {
138 const GraphSearchEvaluator::EvaluationMethod evaluation_method =
139 {
140 .is_lower_better = false,
141 .margin = 0.0f,
142 };
143
144 GraphSearchEvaluator evaluator({ { kFirstTag, evaluation_method }, { kSecondTag, evaluation_method } },
145 { kFirstTag, kSecondTag });
146
147 GraphSearchEvaluationValue left = evaluator.InitializeEvaluationValue();
148 left.value.at(kFirstTag) = -1.0f;
149 left.value.at(kSecondTag) = -1.0f;
150 GraphSearchEvaluationValue right = evaluator.InitializeEvaluationValue();
151 right.value.at(kFirstTag) = 1.0f;
152 right.value.at(kSecondTag) = 1.0f;
153
154 const bool actual = evaluator.LeftIsBetter(left, right);
155
156 const bool expected = false;
157
158 CHECK(actual == expected);
159 }
160 }
161
162 TEST_CASE("LeftIsBetterTest_WhenValuesAreEqual_ShouldReturnTrueOrFalse")
163 {
164 const GraphSearchEvaluator::EvaluationMethod evaluation_method =
165 {
166 .is_lower_better = true,
167 .margin = 1.0f,
168 };
169
170 const GraphSearchEvaluator evaluator({ { kFirstTag, evaluation_method }, { kSecondTag, evaluation_method } },
171 { kFirstTag, kSecondTag });
172
173 GraphSearchEvaluationValue left = evaluator.InitializeEvaluationValue();
174 left.value.at(kFirstTag) = 1.0f;
175 left.value.at(kSecondTag) = 1.0f;
176 GraphSearchEvaluationValue right = evaluator.InitializeEvaluationValue();
177 right.value.at(kFirstTag) = 1.0f;
178 right.value.at(kSecondTag) = 1.0f;
179
180
181 // 左側を優先する設定の場合は true を返す.
182 SUBCASE("WhenReturnTrueCaseOfEqualIsTrue")
183 {
184 const bool actual = evaluator.LeftIsBetter(left, right, true);
185
186 const bool expected = true; // 左側を優先する.
187
188 CHECK(actual == expected);
189 }
190
191 // 左側を優先しない設定の場合は false を返す.
192 SUBCASE("WhenReturnTrueCaseOfEqualIsFalse")
193 {
194 const bool actual = evaluator.LeftIsBetter(left, right, false);
195
196 const bool expected = false; // 左側を優先しない.
197
198 CHECK(actual == expected);
199 }
200 }
201
202 TEST_CASE("LeftIsBetterTest_WhenValuesAreEqualInMargin_ShouldReturnTrueOrFalse")
203 {
204 const GraphSearchEvaluator::EvaluationMethod evaluation_method =
205 {
206 .is_lower_better = true,
207 .margin = 1.0f,
208 };
209
210 const GraphSearchEvaluator evaluator({ { kFirstTag, evaluation_method }, { kSecondTag, evaluation_method } },
211 { kFirstTag, kSecondTag });
212
213 GraphSearchEvaluationValue left = evaluator.InitializeEvaluationValue();
214 left.value.at(kFirstTag) = 1.0f;
215 left.value.at(kSecondTag) = 1.0f;
216 GraphSearchEvaluationValue right = evaluator.InitializeEvaluationValue();
217 right.value.at(kFirstTag) = 0.5f; // 差が margin 以下.
218 right.value.at(kSecondTag) = 1.5f; // 差が margin 以下.
219
220 // 左側を優先する設定の場合は true を返す.
221 SUBCASE("WhenLeftIsPreferred_ShouldReturnTrue")
222 {
223 const bool actual = evaluator.LeftIsBetter(left, right, true);
224
225 const bool expected = true; // 左側を優先する.
226
227 CHECK(actual == expected);
228 }
229
230 // 左側を優先しない設定の場合は false を返す.
231 SUBCASE("WhenRightIsPreferred_ShouldReturnFalse")
232 {
233 const bool actual = evaluator.LeftIsBetter(left, right, false);
234
235 const bool expected = false; // 左側を優先しない.
236
237 CHECK(actual == expected);
238 }
239 }
240
241 TEST_CASE("InitializeEvaluationValueTest_ShouldReturnMinEvaluationValue")
242 {
243 // 初期値は評価が最低となる値あることを確認する.
244
245 const GraphSearchEvaluator::EvaluationMethod evaluation_method_min =
246 {
247 .is_lower_better = true,
248 .margin = 0.0f,
249 };
250
251 const GraphSearchEvaluator::EvaluationMethod evaluation_method_max =
252 {
253 .is_lower_better = false,
254 .margin = 0.0f,
255 };
256
257 // FirstTag は低い値,SecondTag は高い値を高く評価する.
258 GraphSearchEvaluator evaluator({ { kFirstTag, evaluation_method_min }, { kSecondTag, evaluation_method_max } },
259 { kFirstTag, kSecondTag });
260
261 const GraphSearchEvaluationValue actual = evaluator.InitializeEvaluationValue();
262
263 const GraphSearchEvaluationValue expected =
264 {
265 .value = { { kFirstTag, GraphSearchEvaluationValue::kMaxEvaluationValue }, // 最低評価となるのは,最も高い値.
266 { kSecondTag, GraphSearchEvaluationValue::kMinEvaluationValue } }, // 最低評価となるのは,最も低い値.
267 };
268
269 CHECK(actual.value.at(kFirstTag) == expected.value.at(kFirstTag));
270 CHECK(actual.value.at(kSecondTag) == expected.value.at(kSecondTag));
271
272 CHECK(actual.value.size() == expected.value.size());
273 }
274}
275
276
277#endif // DESIGNLAB_GRAPH_SEARCH_EVALUATOR_TEST_H_
グラフ探索の評価値を評価するクラス.
TEST_SUITE("GraphSearchEvaluator")
グラフ探索の評価値を格納する構造体.