test_astar.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. #pragma once
  31. #include "core/math/a_star.h"
  32. #include "tests/test_macros.h"
  33. namespace TestAStar {
  34. class ABCX : public AStar3D {
  35. public:
  36. enum {
  37. A,
  38. B,
  39. C,
  40. X,
  41. };
  42. ABCX() {
  43. add_point(A, Vector3(0, 0, 0));
  44. add_point(B, Vector3(1, 0, 0));
  45. add_point(C, Vector3(0, 1, 0));
  46. add_point(X, Vector3(0, 0, 1));
  47. connect_points(A, B);
  48. connect_points(A, C);
  49. connect_points(B, C);
  50. connect_points(X, A);
  51. }
  52. // Disable heuristic completely.
  53. real_t _compute_cost(int64_t p_from, int64_t p_to) {
  54. if (p_from == A && p_to == C) {
  55. return 1000;
  56. }
  57. return 100;
  58. }
  59. };
  60. TEST_CASE("[AStar3D] ABC path") {
  61. ABCX abcx;
  62. Vector<int64_t> path = abcx.get_id_path(ABCX::A, ABCX::C);
  63. REQUIRE(path.size() == 3);
  64. CHECK(path[0] == ABCX::A);
  65. CHECK(path[1] == ABCX::B);
  66. CHECK(path[2] == ABCX::C);
  67. }
  68. TEST_CASE("[AStar3D] ABCX path") {
  69. ABCX abcx;
  70. Vector<int64_t> path = abcx.get_id_path(ABCX::X, ABCX::C);
  71. REQUIRE(path.size() == 4);
  72. CHECK(path[0] == ABCX::X);
  73. CHECK(path[1] == ABCX::A);
  74. CHECK(path[2] == ABCX::B);
  75. CHECK(path[3] == ABCX::C);
  76. }
  77. TEST_CASE("[AStar3D] Add/Remove") {
  78. AStar3D a;
  79. // Manual tests.
  80. a.add_point(1, Vector3(0, 0, 0));
  81. a.add_point(2, Vector3(0, 1, 0));
  82. a.add_point(3, Vector3(1, 1, 0));
  83. a.add_point(4, Vector3(2, 0, 0));
  84. a.connect_points(1, 2, true);
  85. a.connect_points(1, 3, true);
  86. a.connect_points(1, 4, false);
  87. CHECK(a.are_points_connected(2, 1));
  88. CHECK(a.are_points_connected(4, 1));
  89. CHECK(a.are_points_connected(2, 1, false));
  90. CHECK_FALSE(a.are_points_connected(4, 1, false));
  91. a.disconnect_points(1, 2, true);
  92. CHECK(a.get_point_connections(1).size() == 2); // 3, 4
  93. CHECK(a.get_point_connections(2).size() == 0);
  94. a.disconnect_points(4, 1, false);
  95. CHECK(a.get_point_connections(1).size() == 2); // 3, 4
  96. CHECK(a.get_point_connections(4).size() == 0);
  97. a.disconnect_points(4, 1, true);
  98. CHECK(a.get_point_connections(1).size() == 1); // 3
  99. CHECK(a.get_point_connections(4).size() == 0);
  100. a.connect_points(2, 3, false);
  101. CHECK(a.get_point_connections(2).size() == 1); // 3
  102. CHECK(a.get_point_connections(3).size() == 1); // 1
  103. a.connect_points(2, 3, true);
  104. CHECK(a.get_point_connections(2).size() == 1); // 3
  105. CHECK(a.get_point_connections(3).size() == 2); // 1, 2
  106. a.disconnect_points(2, 3, false);
  107. CHECK(a.get_point_connections(2).size() == 0);
  108. CHECK(a.get_point_connections(3).size() == 2); // 1, 2
  109. a.connect_points(4, 3, true);
  110. CHECK(a.get_point_connections(3).size() == 3); // 1, 2, 4
  111. CHECK(a.get_point_connections(4).size() == 1); // 3
  112. a.disconnect_points(3, 4, false);
  113. CHECK(a.get_point_connections(3).size() == 2); // 1, 2
  114. CHECK(a.get_point_connections(4).size() == 1); // 3
  115. a.remove_point(3);
  116. CHECK(a.get_point_connections(1).size() == 0);
  117. CHECK(a.get_point_connections(2).size() == 0);
  118. CHECK(a.get_point_connections(4).size() == 0);
  119. a.add_point(0, Vector3(0, -1, 0));
  120. a.add_point(3, Vector3(2, 1, 0));
  121. // 0: (0, -1)
  122. // 1: (0, 0)
  123. // 2: (0, 1)
  124. // 3: (2, 1)
  125. // 4: (2, 0)
  126. // Tests for get_closest_position_in_segment.
  127. a.connect_points(2, 3);
  128. CHECK(a.get_closest_position_in_segment(Vector3(0.5, 0.5, 0)) == Vector3(0.5, 1, 0));
  129. a.connect_points(3, 4);
  130. a.connect_points(0, 3);
  131. a.connect_points(1, 4);
  132. a.disconnect_points(1, 4, false);
  133. a.disconnect_points(4, 3, false);
  134. a.disconnect_points(3, 4, false);
  135. // Remaining edges: <2, 3>, <0, 3>, <1, 4> (directed).
  136. CHECK(a.get_closest_position_in_segment(Vector3(2, 0.5, 0)) == Vector3(1.75, 0.75, 0));
  137. CHECK(a.get_closest_position_in_segment(Vector3(-1, 0.2, 0)) == Vector3(0, 0, 0));
  138. CHECK(a.get_closest_position_in_segment(Vector3(3, 2, 0)) == Vector3(2, 1, 0));
  139. Math::seed(0);
  140. // Random tests for connectivity checks
  141. for (int i = 0; i < 20000; i++) {
  142. int u = Math::rand() % 5;
  143. int v = Math::rand() % 4;
  144. if (u == v) {
  145. v = 4;
  146. }
  147. if (Math::rand() % 2 == 1) {
  148. // Add a (possibly existing) directed edge and confirm connectivity.
  149. a.connect_points(u, v, false);
  150. CHECK(a.are_points_connected(u, v, false));
  151. } else {
  152. // Remove a (possibly nonexistent) directed edge and confirm disconnectivity.
  153. a.disconnect_points(u, v, false);
  154. CHECK_FALSE(a.are_points_connected(u, v, false));
  155. }
  156. }
  157. // Random tests for point removal.
  158. for (int i = 0; i < 20000; i++) {
  159. a.clear();
  160. for (int j = 0; j < 5; j++) {
  161. a.add_point(j, Vector3(0, 0, 0));
  162. }
  163. // Add or remove random edges.
  164. for (int j = 0; j < 10; j++) {
  165. int u = Math::rand() % 5;
  166. int v = Math::rand() % 4;
  167. if (u == v) {
  168. v = 4;
  169. }
  170. if (Math::rand() % 2 == 1) {
  171. a.connect_points(u, v, false);
  172. } else {
  173. a.disconnect_points(u, v, false);
  174. }
  175. }
  176. // Remove point 0.
  177. a.remove_point(0);
  178. // White box: this will check all edges remaining in the segments set.
  179. for (int j = 1; j < 5; j++) {
  180. CHECK_FALSE(a.are_points_connected(0, j, true));
  181. }
  182. }
  183. // It's been great work, cheers. \(^ ^)/
  184. }
  185. TEST_CASE("[Stress][AStar3D] Find paths") {
  186. // Random stress tests with Floyd-Warshall.
  187. constexpr int N = 30;
  188. Math::seed(0);
  189. for (int test = 0; test < 1000; test++) {
  190. AStar3D a;
  191. Vector3 p[N];
  192. bool adj[N][N] = { { false } };
  193. // Assign initial coordinates.
  194. for (int u = 0; u < N; u++) {
  195. p[u].x = Math::rand() % 100;
  196. p[u].y = Math::rand() % 100;
  197. p[u].z = Math::rand() % 100;
  198. a.add_point(u, p[u]);
  199. }
  200. // Generate a random sequence of operations.
  201. for (int i = 0; i < 1000; i++) {
  202. // Pick two different vertices.
  203. int u, v;
  204. u = Math::rand() % N;
  205. v = Math::rand() % (N - 1);
  206. if (u == v) {
  207. v = N - 1;
  208. }
  209. // Pick a random operation.
  210. int op = Math::rand();
  211. switch (op % 9) {
  212. case 0:
  213. case 1:
  214. case 2:
  215. case 3:
  216. case 4:
  217. case 5:
  218. // Add edge (u, v); possibly bidirectional.
  219. a.connect_points(u, v, op % 2);
  220. adj[u][v] = true;
  221. if (op % 2) {
  222. adj[v][u] = true;
  223. }
  224. break;
  225. case 6:
  226. case 7:
  227. // Remove edge (u, v); possibly bidirectional.
  228. a.disconnect_points(u, v, op % 2);
  229. adj[u][v] = false;
  230. if (op % 2) {
  231. adj[v][u] = false;
  232. }
  233. break;
  234. case 8:
  235. // Remove point u and add it back; clears adjacent edges and changes coordinates.
  236. a.remove_point(u);
  237. p[u].x = Math::rand() % 100;
  238. p[u].y = Math::rand() % 100;
  239. p[u].z = Math::rand() % 100;
  240. a.add_point(u, p[u]);
  241. for (v = 0; v < N; v++) {
  242. adj[u][v] = adj[v][u] = false;
  243. }
  244. break;
  245. }
  246. }
  247. // Floyd-Warshall.
  248. float d[N][N];
  249. for (int u = 0; u < N; u++) {
  250. for (int v = 0; v < N; v++) {
  251. d[u][v] = (u == v || adj[u][v]) ? p[u].distance_to(p[v]) : Math::INF;
  252. }
  253. }
  254. for (int w = 0; w < N; w++) {
  255. for (int u = 0; u < N; u++) {
  256. for (int v = 0; v < N; v++) {
  257. if (d[u][v] > d[u][w] + d[w][v]) {
  258. d[u][v] = d[u][w] + d[w][v];
  259. }
  260. }
  261. }
  262. }
  263. // Display statistics.
  264. int count = 0;
  265. for (int u = 0; u < N; u++) {
  266. for (int v = 0; v < N; v++) {
  267. if (adj[u][v]) {
  268. count++;
  269. }
  270. }
  271. }
  272. print_verbose(vformat("Test #%4d: %3d edges, ", test + 1, count));
  273. count = 0;
  274. for (int u = 0; u < N; u++) {
  275. for (int v = 0; v < N; v++) {
  276. if (!Math::is_inf(d[u][v])) {
  277. count++;
  278. }
  279. }
  280. }
  281. print_verbose(vformat("%3d/%d pairs of reachable points\n", count - N, N * (N - 1)));
  282. // Check A*'s output.
  283. bool match = true;
  284. for (int u = 0; u < N; u++) {
  285. for (int v = 0; v < N; v++) {
  286. if (u != v) {
  287. Vector<int64_t> route = a.get_id_path(u, v);
  288. if (!Math::is_inf(d[u][v])) {
  289. // Reachable.
  290. if (route.size() == 0) {
  291. print_verbose(vformat("From %d to %d: A* did not find a path\n", u, v));
  292. match = false;
  293. goto exit;
  294. }
  295. float astar_dist = 0;
  296. for (int i = 1; i < route.size(); i++) {
  297. if (!adj[route[i - 1]][route[i]]) {
  298. print_verbose(vformat("From %d to %d: edge (%d, %d) does not exist\n",
  299. u, v, route[i - 1], route[i]));
  300. match = false;
  301. goto exit;
  302. }
  303. astar_dist += p[route[i - 1]].distance_to(p[route[i]]);
  304. }
  305. if (!Math::is_equal_approx(astar_dist, d[u][v])) {
  306. print_verbose(vformat("From %d to %d: Floyd-Warshall gives %.6f, A* gives %.6f\n",
  307. u, v, d[u][v], astar_dist));
  308. match = false;
  309. goto exit;
  310. }
  311. } else {
  312. // Unreachable.
  313. if (route.size() > 0) {
  314. print_verbose(vformat("From %d to %d: A* somehow found a nonexistent path\n", u, v));
  315. match = false;
  316. goto exit;
  317. }
  318. }
  319. }
  320. }
  321. }
  322. exit:
  323. CHECK_MESSAGE(match, "Found all paths.");
  324. }
  325. }
  326. } // namespace TestAStar