test_astar.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*************************************************************************/
  2. /* test_astar.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. PoolVector<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. PoolVector<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));
  94. ok = ok && (a.are_points_connected(4, 1));
  95. ok = ok && (a.are_points_connected(2, 1, false));
  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. }
  153. if (Math::rand() % 2 == 1) {
  154. // Add a (possibly existing) directed edge and confirm connectivity
  155. a.connect_points(u, v, false);
  156. ok = ok && (a.are_points_connected(u, v, false));
  157. } else {
  158. // Remove a (possibly nonexistent) directed edge and confirm disconnectivity
  159. a.disconnect_points(u, v, false);
  160. ok = ok && (a.are_points_connected(u, v, false) == false);
  161. }
  162. }
  163. // Random tests for point removal
  164. for (int i = 0; i < 20000; i++) {
  165. a.clear();
  166. for (int j = 0; j < 5; j++) {
  167. a.add_point(j, Vector3(0, 0, 0));
  168. }
  169. // Add or remove random edges
  170. for (int j = 0; j < 10; j++) {
  171. int u = Math::rand() % 5;
  172. int v = Math::rand() % 4;
  173. if (u == v) {
  174. v = 4;
  175. }
  176. if (Math::rand() % 2 == 1) {
  177. a.connect_points(u, v, false);
  178. } else {
  179. a.disconnect_points(u, v, false);
  180. }
  181. }
  182. // Remove point 0
  183. a.remove_point(0);
  184. // White box: this will check all edges remaining in the segments set
  185. for (int j = 1; j < 5; j++) {
  186. ok = ok && (a.are_points_connected(0, j, true) == false);
  187. }
  188. }
  189. // It's been great work, cheers \(^ ^)/
  190. return ok;
  191. }
  192. bool test_solutions() {
  193. // Random stress tests with Floyd-Warshall
  194. const int N = 30;
  195. Math::seed(0);
  196. for (int test = 0; test < 1000; test++) {
  197. AStar a;
  198. Vector3 p[N];
  199. bool adj[N][N] = { { false } };
  200. // Assign initial coordinates
  201. for (int u = 0; u < N; u++) {
  202. p[u].x = Math::rand() % 100;
  203. p[u].y = Math::rand() % 100;
  204. p[u].z = Math::rand() % 100;
  205. a.add_point(u, p[u]);
  206. }
  207. // Generate a random sequence of operations
  208. for (int i = 0; i < 1000; i++) {
  209. // Pick two different vertices
  210. int u, v;
  211. u = Math::rand() % N;
  212. v = Math::rand() % (N - 1);
  213. if (u == v) {
  214. v = N - 1;
  215. }
  216. // Pick a random operation
  217. int op = Math::rand();
  218. switch (op % 9) {
  219. case 0:
  220. case 1:
  221. case 2:
  222. case 3:
  223. case 4:
  224. case 5:
  225. // Add edge (u, v); possibly bidirectional
  226. a.connect_points(u, v, op % 2);
  227. adj[u][v] = true;
  228. if (op % 2) {
  229. adj[v][u] = true;
  230. }
  231. break;
  232. case 6:
  233. case 7:
  234. // Remove edge (u, v); possibly bidirectional
  235. a.disconnect_points(u, v, op % 2);
  236. adj[u][v] = false;
  237. if (op % 2) {
  238. adj[v][u] = false;
  239. }
  240. break;
  241. case 8:
  242. // Remove point u and add it back; clears adjacent edges and changes coordinates
  243. a.remove_point(u);
  244. p[u].x = Math::rand() % 100;
  245. p[u].y = Math::rand() % 100;
  246. p[u].z = Math::rand() % 100;
  247. a.add_point(u, p[u]);
  248. for (v = 0; v < N; v++) {
  249. adj[u][v] = adj[v][u] = false;
  250. }
  251. break;
  252. }
  253. }
  254. // Floyd-Warshall
  255. float d[N][N];
  256. for (int u = 0; u < N; u++) {
  257. for (int v = 0; v < N; v++) {
  258. d[u][v] = (u == v || adj[u][v]) ? p[u].distance_to(p[v]) : INFINITY;
  259. }
  260. }
  261. for (int w = 0; w < N; w++) {
  262. for (int u = 0; u < N; u++) {
  263. for (int v = 0; v < N; v++) {
  264. if (d[u][v] > d[u][w] + d[w][v]) {
  265. d[u][v] = d[u][w] + d[w][v];
  266. }
  267. }
  268. }
  269. }
  270. // Display statistics
  271. int count = 0;
  272. for (int u = 0; u < N; u++) {
  273. for (int v = 0; v < N; v++) {
  274. if (adj[u][v]) {
  275. count++;
  276. }
  277. }
  278. }
  279. printf("Test #%4d: %3d edges, ", test + 1, count);
  280. count = 0;
  281. for (int u = 0; u < N; u++) {
  282. for (int v = 0; v < N; v++) {
  283. if (!Math::is_inf(d[u][v])) {
  284. count++;
  285. }
  286. }
  287. }
  288. printf("%3d/%d pairs of reachable points\n", count - N, N * (N - 1));
  289. // Check A*'s output
  290. bool match = true;
  291. for (int u = 0; u < N; u++) {
  292. for (int v = 0; v < N; v++) {
  293. if (u != v) {
  294. PoolVector<int> route = a.get_id_path(u, v);
  295. if (!Math::is_inf(d[u][v])) {
  296. // Reachable
  297. if (route.size() == 0) {
  298. printf("From %d to %d: A* did not find a path\n", u, v);
  299. match = false;
  300. goto exit;
  301. }
  302. float astar_dist = 0;
  303. for (int i = 1; i < route.size(); i++) {
  304. if (!adj[route[i - 1]][route[i]]) {
  305. printf("From %d to %d: edge (%d, %d) does not exist\n",
  306. u, v, route[i - 1], route[i]);
  307. match = false;
  308. goto exit;
  309. }
  310. astar_dist += p[route[i - 1]].distance_to(p[route[i]]);
  311. }
  312. if (!Math::is_equal_approx(astar_dist, d[u][v])) {
  313. printf("From %d to %d: Floyd-Warshall gives %.6f, A* gives %.6f\n",
  314. u, v, d[u][v], astar_dist);
  315. match = false;
  316. goto exit;
  317. }
  318. } else {
  319. // Unreachable
  320. if (route.size() > 0) {
  321. printf("From %d to %d: A* somehow found a nonexistent path\n", u, v);
  322. match = false;
  323. goto exit;
  324. }
  325. }
  326. }
  327. }
  328. }
  329. exit:
  330. if (!match) {
  331. return false;
  332. }
  333. }
  334. return true;
  335. }
  336. typedef bool (*TestFunc)();
  337. TestFunc test_funcs[] = {
  338. test_abc,
  339. test_abcx,
  340. test_add_remove,
  341. test_solutions,
  342. nullptr
  343. };
  344. MainLoop *test() {
  345. int count = 0;
  346. int passed = 0;
  347. while (true) {
  348. if (!test_funcs[count]) {
  349. break;
  350. }
  351. bool pass = test_funcs[count]();
  352. if (pass) {
  353. passed++;
  354. }
  355. OS::get_singleton()->print("\t%s\n", pass ? "PASS" : "FAILED");
  356. count++;
  357. }
  358. OS::get_singleton()->print("\n");
  359. OS::get_singleton()->print("Passed %i of %i tests\n", passed, count);
  360. return nullptr;
  361. }
  362. } // namespace TestAStar