test_astar.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /**************************************************************************/
  2. /* test_astar.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_ASTAR_H
  31. #define TEST_ASTAR_H
  32. #include "core/math/a_star.h"
  33. #include "tests/test_macros.h"
  34. namespace TestAStar {
  35. class ABCX : public AStar3D {
  36. public:
  37. enum {
  38. A,
  39. B,
  40. C,
  41. X,
  42. };
  43. ABCX() {
  44. add_point(A, Vector3(0, 0, 0));
  45. add_point(B, Vector3(1, 0, 0));
  46. add_point(C, Vector3(0, 1, 0));
  47. add_point(X, Vector3(0, 0, 1));
  48. connect_points(A, B);
  49. connect_points(A, C);
  50. connect_points(B, C);
  51. connect_points(X, A);
  52. }
  53. // Disable heuristic completely.
  54. real_t _compute_cost(int64_t p_from, int64_t p_to) {
  55. if (p_from == A && p_to == C) {
  56. return 1000;
  57. }
  58. return 100;
  59. }
  60. };
  61. TEST_CASE("[AStar3D] ABC path") {
  62. ABCX abcx;
  63. Vector<int64_t> path = abcx.get_id_path(ABCX::A, ABCX::C);
  64. REQUIRE(path.size() == 3);
  65. CHECK(path[0] == ABCX::A);
  66. CHECK(path[1] == ABCX::B);
  67. CHECK(path[2] == ABCX::C);
  68. }
  69. TEST_CASE("[AStar3D] ABCX path") {
  70. ABCX abcx;
  71. Vector<int64_t> path = abcx.get_id_path(ABCX::X, ABCX::C);
  72. REQUIRE(path.size() == 4);
  73. CHECK(path[0] == ABCX::X);
  74. CHECK(path[1] == ABCX::A);
  75. CHECK(path[2] == ABCX::B);
  76. CHECK(path[3] == ABCX::C);
  77. }
  78. TEST_CASE("[AStar3D] Add/Remove") {
  79. AStar3D a;
  80. // Manual tests.
  81. a.add_point(1, Vector3(0, 0, 0));
  82. a.add_point(2, Vector3(0, 1, 0));
  83. a.add_point(3, Vector3(1, 1, 0));
  84. a.add_point(4, Vector3(2, 0, 0));
  85. a.connect_points(1, 2, true);
  86. a.connect_points(1, 3, true);
  87. a.connect_points(1, 4, false);
  88. CHECK(a.are_points_connected(2, 1));
  89. CHECK(a.are_points_connected(4, 1));
  90. CHECK(a.are_points_connected(2, 1, false));
  91. CHECK_FALSE(a.are_points_connected(4, 1, false));
  92. a.disconnect_points(1, 2, true);
  93. CHECK(a.get_point_connections(1).size() == 2); // 3, 4
  94. CHECK(a.get_point_connections(2).size() == 0);
  95. a.disconnect_points(4, 1, false);
  96. CHECK(a.get_point_connections(1).size() == 2); // 3, 4
  97. CHECK(a.get_point_connections(4).size() == 0);
  98. a.disconnect_points(4, 1, true);
  99. CHECK(a.get_point_connections(1).size() == 1); // 3
  100. CHECK(a.get_point_connections(4).size() == 0);
  101. a.connect_points(2, 3, false);
  102. CHECK(a.get_point_connections(2).size() == 1); // 3
  103. CHECK(a.get_point_connections(3).size() == 1); // 1
  104. a.connect_points(2, 3, true);
  105. CHECK(a.get_point_connections(2).size() == 1); // 3
  106. CHECK(a.get_point_connections(3).size() == 2); // 1, 2
  107. a.disconnect_points(2, 3, false);
  108. CHECK(a.get_point_connections(2).size() == 0);
  109. CHECK(a.get_point_connections(3).size() == 2); // 1, 2
  110. a.connect_points(4, 3, true);
  111. CHECK(a.get_point_connections(3).size() == 3); // 1, 2, 4
  112. CHECK(a.get_point_connections(4).size() == 1); // 3
  113. a.disconnect_points(3, 4, false);
  114. CHECK(a.get_point_connections(3).size() == 2); // 1, 2
  115. CHECK(a.get_point_connections(4).size() == 1); // 3
  116. a.remove_point(3);
  117. CHECK(a.get_point_connections(1).size() == 0);
  118. CHECK(a.get_point_connections(2).size() == 0);
  119. CHECK(a.get_point_connections(4).size() == 0);
  120. a.add_point(0, Vector3(0, -1, 0));
  121. a.add_point(3, Vector3(2, 1, 0));
  122. // 0: (0, -1)
  123. // 1: (0, 0)
  124. // 2: (0, 1)
  125. // 3: (2, 1)
  126. // 4: (2, 0)
  127. // Tests for get_closest_position_in_segment.
  128. a.connect_points(2, 3);
  129. CHECK(a.get_closest_position_in_segment(Vector3(0.5, 0.5, 0)) == Vector3(0.5, 1, 0));
  130. a.connect_points(3, 4);
  131. a.connect_points(0, 3);
  132. a.connect_points(1, 4);
  133. a.disconnect_points(1, 4, false);
  134. a.disconnect_points(4, 3, false);
  135. a.disconnect_points(3, 4, false);
  136. // Remaining edges: <2, 3>, <0, 3>, <1, 4> (directed).
  137. CHECK(a.get_closest_position_in_segment(Vector3(2, 0.5, 0)) == Vector3(1.75, 0.75, 0));
  138. CHECK(a.get_closest_position_in_segment(Vector3(-1, 0.2, 0)) == Vector3(0, 0, 0));
  139. CHECK(a.get_closest_position_in_segment(Vector3(3, 2, 0)) == Vector3(2, 1, 0));
  140. Math::seed(0);
  141. // Random tests for connectivity checks
  142. for (int i = 0; i < 20000; i++) {
  143. int u = Math::rand() % 5;
  144. int v = Math::rand() % 4;
  145. if (u == v) {
  146. v = 4;
  147. }
  148. if (Math::rand() % 2 == 1) {
  149. // Add a (possibly existing) directed edge and confirm connectivity.
  150. a.connect_points(u, v, false);
  151. CHECK(a.are_points_connected(u, v, false));
  152. } else {
  153. // Remove a (possibly nonexistent) directed edge and confirm disconnectivity.
  154. a.disconnect_points(u, v, false);
  155. CHECK_FALSE(a.are_points_connected(u, v, false));
  156. }
  157. }
  158. // Random tests for point removal.
  159. for (int i = 0; i < 20000; i++) {
  160. a.clear();
  161. for (int j = 0; j < 5; j++) {
  162. a.add_point(j, Vector3(0, 0, 0));
  163. }
  164. // Add or remove random edges.
  165. for (int j = 0; j < 10; j++) {
  166. int u = Math::rand() % 5;
  167. int v = Math::rand() % 4;
  168. if (u == v) {
  169. v = 4;
  170. }
  171. if (Math::rand() % 2 == 1) {
  172. a.connect_points(u, v, false);
  173. } else {
  174. a.disconnect_points(u, v, false);
  175. }
  176. }
  177. // Remove point 0.
  178. a.remove_point(0);
  179. // White box: this will check all edges remaining in the segments set.
  180. for (int j = 1; j < 5; j++) {
  181. CHECK_FALSE(a.are_points_connected(0, j, true));
  182. }
  183. }
  184. // It's been great work, cheers. \(^ ^)/
  185. }
  186. TEST_CASE("[Stress][AStar3D] Find paths") {
  187. // Random stress tests with Floyd-Warshall.
  188. const int N = 30;
  189. Math::seed(0);
  190. for (int test = 0; test < 1000; test++) {
  191. AStar3D a;
  192. Vector3 p[N];
  193. bool adj[N][N] = { { false } };
  194. // Assign initial coordinates.
  195. for (int u = 0; u < N; u++) {
  196. p[u].x = Math::rand() % 100;
  197. p[u].y = Math::rand() % 100;
  198. p[u].z = Math::rand() % 100;
  199. a.add_point(u, p[u]);
  200. }
  201. // Generate a random sequence of operations.
  202. for (int i = 0; i < 1000; i++) {
  203. // Pick two different vertices.
  204. int u, v;
  205. u = Math::rand() % N;
  206. v = Math::rand() % (N - 1);
  207. if (u == v) {
  208. v = N - 1;
  209. }
  210. // Pick a random operation.
  211. int op = Math::rand();
  212. switch (op % 9) {
  213. case 0:
  214. case 1:
  215. case 2:
  216. case 3:
  217. case 4:
  218. case 5:
  219. // Add edge (u, v); possibly bidirectional.
  220. a.connect_points(u, v, op % 2);
  221. adj[u][v] = true;
  222. if (op % 2) {
  223. adj[v][u] = true;
  224. }
  225. break;
  226. case 6:
  227. case 7:
  228. // Remove edge (u, v); possibly bidirectional.
  229. a.disconnect_points(u, v, op % 2);
  230. adj[u][v] = false;
  231. if (op % 2) {
  232. adj[v][u] = false;
  233. }
  234. break;
  235. case 8:
  236. // Remove point u and add it back; clears adjacent edges and changes coordinates.
  237. a.remove_point(u);
  238. p[u].x = Math::rand() % 100;
  239. p[u].y = Math::rand() % 100;
  240. p[u].z = Math::rand() % 100;
  241. a.add_point(u, p[u]);
  242. for (v = 0; v < N; v++) {
  243. adj[u][v] = adj[v][u] = false;
  244. }
  245. break;
  246. }
  247. }
  248. // Floyd-Warshall.
  249. float d[N][N];
  250. for (int u = 0; u < N; u++) {
  251. for (int v = 0; v < N; v++) {
  252. d[u][v] = (u == v || adj[u][v]) ? p[u].distance_to(p[v]) : INFINITY;
  253. }
  254. }
  255. for (int w = 0; w < N; w++) {
  256. for (int u = 0; u < N; u++) {
  257. for (int v = 0; v < N; v++) {
  258. if (d[u][v] > d[u][w] + d[w][v]) {
  259. d[u][v] = d[u][w] + d[w][v];
  260. }
  261. }
  262. }
  263. }
  264. // Display statistics.
  265. int count = 0;
  266. for (int u = 0; u < N; u++) {
  267. for (int v = 0; v < N; v++) {
  268. if (adj[u][v]) {
  269. count++;
  270. }
  271. }
  272. }
  273. print_verbose(vformat("Test #%4d: %3d edges, ", test + 1, count));
  274. count = 0;
  275. for (int u = 0; u < N; u++) {
  276. for (int v = 0; v < N; v++) {
  277. if (!Math::is_inf(d[u][v])) {
  278. count++;
  279. }
  280. }
  281. }
  282. print_verbose(vformat("%3d/%d pairs of reachable points\n", count - N, N * (N - 1)));
  283. // Check A*'s output.
  284. bool match = true;
  285. for (int u = 0; u < N; u++) {
  286. for (int v = 0; v < N; v++) {
  287. if (u != v) {
  288. Vector<int64_t> route = a.get_id_path(u, v);
  289. if (!Math::is_inf(d[u][v])) {
  290. // Reachable.
  291. if (route.size() == 0) {
  292. print_verbose(vformat("From %d to %d: A* did not find a path\n", u, v));
  293. match = false;
  294. goto exit;
  295. }
  296. float astar_dist = 0;
  297. for (int i = 1; i < route.size(); i++) {
  298. if (!adj[route[i - 1]][route[i]]) {
  299. print_verbose(vformat("From %d to %d: edge (%d, %d) does not exist\n",
  300. u, v, route[i - 1], route[i]));
  301. match = false;
  302. goto exit;
  303. }
  304. astar_dist += p[route[i - 1]].distance_to(p[route[i]]);
  305. }
  306. if (!Math::is_equal_approx(astar_dist, d[u][v])) {
  307. print_verbose(vformat("From %d to %d: Floyd-Warshall gives %.6f, A* gives %.6f\n",
  308. u, v, d[u][v], astar_dist));
  309. match = false;
  310. goto exit;
  311. }
  312. } else {
  313. // Unreachable.
  314. if (route.size() > 0) {
  315. print_verbose(vformat("From %d to %d: A* somehow found a nonexistent path\n", u, v));
  316. match = false;
  317. goto exit;
  318. }
  319. }
  320. }
  321. }
  322. }
  323. exit:
  324. CHECK_MESSAGE(match, "Found all paths.");
  325. }
  326. }
  327. } // namespace TestAStar
  328. #endif // TEST_ASTAR_H