forked from Ohohcakester/Any-Angle-Pathfinding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgoTest.java
1046 lines (880 loc) · 44.4 KB
/
AlgoTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main;
import java.util.ArrayList;
import algorithms.AStar;
import algorithms.AStarOctileHeuristic;
import algorithms.AStarStaticMemory;
import algorithms.AcceleratedAStar;
import algorithms.Anya;
import algorithms.anya16.Anya16;
import algorithms.BasicThetaStar;
import algorithms.BreadthFirstSearch;
import algorithms.JumpPointSearch;
import algorithms.LazyThetaStar;
import algorithms.PathFindingAlgorithm;
import algorithms.RecursiveThetaStar;
import algorithms.VisibilityGraphAlgorithm;
import algorithms.datatypes.Point;
import algorithms.sparsevgs.EdgeNLevelSparseVisibilityGraphAlgorithm;
import algorithms.sparsevgs.EdgeNLevelSparseVisibilityGraphAlgorithmFibHeap;
import algorithms.sparsevgs.SparseVisibilityGraphAlgorithm;
import algorithms.sparsevgs.SparseVisibilityGraphAlgorithmFibHeap;
import algorithms.sparsevgs.VisibilityGraphAlgorithmOptimised;
import algorithms.strictthetastar.RecursiveStrictThetaStar;
import algorithms.strictthetastar.StrictThetaStar;
import algorithms.sg16.SG16Algorithm;
import grid.GridGraph;
import main.analysis.MazeAnalysis;
import main.analysis.TwoPoint;
import main.mazes.MazeAndTestCases;
import main.mazes.StoredTestMazes;
import main.testgen.PathLengthClass;
import main.testgen.StandardMazes;
import main.testgen.StartEndPointData;
import main.testgen.TestDataGenerator;
import main.testgen.TestDataLibrary;
import main.utility.TimeCounter;
import main.utility.Utility;
import uiandio.BenchmarkGraphImporter;
import uiandio.BenchmarkGraphSets;
import uiandio.FileIO;
import uiandio.GraphImporter;
public class AlgoTest {
private static FileIO io;
private static boolean writeToFile = true;
private static String outputdir = "testResults/";
private static void setOutputdir(String postfix) {
outputdir = "testResults_" + postfix + "/";
}
private static boolean pleaseTurnOffAssertions() {
System.out.println("Please turn off assertions during tests (Run -> Run Configurations -> Arguments -> remove -ea)");
return false;
}
public static void run() {
assert pleaseTurnOffAssertions();
FileIO.makeDirs(outputdir);
System.gc(); System.gc();
String[] algoNames = new String[]{
// Define algorithms to test here
"Anya16",
"BasicThetaStar",
};
String[] mapSetNames = new String[]{
// Define the map sets to test on here
"benchmarks",
"automatadcmazes",
};
for (int i=0; i<algoNames.length; ++i) {
for (int j=0; j<mapSetNames.length; ++j) {
testSequence(getAlgo(algoNames[i]), algoNames[i], mapSetNames[j], "");
}
}
}
public static void runWithArgs(String[] args) {
assert pleaseTurnOffAssertions();
// 1. Algorithm Name
// 2. Map Set
// 3. Test Type
// 4. Output Directory name postfix
String algoName = args[1];
String mapSetName = args[2];
String testType = args.length >= 4 ? args[3] : "";
if (args.length >= 5) setOutputdir(args[4]);
FileIO.makeDirs(outputdir);
System.gc(); System.gc();
AlgoFunction algo = getAlgo(algoName);
testSequence(algo, algoName, mapSetName, testType);
}
public static AlgoFunction getAlgo(String algoName) {
switch (algoName) {
case "AStar": return AStar::new;
case "AStarSLD": return AStarStaticMemory::new;
case "AStarPS": return AStar::postSmooth;
case "AStar Octile": return AStarOctileHeuristic::new;
case "AStarOctile PostSmooth": return AStarOctileHeuristic::postSmooth;
case "BreadthFirstSearch": return BreadthFirstSearch::new;
case "JumpPointSearch": return JumpPointSearch::new;
case "JPS PostSmooth": return JumpPointSearch::postSmooth;
case "LazyThetaStar": return LazyThetaStar::new;
case "BasicThetaStar": return BasicThetaStar::new;
case "BasicThetaStar_PS": return BasicThetaStar::postSmooth;
case "Dijkstra": return AStar::dijkstra;
case "AcceleratedAStar": return AcceleratedAStar::new;
case "Anya": return Anya::new;
case "Anya16": return Anya16::new;
case "VisibilityGraphs": return VisibilityGraphAlgorithm::new;
case "VisibilityGraphReuse": return VisibilityGraphAlgorithm::graphReuse;
case "VisibilityGraphReuseOptimised": return VisibilityGraphAlgorithmOptimised::graphReuse;
case "StrictThetaStar": return StrictThetaStar::new;
case "StrictThetaStarPS": return StrictThetaStar::postSmooth;
case "RecStrictThetaStar": return RecursiveStrictThetaStar::new;
case "RecStrictThetaStarPS": return RecursiveStrictThetaStar::postSmooth;
case "RecStrictThetaStar_2": return (a, b, c, d, e) -> RecursiveStrictThetaStar.depthLimit(a, b, c, d, e, 2);
case "RecursiveThetaStar": return RecursiveThetaStar::new;
case "SparseVisibilityGraphs": return SparseVisibilityGraphAlgorithm::graphReuse;
case "SparseVisibilityGraphsFibHeap": return SparseVisibilityGraphAlgorithmFibHeap::graphReuse;
case "EdgeNLevelSparseVisibilityGraphs": return EdgeNLevelSparseVisibilityGraphAlgorithm::graphReuse;
case "EdgeNLevelSparseVisibilityGraphsFibHeap": return EdgeNLevelSparseVisibilityGraphAlgorithmFibHeap::graphReuse;
case "Edge1LevelSparseVisibilityGraphs": return EdgeNLevelSparseVisibilityGraphAlgorithm.withLevelLimit(1);
case "Edge2LevelSparseVisibilityGraphs": return EdgeNLevelSparseVisibilityGraphAlgorithm.withLevelLimit(2);
case "Edge3LevelSparseVisibilityGraphs": return EdgeNLevelSparseVisibilityGraphAlgorithm.withLevelLimit(3);
case "SG16A": return SG16Algorithm::new;
}
throw new UnsupportedOperationException("Invalid Algorithm! " + algoName);
}
public static void testSequence(AlgoFunction algo, String name, String mapSetName, String testType) {
String path = outputdir + name.replace(" ", "_") + ".txt";
if (writeToFile) io = new FileIO(path);
boolean pathLengthOnly = false;
boolean runningTimeOnly = false;
boolean initialisationTime = false;
// TestFunctionData testFunction_slow = printAverageData(50, 5);
TestFunctionData testFunction_single = printAverageData(1,1);
TestFunctionData testFunction_slow = printAverageData(5, 5);
TestFunctionData testFunction_fast = printAverageData(50, 30);
switch (testType) {
case "pathLengthOnly":
testFunction_single = testPathLengthOnly;
testFunction_slow = testPathLengthOnly;
testFunction_fast = testPathLengthOnly;
break;
case "individualPathLengthOnly":
testFunction_single = analyseIndividualPaths;
testFunction_slow = analyseIndividualPaths;
testFunction_fast = analyseIndividualPaths;
break;
case "runningTimeOnly":
testFunction_single = testIndividualRunningTimes(1, 1);
testFunction_slow = testIndividualRunningTimes(20, 1);
testFunction_fast = testIndividualRunningTimes(50, 5);
break;
case "initialisationTime":
testFunction_single = testInitialisationTime;
testFunction_slow = testInitialisationTime;
testFunction_fast = testInitialisationTime;
break;
case "testing":
testFunction_single = (a,b,c,d) -> {};
testFunction_slow = (a,b,c,d) -> {};
testFunction_fast = (a,b,c,d) -> {};
break;
case "":
case "default":
break;
default:
throw new UnsupportedOperationException("Invalid Test Type");
}
println("=== Testing " + name + " === " + mapSetName + " ===");
switch (mapSetName) {
case "gamemaps": {
testOnMazeData("sc2_steppesofwar", algo, testFunction_slow);
testOnMazeData("sc2_losttemple", algo, testFunction_slow);
testOnMazeData("sc2_extinction", algo, testFunction_slow);
testOnMazeData("baldursgate_AR0070SR", algo, testFunction_slow);
testOnMazeData("baldursgate_AR0705SR", algo, testFunction_slow);
testOnMazeData("baldursgate_AR0418SR", algo, testFunction_slow);
testOnMazeData("sc1_TheFrozenSea", algo, testFunction_slow);
testOnMazeData("sc1_SpaceDebris", algo, testFunction_slow);
testOnMazeData("wc3_icecrown", algo, testFunction_slow);
testOnMazeData("wc3_dragonfire", algo, testFunction_slow);
testOnMazeData("sc1_Legacy", algo, testFunction_slow);
testOnMazeData("sc1_GreenerPastures", algo, testFunction_slow);
testOnMazeData("baldursgate_AR0603SR", algo, testFunction_slow);
testOnMazeData("wc3_mysticisles", algo, testFunction_slow);
testOnMazeData("wc3_petrifiedforest", algo, testFunction_slow);
break;
}
case "generatedmaps": {
// Low Density - 6% - 50x50
testOnMazeData("def_iCUZANYD_iSB_iSB_iSB", algo, testFunction_slow);
// Medium Density - 20% - 50x50
testOnMazeData("def_i10VA3PD_iSB_iSB_iP", algo, testFunction_slow);
// High Density - 40% - 50x50
testOnMazeData("def_i3ML5FBD_iSB_iSB_iH", algo, testFunction_slow);
// Low Density - 6% - 300x300
testOnMazeData("def_iHHLNUOB_iMJ_iMJ_iSB", algo, testFunction_slow);
// Medium Density - 20% - 300x300
testOnMazeData("def_iZLPIX5B_iMJ_iMJ_iP", algo, testFunction_slow);
// High Density - 40% - 300x300
testOnMazeData("def_iVVJKDR_iMJ_iMJ_iH", algo, testFunction_slow);
// Low Density - 6% - 500x500
testOnMazeData("def_iIRXXUKC_iUP_iUP_iSB", algo, testFunction_slow);
// Medium Density - 20% - 500x500
testOnMazeData("def_iOMJ14Z_iUP_iUP_iP", algo, testFunction_slow);
// LowDensity2 - 6% - 300x300
testOnMazeData("def_iMJWB0QC_iMJ_iMJ_iSB", algo, testFunction_slow);
// MediumDensity2 - 20% - 300x300
testOnMazeData("def_iBCA5SS_iMJ_iMJ_iP", algo, testFunction_slow);
// HighDensity2 - 40% - 300x300
testOnMazeData("def_i11AHREB_iMJ_iMJ_iH", algo, testFunction_slow);
// LowDensity3 - 6% - 300x300
testOnMazeData("def_iOH1TZ0D_iMJ_iMJ_iSB", algo, testFunction_slow);
// MediumDensity3 - 20% - 300x300
testOnMazeData("def_iAN3IE0C_iMJ_iMJ_iP", algo, testFunction_slow);
// HighDensity3 - 40% - 300x300
testOnMazeData("def_iI0RFKYC_iMJ_iMJ_iH", algo, testFunction_slow);
// 6%Density - 500x500
testOnMazeData("def_iIRXXUKC_iUP_iUP_iSB", algo, testFunction_slow);
// 20%Density - 500x500
testOnMazeData("def_iOMJ14Z_iUP_iUP_iP", algo, testFunction_slow);
// 40%Density - 500x500
testOnMazeData("def_iREPZHKB_iUP_iUP_iH", algo, testFunction_slow);
// 6%Density2 - 500x500
testOnMazeData("def_i5QEPQ2C_iUP_iUP_iSB", algo, testFunction_slow);
// 20%Density2 - 500x500
testOnMazeData("def_iKMRKFCD_iUP_iUP_iP", algo, testFunction_slow);
// 40%Density2 - 500x500
testOnMazeData("def_i5GM4YH_iUP_iUP_iH", algo, testFunction_slow);
// 6%Density3 - 500x500
testOnMazeData("def_iFOLAODC_iUP_iUP_iSB", algo, testFunction_slow);
// 20%Density3 - 500x500
testOnMazeData("def_i5GZXLUD_iUP_iUP_iP", algo, testFunction_slow);
// 40%Density3 - 500x500
testOnMazeData("def_iA0VKRW_iUP_iUP_iH", algo, testFunction_slow);
testOnMazeData("gen_1000x1000_50_iY5U5GAC", algo, testFunction_slow);
testOnMazeData("gen_1000x1000_15_iFTBETRD", algo, testFunction_slow);
testOnMazeData("gen_1000x1000_7_i3WH4IJD", algo, testFunction_slow);
testOnMazeData("gen_2000x2000_50_iK54OY1C", algo, testFunction_slow);
testOnMazeData("gen_2000x2000_15_i4Z44NPC", algo, testFunction_slow);
testOnMazeData("gen_2000x2000_7_iXT3AEED", algo, testFunction_slow);
testOnMazeData("gen_3000x3000_50_iUE2IRAD", algo, testFunction_slow);
testOnMazeData("gen_3000x3000_15_iGUM1R2C", algo, testFunction_slow);
testOnMazeData("gen_3000x3000_7_iSR3L1J", algo, testFunction_slow);
testOnMazeData("gen_4000x4000_50_i0GHV1UD", algo, testFunction_slow);
testOnMazeData("gen_4000x4000_15_iNK5KHO", algo, testFunction_slow);
testOnMazeData("gen_4000x4000_7_iNUJNZ3", algo, testFunction_slow);
testOnMazeData("gen_5000x5000_50_iCFL2G3B", algo, testFunction_slow);
testOnMazeData("gen_5000x5000_15_i0BTXUD", algo, testFunction_slow);
testOnMazeData("gen_5000x5000_7_iHOPN1S", algo, testFunction_slow);
testOnMazeData("obst10_random512-10-7", algo, testFunction_slow);
testOnMazeData("obst40_random512-40-0", algo, testFunction_slow);
testOnMazeData("obst40_random512-40-7", algo, testFunction_slow);
testOnMazeData("corr2_maze512-2-5", algo, testFunction_slow);
testOnMazeData("corr2_maze512-2-1", algo, testFunction_slow);
testOnMazeData("corr2_maze512-2-7", algo, testFunction_slow);
testOnMazeData("corr2_maze512-2-3", algo, testFunction_slow);
testOnMazeData("corr2_maze512-2-9", algo, testFunction_slow);
break;
}
case "benchmarks": {
testOnBenchmarkMapSet("bg512", algo, testFunction_single);
// testOnBenchmarkMapSet("dao", algo, testFunction_single);
testOnBenchmarkMapSet("sc1", algo, testFunction_single);
testOnBenchmarkMapSet("wc3maps512", algo, testFunction_single);
break;
}
case "benchmarksrandom": {
testOnBenchmarkMapSet("random10", algo, testFunction_single);
testOnBenchmarkMapSet("random20", algo, testFunction_single);
testOnBenchmarkMapSet("random30", algo, testFunction_single);
testOnBenchmarkMapSet("random40", algo, testFunction_single);
break;
}
case "automataoriginal": {
for (int resolutionIndex=0; resolutionIndex<10; resolutionIndex++) {
for (int scaleIndex=0; scaleIndex<7; ++scaleIndex) {
testOnStoredMaze(StoredTestMazes.loadAutomataMaze(scaleIndex, resolutionIndex), algo, testFunction_slow);
System.gc();System.gc();
try {Thread.sleep(2000);}
catch (Exception e) {throw new UnsupportedOperationException(e.getMessage());}
System.gc();System.gc();
}
}
break;
}
case "scaledmazes": {
testScaledMazes("sc1_NovaStation", algo, testFunction_slow);
testScaledMazes("wc3_darkforest", algo, testFunction_slow);
testScaledMazes("sc1_RedCanyons", algo, testFunction_slow);
testScaledMazes("wc3_swampofsorrows", algo, testFunction_slow);
testScaledMazes("sc1_Triskelion", algo, testFunction_slow);
testScaledMazes("wc3_theglaive", algo, testFunction_slow);
break;
}
case "tiledmazes": {
for (int mazePoolIndex=0;mazePoolIndex<4;++mazePoolIndex) {
for (int size = 4; size <= 12; size+=4) {
testOnStoredMaze(StoredTestMazes.loadTiledMaze(mazePoolIndex, size), algo, testFunction_slow);
System.gc();System.gc();
try {Thread.sleep(2000);}
catch (Exception e) {throw new UnsupportedOperationException(e.getMessage());}
System.gc();System.gc();
}
}
break;
}
case "automatadcmazes": {
for (int percentBlockedIndex=0; percentBlockedIndex<2; ++percentBlockedIndex) {
for (int resolutionIndex=0; resolutionIndex<5; resolutionIndex+=2) {
for (int sizeIndex=0; sizeIndex<5; sizeIndex+=2) {
testOnStoredMaze(StoredTestMazes.loadAutomataDCMaze(sizeIndex, resolutionIndex, percentBlockedIndex), algo, testFunction_single);
System.gc();
try {Thread.sleep(2000);}
catch (Exception e) {throw new UnsupportedOperationException(e.getMessage());}
System.gc();
try {Thread.sleep(2000);}
catch (Exception e) {throw new UnsupportedOperationException(e.getMessage());}
System.gc();
try {Thread.sleep(2000);}
catch (Exception e) {throw new UnsupportedOperationException(e.getMessage());}
System.gc();
}
}
}
break;
}
case "mazemaps": {
int[] connectednessRatioIndexes = new int[] {0,1,2};
for (int sizeIndex=0; sizeIndex<7; sizeIndex+=2) {
int corridorWidthIndex = 1;
for (int j=0; j<connectednessRatioIndexes.length; ++j) {
int connectednessRatioIndex = connectednessRatioIndexes[j];
testOnStoredMaze(StoredTestMazes.loadMazeMaze(sizeIndex, corridorWidthIndex, connectednessRatioIndex), algo, testFunction_slow);
System.gc();System.gc();
try {Thread.sleep(2000);}
catch (Exception e) {throw new UnsupportedOperationException(e.getMessage());}
System.gc();System.gc();
}
}
break;
}
default:
throw new UnsupportedOperationException("Invalid Map Set Name!");
}
println("=== FINISHED TEST FOR " + name + " === " + mapSetName + " ===");
println();
if (writeToFile) io.close();
}
public static void testOnBenchmarkMapSet(String setName, AlgoFunction algo, TestFunctionData testFunction) {
String[] mapNames = BenchmarkGraphSets.getBenchmarkSet(setName);
for (int i=0; i<mapNames.length;++i) {
Utility.cleanUpPreallocatedMemory();
testOnBenchmarkMaze(mapNames[i], algo, testFunction);
Utility.cleanUpPreallocatedMemory();
System.gc();
try {Thread.sleep(2000);}
catch (Exception e) {throw new UnsupportedOperationException(e.getMessage());}
System.gc();
}
}
public static void testScaledMazes(String mazeName, AlgoFunction algo, TestFunctionData testFunction) {
for (int scale = 4; scale <= 12; scale += 4) {
testOnStoredMaze(StoredTestMazes.loadScaledMaze(mazeName,scale), algo, testFunction);
System.gc();System.gc();
try {Thread.sleep(2000);}
catch (Exception e) {throw new UnsupportedOperationException(e.getMessage());}
System.gc();System.gc();
}
}
public static void printMazeDetails(String mazeName, GridGraph gridGraph) {
MazeAnalysis.Options options = new MazeAnalysis.Options();
options.sizeX = true;
options.sizeY = true;
options.averageBlockedIslandSize = true;
options.averageFloatingBlockedIslandSize = true;
options.blockDensity = true;
options.averageOpenSpaceSize = true;
options.largestRatioToSecond = true;
options.largestRatioToRemaining = true;
MazeAnalysis an = new MazeAnalysis(gridGraph, options);
StringBuilder sb = new StringBuilder();
sb.append(mazeName);
sb.append(" - ").append(an.sizeX);
sb.append(" - ").append(an.sizeY);
sb.append(" - ").append(an.blockDensity);
sb.append(" - ").append(an.averageBlockedIslandSize);
sb.append(" - ").append(an.averageFloatingBlockedIslandSize);
sb.append(" - ").append(an.averageOpenSpaceSize);
sb.append(" - ").append(an.largestRatioToSecond);
sb.append(" - ").append(an.largestRatioToRemaining);
println(sb.toString());
}
private static void println(Object line) {
if (writeToFile) {
io.writeLine(line.toString());
io.flush();
System.out.println(line);
} else {
System.out.println(line);
}
}
private static void println() {
println("");
}
public static ArrayList<TwoPoint> toTwoPointlist(int... points) {
return TestDataGenerator.generateTwoPointList(points);
}
public static void testOnMaze(String mazeName, AlgoFunction algoFunction, TestFunction test) {
ArrayList<TwoPoint> problems = GraphImporter.loadStoredMazeProblems(mazeName);
testOnMaze(mazeName, problems, algoFunction, test);
}
public static void testOnMaze(String mazeName, ArrayList<TwoPoint> problems, AlgoFunction algoFunction,
TestFunction test) {
GridGraph gridGraph = GraphImporter.loadStoredMaze(mazeName);
test.test(mazeName, gridGraph, problems, algoFunction);
}
public static void testOnGraph(GridGraph gridGraph, ArrayList<TwoPoint> problems, AlgoFunction algoFunction,
TestFunction test) {
test.test("undefined", gridGraph, problems, algoFunction);
}
public static void testOnBenchmarkMaze(String mazeName, AlgoFunction algoFunction, TestFunctionData test) {
Utility.cleanUpPreallocatedMemory();
{
ArrayList<StartEndPointData> problems = BenchmarkGraphImporter.loadBenchmarkMazeProblems(mazeName);
GridGraph gridGraph = BenchmarkGraphImporter.loadBenchmarkMaze(mazeName);
printMazeDetails(mazeName, gridGraph);
test.test(mazeName, gridGraph, problems, algoFunction);
}
Utility.cleanUpPreallocatedMemory();
}
public static void testOnStoredMaze(MazeAndTestCases mazeAndTestCases, AlgoFunction algoFunction, TestFunctionData test) {
Utility.cleanUpPreallocatedMemory();
{
ArrayList<StartEndPointData> problems = mazeAndTestCases.problems;
GridGraph gridGraph = mazeAndTestCases.gridGraph;
String mazeName = mazeAndTestCases.mazeName;
printMazeDetails(mazeName, gridGraph);
test.test(mazeName, gridGraph, problems, algoFunction);
mazeAndTestCases = null;
}
Utility.cleanUpPreallocatedMemory();
}
public static void testOnMazeData(String mazeName, AlgoFunction algoFunction, TestFunctionData test) {
ArrayList<StartEndPointData> problems = GraphImporter.loadStoredMazeProblemData(mazeName);
testOnMazeData(mazeName, problems, algoFunction, test);
Utility.cleanUpPreallocatedMemory();
}
public static void testOnMazeData(String mazeName, ArrayList<StartEndPointData> problems, AlgoFunction algoFunction, TestFunctionData test) {
GridGraph gridGraph = GraphImporter.loadStoredMaze(mazeName);
printMazeDetails(mazeName, gridGraph);
test.test(mazeName, gridGraph, problems, algoFunction);
}
public static void testOnGraphData(GridGraph gridGraph, ArrayList<StartEndPointData> problems, AlgoFunction algoFunction, TestFunctionData test) {
test.test("undefined", gridGraph, problems, algoFunction);
}
private static final TestFunctionData testPathLengthOnly = (mazeName, gridGraph, problems, algoFunction) -> {
TestResult[] testResults = new TestResult[problems.size()];
int index = 0;
double totalPathLength = 0;
int totalTautPaths = 0;
int totalOptimalPaths = 0;
TimeCounter.reset();
for (StartEndPointData problem : problems) {
TwoPoint tp = new TwoPoint(problem.start, problem.end);
testResults[index] = testAlgorithmPathLength(gridGraph, algoFunction, tp);
totalPathLength += testResults[index].pathLength / problem.shortestPath;
totalTautPaths += (testResults[index].isTaut ? 1 : 0);
totalOptimalPaths += (Utility.isOptimal(testResults[index].pathLength, problem.shortestPath) ? 1 : 0);
index++;
TimeCounter.iterations++;
}
int nResults = testResults.length;
TimeCounter.printAverage();
println("Average Path Length: " + (totalPathLength / nResults));
println("Percentage Taut: " + (totalTautPaths / (float) nResults));
println("Percentage Optimal: " + (totalOptimalPaths / (float) nResults));
};
private static final TestFunctionData analyseIndividualPaths = (mazeName, gridGraph, problems, algoFunction) -> {
println("|||||mazeName,start,end,optimalLength,actualLength,isTaut,isOptimal");
for (StartEndPointData problem : problems) {
TwoPoint tp = new TwoPoint(problem.start, problem.end);
TestResult testResult = testAlgorithmPathLength(gridGraph, algoFunction, tp);
// double pathLengthRatio = testResult.pathLength / problem.shortestPath;
int isTaut = (testResult.isTaut ? 1 : 0);
int isOptimal = (Utility.isOptimal(testResult.pathLength, problem.shortestPath) ? 1 : 0);
StringBuilder sb = new StringBuilder(">>>>>");
sb.append(mazeName).append(",");
sb.append(problem.start).append(",");
sb.append(problem.end).append(",");
sb.append(problem.shortestPath).append(",");
sb.append(testResult.pathLength).append(",");
sb.append(isTaut).append(",");
sb.append(isOptimal);
println(sb.toString());
}
};
private static final TestFunction printAverage = (mazeName, gridGraph, problems, algoFunction) -> {
int sampleSize = 10;
int nTrials = 5;
TestResult[] testResults = new TestResult[problems.size()];
int index = 0;
for (TwoPoint problem : problems) {
testResults[index] = testAlgorithm(gridGraph, algoFunction, problem, sampleSize, nTrials);
index++;
}
double totalMean = 0;
double totalSD = 0;
double totalPathLength = 0;
int totalTautPaths = 0;
for (TestResult testResult : testResults) {
totalMean += testResult.time;
totalSD += testResult.timeSD;
totalPathLength += testResult.pathLength;
totalTautPaths += (testResult.isTaut ? 1 : 0);
}
int nResults = testResults.length;
println("Sample Size: " + sampleSize + " x " + nTrials + " trials");
println("Average Time: " + (totalMean / nResults));
println("Average SD: " + (totalSD / nResults));
println("Average Path Length: " + (totalPathLength / nResults));
println("Percentage Taut: " + (totalTautPaths / (float) nResults));
};
private static final TestFunctionData printAverageData(int sampleSize, int nTrials) {
return (mazeName, gridGraph, problems, algoFunction) -> {
double sum = 0;
double sumSquare = 0;
double totalPathLength = 0;
int totalTautPaths = 0;
int totalOptimalPaths = 0;
int nResults = 0;
TimeCounter.reset();
for (StartEndPointData problem : problems) {
TwoPoint tp = new TwoPoint(problem.start, problem.end);
TestResult testResult = testAlgorithm(gridGraph, algoFunction, tp, sampleSize, nTrials);
sum += testResult.time;
sumSquare += testResult.time * testResult.time;
totalPathLength += testResult.pathLength / problem.shortestPath;
totalTautPaths += (testResult.isTaut ? 1 : 0);
totalOptimalPaths += (Utility.isOptimal(testResult.pathLength, problem.shortestPath) ? 1 : 0);
nResults++;
TimeCounter.iterations += nTrials * sampleSize;
}
//TimeCounter.printAverage();
println(TimeCounter.getPrintAverageString());
double mean = (double) sum / nResults;
double secondMomentTimesN = (double) sumSquare;
double sampleVariance = (secondMomentTimesN - nResults * (mean * mean)) / (nResults - 1);
double standardDeviation = Math.sqrt(sampleVariance);
println("Sample Size: " + sampleSize + " x " + nTrials + " trials");
println("Average Time: " + mean);
println("Standard Dev: " + standardDeviation);
println("Average Path Length: " + (totalPathLength / nResults));
println("Percentage Taut: " + (totalTautPaths / (float) nResults));
println("Percentage Optimal: " + (totalOptimalPaths / (float) nResults));
// StrictVisibilityGraphAlgorithmV2.printTimes();a=true;
println();
};
}
private static final TestFunctionData testIndividualRunningTimes(int sampleSize, int nTrials) {
return (mazeName, gridGraph, problems, algoFunction) -> {
ArrayList<TwoPoint> twoPointList = new ArrayList<>();
ArrayList<long[]> runningTimesList = new ArrayList<>();
TimeCounter.reset();
for (StartEndPointData problem : problems) {
TwoPoint tp = new TwoPoint(problem.start, problem.end);
long[] runningTimes = new long[sampleSize];
TimeCounter.freeze();
// Do two blank runs first to increase consistency of results.
testAlgorithmTimeOnce(gridGraph, algoFunction, tp, 2);
TimeCounter.unfreeze();
for (int i = 0; i < sampleSize; ++i) {
runningTimes[i] = testAlgorithmTimeOnce(gridGraph, algoFunction, tp, nTrials);
}
twoPointList.add(tp);
runningTimesList.add(runningTimes);
}
TimeCounter.print();
println("Maze Name: " + mazeName);
println("Sample Size: " + sampleSize + " x " + nTrials + " trials");
for (int i = 0; i < twoPointList.size(); ++i) {
TwoPoint tp = twoPointList.get(i);
long[] runningTimes = runningTimesList.get(i);
StringBuilder sb = new StringBuilder();
sb.append(tp.p1.x + "-" + tp.p1.y + "_" + tp.p2.x + "-" + tp.p2.y);
for (long runningTime : runningTimes) {
sb.append(" ");
sb.append(runningTime);
}
println(sb);
}
println();
};
}
private static final TestFunctionData testInitialisationTime = (mazeName, gridGraph, problems, algoFunction) -> {
StartEndPointData problem = problems.get(0);
Point p1 = problem.start;
Point p2 = problem.end;
TimeCounter.reset();
long startTime = System.nanoTime();
Utility.generatePath(algoFunction, gridGraph, p1.x, p1.y, p2.x, p2.y);
long endTime = System.nanoTime();
double timeTaken = (endTime - startTime) / 1000000.0;
TimeCounter.printAverage();
println("Initialisation Time: " + timeTaken);
println();
};
private static long testAlgorithmTimeOnce(GridGraph gridGraph, AlgoFunction algoFunction, TwoPoint tp,
int nTrials) {
int startX = tp.p1.x;
int startY = tp.p1.y;
int endX = tp.p2.x;
int endY = tp.p2.y;
long start = System.nanoTime();
for (int i = 0; i < nTrials; i++) {
AlgoTest.testAlgorithmSpeed(algoFunction, gridGraph, startX, startY, endX, endY);
}
long end = System.nanoTime();
long timeTakenNanosecs = end - start;
// System.out.println(timeTakenNanosecs);
return timeTakenNanosecs;
}
private static TestResult testAlgorithmTime(GridGraph gridGraph, AlgoFunction algoFunction, TwoPoint tp,
int sampleSize, int nTrials) {
double[] data = new double[sampleSize];
double sum = 0;
double sumSquare = 0;
int startX = tp.p1.x;
int startY = tp.p1.y;
int endX = tp.p2.x;
int endY = tp.p2.y;
for (int s = 0; s < sampleSize; s++) {
long start = System.nanoTime();
for (int i = 0; i < nTrials; i++) {
AlgoTest.testAlgorithmSpeed(algoFunction, gridGraph, startX, startY, endX, endY);
}
long end = System.nanoTime();
// System.gc();
data[s] = (double) (end - start) / 1000000.;
sum += data[s];
sumSquare += data[s] * data[s];
}
double mean = (double) sum / nTrials / sampleSize;
double secondMomentTimesN = (double) sumSquare / nTrials / nTrials;
double sampleVariance = (secondMomentTimesN - sampleSize * (mean * mean)) / (sampleSize - 1);
double standardDeviation = Math.sqrt(sampleVariance);
TestResult testResult = new TestResult(sampleSize, mean, standardDeviation, -1f, false);
return testResult;
}
private static TestResult testAlgorithmPathLength(GridGraph gridGraph, AlgoFunction algoFunction, TwoPoint tp) {
int[][] path = Utility.generatePath(algoFunction, gridGraph, tp.p1.x, tp.p1.y, tp.p2.x, tp.p2.y);
double pathLength = Utility.computePathLength(gridGraph, path);
boolean isTaut = Utility.isPathTaut(gridGraph, path);
TestResult testResult = new TestResult(-1, -1, -1, pathLength, isTaut);
return testResult;
}
private static TestResult testAlgorithm(GridGraph gridGraph, AlgoFunction algoFunction, TwoPoint tp, int sampleSize,
int nTrials) {
TimeCounter.freeze();
TestResult pathLength = testAlgorithmPathLength(gridGraph, algoFunction, tp);
TimeCounter.unfreeze();
TestResult time = testAlgorithmTime(gridGraph, algoFunction, tp, sampleSize, nTrials);
return new TestResult(time.timesRan, time.time, time.timeSD, pathLength.pathLength, pathLength.isTaut);
}
/**
* Run tests for all the algorithms, and outputs them to the file.<br>
* Please look into this method for more information.<br>
* Comment out the algorithms you don't want to test. EDIT: No longer used.
* Replaced by run() as the main testing method.
*/
@Deprecated
private static void runTestAllAlgos() {
/*
* AnyAnglePathfinding.algoFunction = (gridGraph, sx, sy, ex, ey) -> new
* AStar(gridGraph, sx, sy, ex, ey); runTests("AStar_TI");
*
* AnyAnglePathfinding.algoFunction = (gridGraph, sx, sy, ex, ey) -> new
* BreadthFirstSearch(gridGraph, sx, sy, ex, ey); runTests("BFS");
*
* AnyAnglePathfinding.algoFunction = (gridGraph, sx, sy, ex, ey) ->
* BreadthFirstSearch.postSmooth(gridGraph, sx, sy, ex, ey);
* runTests("BFS-PS");
*
* AnyAnglePathfinding.algoFunction = (gridGraph, sx, sy, ex, ey) ->
* AStar.postSmooth(gridGraph, sx, sy, ex, ey); runTests("AStar-PS_TI");
*
* AnyAnglePathfinding.algoFunction = (gridGraph, sx, sy, ex, ey) ->
* AStar.dijkstra(gridGraph, sx, sy, ex, ey); runTests("Dijkstra");
*
* // Warning: Anya is unstable AnyAnglePathfinding.algoFunction =
* (gridGraph, sx, sy, ex, ey) -> new Anya(gridGraph, sx, sy, ex, ey);
* runTests("Anya");
*
* AnyAnglePathfinding.algoFunction = (gridGraph, sx, sy, ex, ey) -> new
* BasicThetaStar(gridGraph, sx, sy, ex, ey);
* runTests("BasicThetaStar_TI");
*
* AnyAnglePathfinding.algoFunction = (gridGraph, sx, sy, ex, ey) ->
* BasicThetaStar.postSmooth(gridGraph, sx, sy, ex, ey);
* runTests("BasicThetaStar-PS_TI");
*
* Warning: VisibilityGraph is slow AnyAnglePathfinding.algoFunction =
* (gridGraph, sx, sy, ex, ey) -> new
* VisibilityGraphAlgorithm(gridGraph, sx, sy, ex, ey);
* runTests("VisibilityGraph");
*
* AnyAnglePathfinding.algoFunction = (gridGraph, sx, sy, ex, ey) ->
* VisibilityGraphAlgorithm.graphReuse(gridGraph, sx, sy, ex, ey);
* runTests("VisibilityGraph_REUSE");
*
* AnyAnglePathfinding.algoFunction = (gridGraph, sx, sy, ex, ey) ->
* VisibilityGraphAlgorithm.graphReuseSlowDijkstra(gridGraph, sx, sy,
* ex, ey); runTests("VisibilityGraph_REUSE_SLOWDIJKSTRA");
*
* AnyAnglePathfinding.algoFunction = (gridGraph, sx, sy, ex, ey) -> new
* StrictThetaStar(gridGraph, sx, sy, ex, ey);
* runTests("StrictThetaStar_5b");
*
* AnyAnglePathfinding.algoFunction = (gridGraph, sx, sy, ex, ey) ->
* StrictThetaStar.noHeuristic(gridGraph, sx, sy, ex, ey);
* runTests("StrictThetaStar_NoHeuristic_5");
*/
}
/**
* Runs tests for the specified algorithm. Uses the runTest method, which
* will output the test results to the file. EDIT: No longer used. Replaced
* by run() as the main testing method.
*
* @param algoName
* The name of the algorithm to be used in the file.
*/
@Deprecated
private static void runTests(AlgoFunction algo, String algoName) {
// runTest(algoName, 4, PathLengthClass.LONGEST);
for (int i = 1; i <= 6; i++) {
AlgoTest.runTest(algo, algoName, i, PathLengthClass.ALL);
}
}
/**
* Runs a test stated in the TestDataLibrary, and outputs the test result to
* a file.
*
* @param algoName
* The name of the algorithm to be used in the file.
* @param index
* The index of the test to use (which maze)
* @param pathLengthClass
* The category of path lengths to use in the test. Refer to
* TestDataLibrary for more information. EDIT: No longer used.
* Replaced by run() as the main testing method.
*/
@Deprecated
private static void runTest(AlgoFunction algo, String algoName, int index, PathLengthClass pathLengthClass) {
String filename = algoName + "_Maze" + index + "_" + pathLengthClass.name();
System.out.println("RUNNING TEST: " + filename);
TestDataLibrary library = new StandardMazes(index, pathLengthClass);
GridGraph gridGraph = library.generateGraph();
System.out.println("Percentage Blocked:" + gridGraph.getPercentageBlocked());
FileIO fileIO = new FileIO(AnyAnglePathfinding.PATH_TESTDATA + filename + ".txt");
fileIO.writeRow("Algorithm", "Maze", "ComputedPath", "OptimalPath", "PathLengthRatio", "Time", "TimeSD",
"Start", "End", "Trails");
TestResult tempRes = AlgoTest.testAlgorithm(algo, gridGraph, 0, 0, 1, 0, 1, 1);
System.out.println("Preprocess time: " + tempRes.time);
while (library.hasNextData()) {
StartEndPointData data = library.getNextData();
TestResult testResult = AlgoTest.testAlgorithm(algo, gridGraph, data.start.x, data.start.y, data.end.x,
data.end.y, 10, library.getNTrials());
boolean valid = (testResult.pathLength > 0.00001f);
double ratio = testResult.pathLength / data.shortestPath;
String algorithm = algoName;
String maze = index + "";
String pathLength = testResult.pathLength + "";
String shortestPathLength = data.shortestPath + "";
String pathLengthRatio = ratio + "";
String time = testResult.time + "";
String timeSD = testResult.timeSD + "";
String start = data.start + "";
String end = data.end + "";
String nTrials = library.getNTrials() + "";
if (!valid) {
pathLength = "N/A";
pathLengthRatio = "N/A";
}
fileIO.writeRow(algorithm, maze, pathLength, shortestPathLength, pathLengthRatio, time, timeSD, start, end,
nTrials);
fileIO.flush();
}
fileIO.close();
}
/**
* Conducts a running time / path length test on the current algorithm on a
* specified gridGraph. The algorithm used is the algorithm stored in
* algoFunction.
*
* @param gridGraph
* the grid to run the algorithm on.
* @param startX
* x-coordinate of start point
* @param startY
* y-coordinate of start point
* @param endX
* x-coordinate of goal point
* @param endY
* y-coordinate of goal point
* @param sampleSize
* The size of the sample to use. Used in computing Standard
* Deviation.
* @param nTrials
* The number of times the algorithm is run per test. For
* example, if sampleSize is 10 and nTrials is 50, the algorithm
* is run 50 times for each of the 10 tests to get 10 results.
* This is used for test cases which possbly last shorter than a
* millisecond, making it hard to record the running time of we
* only ran the algorithm once per record.
* @return
*/
private static TestResult testAlgorithm(AlgoFunction algoFunction, GridGraph gridGraph, int startX, int startY,
int endX, int endY, int sampleSize, int nTrials) {
int[] data = new int[sampleSize];
int sum = 0;
long sumSquare = 0;
// sampleSize = 0;// UNCOMMENT TO TEST PATH LENGTH ONLY
for (int s = 0; s < sampleSize; s++) {
long start = System.nanoTime();
for (int i = 0; i < nTrials; i++) {
AlgoTest.testAlgorithmSpeed(algoFunction, gridGraph, startX, startY, endX, endY);
}
long end = System.nanoTime();
System.gc();
data[s] = (int) (end - start);
sum += data[s];
sumSquare += data[s] * data[s];
}
double mean = (double) sum / nTrials / sampleSize;
double secondMomentTimesN = (double) sumSquare / nTrials / nTrials;
double sampleVariance = (secondMomentTimesN - sampleSize * (mean * mean)) / (sampleSize - 1);
double standardDeviation = Math.sqrt(sampleVariance);
int[][] path = Utility.generatePath(algoFunction, gridGraph, startX, startY, endX, endY);
double pathLength = Utility.computePathLength(gridGraph, path);
boolean isTaut = Utility.isPathTaut(gridGraph, path);
TestResult testResult = new TestResult(sampleSize, mean, standardDeviation, pathLength, isTaut);
return testResult;
}
/**