-
Notifications
You must be signed in to change notification settings - Fork 1
/
256ca.el
1504 lines (1400 loc) · 59.2 KB
/
256ca.el
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
;;; 256ca.el
;; Copyright (C) 2014 Joseph Corneli
;; Maintainer: [email protected]
;; Keywords: science, simulation
;; This file is NOT part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 2 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This code creates "meta-" cellular automata spacetime diagrams.
;; That is, the CA's evolve their own rules locally, rather evolving
;; an abstract state according to one global rule.
;; Previous work with CAs feature evolving global rules. This is the
;; first work that I am aware of that evolves the rule locally.
;; Alternate versions of the functions add an abstract "phenotype" on
;; top of the "genotype." One research question of interest is: Can
;; we define a simple "Baldwin effect" that feeds back from the
;; phenotype to the genotype and leads to the production of rules with
;; edge-of-chaos behavior? This can be seen as a simple analogue to
;; the cosmological question "How widespread is life in the universe?"
;; An example of a relevant Baldwin effect is:
;; XXX XXO XOO OOO
;; X X X X
;; === === === ===
;; 1 2 3 4
;;
;; Condition 1: there is no entropy; induce heavy mutation
;; Condition 2: there is moderate entropy; induce medium mutation
;; Condition 3: there is moderate entropy; induce low mutation
;; Condition 4: there is no(?) entropy; induce no mutation
;;; Next step:
;; The standard template could be understood to be generated by
;; locking in 0 0 0→ 0 along with a “variation” 0 1 0→ 0 and the
;; bitwise inverses of these. A wider class of templates could be
;; calculated from arbitrary phenotype data by the same operations.
;;
;; I.e. observing that our default rule is generated by:
;; 0 0 0→ 0 1 1 1→ 1 0 1 0→ 0 1 0 1→ 1
;;
;; Then we can generalise this to a rule would take the arbitrary observed local
;; phenotype "heredity" data a b c→ d and create the local template:
;; a b c→ d a' b' c'→ d' a b' c→ d a' b c'→ d'
;; where the primed symbols denote bitwise inverses.
;;
;; Let's call this the `generalised-template'. The question is then where
;; to embed that in the code.
;;; Notes:
;; Related CAs are determined by mirror image, complement, and mirror complement
;; 0000 0
;; 0001 1
;; 0010 2
;; 0011 3
;; 0100 4
;; 0101 5
;; 0110 6
;; 0111 7
;; 1000 8
;; 1001 9
;; 1010 A
;; 1011 B
;; 1100 C
;; 1101 D
;; 1110 E
;; 1111 F
;;; Code:
(require 'hexrgb)
;;; Convenience
(defvar hexcolour-keywords
'(("#[abcdef[:digit:]]\\{6\\}"
(0 (put-text-property (match-beginning 0)
(match-end 0)
'face (list :background
(match-string-no-properties 0)))))))
(defun hexcolour-add-to-font-lock ()
(font-lock-add-keywords nil hexcolour-keywords))
(add-hook 'emacs-lisp-mode-hook 'hexcolour-add-to-font-lock)
(defvar truth-table-3 '("000" "001" "010" "100" "011" "101" "110" "111")
"Define the standard list of possible 3-bit states.")
;; Note, this could be done in Latin but Chinese has nicer spacing!
;; The list of frequent ideographs and their definitions (in comments) are from
;; http://www.commonchinesecharacters.com/Lists/MostCommon2500ChineseCharacters
(defvar truth-table-8
'(("00000000" "一" "#000000") ; one
("00000001" "乙" "#010101") ; second
("00000010" "二" "#020202") ; two
("00000100" "十" "#040404") ; ten
("00000011" "丁" "#030303") ; fourth
("00000101" "厂" "#050505") ; factory
("00000110" "七" "#060606") ; seven
("00000111" "卜" "#070707") ; see
("00001000" "八" "#080808") ; eight
("00001001" "人" "#090909") ; man
("00001010" "入" "#0a0a0a") ; enter
("00001100" "儿" "#0c0c0c") ; son
("00001011" "九" "#0b0b0b") ; nine
("00001101" "几" "#0d0d0d") ; how much
("00001110" "了" "#0e0e0e") ; finish
("00001111" "乃" "#0f0f0f") ; so
("00010000" "刀" "#101010") ; knife
("00010001" "力" "#111111") ; power
("00010010" "又" "#121212") ; again
("00010100" "三" "#141414") ; three
("00010011" "干" "#131313") ; trunk
("00010101" "于" "#151515") ; in
("00010110" "亏" "#161616") ; deficit
("00010111" "士" "#171717") ; scholar
("00011000" "土" "#181818") ; earth
("00011001" "工" "#191919") ; work
("00011010" "才" "#1a1a1a") ; ability
("00011100" "下" "#1c1c1c") ; down
("00011011" "寸" "#1b1b1b") ; inch
("00011101" "丈" "#00ff33") ; ten feet #1d1d1d 00011101 11100010 10111000 01000111
("00011110" "大" "#0033ff") ; big #1e1e1e 00011110 11100001 01111000 10000111
("00011111" "与" "#1f1f1f") ; doubt
("00100000" "万" "#202020") ; see
("00100001" "上" "#212121") ; above
("00100010" "小" "#222222") ; small
("00100100" "口" "#242424") ; mouth
("00100011" "山" "#232323") ; mountain
("00100101" "巾" "#252525") ; cloth
("00100110" "千" "#262626") ; thousand
("00100111" "乞" "#272727") ; beg
("00101000" "川" "#282828") ; plain
("00101001" "亿" "#292929") ; 100 million
("00101010" "个" "#2a2a2a") ; this
("00101100" "么" "#2c2c2c") ; what
("00101011" "久" "#2b2b2b") ; long time
("00101101" "勺" "#2d2d2d") ; spoon
("00101110" "丸" "#2e2e2e") ; pill
("00101111" "夕" "#2f2f2f") ; dusk
("00110000" "凡" "#303030") ; ordinary
("00110001" "及" "#313131") ; and
("00110010" "广" "#323232") ; wide
("00110100" "亡" "#343434") ; lose
("00110011" "门" "#333333") ; door
("00110101" "义" "#353535") ; justice
("00110110" "之" "#363636") ; him/her
("00110111" "尸" "#373737") ; corpse
("00111000" "已" "#383838") ; then
("00111001" "弓" "#393939") ; bow
("00111010" "己" "#3a3a3a") ; self
("00111100" "卫" "#3c3c3c") ; guard
("00111011" "子" "#3b3b3b") ; child
("00111101" "也" "#3d3d3d") ; also
("00111110" "女" "#3e3e3e") ; female
("00111111" "飞" "#3f3f3f") ; fly
("01000000" "刃" "#404040") ; edge of blade
("01000001" "习" "#414141") ; practice
("01000010" "叉" "#424242") ; cross
("01000100" "马" "#444444") ; horse
("01000011" "乡" "#434343") ; home
("01000101" "丰" "#454545") ; luxuriant
("01000110" "王" "#464646") ; king
("01000111" "井" "#00ff33") ; neat #474747
("01001000" "开" "#484848") ; open
("01001001" "夫" "#494949") ; that
("01001010" "天" "#4a4a4a") ; heaven
("01001100" "元" "#4c4c4c") ; money
("01001011" "无" "#4b4b4b") ; no
("01001101" "云" "#4d4d4d") ; cloud
("01001110" "专" "#4e4e4e") ; special
("01001111" "扎" "#4f4f4f") ; tie
("01010000" "艺" "#505050") ; skill
("01010001" "木" "#515151") ; tree
("01010010" "五" "#525252") ; five
("01010100" "支" "#545454") ; support
("01010011" "厅" "#535353") ; hall
("01010101" "不" "#555555") ; not
("01010110" "太" "#565656") ; highest
("01010111" "犬" "#575757") ; dog
("01011000" "区" "#585858") ; area
("01011001" "历" "#595959") ; undergo
("01011010" "友" "#ffcc00") ; friend #5a5a5a 01011010 10100101
("01011100" "尤" "#5c5c5c") ; particular
("01011011" "匹" "#5b5b5b") ; mate
("01011101" "车" "#5d5d5d") ; car
("01011110" "巨" "#5e5e5e") ; huge
("01011111" "牙" "#5f5f5f") ; tooth
("01100000" "屯" "#606060") ; village
("01100001" "比" "#616161") ; near
("01100010" "互" "#626262") ; mutual
("01100100" "切" "#646464") ; definitely
("01100011" "瓦" "#636363") ; tile
("01100101" "止" "#656565") ; stop
("01100110" "少" "#666666") ; young
("01100111" "日" "#676767") ; sun
("01101000" "中" "#686868") ; hit
("01101001" "贝" "#696969") ; shellfish
("01101010" "内" "#6a6a6a") ; inside
("01101100" "水" "#6c6c6c") ; water
("01101011" "冈" "#6b6b6b") ; ridge
("01101101" "见" "#6d6d6d") ; meet
("01101110" "手" "#ff3300") ; hand #6e6e6e 01101110 01110110 10010001 10001001
("01101111" "午" "#6f6f6f") ; noon
("01110000" "牛" "#707070") ; ox
("01110001" "毛" "#717171") ; hair
("01110010" "气" "#727272") ; air
("01110100" "升" "#747474") ; ascend
("01110011" "长" "#737373") ; long
("01110101" "仁" "#757575") ; humane
("01110110" "什" "#ff3300") ; assorted #767676
("01110111" "片" "#777777") ; flake
("01111000" "仆" "#0033ff") ; servant #787878
("01111001" "化" "#797979") ; change
("01111010" "仇" "#7a7a7a") ; hatred
("01111100" "币" "#7c7c7c") ; money
("01111011" "仍" "#7b7b7b") ; still
("01111101" "仅" "#7d7d7d") ; barely
("01111110" "斤" "#7e7e7e") ; half kilo
("01111111" "爪" "#7f7f7f") ; claw
("10000000" "反" "#808080") ; contrary
("10000001" "介" "#818181") ; introduce
("10000010" "父" "#828282") ; father
("10000100" "从" "#848484") ; from
("10000011" "今" "#838383") ; today
("10000101" "凶" "#858585") ; terrible
("10000110" "分" "#868686") ; part
("10000111" "乏" "#0033ff") ; depleted #878787
("10001000" "公" "#888888") ; public
("10001001" "仓" "#ff3300") ; barn #898989
("10001010" "月" "#8a8a8a") ; moon
("10001100" "氏" "#8c8c8c") ; name
("10001011" "勿" "#8b8b8b") ; do not
("10001101" "风" "#8d8d8d") ; wind
("10001110" "欠" "#8e8e8e") ; owe
("10001111" "丹" "#8f8f8f") ; red
("10010000" "匀" "#909090") ; even
("10010001" "乌" "#ff3300") ; crow #919191
("10010010" "勾" "#929292") ; affair
("10010100" "凤" "#949494") ; phoenix
("10010011" "六" "#939393") ; six
("10010101" "文" "#959595") ; language
("10010110" "方" "#969696") ; square
("10010111" "火" "#979797") ; fire
("10011000" "为" "#989898") ; take
("10011001" "斗" "#999999") ; fight
("10011010" "忆" "#9a9a9a") ; remember
("10011100" "计" "#9c9c9c") ; calculate
("10011011" "订" "#9b9b9b") ; agree
("10011101" "户" "#9d9d9d") ; household
("10011110" "认" "#9e9e9e") ; recognize
("10011111" "心" "#9f9f9f") ; heart
("10100000" "尺" "#a0a0a0") ; foot
("10100001" "引" "#a1a1a1") ; pull
("10100010" "丑" "#a2a2a2") ; clown
("10100100" "巴" "#a4a4a4") ; wish
("10100011" "孔" "#a3a3a3") ; hole
("10100101" "队" "#ffcc00") ; team #a5a5a5
("10100110" "办" "#a6a6a6") ; manage
("10100111" "以" "#a7a7a7") ; because
("10101000" "允" "#a8a8a8") ; fair
("10101001" "予" "#a9a9a9") ; give
("10101010" "劝" "#aaaaaa") ; advise
("10101100" "双" "#acacac") ; pair
("10101011" "书" "#ababab") ; document
("10101101" "幻" "#adadad") ; fantasy
("10101110" "玉" "#aeaeae") ; jade
("10101111" "刊" "#afafaf") ; publish
("10110000" "末" "#b0b0b0") ; end
("10110001" "未" "#b1b1b1") ; not yet
("10110010" "示" "#b2b2b2") ; show
("10110100" "击" "#b4b4b4") ; hit
("10110011" "打" "#b3b3b3") ; dozen
("10110101" "巧" "#b5b5b5") ; opportunely
("10110110" "正" "#b6b6b6") ; straight
("10110111" "扑" "#b7b7b7") ; devote
("10111000" "扒" "#00ff33") ; cling #b8b8b8
("10111001" "功" "#b9b9b9") ; achievement
("10111010" "扔" "#bababa") ; throw
("10111100" "去" "#bcbcbc") ; go
("10111011" "甘" "#bbbbbb") ; sweet
("10111101" "世" "#bdbdbd") ; life
("10111110" "古" "#bebebe") ; ancient
("10111111" "节" "#bfbfbf") ; festival
("11000000" "本" "#c0c0c0") ; source
("11000001" "术" "#c1c1c1") ; method
("11000010" "可" "#c2c2c2") ; see
("11000100" "丙" "#c4c4c4") ; third
("11000011" "左" "#c3c3c3") ; unorthodox
("11000101" "厉" "#c5c5c5") ; strict
("11000110" "石" "#c6c6c6") ; stone
("11000111" "右" "#c7c7c7") ; right
("11001000" "布" "#c8c8c8") ; spread
("11001001" "龙" "#c9c9c9") ; dragon
("11001010" "平" "#cacaca") ; flat
("11001100" "灭" "#cccccc") ; extinguish
("11001011" "轧" "#cbcbcb") ; crush
("11001101" "东" "#cdcdcd") ; east
("11001110" "卡" "#cecece") ; block
("11001111" "北" "#cfcfcf") ; north
("11010000" "占" "#d0d0d0") ; occupy
("11010001" "业" "#d1d1d1") ; job
("11010010" "旧" "#d2d2d2") ; worn
("11010100" "帅" "#d4d4d4") ; smart
("11010011" "归" "#d3d3d3") ; return
("11010101" "目" "#d5d5d5") ; eye
("11010110" "旦" "#d6d6d6") ; dawn
("11010111" "且" "#d7d7d7") ; moreover
("11011000" "叮" "#d8d8d8") ; sting
("11011001" "叶" "#d9d9d9") ; harmony
("11011010" "甲" "#dadada") ; first
("11011100" "申" "#dcdcdc") ; extend
("11011011" "号" "#dbdbdb") ; roar
("11011101" "电" "#dddddd") ; electric
("11011110" "田" "#dedede") ; field
("11011111" "由" "#dfdfdf") ; follow
("11100000" "只" "#e0e0e0") ; only
("11100001" "央" "#0033ff") ; center #e1e1e1
("11100010" "史" "#00ff33") ; history #e2e2e2
("11100100" "兄" "#e4e4e4") ; elder brother
("11100011" "叼" "#e3e3e3") ; hold in the mouth
("11100101" "叫" "#e5e5e5") ; shout
("11100110" "叨" "#e6e6e6") ; garrulous
("11100111" "另" "#e7e7e7") ; another
("11101000" "叹" "#e8e8e8") ; sigh
("11101001" "四" "#e9e9e9") ; four
("11101010" "生" "#eaeaea") ; life
("11101100" "失" "#ececec") ; lose
("11101011" "禾" "#ebebeb") ; grain
("11101101" "丘" "#ededed") ; mound
("11101110" "付" "#eeeeee") ; pay
("11101111" "仗" "#efefef") ; weild
("11110000" "代" "#f0f0f0") ; substitute
("11110001" "仙" "#f1f1f1") ; immortl
("11110010" "们" "#f2f2f2") ; plural
("11110100" "仪" "#f4f4f4") ; apparatus
("11110011" "白" "#f3f3f3") ; free
("11110101" "仔" "#f5f5f5") ; meticulous
("11110110" "他" "#f6f6f6") ; him
("11110111" "斥" "#f7f7f7") ; blame
("11111000" "瓜" "#f8f8f8") ; melon
("11111001" "乎" "#f9f9f9") ; in
("11111010" "丛" "#fafafa") ; cluster
("11111100" "令" "#fcfcfc") ; see
("11111011" "用" "#fbfbfb") ; use
("11111101" "甩" "#fdfdfd") ; throw
("11111110" "印" "#fefefe") ; mark
("11111111" "乐" "#ffffff")); happy
"Define the standard list of possible 8-bit states.")
;; Further convenience for rendering results
(setq sigil-keywords
(map 'list (lambda (elt) (list (second elt)
`(0 (put-text-property (match-beginning 0)
(match-end 0)
'face (list :background
,(third elt)
:foreground
"dark magenta")))))
truth-table-8))
(defun sigil-add-to-font-lock ()
(font-lock-add-keywords nil sigil-keywords))
(add-hook 'emacs-lisp-mode-hook 'sigil-add-to-font-lock)
;; Functions:
(defun random-sigil ()
(second (nth (random 256) truth-table-8)))
(defun random-sigil-string (len)
(let ((res ""))
(dotimes (i len) (setq res (concat res (random-sigil))))
res))
(defun random-phenotype-string (len)
(let ((res ""))
(dotimes (i len) (setq res (concat res (int-to-string (random 2)))))
res))
(defun get-genotype-from-sigil (sig)
(car (member-if (lambda (elt) (string= (second elt) sig)) truth-table-8)))
; (get-genotype-from-sigil "一") ;=> ("00000000" "一" "#000000")
(defun get-genotype-from-rule (rule)
(car (member-if (lambda (elt) (string= (first elt) rule)) truth-table-8)))
; (get-genotype-from-rule "11111001") ;=> ("11111001" "乎" "#f9f9f9")
(defun get-sigil-from-rule (rule)
(second (car (member-if (lambda (elt) (string= (first elt) rule)) truth-table-8))))
(defun get-rule-from-sigil (sigil)
(first (car (member-if (lambda (elt) (string= (second elt) sigil)) truth-table-8))))
; (get-rule-from-sigil "#")
(defun evolve-sigil (sig &optional pred next ignore)
"Define the basic evolution of sigil SIG with (optional) neighbours PRED and NEXT.
An additional optional argument IGNORE, present for a consistent
signature, is simply ignored. If PRED and NEXT are not supplied,
use standard boundary conditions.
This version of evolution is the straightforward \"muliplication\" defined in our paper
that looks up the target for each allele according to the local rule."
(let* ((p (if pred (first (get-genotype-from-sigil pred))
(first (get-genotype-from-sigil "一"))))
(s (first (get-genotype-from-sigil sig)))
(n (if next (first (get-genotype-from-sigil next))
(first (get-genotype-from-sigil "一"))))
(s-ints (map 'list (lambda (a) (string-to-int (char-to-string a))) s))
(local-rule (mapcar* #'list truth-table-3 s-ints))
(local-data (map 'list (lambda (a b c) (concat (char-to-string a)
(char-to-string b)
(char-to-string c)))
p s n))
(output ""))
;; combine the results...
(mapc (lambda (num) (setq output (concat output (int-to-string num))))
;; of looking up each element of the local data according to the local rule
(map 'list (lambda (data)
(second (car (member-if (lambda (elt) (string= (first elt) data))
local-rule))))
local-data))
(get-genotype-from-rule output)))
; (evolve-sigil "«" "Å" "«") ;=> ("01101110" "Å" "#87afd7")
(defun evolve-sigil-with-blending (sig &optional pred next ignore)
"Define the blended evolution of sigil SIG with (optional) neighbours PRED and NEXT.
An additional optional argument IGNORE, present for a consistent
signature, is simply ignored. If PRED and NEXT are not supplied,
use standard boundary conditions.
This is the blending variant of evolution introduces a modification to the
multiplication rule from `evolve-sigil'. In this version, if both next-door neighbours are the same, then that result is used directly; only if they are different then the multiplication looks up the target for each allele according to the local rule."
(let* ((p (if pred (first (get-genotype-from-sigil pred))
(first (get-genotype-from-sigil "一"))))
(s (first (get-genotype-from-sigil sig)))
(n (if next (first (get-genotype-from-sigil next))
(first (get-genotype-from-sigil "一"))))
(s-ints (map 'list (lambda (a) (string-to-int (char-to-string a))) s))
(local-rule (mapcar* #'list truth-table-3 s-ints))
(local-data (map 'list (lambda (a b c) (concat (char-to-string a)
(char-to-string b)
(char-to-string c)))
p s n))
(output ""))
;; combine the results...
(mapc (lambda (num) (setq output (concat output (int-to-string num))))
;; of first looking at blends and then defaulting to looking
;; up each element of the local data according to the local rule
(map 'list (lambda (data)
(cond
((and (string= (substring data 0 1) "0")
(string= (substring data 2 3) "0"))
0)
((and (string= (substring data 0 1) "1")
(string= (substring data 2 3) "1"))
1)
(t
(second (car (member-if (lambda (elt)
(string= (first elt) data))
local-rule))))))
local-data))
(get-genotype-from-rule output)))
;; Here's a test case to show how the results of the two operations
;; can differ:
; (get-sigil-from-rule "01010100") "©"
; (get-sigil-from-rule "01101110") "Å"
; (get-sigil-from-rule "01010101") "«"
;; (evolve-sigil "©" "Å" "«") ;=> ("00101011" "Z" "#00d7d7")
;; (evolve-sigil-with-blending "©" "Å" "«");=> ("01101111" "Æ" "#87afff")
(defun evolve-sigil-with-blending-3 (sig &optional pred next ignore)
"A more choosy version of `evolve-sigil-with-blending'."
(let* ((p (if pred (first (get-genotype-from-sigil pred))
(first (get-genotype-from-sigil "一"))))
(s (first (get-genotype-from-sigil sig)))
(n (if next (first (get-genotype-from-sigil next))
(first (get-genotype-from-sigil "一"))))
(s-ints (map 'list (lambda (a) (string-to-int (char-to-string a))) s))
(local-rule (mapcar* #'list truth-table-3 s-ints))
(local-data (map 'list (lambda (a b c) (concat (char-to-string a)
(char-to-string b)
(char-to-string c)))
p s n))
(output ""))
;; combine the results...
(mapc (lambda (num) (setq output (concat output (int-to-string num))))
;; of first looking at blends and then defaulting to looking
;; up each element of the local data according to the local rule
(map 'list (lambda (data)
(cond
((and (string= (substring data 0 1) "0")
(string= (substring data 1 2) "0")
(string= (substring data 2 3) "0"))
0)
((and (string= (substring data 0 1) "1")
(string= (substring data 2 3) "1")
(string= (substring data 2 3) "1"))
1)
(t
(second (car (member-if (lambda (elt)
(string= (first elt) data))
local-rule))))))
local-data))
(get-genotype-from-rule output)))
(defun evolve-sigil-with-blending-flip (sig &optional pred next flip)
(let* ((p (if pred (first (get-genotype-from-sigil pred))
(first (get-genotype-from-sigil "一"))))
(s (first (get-genotype-from-sigil sig)))
(n (if next (first (get-genotype-from-sigil next))
(first (get-genotype-from-sigil "一"))))
(s-ints (map 'list (lambda (a) (string-to-int (char-to-string a))) s))
(local-rule (mapcar* #'list truth-table-3 s-ints))
(local-data (map 'list (lambda (a b c) (concat (char-to-string a)
(char-to-string b)
(char-to-string c)))
p s n))
(output ""))
;; combine the results...
(mapc (lambda (num) (setq output (concat output (int-to-string num))))
;; of first looking at blends and then defaulting to looking
;; up each element of the local data according to the local rule
;; ... conditionally flipping the locally-determined elements elements
(map 'list (lambda (data)
(cond
((and (string= (substring data 0 1) "0")
(string= (substring data 2 3) "0"))
(if flip 1 0))
((and (string= (substring data 0 1) "1")
(string= (substring data 2 3) "1"))
(if flip 0 1))
(t
(second (car (member-if (lambda (elt)
(string= (first elt) data))
local-rule))))))
local-data))
(get-genotype-from-rule output)))
(defun mutate-rule-n (genotype n)
(dotimes (j n)
(let* ((pos (random 8))
(elt (substring genotype pos (1+ pos))))
(cond
((string= elt "0")
(setq genotype (with-temp-buffer (insert genotype)
(goto-char (1+ pos))
(delete-char 1)
(insert "1")
(buffer-substring-no-properties (point-min)
(point-max)))))
(t
(setq genotype (with-temp-buffer (insert genotype)
(goto-char (1+ pos))
(delete-char 1)
(insert "0")
(buffer-substring-no-properties (point-min)
(point-max))))
))))
genotype)
; (mutate-rule-n "10101010" 1) ;=> "10101110"
(defun evolve-sigil-with-blending-mutation (sig &optional pred next ignore)
(let* ((mutation 1)
(p (if pred (first (get-genotype-from-sigil pred))
(first (get-genotype-from-sigil "一"))))
(s (first (get-genotype-from-sigil sig)))
(n (if next (first (get-genotype-from-sigil next))
(first (get-genotype-from-sigil "一"))))
(s-ints (map 'list (lambda (a) (string-to-int (char-to-string a))) s))
(local-rule (mapcar* #'list truth-table-3 s-ints))
(local-data (map 'list (lambda (a b c) (concat (char-to-string a)
(char-to-string b)
(char-to-string c)))
p s n))
(output ""))
;; combine the results...
(mapc (lambda (num) (setq output (concat output (int-to-string num))))
;; of first looking at blends and then defaulting to looking
;; up each element of the local data according to the local rule
(map 'list (lambda (data)
(cond
((and (string= (substring data 0 1) "0")
(string= (substring data 2 3) "0"))
0)
((and (string= (substring data 0 1) "1")
(string= (substring data 2 3) "1"))
1)
(t
(second (car (member-if (lambda (elt)
(string= (first elt) data))
local-rule))))))
local-data))
;; mutate the output in *mutation* places and return
(get-genotype-from-rule (mutate-rule-n output mutation))))
;; (evolve-sigil-with-blending-mutation "©" "Å" "«" 0) => ("01101111" "Æ" "#6f6f6f")
;; (evolve-sigil-with-blending-mutation "©" "Å" "«" 1) => ("01101111" "Æ" "#6f6f6f")
;; (evolve-sigil-with-blending-mutation "©" "Å" "«" 2) => ("01101011" "Ã" "#6b6b6b")
;; (evolve-sigil-with-blending-mutation "©" "Å" "«" 3) => ("01110101" "Ì" "#757575")
(defun evolve-sigil-with-blending-baldwin (sig &optional pred next context)
"Evolve sigil SIG with (optional) neighbours PRED and NEXT in a phenotypic CONTEXT.
If PRED and NEXT are not supplied, use standard boundary conditions. Optional argument
CONTEXT specifies the previous phenotype (3 old values and the new computed state).
This variant of evolution introduces a basic Baldwin effect, which changes the degree
of mutation depending on how boring the evolution in the phenotype seems to be."
(let* ((p (if pred (first (get-genotype-from-sigil pred))
(first (get-genotype-from-sigil "一"))))
(s (first (get-genotype-from-sigil sig)))
(n (if next (first (get-genotype-from-sigil next))
(first (get-genotype-from-sigil "一"))))
(s-ints (map 'list (lambda (a) (string-to-int (char-to-string a))) s))
(local-rule (mapcar* #'list truth-table-3 s-ints))
(local-data (map 'list (lambda (a b c) (concat (char-to-string a)
(char-to-string b)
(char-to-string c)))
p s n))
(output ""))
;; combine the results...
(mapc (lambda (num) (setq output (concat output (int-to-string num))))
;; of first looking at blends and then defaulting to looking
;; up each element of the local data according to the local rule
(map 'list (lambda (data)
(cond
((and (string= (substring data 0 1) "0")
(string= (substring data 2 3) "0"))
0)
((and (string= (substring data 0 1) "1")
(string= (substring data 2 3) "1"))
1)
(t
(second (car (member-if (lambda (elt)
(string= (first elt) data))
local-rule))))))
local-data))
;; Count the number of times the old context matches the new
;; state, and then mutate output that many times, then return.
;; Note, it could be interesting to create a variant that would
;; only mutate the contextually underdetermined "alleles",
;; combining this step with what we have above.
;;
;; Try mutating only 1/3 of the time or so...
(if (and context (< (random 3) 1))
(let* ((mutations 0)
(context-seq (string-to-list context))
(to-match (car (last context-seq))))
(map 'list (lambda (elt)
(when (eq elt to-match)
(setq mutations (1+ mutations))))
(nbutlast context-seq))
(get-genotype-from-rule (mutate-rule-n output (+ mutations 2))))
(get-genotype-from-rule output))))
;; (evolve-sigil-with-blending-baldwin "n" "Æ" "Ö") ;=> ("01111111" "Ö" "#7f7f7f")
;; (evolve-sigil-with-blending-baldwin "n" "Æ" "Ö" "0010") ;=> ("00111111" "q" "#3f3f3f")
(defun evolve-sigil-with-blending-baldwin-2 (sig &optional pred next context)
"Evolve sigil SIG with (optional) neighbours PRED and NEXT in a phenotypic CONTEXT.
If PRED and NEXT are not supplied, use standard boundary conditions. Optional argument CONTEXT specifies the previous phenotype (3 old values and the new computed state).
This is the blending variant of evolution introduces a modification to the
multiplication rule from `evolve-sigil'. In this version, if both next-door neighbours are the same, then that result is used directly; only if they are different then the multiplication looks up the target for each allele according to the local rule."
(let* ((p (if pred (first (get-genotype-from-sigil pred))
(first (get-genotype-from-sigil "一"))))
(s (first (get-genotype-from-sigil sig)))
(n (if next (first (get-genotype-from-sigil next))
(first (get-genotype-from-sigil "一"))))
(s-ints (map 'list (lambda (a) (string-to-int (char-to-string a))) s))
(local-rule (mapcar* #'list truth-table-3 s-ints))
(local-data (map 'list (lambda (a b c) (concat (char-to-string a)
(char-to-string b)
(char-to-string c)))
p s n))
(output "")
(mutations 0))
;; Count the number of times the old context matches the new
;; state. We will mutate the output that many times, but
;; only where there are contextually underdetermined "alleles".
(if context
(let* ((context-seq (string-to-list context))
(to-match (car (last context-seq))))
(map 'list (lambda (elt)
(when (eq elt to-match)
(setq mutations (1+ mutations))))
(nbutlast context-seq))))
;; Now, combine the results...
;; TODO: finish this
(mapc (lambda (num) (setq output (concat output (int-to-string num))))
;; of first looking at blends and then defaulting to looking
;; up each element of the local data according to the local rule
(map 'list (lambda (data)
(cond
((and (string= (substring data 0 1) "0")
(string= (substring data 2 3) "0"))
0)
((and (string= (substring data 0 1) "1")
(string= (substring data 2 3) "1"))
1)
(t
(second (car (member-if (lambda (elt)
(string= (first elt) data))
local-rule))))))
local-data))))
(defun bitflip (the-list &optional flip-here)
"Flip each bit of THE-LIST that is specified by the list of values FLIP-HERE.
The elements of FLIP-HERE ranges from 0 to 1 less than the length of THE-LIST.
For example, if THE-LIST is (0 1 1 0) and FLIP-HERE is (0 1 2) we would flip the
first three bits, to produce (1 0 0 0). If FLIP-HERE is not specified, flip everything
in the list."
(if flip-here
(let ((len (length the-list)))
(mapc (lambda (x)
(setq the-list (nconc
;; head - up to, but not including, middle
(butlast the-list (- len x))
;; middle, bit-flipped
(list (mod (1+ (nth x the-list)) 2))
;; tail - everything after middle
(nthcdr (1+ x) the-list))))
flip-here)
the-list)
(mapcar (lambda (x) (mod (1+ x) 2)) the-list)))
;; The big achievement with this version is to somewhat miraculously
;; to overcome the barrier of global stability. However, it produces
;; results that *locally* stabilize, to 一 or 乐. Introducing a
;; little random mutation restores some genotypic diversity.
;;
;; But it occurs to me that one way to go in this regard would be with
;; some variant of the PICARD rule, i.e. to impose the local constraint that
;; the number of 0's and 1's in the genotype should be equal or approximately
;; equal. This would certainly drive evolution away from the 00000000 and
;; 11111111 extremes. It might be possible combine that with a sort of "feature
;; collection" preference, namely, "if the phenotype data a b c→ d would add
;; to the diversity of my portfolio, then add it; otherwise, carry on with whatever
;; I'm doing." Even more explicitly than the "ad hoc template" strategy, this would
;; involve using the MetaCA as a recording device, and would also require it to have the
;; ability to introspect. However, all of that could be done without violating
;; "locality". It does, however, increase the complexity of the "organism" in a
;; rather radical way. Perhaps the "curiousity" about new features would be increased
;; in organisms that are less internally diverse, which would allow the ones that
;; are fairly diverse to enjoy longer population-level stability. The ability to
;; look at a neighbour's genes and borrow from those is another interesting possibility,
;; along with horizontal genetic drift that happens like it or not.
(defun evolve-sigil-with-ad-hoc-template (sig &optional pred next context)
"Evolve sigil SIG with (optional) neighbours PRED and NEXT in a phenotypic CONTEXT.
If PRED and NEXT are not supplied, use standard boundary conditions. Optional argument CONTEXT specifies the previous phenotype (3 old values and the new computed state).
`...' The goal of this function in to change the blending template based on contextual
data: specifically, observing that the default rule in `evolve-sigil-with-blending' is
generated by 0 0 0→ 0 1 1 1→ 1 0 1 0→ 0 1 0 1→ 1, this function
generalises \"blending\" to operate via a rule that would take the arbitrary observed
local phenotype heredity data a b c→ d (i.e. CONTEXT) and create the local template:
a b c→ d a' b' c'→ d' a b' c→ d a' b c'→ d'
where the primed symbols denote bitwise inverses."
(let* ((p (if pred (first (get-genotype-from-sigil pred))
(first (get-genotype-from-sigil "一"))))
(s (first (get-genotype-from-sigil sig)))
(n (if next (first (get-genotype-from-sigil next))
(first (get-genotype-from-sigil "一"))))
(s-ints (map 'list (lambda (a) (string-to-int (char-to-string a))) s))
(local-rule (mapcar* #'list truth-table-3 s-ints))
(local-data (map 'list (lambda (a b c) (concat (char-to-string a)
(char-to-string b)
(char-to-string c)))
p s n))
(output "")
(mutations 0))
;; Find the old context and the new state in the phenotype data.
;; This is interesting because we want to use the old data (`context-seq')
;; to create a template, which we will use as a guide in order to determine how
;; to mutate the output. More secifically, if the old data is "a b c" and
;; new data is d, then the template will be:
;; a b c→ d a' b' c'→ d' a b' c→ d a' b c'→ d'
;; and all other elements will be determined by local data.
;;
;; we define and augment the template step by step
(if context
(let* ((actual (map 'list (lambda (a) (string-to-int (char-to-string a)))
context)))
(setq template (list actual
(bitflip actual)
(bitflip actual '(1))
(bitflip actual '(0 2 3))
)))
(setq template
;; (list '(0 0 0 0) '(1 1 1 1) '(0 1 0 0) '(1 0 1 1))
nil))
;;
;; Now, combine the results according to the template we determined in the previous
;; step.
;;
;; Combine into a string
(mapc (lambda (num) (setq output (concat output (int-to-string num))))
;; map over the local data
(map 'list
(lambda (data)
;; pick the putative "parent generation" from memory
(let ((parent-generation (list (string-to-int (substring data 0 1))
(string-to-int (substring data 1 2))
(string-to-int (substring data 2 3))))
(template-to-match template)
(match-found nil)
result)
;; compare the "parent generation" to the template
(while (and (not match-found) template-to-match)
(let* ((candidate (car template-to-match)))
(if (equal parent-generation (butlast candidate 1))
(progn (setq match-found t)
(setq result (car (last candidate))))
(setq template-to-match (cdr template-to-match)))))
;; if no match from the template, use local logic
(unless match-found
(setq result
(second (car (member-if (lambda (elt)
(string= (first elt) data))
local-rule)))))
result))
local-data))
(get-genotype-from-rule output)
))
;; Similar to `evolve-sigil-with-ad-hoc-template' but try to maintain a balance
;; between zeros and ones. This doesn't seem to work well at all.
(defun evolve-sigil-with-collection-template (sig &optional pred next context)
(let* ((p (if pred (first (get-genotype-from-sigil pred))
(first (get-genotype-from-sigil "一"))))
(s (first (get-genotype-from-sigil sig)))
(n (if next (first (get-genotype-from-sigil next))
(first (get-genotype-from-sigil "一"))))
(s-ints (map 'list (lambda (a) (string-to-int (char-to-string a))) s))
(local-rule (mapcar* #'list truth-table-3 s-ints))
;; there will be anywhere between zero and eight 1's in the local rule
;; If `to-swap' is negative, there are more than 4 1's => shortage of 0's
;; If `to-swap' is positive, there are less than 4 1's => shortage of 1's
(to-swap (- 4 (count 1 local-rule :test #'equal)))
(local-data (map 'list (lambda (a b c) (concat (char-to-string a)
(char-to-string b)
(char-to-string c)))
p s n))
(output "")
(mutations 0))
;; we define and augment the template step by step
(if context
(let* ((actual (map 'list (lambda (a) (string-to-int (char-to-string a)))
context)))
(setq template (list actual
(bitflip actual)
(bitflip actual '(1))
(bitflip actual '(0 2 3))
)))
(setq template nil))
(cond
;; if we have shortage of 1s, delete items that map to 0 from the template
((> to-swap 0)
(delete-if (lambda (x) (eq (car (last x)) 0)) template))
;; otherwise if we have shortage of 0s, delete items that map to 1
((< to-swap 0)
(delete-if (lambda (x) (eq (car (last x)) 1)) template)))
;; Now, combine the results according to the template we determined in the previous
;; step.
;;
;; Combine into a string
(mapc (lambda (num) (setq output (concat output (int-to-string num))))
;; map over the local data, i.e. the genotypes that we're "multiplying"
(map 'list
(lambda (data)
;; pick the putative "parent generation" from memory
(let ((parent-generation (list (string-to-int (substring data 0 1))
(string-to-int (substring data 1 2))
(string-to-int (substring data 2 3))))
(template-to-match template)
(match-found nil)
result)
;; compare the "parent generation" to the template
(while (and (not match-found) template-to-match)
(let* ((candidate (car template-to-match)))
(if (equal parent-generation (butlast candidate 1))
(progn (setq match-found t)
(setq result (car (last candidate))))
(setq template-to-match (cdr template-to-match)))))
;; if no match from the template, use local logic
(unless match-found
(setq result
(second (car (member-if (lambda (elt)
(string= (first elt) data))
local-rule)))))
result))
local-data))
(get-genotype-from-rule output)
))
;; (defun remove-nth (index list)
;; (remove-if (constantly t) list :start index :end (1+ index)))
(defun randomize-sequence (sequence)
(let ((len (length sequence)))
(loop
with vector = (coerce sequence 'vector)
for i from 0 upto (- len 1)
do (rotatef (aref vector i) (aref vector (+ i (random (- len i)))))
finally (return (coerce vector 'list)))))
(defun randomly-flip-some-selected-values (the-list value quantity)
"Flip QUANTITY randomly-selected bits from THE-LIST that are initially set to VALUE.
For example, if THE-LIST is (0 1 1 0) and VALUE is 1 and QUANTITY
is 1 produce either (0 1 0 0) or (0 0 1 0)."
(let (matching-positions
(count 0))
;; find the matching positions; in our example, that's (1 2)
(mapc (lambda (elt)
(when (eq elt value)
(setq matching-positions (nconc matching-positions (list count))))
(setq count (1+ count)))
the-list)
;; decide how many of these to actually flip: `quantity' or `number-of-matches'?
(let* ((number-of-matches (length matching-positions))
(to-flip (cond ((>= quantity number-of-matches)
number-of-matches)
((< quantity number-of-matches)
quantity))))
;; now that we know how many to flip and the location of the population of
;; available bits to flip we must make our selection among these
;; bits. One way to do this is to randomly sort the list of matching
;; positions, and then truncate it, then flip the remaining positions in
;; the original list.
(bitflip the-list (nthcdr (- number-of-matches to-flip)
(randomize-sequence matching-positions))))))
;; This could be parameterized, so that we could search for interesting definitions
;; of "too many 1's", and also tune the chances of mutation and the number of bits
;; that will be flipped.
(defun balance-mutation (genotype)
"A random mutation on GENOTYPE that tends to balance the number of 0's and 1's."
(let* ((gene-list (map 'list (lambda (a) (string-to-int (char-to-string a)))
genotype))
(ones (count 1 gene-list :test #'equal))
(ret ""))
(cond
;; if there are too many 1's, randomly select and flip some of the 1's
((and (> ones 6) (< (random 20) 1))
(mapc (lambda (num) (setq ret (concat ret (int-to-string num))))
(randomly-flip-some-selected-values gene-list 1 1)))
;; if there are too many 0's, randomly select and flip some of the 0's
((and (< ones 2) (< (random 20) 1))
(mapc (lambda (num) (setq ret (concat ret (int-to-string num))))
(randomly-flip-some-selected-values gene-list 0 1)))
(t
(setq ret genotype)))
ret))
(defun evolve-sigil-with-mutating-template (sig &optional pred next context)
"Evolve sigil SIG with (optional) neighbours PRED and NEXT in a phenotypic CONTEXT.
If PRED and NEXT are not supplied, use standard boundary conditions. Optional argument CONTEXT specifies the previous phenotype (3 old values and the new computed state).
`...' The goal of this function in to change the blending template based on contextual
data: specifically, observing that the default rule in `evolve-sigil-with-blending' is
generated by 0 0 0→ 0 1 1 1→ 1 0 1 0→ 0 1 0 1→ 1, this function
generalises \"blending\" to operate via a rule that would take the arbitrary observed
local phenotype heredity data a b c→ d (i.e. CONTEXT) and create the local template:
a b c→ d a' b' c'→ d' a b' c→ d a' b c'→ d'
where the primed symbols denote bitwise inverses."