test_astar.h 11 KB

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