-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbellman.c
1206 lines (957 loc) · 43.1 KB
/
bellman.c
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
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <time.h>
#include <assert.h>
#include "universe.h"
#include "readwrite.h"
#include "bitwise.h"
#define RECURSE(x) //printf x
#define PRUNE(x) //printf x
#define CHECK(x) //printf x
#define YES 1
#define NO 0
static universe *u_static, *u_evolving, *u_forbidden, *u_filter;
// Search space restriction parameters:
static unsigned int first_encounter_gen = 2;
static unsigned int last_encounter_gen = 20;
static unsigned int repair_interval = 20;
static unsigned int stable_interval = 5;
static unsigned int max_live = 10;
static unsigned int max_active = 10;
// Symmetry constraints
static enum {
NONE, HORIZ, VERT, DIAG, DIAG_INVERSE
} symmetry_type = NONE;
static unsigned int symmetry_ofs = 0;
static unsigned int diagonal_x, diagonal_y;
static unsigned int inverse_x, inverse_y;
// Other global values
static unsigned int max_gens;
static unsigned int n_live;
static int last_print_time = 0;
static int total_time = 0;
// Prune counters
static uint64_t prune_unstable;
static uint64_t prune_solution;
static uint64_t prune_encounter_too_early;
static uint64_t prune_encounter_too_late;
static uint64_t prune_stabilises_too_slowly;
static uint64_t prune_too_many_active;
static uint64_t prune_too_many_live;
static uint64_t prune_forbidden;
static uint64_t prune_filter;
static uint64_t prune_oldtotal;
#define PRINT_PRUNE(fmt, count) do { \
printf(" " fmt ": %lld\n", (long long)count); \
prune_total += count; \
} while(0)
static void print_prune_counters(void) {
uint64_t prune_total = 0;
printf("Reasons why search space was pruned:\n");
PRINT_PRUNE("Catalyst is unstable", prune_unstable);
PRINT_PRUNE("Found a solution", prune_solution);
PRINT_PRUNE("First encounter too early (first-encounter)", prune_encounter_too_early);
PRINT_PRUNE("First encounter too late (last-encounter)", prune_encounter_too_late);
PRINT_PRUNE("Took too long to stabilise (repair-interval)", prune_stabilises_too_slowly);
PRINT_PRUNE("Too many active cells (max-active)", prune_too_many_active);
PRINT_PRUNE("Too many live cells (max-live)", prune_too_many_live);
PRINT_PRUNE("Hit forbidden region", prune_forbidden);
PRINT_PRUNE("Filter mismatch", prune_filter);
double prune_rate = prune_total - prune_oldtotal;
prune_oldtotal = prune_total;
prune_rate = prune_rate / 10000.0;
printf(" Total: %lld (%f Kprunes/sec)\n", (long long)prune_total, prune_rate);
}
static void read_cb(void *u_, char area, int gen, int x, int y, char c) {
cellvalue vs = OFF, ve = OFF, vf = OFF;
(void)u_;
if((area == 'P') && (gen == 0)) {
switch(c) {
case '.': break;
case '*': vs = ve = ON; break;
case '@': ve = ON; break;
case '?': vs = ve = UNKNOWN_STABLE; break;
case '!': vs = ve = UNKNOWN_STABLE; vf = ON; break;
}
generation_set_cell(u_static->first, x, y, vs);
generation_set_cell(u_evolving->first, x, y, ve);
generation_set_cell(u_forbidden->first, x, y, vf);
} else if(area == 'F') {
generation *g = universe_find_generation(u_filter, gen, 1);
switch(c)
{
case '*' :
generation_set_cell(g, x, y, ON);
break;
case '.' :
case ' ' :
generation_set_cell(g, x, y, OFF);
break;
}
}
}
static void read_param_cb(void *u_, const char *param, const char *value) {
(void)u_;
char typebuff[20];
unsigned int coord;
if(!strcmp(param, "first-encounter"))
first_encounter_gen = strtoul(value, NULL, 10);
else if(!strcmp(param, "last-encounter"))
last_encounter_gen = strtoul(value, NULL, 10);
else if(!strcmp(param, "repair-interval"))
repair_interval = strtoul(value, NULL, 10);
else if(!strcmp(param, "stable-interval"))
stable_interval = strtoul(value, NULL, 10);
else if(!strcmp(param, "max-live"))
max_live = strtoul(value, NULL, 10);
else if(!strcmp(param, "max-active"))
max_active = strtoul(value, NULL, 10);
else if(!strcmp(param, "symmetry-horiz-odd")) {
coord = strtoul(value, NULL, 10);
symmetry_type = HORIZ;
symmetry_ofs = (coord * 2);
}
else if(!strcmp(param, "symmetry-horiz-even")) {
coord = strtoul(value, NULL, 10);
symmetry_type = HORIZ;
symmetry_ofs = (coord * 2) + 1;
}
else if(!strcmp(param, "symmetry-vert-odd")) {
coord = strtoul(value, NULL, 10);
symmetry_type = VERT;
symmetry_ofs = (coord * 2);
}
else if(!strcmp(param, "symmetry-vert-even")) {
coord = strtoul(value, NULL, 10);
symmetry_type = VERT;
symmetry_ofs = (coord * 2) + 1;
}
else if(!strcmp(param, "symmetry-diag")) {
if(sscanf(value, "%d %d", &diagonal_x, &diagonal_y) != 2) {
fprintf(stderr, "Bad symmetry parameter: '%s'\n", value);
exit(-1);
}
symmetry_type = DIAG;
}
else if(!strcmp(param, "symmetry-diag-inverse")) {
if(sscanf(value, "%d %d", &inverse_x, &inverse_y) != 2) {
fprintf(stderr, "Bad symmetry parameter: '%s'\n", value);
exit(-1);
}
symmetry_type = DIAG_INVERSE;
}
else fprintf(stderr, "Bad parameter: '%s' (%s)\n",
param, value);
}
static evolve_result bellman_evolve(tile *t, tile *out) {
// Our evolution function is based on the 3 state Life variant.
out->flags = tile_evolve_bitwise_3state(t, out) | CHANGED;
// But we do another pass to (a) stop the UNKNOWN_STABLE area
// from growing and (b) check for boundary condition
// violations.
tile *stable = (tile *)t->auxdata;
if(!stable) return out->flags;
tile *forbidden = (tile *)stable->auxdata;
tile *filter = t->filter;
tile *prev = t->prev;
int y;
TILE_WORD ul_bit0, u_bit0, ur_bit0;
TILE_WORD ul_bit1, u_bit1, ur_bit1;
TILE_WORD ul_bit0s, u_bit0s, ur_bit0s;
TILE_WORD ul_bit1s, u_bit1s, ur_bit1s;
tile *t_up = t->up;
if(t_up) {
GET3WORDS(ul_bit0, u_bit0, ur_bit0, t_up, 0, TILE_HEIGHT-1);
GET3WORDS(ul_bit1, u_bit1, ur_bit1, t_up, 1, TILE_HEIGHT-1);
} else {
ul_bit0 = u_bit0 = ur_bit0 = 0;
ul_bit1 = u_bit1 = ur_bit1 = 0;
}
t_up = stable->up;
if(t_up) {
GET3WORDS(ul_bit0s, u_bit0s, ur_bit0s, t_up, 0, TILE_HEIGHT-1);
GET3WORDS(ul_bit1s, u_bit1s, ur_bit1s, t_up, 1, TILE_HEIGHT-1);
} else {
ul_bit0s = u_bit0s = ur_bit0s = 0;
ul_bit1s = u_bit1s = ur_bit1s = 0;
}
TILE_WORD l_bit0, bit0, r_bit0;
TILE_WORD l_bit1, bit1, r_bit1;
TILE_WORD l_bit0s, bit0s, r_bit0s;
TILE_WORD l_bit1s, bit1s, r_bit1s;
GET3WORDS(l_bit0, bit0, r_bit0, t, 0, 0);
GET3WORDS(l_bit1, bit1, r_bit1, t, 1, 0);
GET3WORDS(l_bit0s, bit0s, r_bit0s, stable, 0, 0);
GET3WORDS(l_bit1s, bit1s, r_bit1s, stable, 1, 0);
TILE_WORD dl_bit0, d_bit0, dr_bit0;
TILE_WORD dl_bit1, d_bit1, dr_bit1;
TILE_WORD dl_bit0s, d_bit0s, dr_bit0s;
TILE_WORD dl_bit1s, d_bit1s, dr_bit1s;
TILE_WORD all_non_active = 0;
TILE_WORD interaction = 0, activity = 0, unk_succ = 0, delta_from_stable_count = 0;
TILE_WORD delta_from_previous_count = 0;
TILE_WORD has_ON_cells = 0;
TILE_WORD forbid = 0;
TILE_WORD activity2 = 0, live = 0;
TILE_WORD filter_diff_all = 0;
for(y=0; y<TILE_HEIGHT; y++) {
if(y == TILE_HEIGHT-1) {
if(t->down) {
GET3WORDS(dl_bit0, d_bit0, dr_bit0, t->down, 0, 0);
GET3WORDS(dl_bit1, d_bit1, dr_bit1, t->down, 1, 0);
} else {
dl_bit0 = d_bit0 = dr_bit0 = 0;
dl_bit1 = d_bit1 = dr_bit1 = 0;
}
if(stable->down) {
GET3WORDS(dl_bit0s, d_bit0s, dr_bit0s, stable->down, 0, 0);
GET3WORDS(dl_bit1s, d_bit1s, dr_bit1s, stable->down, 1, 0);
} else {
dl_bit0s = d_bit0s = dr_bit0s = 0;
dl_bit1s = d_bit1s = dr_bit1s = 0;
}
} else {
GET3WORDS(dl_bit0, d_bit0, dr_bit0, t, 0, y+1);
GET3WORDS(dl_bit1, d_bit1, dr_bit1, t, 1, y+1);
GET3WORDS(dl_bit0s, d_bit0s, dr_bit0s, stable, 0, y+1);
GET3WORDS(dl_bit1s, d_bit1s, dr_bit1s, stable, 1, y+1);
}
//active is either 1 or unknown. (optimization)
/*
all_non_active |= (((ul_bit0) & (~ul_bit1)) | ((~ul_bit0) & (ul_bit1)));
all_non_active |= (((ur_bit0) & (~ur_bit1)) | ((~ur_bit0) & (ur_bit1)));
all_non_active |= (((u_bit0) & (~u_bit1)) | ((~u_bit0) & (u_bit1)));
if(all_non_active == 0)
{
all_non_active |= (((dl_bit0) & (~dl_bit1)) | ((~dl_bit0) & (dl_bit1)));
all_non_active |= (((dr_bit0) & (~dr_bit1)) | ((~dr_bit0) & (dr_bit1)));
all_non_active |= (((d_bit0) & (~d_bit1)) | ((~d_bit0) & (d_bit1)));
}
if(all_non_active == 0)
{
all_non_active |= (((l_bit0) & (~l_bit1)) | ((~l_bit0) & (l_bit1)));
all_non_active |= (((r_bit0) & (~r_bit1)) | ((~r_bit0) & (r_bit1)));
all_non_active |= (((bit0) & (~bit1)) | ((~bit0) & (bit1)));
}
*/
if(all_non_active == 0)
{
// Any neighbourhood which is identical to the stable
// universe should remain stable.
TILE_WORD stable_diff_above = 0;
stable_diff_above |= (ul_bit0s ^ ul_bit0);
stable_diff_above |= (ul_bit1s ^ ul_bit1);
stable_diff_above |= (u_bit0s ^ u_bit0);
stable_diff_above |= (u_bit1s ^ u_bit1);
stable_diff_above |= (ur_bit0s ^ ur_bit0);
stable_diff_above |= (ur_bit1s ^ ur_bit1);
TILE_WORD stable_diff_mid = 0;
stable_diff_mid |= (l_bit0s ^ l_bit0);
stable_diff_mid |= (l_bit1s ^ l_bit1);
stable_diff_mid |= (bit0s ^ bit0);
stable_diff_mid |= (bit1s ^ bit1);
stable_diff_mid |= (r_bit0s ^ r_bit0);
stable_diff_mid |= (r_bit1s ^ r_bit1);
TILE_WORD stable_diff_below = 0;
stable_diff_below |= (dl_bit0s ^ dl_bit0);
stable_diff_below |= (dl_bit1s ^ dl_bit1);
stable_diff_below |= (d_bit0s ^ d_bit0);
stable_diff_below |= (d_bit1s ^ d_bit1);
stable_diff_below |= (dr_bit0s ^ dr_bit0);
stable_diff_below |= (dr_bit1s ^ dr_bit1);
TILE_WORD diff_mask = stable_diff_above | stable_diff_mid | stable_diff_below;
out->bit0[y] = (out->bit0[y] & diff_mask) | (stable->bit0[y] & ~diff_mask);
out->bit1[y] = (out->bit1[y] & diff_mask) | (stable->bit1[y] & ~diff_mask);
// Generate a mask representing anything that's set in
// the stable region.
TILE_WORD stable_set_above = 0;
stable_set_above |= (ul_bit0s & ~ul_bit1s);
stable_set_above |= (u_bit0s & ~u_bit1s);
stable_set_above |= (ur_bit0s & ~ur_bit1s);
TILE_WORD stable_set_mid = 0;
stable_set_mid |= (l_bit0s & ~l_bit1s);
stable_set_mid |= (bit0s & ~bit1s);
stable_set_mid |= (r_bit0s & ~r_bit1s);
TILE_WORD stable_set_below = 0;
stable_set_below |= (dl_bit0s & ~dl_bit1s);
stable_set_below |= (d_bit0s & ~d_bit1s);
stable_set_below |= (dr_bit0s & ~dr_bit1s);
TILE_WORD set_mask = stable_set_above | stable_set_mid | stable_set_below;
// Look for places where the output differs from the
// stable input
TILE_WORD was0now1 = (~bit0s & ~bit1s) & (out->bit0[y] & ~out->bit1[y]);
TILE_WORD was1now0 = (bit0s & ~bit1s) & (~out->bit0[y] & ~out->bit1[y]);
TILE_WORD delta_from_stable = was0now1 | was1now0;
live |= delta_from_stable;
delta_from_stable &= set_mask;
interaction |= delta_from_stable;
// Have any forbidden cells changed?
if(forbidden)
forbid |= forbidden->bit0[y] & (was0now1 | was1now0);
// Also count the number of cells which differ from
// the stable input. 4 rounds of the bitwise bit
// counting algorithm gets us to 16 bit subtotals
// which we accumulate; we finish off the addition
// outside the loop.
// With a careful choice of tile size it should be
// possible to move the last round out of the loop
// too.
delta_from_stable = (delta_from_stable & 0x5555555555555555) + ((delta_from_stable >> 1) & 0x5555555555555555);
delta_from_stable = (delta_from_stable & 0x3333333333333333) + ((delta_from_stable >> 2) & 0x3333333333333333);
delta_from_stable = (delta_from_stable & 0x0f0f0f0f0f0f0f0f) + ((delta_from_stable >> 4) & 0x0f0f0f0f0f0f0f0f);
delta_from_stable = (delta_from_stable & 0x00ff00ff00ff00ff) + ((delta_from_stable >> 8) & 0x00ff00ff00ff00ff);
delta_from_stable_count += delta_from_stable;
// Look for places where the universe is changing
was0now1 = (~bit0 & ~bit1) & (out->bit0[y] & ~out->bit1[y]);
was1now0 = (bit0 & ~bit1) & (~out->bit0[y] & ~out->bit1[y]);
TILE_WORD delta_from_previous = (was0now1 | was1now0);
activity |= delta_from_previous;
delta_from_previous &= set_mask;
delta_from_previous = (delta_from_previous & 0x5555555555555555) + ((delta_from_previous >> 1) & 0x5555555555555555);
delta_from_previous = (delta_from_previous & 0x3333333333333333) + ((delta_from_previous >> 2) & 0x3333333333333333);
delta_from_previous = (delta_from_previous & 0x0f0f0f0f0f0f0f0f) + ((delta_from_previous >> 4) & 0x0f0f0f0f0f0f0f0f);
delta_from_previous = (delta_from_previous & 0x00ff00ff00ff00ff) + ((delta_from_previous >> 8) & 0x00ff00ff00ff00ff);
delta_from_previous_count += delta_from_previous;
if(prev) {
was0now1 = (~prev->bit0[y] & ~prev->bit1[y]) & (out->bit0[y] & ~out->bit1[y]);
was1now0 = (prev->bit0[y] & ~prev->bit1[y]) & (~out->bit0[y] & ~out->bit1[y]);
TILE_WORD delta_from_2prev = (was0now1 | was1now0);
activity2 |= delta_from_2prev;
}
// Look for unknown successors
unk_succ |= (out->bit1[y] & ~out->bit0[y]);
//Update has on cells flag.
has_ON_cells |= (~out->bit1[y] & out->bit0[y]);
// Compare against user-specified filter pattern
TILE_WORD filter_bit0 = filter ? filter->bit0[y] : 0;
TILE_WORD filter_bit1 = filter ? filter->bit1[y] : (TILE_WORD)~0;
TILE_WORD filter_diff = out->bit0[y] ^ filter_bit0;
filter_diff &= ~(filter_bit1 | out->bit1[y]);
filter_diff_all |= filter_diff;
#if 0
if(filter_bit1 != ~0) {
printf("f%d: %16llx/%16llx\n", y, filter_bit0 & ~filter_bit1, filter_bit1);
printf("o%d: %16llx/%16llx\n", y, out->bit0[y] & ~filter_bit1, out->bit1[y] & ~filter_bit1);
printf("d%d: %16llx\n", y, filter_diff);
}
#endif
#if 0
int x;
for(x=0; x<TILE_WIDTH; x++) {
int cb0 = (neigh_total0 >> x) & 1;
int cb1 = (neigh_total1 >> x) & 1;
int cb2 = (neigh_total2 >> x) & 1;
int cb3 = (neigh_total3 >> x) & 1;
int ub0 = (neigh_unk_total0 >> x) & 1;
int ub1 = (neigh_unk_total1 >> x) & 1;
int ub2 = (neigh_unk_total2 >> x) & 1;
int ub3 = (neigh_unk_total3 >> x) & 1;
int v = (mid >> x) & 1;
v += ((mid_unk >> x) & 1) << 1;
int nv = (is_live >> x) & 1;
nv += ((is_unk >> x) & 1) << 1;
printf("%d, %d: v=%d, count=%d, unk=%d, new=%d, abort %x\n",
x, y, v, (cb3 * 8) + (cb2 * 4) + (cb1 * 2) + cb0,
(ub3 * 8) + (ub2 * 4) + (ub1 * 2) + ub0, nv, abort);
}
#endif
}
else
{
//if all activity is stable - remain the same (optimization)
out->bit0[y] = t->bit0[y];
out->bit1[y] = t->bit1[y];
}
// Shift the previous results
ul_bit0 = l_bit0; u_bit0 = bit0; ur_bit0 = r_bit0;
ul_bit1 = l_bit1; u_bit1 = bit1; ur_bit1 = r_bit1;
l_bit0 = dl_bit0; bit0 = d_bit0; r_bit0 = dr_bit0;
l_bit1 = dl_bit1; bit1 = d_bit1; r_bit1 = dr_bit1;
ul_bit0s = l_bit0s; u_bit0s = bit0s; ur_bit0s = r_bit0s;
ul_bit1s = l_bit1s; u_bit1s = bit1s; ur_bit1s = r_bit1s;
l_bit0s = dl_bit0s; bit0s = d_bit0s; r_bit0s = dr_bit0s;
l_bit1s = dl_bit1s; bit1s = d_bit1s; r_bit1s = dr_bit1s;
}
// The delta_from_stable and delta_from_previous counters are
// still split into 16 bit subtotals; finish them off here
delta_from_stable_count = (delta_from_stable_count & 0x0000ffff0000ffff) + ((delta_from_stable_count >> 16) & 0x0000ffff0000ffff);
delta_from_stable_count = (delta_from_stable_count & 0x00000000ffffffff) + ((delta_from_stable_count >> 32) & 0x00000000ffffffff);
delta_from_previous_count = (delta_from_previous_count & 0x0000ffff0000ffff) + ((delta_from_previous_count >> 16) & 0x0000ffff0000ffff);
delta_from_previous_count = (delta_from_previous_count & 0x00000000ffffffff) + ((delta_from_previous_count >> 32) & 0x00000000ffffffff);
out->n_active = delta_from_stable_count;
out->delta_prev = delta_from_previous_count;
if(interaction != 0) out->flags |= DIFFERS_FROM_STABLE;
if(unk_succ != 0) out->flags |= HAS_UNKNOWN_CELLS;
if(has_ON_cells != 0) out->flags |= HAS_ON_CELLS;
if(forbid != 0) out->flags |= IN_FORBIDDEN_REGION;
if(activity != 0) out->flags |= DIFFERS_FROM_PREVIOUS;
if((activity2 != 0) || !prev) out->flags |= DIFFERS_FROM_2PREV;
if(live != 0) out->flags |= IS_LIVE;
if(filter_diff_all != 0) out->flags |= FILTER_MISMATCH;
return out->flags;
}
static generation *bellman_evolve_generations(generation *g, unsigned int end) {
tile *t;
g->flags |= CHANGED;
for(t = g->all_first; t; t = t->all_next)
t->flags |= CHANGED;
while(g->gen < end) {
//printf("Evolving: %d\n", g->gen);
generation_evolve(g, bellman_evolve);
g = g->next;
}
return g->prev;
}
static int dumpcount = 0;
static void dump(int full, unsigned int gen_nr) {
char name[30];
unsigned int i;
printf("Dumping %d\n", dumpcount);
if(full) {
for(i=0; i<max_gens; i++) {
printf(" %03d: %s\n", i,
flag2str(universe_find_generation(u_evolving, i, 0)->flags));
}
}
dumpcount++;
}
static int solcount = 0;
static void bellman_found_solution(const char *type, unsigned int gens) {
solcount++;
printf("Found solution %d type %s, gens %d\n", solcount, type, gens);
char name[30];
unsigned int i;
tile *t;
snprintf(name, sizeof name, "result%06d-%s.out", solcount, type);
FILE *f = fopen(name, "w");
if(f) {
fprintf(f, "#S first-encounter %d\n", first_encounter_gen);
fprintf(f, "#S last-encounter %d\n", last_encounter_gen);
fprintf(f, "#S repair-interval %d\n", repair_interval);
fprintf(f, "#S stable-interval %d\n", stable_interval);
fprintf(f, "#S max-live %d\n", max_live);
fprintf(f, "#S max-active %d\n", max_active);
for(t = u_static->first->all_first; t; t = t->all_next) {
tile *t2 = universe_find_tile(u_evolving, 0, t->xpos, t->ypos, 0);
fprintf(f, "#P %d %d\n", t->xpos, t->ypos);
int x, y;
for(y=0; y<TILE_HEIGHT; y++) {
for(x=0; x<TILE_WIDTH; x++) {
char c = '.';
if(t2 && tile_get_cell(t2, x, y) == ON)
c = '@';
if(tile_get_cell(t, x, y) == ON)
c = '*';
else if(tile_get_cell(t, x, y) != OFF)
c = '?';
fputc(c, f);
}
fputc('\n', f);
}
}
fclose(f);
} else perror(name);
}
static int all_static_unknown(generation *ufirst, generation *ge)
{
tile *t, *tge;
tge = ge->all_first;
int y;
for(t = ufirst->all_first; t; t = t->all_next) {
for(y=0; y<TILE_HEIGHT; y++) {
if((~tge->bit1[y] & t->bit1[y] & t->bit0[y]) != 0)
return NO;
}
tge = tge->all_next;
}
return YES;
}
static void bellman_choose_cells(universe *u, generation *g);
static void bellman_recurse(universe *u, generation *g) {
int t_now = time(NULL);
if((t_now - last_print_time) > 10) {
last_print_time = t_now;
print_prune_counters();
total_time++;
if(total_time % 6 == 0)
printf("Total time %d min \n", total_time / 6);
}
// First make sure the static pattern is truly static
// TODO: check only the neighbourhood of the cell we just
// modified!
tile *t;
for(t = u_static->first->all_first; t; t = t->all_next) {
evolve_result res = tile_stabilise_3state(t, t->next);
if(res & ABORT) {
PRUNE(("Stable world is unstable\n"));
prune_unstable++;
return;
}
}
// Now check that the evolving universe is behaving itself
generation *ge;
evolve_result all_gens = 0;
int first_active_gen = 0;
int changed = 0;
int stabilized = NO;
int stabilized_gen = -1;
for(ge = u->first; ge && ge->next; ge = ge->next) {
if(ge->flags & CHANGED) {
changed = 1;
ge->flags &= ~CHANGED;
}
if(changed) {
CHECK(("Evolving generation %d\n", ge->next->gen));
generation_evolve(ge, bellman_evolve);
}
//write_life105(stdout, ge);
//getchar();
all_gens |= ge->flags;
if((first_active_gen == 0) && (ge->flags & DIFFERS_FROM_STABLE))
first_active_gen = ge->gen;
//stabilized flag is set to handle secondary destabilization.
if(first_active_gen > 0 && ge->n_active == 0 && stabilized == NO)
{
stabilized = YES;
stabilized_gen = ge->gen;
}
//If stabilized and destabilized again, before reaching stable_interval,
//then first_active_gen will be set to the new active generation.
if((stabilized == YES) && (ge->flags & DIFFERS_FROM_STABLE))
{
first_active_gen = ge->gen;
stabilized = NO;
}
CHECK(("Checking generation %d, flags %x all %x first_active_gen %d n_active %d changed %d\n",
ge->gen, ge->flags, all_gens, first_active_gen, ge->n_active, changed));
if(ge->flags & FILTER_MISMATCH) {
PRUNE(("Didn't match filter\n"));
//dump(1, 0);
prune_filter++;
return;
}
if(ge->flags & IN_FORBIDDEN_REGION) {
PRUNE(("Hit forbidden region\n"));
prune_forbidden++;
return;
}
// No activity is allowed prior to a point
if(ge->gen < first_encounter_gen) {
if(ge->flags & DIFFERS_FROM_STABLE) {
PRUNE(("Activity before generation %d\n", first_encounter_gen));
prune_encounter_too_early++;
return;
}
}
if(!(ge->flags & HAS_ON_CELLS) && (ge->flags & HAS_UNKNOWN_CELLS))
break;
if(stabilized == NO && all_static_unknown(u->first, ge) == YES && (ge->flags & HAS_UNKNOWN_CELLS))
break;
if(last_encounter_gen && (ge->gen >= last_encounter_gen)) {
if(!(all_gens & DIFFERS_FROM_STABLE) && !(ge->flags & HAS_UNKNOWN_CELLS)) {
PRUNE(("No activity before generation %d\n", last_encounter_gen));
prune_encounter_too_late++;
return;
}
}
if((first_active_gen > 0) && (ge->gen > first_active_gen + repair_interval)) {
// We reached the end of the repair interval;
// now the pattern must remain stable
if(ge->n_active > 0) {
PRUNE(("Activity after generation %d\n", first_active_gen + repair_interval));
prune_stabilises_too_slowly++;
return;
}
}
if((stabilized == YES) && (ge->gen >= stabilized_gen + stable_interval) && (ge->gen >= (u_filter->n_gens - 1))) {
// We reached the end of the stable interval;
// we may have a solution
if(!(ge->flags & HAS_UNKNOWN_CELLS)) {
bellman_found_solution("4", ge->gen);
//dump(1, 0);
PRUNE(("found a solution\n"));
prune_solution++;
return;
} else {
break;
}
}
if(ge->n_active > max_active) {
PRUNE(("Too much activity at generation %d\n", ge->gen));
prune_too_many_active++;
return;
}
#if 0
if((ge->gen == first_activity) && !(all_gens & DIFFERS_FROM_STABLE) && !(ge->flags & HAS_UNKNOWN_CELLS)) {
PRUNE(("No activity at generation %d\n", first_activity));
//dump(1, 0);
return;
}
#endif
//printf("First activity %d, checking %d\n", first_activity, ge->gen);
#if 0
if(ge->gen >= first_activity + repair_interval - 1) {
if(ge->flags & HAS_ACTIVE_SUCCESSORS) {
PRUNE(("Activity after generation %d\n", first_activity + repair_interval));
return;
}
}
#endif
}
//dump(1, 0);
bellman_choose_cells(u, g);
}
#define TRY(cdx, cdy) \
if(tile_get_cell(t->prev, x + cdx, y + cdy) == UNKNOWN_STABLE && validate_xy_for_symmetry(x + cdx, y + cdy) == YES) { \
dx = cdx; \
dy = cdy; \
goto found; \
}
static int validate_xy_for_symmetry(int x, int y)
{
switch(symmetry_type) {
case NONE:
return YES;
case HORIZ:
if(y >= symmetry_ofs - y)
return YES;
else
return NO;
case VERT:
if(x >= symmetry_ofs - x)
return YES;
else
return NO;
}
}
static int xy_symmetry(int x, int y, int* mirrorx_arr, int* mirrory_arr)
{
mirrorx_arr[0] = x;
mirrory_arr[0] = y;
switch(symmetry_type) {
case NONE:
return 1;
case HORIZ:
if(y == symmetry_ofs - y)
return 1;
mirrorx_arr[1] = x;
mirrory_arr[1] = symmetry_ofs - y;
return 2;
case VERT:
if(x == symmetry_ofs - x)
return 1;
mirrorx_arr[1] = symmetry_ofs - x;
mirrory_arr[1] = y;
return 2;
}
}
static void bellman_choose_cells(universe *u, generation *g) {
// Look for a tile with some unknown cells.
g = u_evolving->first;
tile *t;
do {
for(t = g->all_first;
t && !(t->flags & HAS_UNKNOWN_CELLS);
t = t->all_next)
;
if(!t) g = g->next;
} while(g && !t);
if(!g) {
// We got all the way to the end of the pattern.
bellman_found_solution("1", max_gens);
//dump(1, 0);
PRUNE(("found a solution\n"));
prune_solution++;
return;
}
// Find an unknown successor cell that's in the neighbourhood
// of an unknown-stable predecessor cell.
assert(t->prev);
//generation_evolve(g, bellman_evolve);
//printf("Generation %d has unknown cells\n", g->gen);
int x, y, dx = 2, dy = 2;
// Look for direct predecessors first ...
for(y=0; y<TILE_HEIGHT; y++) {
TILE_WORD is_unk = 0;
is_unk = t->bit1[y] & ~t->bit0[y];
if(is_unk) {
for(x = 0; x < TILE_WIDTH; x++) {
if((is_unk >> x) & 1) {
assert(tile_get_cell(t, x, y) == UNKNOWN);
// Now look for an unknown-stable cell near it.
if((x == 0) || (x == TILE_WIDTH-1) || (y == 0) || (y == TILE_HEIGHT-1)) {
printf("TODO: handle tile wrap! (%d, %d, %d)\n", g->gen, x, y);
dump(1, 0);
assert(0);
}
TRY(0, 0);
}
}
}
}
// ... then orthogonally adjacent cells ...
for(y=0; y<TILE_HEIGHT; y++) {
TILE_WORD is_unk = 0;
is_unk = t->bit1[y] & ~t->bit0[y];
if(is_unk) {
for(x = 0; x < TILE_WIDTH; x++) {
if((is_unk >> x) & 1) {
assert(tile_get_cell(t, x, y) == UNKNOWN);
// Now look for an unknown-stable cell near it.
if((x == 0) || (x == TILE_WIDTH-1) || (y == 0) || (y == TILE_HEIGHT-1)) {
printf("TODO: handle tile wrap! (%d, %d, %d)\n", g->gen, x, y);
dump(1, 0);
assert(0);
}
TRY(1, 0);
TRY(0, 1);
TRY(-1, 0);
TRY(0, -1);
}
}
}
}
// ... then diagonally adjacent ones.
for(y=0; y<TILE_HEIGHT; y++) {
TILE_WORD is_unk = 0;
is_unk = t->bit1[y] & ~t->bit0[y];
if(is_unk) {
for(x = 0; x < TILE_WIDTH; x++) {
if((is_unk >> x) & 1) {
assert(tile_get_cell(t, x, y) == UNKNOWN);
// Now look for an unknown-stable cell near it.
if((x == 0) || (x == TILE_WIDTH-1) || (y == 0) || (y == TILE_HEIGHT-1)) {
printf("TODO: handle tile wrap! (%d, %d, %d)\n", g->gen, x, y);
dump(1, 0);
assert(0);
}
TRY(-1, -1);
TRY(-1, 1);
TRY(1, -1);
TRY(1, 1);
}
}
}
}
printf("Didn't find an unknown cell!\n");
dump(1, 0);
assert(0);
return;
found:
assert(tile_get_cell(t, x, y) == UNKNOWN);
assert(tile_get_cell(t->prev, x+dx, y+dy) == UNKNOWN_STABLE);
assert(tile_get_cell((tile *)t->auxdata, x+dx, y+dy) == UNKNOWN_STABLE);
RECURSE(("Generation %d, unknown cell at (%d, %d, %d)\n",
g->gen, g->gen + 1, x+dx, y+dy));
assert(dx <= 1);
assert(dy <= 1);
x += dx;
y += dy;
int xmirror[8], ymirror[8], n_sym, i;
n_sym = xy_symmetry(x, y, xmirror, ymirror);
for(i = 0; i < n_sym; i++) {
if(tile_get_cell(t->prev, xmirror[i], ymirror[i]) != UNKNOWN_STABLE) {
fprintf(stderr, "Input region is asymmetric (%d,%d)=%d (%d,%d)=%d\n",
x, y, tile_get_cell(t->prev, x, y),
xmirror, ymirror, tile_get_cell(t->prev, xmirror, ymirror));
exit(-1);
}
}
#if 0
tile_set_cell(t->prev, x, y, OFF);
tile_set_cell(t->auxdata, x, y, OFF);
g->prev->flags |= CHANGED;
RECURSE(("Recursing with (%d,%d) = OFF\n", x, y));
bellman_recurse(u, g->prev);
#endif
if(n_live + n_sym <= max_live) {
for(i = 0; i < n_sym; i++){
tile_set_cell(t->prev, xmirror[i], ymirror[i], ON);
tile_set_cell((tile *)t->auxdata, xmirror[i], ymirror[i], ON);
}
g->prev->flags |= CHANGED;
RECURSE(("Recursing with (%d,%d) = ON\n", x, y));
n_live+=n_sym;
bellman_recurse(u, g->prev);
n_live-=n_sym;
} else {
PRUNE(("Too many live cells\n"));
prune_too_many_live++;
}
#if 1
for(i = 0; i < n_sym; i++){
tile_set_cell(t->prev, xmirror[i], ymirror[i], OFF);
tile_set_cell((tile *)t->auxdata, xmirror[i], ymirror[i], OFF);
}
g->prev->flags |= CHANGED;
RECURSE(("Recursing with (%d,%d) = OFF\n", x, y));
bellman_recurse(u, g->prev);
#endif
for(i = 0; i < n_sym; i++){