Repository navigation
Expand file tree
/
Copy pathAnya.java
More file actions
975 lines (804 loc) · 35.8 KB
/
Copy pathAnya.java
File metadata and controls
975 lines (804 loc) · 35.8 KB
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
package algorithms;
import grid.GridGraph;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import algorithms.anya.Fraction;
import algorithms.datatypes.Point;
import algorithms.datatypes.SnapshotItem;
import algorithms.priorityqueue.FastVariableSizeIndirectHeap;
public class Anya extends PathFindingAlgorithm {
private AnyaState goalState;
private AnyaState[] states;
private FastVariableSizeIndirectHeap pq;
private HashMap<AnyaState, Integer> existingStates;
private static int[][] rightDownExtents;
private static int[][] leftDownExtents;
public static void initialiseUpExtents(GridGraph graph) {
// Don't reinitialise if graph is the same size as the last time.
if (rightDownExtents != null && graph.sizeY+2 == rightDownExtents.length && graph.sizeX+1 == rightDownExtents[0].length) return;
rightDownExtents = new int[graph.sizeY+2][];
leftDownExtents = new int[graph.sizeY+2][];
for (int y=0;y<graph.sizeY+2;++y) {
rightDownExtents[y] = new int[graph.sizeX+1];
leftDownExtents[y] = new int[graph.sizeX+1];
}
}
public Anya(GridGraph graph, int sx, int sy, int ex, int ey) {
super(graph, graph.sizeX, graph.sizeY, sx, sy, ex, ey);
}
@Override
public void computePath() {
existingStates = new HashMap<>();
pq = new FastVariableSizeIndirectHeap();
states = new AnyaState[11];
goalState = null;
computeExtents();
generateStartingStates();
while (!pq.isEmpty()) {
maybeSaveSearchSnapshot();
int currentID = pq.popMinIndex();
AnyaState currState = states[currentID];
currState.visited = true;
//System.out.println("Explore " + currState + " :: " + currState.fValue);
// Check if goal state.
if (currState.y == ey && currState.xL.isLessThanOrEqual(ex) && !currState.xR.isLessThan(ex)) {
goalState = currState;
break;
}
generateSuccessors(currState);
}
}
private void generateStartingStates() {
boolean bottomLeftOfBlocked = graph.bottomLeftOfBlockedTile(sx, sy);
boolean bottomRightOfBlocked = graph.bottomRightOfBlockedTile(sx, sy);
boolean topLeftOfBlocked = graph.topLeftOfBlockedTile(sx, sy);
boolean topRightOfBlocked = graph.topRightOfBlockedTile(sx, sy);
Point start = new Point(sx,sy);
// Generate up
if (!bottomLeftOfBlocked || !bottomRightOfBlocked) {
Fraction leftExtent, rightExtent;
if (bottomLeftOfBlocked) {
// Explore up-left
leftExtent = new Fraction(leftUpExtent(sx, sy));
rightExtent = new Fraction(sx);
} else if (bottomRightOfBlocked) {
// Explore up-right
leftExtent = new Fraction(sx);
rightExtent = new Fraction(rightUpExtent(sx, sy));
} else {
// Explore up-left-right
leftExtent = new Fraction(leftUpExtent(sx, sy));
rightExtent = new Fraction(rightUpExtent(sx, sy));
}
this.generateUpwardsStart(leftExtent, rightExtent, start);
}
// Generate down
if (!topLeftOfBlocked || !topRightOfBlocked) {
Fraction leftExtent, rightExtent;
if (topLeftOfBlocked) {
// Explore down-left
leftExtent = new Fraction(leftDownExtent(sx, sy));
rightExtent = new Fraction(sx);
} else if (topRightOfBlocked) {
// Explore down-right
leftExtent = new Fraction(sx);
rightExtent = new Fraction(rightDownExtent(sx, sy));
} else {
// Explore down-left-right
leftExtent = new Fraction(leftDownExtent(sx, sy));
rightExtent = new Fraction(rightDownExtent(sx, sy));
}
this.generateDownwardsStart(leftExtent, rightExtent, start);
}
// Generate left
if (!topRightOfBlocked || !bottomRightOfBlocked) {
this.generateSameLevelStart(start, leftAnyExtent(sx,sy), sx);
}
// Generate right
if (!topLeftOfBlocked || !bottomLeftOfBlocked) {
this.generateSameLevelStart(start, sx, rightAnyExtent(sx,sy));
}
}
private void addSuccessor(AnyaState source, AnyaState successor) {
Integer existingHandle = existingStates.get(successor);
if (existingHandle == null) {
addToOpen(successor);
} else {
relaxExisting(source, successor, existingHandle);
}
//maybeSaveSearchSnapshot();
}
private void addToOpen(AnyaState successor) {
// set heuristic and f-value
successor.hValue = heuristic(successor);
successor.fValue = successor.gValue + successor.hValue;
int handle = pq.insert(successor.fValue);
if (handle >= states.length) {
states = Arrays.copyOf(states, states.length*2);
}
states[handle] = successor;
existingStates.put(successor, handle);
//System.out.println("Generate " + successor + " -> " + handle);
}
private void relaxExisting(AnyaState source, AnyaState successorCopy, int existingHandle) {
AnyaState successor = states[existingHandle];
if (successor.visited) return;
int dx = successor.basePoint.x - source.basePoint.x;
int dy = successor.basePoint.y - source.basePoint.y;
float newgValue = source.gValue + (float)Math.sqrt(dx*dx+dy*dy);
if (newgValue < successor.gValue) {
successor.gValue = newgValue;
successor.fValue = newgValue + successor.hValue;
successor.parent = successorCopy.parent;
pq.decreaseKey(existingHandle, successor.fValue);
//System.out.println("Relax " + successor + " : " + successor.fValue);
}
//else System.out.println("Failed to relax " + successor + ": " + successor.fValue);
}
private void computeExtents() {
// graph.isBlocked(x,y) is the same as graph.bottomLeftOfBlockedTile(x,y)
Anya.initialiseUpExtents(graph);
for (int y=0;y<sizeY+2;++y) {
boolean lastIsBlocked = true;
int lastX = -1;
for (int x=0;x<=sizeX;++x) {
leftDownExtents[y][x] = lastX;
if (graph.isBlocked(x, y-1) != lastIsBlocked) {
lastX = x;
lastIsBlocked = !lastIsBlocked;
}
}
lastIsBlocked = true;
lastX = sizeX+1;
for (int x=sizeX;x>=0;--x) {
rightDownExtents[y][x] = lastX;
if (graph.isBlocked(x-1, y-1) != lastIsBlocked) {
lastX = x;
lastIsBlocked = !lastIsBlocked;
}
}
}
}
/// === GENERATE SUCCESSORS - PATTERNS - START ===
private void generateSuccessors(AnyaState currState) {
Point basePoint = currState.basePoint;
if (basePoint.y == currState.y) {
exploreFromSameLevel(currState, basePoint);
} else if (basePoint.y < currState.y) {
explorefromBelow(currState, basePoint);
} else {
explorefromAbove(currState, basePoint);
}
}
private void exploreFromSameLevel(AnyaState currState, Point basePoint) {
// Note: basePoint.y == currState.y
// Note: basePoint == currState.basePoint
// Property 1: basePoint is not strictly between the two endpoints of the interval.
// Property 2: the endpoints of the interval are integers.
assert basePoint.y == currState.y;
assert currState.xL.isWholeNumber();
assert currState.xR.isWholeNumber();
int y = basePoint.y;
if (currState.xR.n <= basePoint.x) { // currState.xR <= point.x (explore left)
int xL = currState.xL.n;
if (graph.bottomLeftOfBlockedTile(xL, y)) {
if (!graph.bottomRightOfBlockedTile(xL, y)) {
/* ----- |XXXXXXXX|
* |XXXXXXXX|
* ----- P========B
*/
Fraction leftBound = new Fraction(leftUpExtent(xL, y));
generateUpwardsUnobservable(new Point(xL,y), leftBound, currState.xL, currState);
}
} else if (graph.topLeftOfBlockedTile(xL, y)) {
if (!graph.topRightOfBlockedTile(xL, y)) {
/* ----- P========B
* |XXXXXXXX|
* ----- |XXXXXXXX|
*/
Fraction leftBound = new Fraction(leftDownExtent(xL, y));
generateDownwardsUnobservable(new Point(xL,y), leftBound, currState.xL, currState);
}
}
if (!graph.bottomRightOfBlockedTile(xL, y) || !graph.topRightOfBlockedTile(xL, y)) {
int leftBound = leftAnyExtent(xL, y);
generateSameLevelObservable(leftBound, xL, currState);
}
} else { // point.x <= currState.xL (explore right)
assert basePoint.x <= currState.xL.n;
int xR = currState.xR.n;
if (graph.bottomRightOfBlockedTile(xR, y)) {
if (!graph.bottomLeftOfBlockedTile(xR, y)) {
/* |XXXXXXXX| -----
* |XXXXXXXX|
* B========P -----
*/
Fraction rightBound = new Fraction(rightUpExtent(xR, y));
generateUpwardsUnobservable(new Point(xR,y), currState.xR, rightBound, currState);
}
} else if (graph.topRightOfBlockedTile(xR, y)) {
if (!graph.topLeftOfBlockedTile(xR, y)) {
/* B========P -----
* |XXXXXXXX|
* |XXXXXXXX| -----
*/
Fraction rightBound = new Fraction(rightDownExtent(xR, y));
generateDownwardsUnobservable(new Point(xR,y), currState.xR, rightBound, currState);
}
}
if (!graph.bottomLeftOfBlockedTile(xR, y) || !graph.topLeftOfBlockedTile(xR, y)) {
int rightBound = rightAnyExtent(xR, y);
generateSameLevelObservable(xR, rightBound, currState);
}
}
}
private void explorefromBelow(AnyaState currState, Point basePoint) {
// Note: basePoint.y < currState.y
// Note: basePoint == currState.basePoint
assert basePoint.y < currState.y;
if (graph.bottomLeftOfBlockedTile(currState.xL.floor(), currState.y)) {
// Is Blocked Above
if (currState.xL.isWholeNumber()) {
int xL = currState.xL.n;
if (xL < basePoint.x && !graph.bottomRightOfBlockedTile(xL, currState.y)) {
/*
* .-----|XXXXXXX
* '. |XXXXXXXX
* '. |XXXXXXXX
* 'P========
* '. ?
* '. ?
* B
*/
// (Px-Bx)*(Py-By+1)/(Py-By) + Bx
int dy = currState.y - basePoint.y;
Fraction leftProjection = new Fraction((xL-basePoint.x)*(dy+1), dy).plus(basePoint.x);
int leftBound = leftUpExtent(xL, currState.y);
if (leftProjection.isLessThan(leftBound)) { // leftProjection < leftBound
leftProjection = new Fraction(leftBound);
}
generateUpwardsUnobservable(new Point(xL,currState.y), leftProjection, currState.xL, currState);
}
}
if (currState.xR.isWholeNumber()) {
int xR = currState.xR.n;
if (basePoint.x < xR && !graph.bottomLeftOfBlockedTile(xR, currState.y)) {
/*
* XXXXXXX|-----.
* XXXXXXXX| .'
* XXXXXXXX| .'
* ========P'
* ? .'
* ? .'
* B
*/
// (Px-Bx)*(Py-By+1)/(Py-By) + Bx
int dy = currState.y - basePoint.y;
Fraction rightProjection = new Fraction((xR-basePoint.x)*(dy+1), dy).plus(basePoint.x);
int rightBound = rightUpExtent(xR, currState.y);
if (!rightProjection.isLessThanOrEqual(rightBound)) { // rightBound < rightProjection
rightProjection = new Fraction(rightBound);
}
generateUpwardsUnobservable(new Point(xR,currState.y), currState.xR, rightProjection, currState);
}
}
} else {
// Is not Blocked Above
/*
* ======= ===== =====
* \ / / .' '. \
* \ / OR /.' OR '.\
* B B B
*/
// (Px-Bx)*(Py-By+1)/(Py-By) + Bx
int dy = currState.y - basePoint.y;
Fraction leftProjection = currState.xL.minus(basePoint.x).multiplyDivide(dy+1, dy).plus(basePoint.x);
int leftBound = leftUpExtent(currState.xL.floor()+1, currState.y);
if (leftProjection.isLessThan(leftBound)) { // leftProjection < leftBound
leftProjection = new Fraction(leftBound);
}
// (Px-Bx)*(Py-By+1)/(Py-By) + Bx
Fraction rightProjection = currState.xR.minus(basePoint.x).multiplyDivide(dy+1, dy).plus(basePoint.x);
int rightBound = rightUpExtent(currState.xR.ceil()-1, currState.y);
if (!rightProjection.isLessThanOrEqual(rightBound)) { // rightBound < rightProjection
rightProjection = new Fraction(rightBound);
}
if (leftProjection.isLessThan(rightProjection)) {
generateUpwardsObservable(leftProjection, rightProjection, currState);
}
}
if (currState.xL.isWholeNumber()) {
int xL = currState.xL.n;
if (graph.topRightOfBlockedTile(xL, currState.y) && !graph.bottomRightOfBlockedTile(xL, currState.y)) {
/*
* .------P======
* |XXXXXX|\ /
* |XXXXXX| \ /
* B
*/
Point pivot = new Point(xL,currState.y);
{
int leftBound = leftAnyExtent(xL, currState.y);
generateSameLevelUnobservable(pivot, leftBound, xL, currState);
}
{
int dy = currState.y - basePoint.y;
Fraction leftProjection = new Fraction((xL-basePoint.x)*(dy+1), dy).plus(basePoint.x);
int leftBound = leftUpExtent(xL, currState.y);
if (!leftProjection.isLessThanOrEqual(leftBound)) { // leftBound < leftProjection
this.generateUpwardsUnobservable(pivot, new Fraction(leftBound), leftProjection, currState);
}
}
}
}
if (currState.xR.isWholeNumber()) {
int xR = currState.xR.n;
if (graph.topLeftOfBlockedTile(xR, currState.y) && !graph.bottomLeftOfBlockedTile(xR, currState.y)) {
/*
* ======P------.
* \ /|XXXXXX|
* \ / |XXXXXX|
* B
*/
Point pivot = new Point(xR,currState.y);
{
int rightBound = rightAnyExtent(xR, currState.y);
generateSameLevelUnobservable(new Point(xR,currState.y), xR, rightBound, currState);
}
{
int dy = currState.y - basePoint.y;
Fraction rightProjection = new Fraction((xR-basePoint.x)*(dy+1), dy).plus(basePoint.x);
int rightBound = rightUpExtent(xR, currState.y);
if (rightProjection.isLessThan(rightBound)) { // rightProjection < rightBound
this.generateUpwardsUnobservable(pivot, rightProjection, new Fraction(rightBound), currState);
}
}
}
}
}
private void explorefromAbove(AnyaState currState, Point basePoint) {
// Note: basePoint.y > currState.y
// Note: basePoint == currState.basePoint
assert basePoint.y > currState.y;
if (graph.topLeftOfBlockedTile(currState.xL.floor(), currState.y)) {
// Is Blocked Below
if (currState.xL.isWholeNumber()) {
int xL = currState.xL.n;
if (xL < basePoint.x && !graph.topRightOfBlockedTile(xL, currState.y)) {
/*
* B
* .' ?
* .' ?
* .P========
* .' |XXXXXXXX
* .' |XXXXXXXX
* '-----|XXXXXXX
*/
// (Px-Bx)*(Py-By+1)/(Py-By) + Bx
int dy = basePoint.y - currState.y;
Fraction leftProjection = new Fraction((xL-basePoint.x)*(dy+1), dy).plus(basePoint.x);
int leftBound = leftDownExtent(xL, currState.y);
if (leftProjection.isLessThan(leftBound)) { // leftProjection < leftBound
leftProjection = new Fraction(leftBound);
}
generateDownwardsUnobservable(new Point(xL,currState.y), leftProjection, currState.xL, currState);
}
}
if (currState.xR.isWholeNumber()) {
int xR = currState.xR.n;
if (basePoint.x < xR && !graph.topLeftOfBlockedTile(xR, currState.y)) {
/*
* B
* ? '.
* ? '.
* ========P.
* XXXXXXXX| '.
* XXXXXXXX| '.
* XXXXXXX|-----'
*/
// (Px-Bx)*(Py-By+1)/(Py-By) + Bx
int dy = basePoint.y - currState.y;
Fraction rightProjection = new Fraction((xR-basePoint.x)*(dy+1), dy).plus(basePoint.x);
int rightBound = rightDownExtent(xR, currState.y);
if (!rightProjection.isLessThanOrEqual(rightBound)) { // rightBound < rightProjection
rightProjection = new Fraction(rightBound);
}
generateDownwardsUnobservable(new Point(xR,currState.y), currState.xR, rightProjection, currState);
}
}
} else {
// Is not Blocked Below
/*
* B B B
* / \ OR \'. OR .'/
* / \ \ '. .' /
* ======= ===== =====
*/
// (Px-Bx)*(Py-By+1)/(Py-By) + Bx
int dy = basePoint.y - currState.y;
Fraction leftProjection = currState.xL.minus(basePoint.x).multiplyDivide(dy+1, dy).plus(basePoint.x);
int leftBound = leftDownExtent(currState.xL.floor()+1, currState.y);
if (leftProjection.isLessThan(leftBound)) { // leftProjection < leftBound
leftProjection = new Fraction(leftBound);
}
// (Px-Bx)*(Py-By+1)/(Py-By) + Bx
Fraction rightProjection = currState.xR.minus(basePoint.x).multiplyDivide(dy+1, dy).plus(basePoint.x);
int rightBound = rightDownExtent(currState.xR.ceil()-1, currState.y);
if (!rightProjection.isLessThanOrEqual(rightBound)) { // rightBound < rightProjection
rightProjection = new Fraction(rightBound);
}
if (leftProjection.isLessThan(rightProjection)) {
generateDownwardsObservable(leftProjection, rightProjection, currState);
}
}
if (currState.xL.isWholeNumber()) {
int xL = currState.xL.n;
if (graph.bottomRightOfBlockedTile(xL, currState.y) && !graph.topRightOfBlockedTile(xL, currState.y)) {
/*
* B
* |XXXXXX| / \
* |XXXXXX|/ \
* '------P======
*/
Point pivot = new Point(xL,currState.y);
{
int leftBound = leftAnyExtent(xL, currState.y);
generateSameLevelUnobservable(pivot, leftBound, xL, currState);
}
{
int dy = basePoint.y - currState.y;
Fraction leftProjection = new Fraction((xL-basePoint.x)*(dy+1), dy).plus(basePoint.x);
int leftBound = leftDownExtent(xL, currState.y);
if (!leftProjection.isLessThanOrEqual(leftBound)) { // leftBound < leftProjection
this.generateDownwardsUnobservable(pivot, new Fraction(leftBound), leftProjection, currState);
}
}
}
}
if (currState.xR.isWholeNumber()) {
int xR = currState.xR.n;
if (graph.bottomLeftOfBlockedTile(xR, currState.y) && !graph.topLeftOfBlockedTile(xR, currState.y)) {
/*
* B
* / \ |XXXXXX|
* / \|XXXXXX|
* ======P------'
*/
Point pivot = new Point(xR,currState.y);
{
int rightBound = rightAnyExtent(xR, currState.y);
generateSameLevelUnobservable(new Point(xR,currState.y), xR, rightBound, currState);
}
{
int dy = basePoint.y - currState.y;
Fraction rightProjection = new Fraction((xR-basePoint.x)*(dy+1), dy).plus(basePoint.x);
int rightBound = rightDownExtent(xR, currState.y);
if (rightProjection.isLessThan(rightBound)) { // rightProjection < rightBound
this.generateDownwardsUnobservable(pivot, rightProjection, new Fraction(rightBound), currState);
}
}
}
}
}
/// === GENERATE SUCCESSORS - PATTERNS - END ===
/// === GENERATE SUCCESSORS - UTILITY - START ===
private int leftUpExtent(int xL, int y) {
return leftDownExtents[y+1][xL];
}
private int leftDownExtent(int xL, int y) {
return leftDownExtents[y][xL];
}
private int leftAnyExtent(int xL, int y) {
return Math.max(leftDownExtents[y][xL], leftDownExtents[y+1][xL]);
}
private int rightUpExtent(int xR, int y) {
return rightDownExtents[y+1][xR];
}
private int rightDownExtent(int xR, int y) {
return rightDownExtents[y][xR];
}
private int rightAnyExtent(int xR, int y) {
return Math.min(rightDownExtents[y][xR], rightDownExtents[y+1][xR]);
}
/**
* Can be used for exploreLeftwards or exploreRightwards.
* This function will not split intervals.
*/
private void generateSameLevelObservable(int leftBound, int rightBound, AnyaState source) {
addSuccessor(source,
AnyaState.createObservableSuccessor(new Fraction(leftBound), new Fraction(rightBound), source.y, source));
}
/**
* Can be used for exploreLeftwards or exploreRightwards.
* This function will not split intervals.
*/
private void generateSameLevelUnobservable(Point basePoint, int leftBound, int rightBound, AnyaState source) {
addSuccessor(source,
AnyaState.createUnobservableSuccessor(new Fraction(leftBound), new Fraction(rightBound), source.y, basePoint, source));
}
/**
* Can be used for exploreLeftwards or exploreRightwards.
* This function will not split intervals.
*/
private void generateSameLevelStart(Point start, int leftBound, int rightBound) {
addSuccessor(null,
AnyaState.createStartState(new Fraction(leftBound), new Fraction(rightBound), start.y, start));
}
private void generateUpwardsUnobservable(Point basePoint, Fraction leftBound, Fraction rightBound, AnyaState source) {
generateAndSplitIntervals(
source.y + 2, source.y + 1,
basePoint,
leftBound, rightBound,
source);
}
private void generateUpwardsObservable(Fraction leftBound, Fraction rightBound, AnyaState source) {
generateAndSplitIntervals(
source.y + 2, source.y + 1,
null,
leftBound, rightBound,
source);
}
private void generateUpwardsStart(Fraction leftBound, Fraction rightBound, Point start) {
generateAndSplitIntervals(
start.y + 2, start.y + 1,
start,
leftBound, rightBound,
null);
}
private void generateDownwardsUnobservable(Point basePoint, Fraction leftBound, Fraction rightBound, AnyaState source) {
generateAndSplitIntervals(
source.y - 1, source.y - 1,
basePoint,
leftBound, rightBound,
source);
}
private void generateDownwardsObservable(Fraction leftBound, Fraction rightBound, AnyaState source) {
generateAndSplitIntervals(
source.y - 1, source.y - 1,
null,
leftBound, rightBound,
source);
}
private void generateDownwardsStart(Fraction leftBound, Fraction rightBound, Point start) {
generateAndSplitIntervals(
start.y - 1, start.y - 1,
start,
leftBound, rightBound,
null);
}
/**
* Called by generateUpwards / Downwards.
* basePoint is null if observable. Not null if unobservable.
* source is null if start state.
*
* This is used to avoid repeated code in generateUpwardsUnobservable, generateUpwardsObservable,
* // generateDownwardsUnobservable, generateDownwardsObservable, generateDownwardsStart, generateDownwardsStart.
*/
private void generateAndSplitIntervals(int checkY, int newY, Point basePoint, Fraction leftBound, Fraction rightBound, AnyaState source) {
Fraction left = leftBound;
int leftFloor = left.floor();
// Divide up the intervals.
while(true) {
int right = rightDownExtents[checkY][leftFloor]; // it's actually rightDownExtents for exploreDownwards. (thus we use checkY = currY - 2)
if (rightBound.isLessThanOrEqual(right)) break; // right < rightBound
if (basePoint == null) {
addSuccessor(source, AnyaState.createObservableSuccessor(left, new Fraction(right), newY, source));
} else {
if (source == null) {
addSuccessor(null, AnyaState.createStartState(left, new Fraction(right), newY, basePoint));
} else {
addSuccessor(source, AnyaState.createUnobservableSuccessor(left, new Fraction(right), newY, basePoint, source));
}
}
leftFloor = right;
left = new Fraction(leftFloor);
}
if (basePoint == null) {
addSuccessor(source, AnyaState.createObservableSuccessor(left, rightBound, newY, source));
} else {
if (source == null) {
addSuccessor(null, AnyaState.createStartState(left, rightBound, newY, basePoint));
} else {
addSuccessor(source, AnyaState.createUnobservableSuccessor(left, rightBound, newY, basePoint, source));
}
}
}
/// === GENERATE SUCCESSORS - UTILITY - END ===
private float heuristic(AnyaState currState) {
int baseX = currState.basePoint.x;
int baseY = currState.basePoint.y;
Fraction xL = currState.xL;
Fraction xR = currState.xR;
// Special case: base, goal, interval all on same row.
if (currState.y == baseY && currState.y == ey) {
// Case 1: base and goal on left of interval.
// baseX < xL && ex < xL
if (!xL.isLessThanOrEqual(baseX) && !xL.isLessThanOrEqual(ex)) {
return 2*xL.toFloat() - baseX - ex; // (xL-baseX) + (xL-ex);
}
// Case 2: base and goal on right of interval.
// xR < baseX && xR < ex
else if (xR.isLessThan(baseX) && xR.isLessThan(ex)) {
return baseX + ex - 2*xL.toFloat(); // (baseX-xL) + (ex-xL)
}
// Case 3: Otherwise, the direct path from base to goal will pass through the interval.
else {
return Math.abs(baseX - ex);
}
}
int dy1 = baseY - currState.y;
int dy2 = ey - currState.y;
// If goal and base on same side of interval, reflect goal about interval -> ey2.
int ey2 = ey;
if (dy1 * dy2 > 0) ey2 = 2*currState.y - ey;
/* E
* '.
* ----X----- <--currState.y
* '.
* B
*/
// (ey-by)/(ex-bx) = (cy-by)/(cx-bx)
// cx = bx + (cy-by)(ex-bx)/(ey-by)
// Find the pivot point on the interval for shortest path from base to goal.
float intersectX = baseX + (float)(currState.y - baseY)*(ex - baseX)/(ey2-baseY);
float xlf = xL.toFloat();
float xrf = xR.toFloat();
// Snap to endpoints of interval if intersectX it lies outside interval.
if (intersectX < xlf) intersectX = xlf;
if (intersectX > xrf) intersectX = xrf;
{
// Return sum of euclidean distances. (base~intersection~goal)
float dx1 = intersectX - baseX;
float dx2 = intersectX - ex;
return (float)(Math.sqrt(dx1*dx1+dy1*dy1) + Math.sqrt(dx2*dx2+dy2*dy2));
}
}
private int pathLength() {
int length = 1;
AnyaState current = goalState;
while (current != null) {
current = current.parent;
length++;
}
return length;
}
@Override
public int[][] getPath() {
if (goalState == null) return new int[0][]; // Fail
// Start from goalState and traverse backwards.
int length = pathLength();
int[][] path = new int[length][];
AnyaState current = goalState;
path[length-1] = new int[2];
path[length-1][0] = ex;
path[length-1][1] = ey;
int index = length-2;
while (current != null) {
path[index] = new int[2];
path[index][0] = current.basePoint.x;
path[index][1] = current.basePoint.y;
index--;
current = current.parent;
}
return path;
}
@Override
public float getPathLength() {
if (goalState == null) return -1; // Fail
// Start from goalState and traverse backwards.
double pathLength = 0;
int currX = ex;
int currY = ey;
AnyaState current = goalState;
while (current != null) {
int nextX = current.basePoint.x;
int nextY = current.basePoint.y;
pathLength += graph.distance_double(currX,currY,nextX,nextY);
current = current.parent;
currX = nextX;
currY = nextY;
}
return (float)pathLength;
}
@Override
protected List<SnapshotItem> computeSearchSnapshot() {
ArrayList<SnapshotItem> list = new ArrayList<>(states.length);
for (AnyaState in : states) {
// y, xLn, xLd, xRn, xRd, px, py
if (in == null) continue;
Integer[] line = new Integer[7];
line[0] = in.y;
line[1] = in.xL.n;
line[2] = in.xL.d;
line[3] = in.xR.n;
line[4] = in.xR.d;
line[5] = in.basePoint.x;
line[6] = in.basePoint.y;
list.add(SnapshotItem.generate(line));
}
if (!pq.isEmpty()) {
int index = pq.getMinIndex();
AnyaState in = states[index];
Integer[] line = new Integer[5];
line[0] = in.y;
line[1] = in.xL.n;
line[2] = in.xL.d;
line[3] = in.xR.n;
line[4] = in.xR.d;
list.add(SnapshotItem.generate(line));
}
return list;
}
public static void clearMemory() {
leftDownExtents = null;
rightDownExtents = null;
System.gc();
}
}
class AnyaState {
public final Fraction xL;
public final Fraction xR;
public final int y;
public final Point basePoint;
public float hValue;
public float fValue;
public float gValue;
public AnyaState parent;
public boolean visited;
private AnyaState(Fraction xL, Fraction xR, int y, Point basePoint, float gValue, AnyaState parent) {
this.xL = xL;
this.xR = xR;
this.y = y;
this.basePoint = basePoint;
this.gValue = gValue;
this.parent = parent;
this.visited = false;
}
public static AnyaState createStartState(Fraction xL, Fraction xR, int y, Point start) {
return new AnyaState(xL,xR,y,
start,
0f,
null);
}
public static AnyaState createObservableSuccessor(Fraction xL, Fraction xR, int y, AnyaState sourceInterval) {
return new AnyaState(xL,xR,y,
sourceInterval.basePoint,
sourceInterval.gValue,
sourceInterval.parent);
}
public static AnyaState createUnobservableSuccessor(Fraction xL, Fraction xR, int y, Point basePoint, AnyaState sourceInterval) {
int dx = basePoint.x - sourceInterval.basePoint.x;
int dy = basePoint.y - sourceInterval.basePoint.y;
return new AnyaState(xL,xR,y,
basePoint,
sourceInterval.gValue + (float)Math.sqrt(dx*dx+dy*dy),
sourceInterval);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
// Removed null checks.
result = prime * result + basePoint.hashCode();
result = prime * result + xL.hashCode();
result = prime * result + xR.hashCode();
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
// Removed type checks. Removed null checks.
AnyaState other = (AnyaState) obj;
if (!xL.equals(other.xL)) return false;
if (!xR.equals(other.xR)) return false;
if (y != other.y) return false;
if (!basePoint.equals(other.basePoint)) return false;
return true;
}
@Override
public String toString() {
return "(" + xL + " " + xR + ") - " + y;
}
}