test_astar.cpp 11 KB

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