-
Notifications
You must be signed in to change notification settings - Fork 20
/
codelets.l
1423 lines (1370 loc) · 57.2 KB
/
codelets.l
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
;; May 21 09:31 1987 codelets.l Page 1
;ACTIVATE CODELET
(defun activate (activation pnode repump-yes-or-no)
;Activates a given pnode to a given level
;If the pnode is linked to a target, the add, subtract and multiply
;pnodes are activated as a function of the value of pnode
(let (val val- valx)
(send (eval pnode) :add-activation activation)
(cond
(repump-yes-or-no
(setq val (send pnode :value))
(setq val- (plus 5 (times 50 (expt 0.9 val))))
(setq valx (times 4 (sqrt val)))
(send node-add :set-activation (diff 60 val- valx))
(send node-subtract :set-activation val-)
(send node-multiply :set-activation valx)
(repump))
(t nil))))
;CHECK-TEMPERATURE FUNCTION
(defun check-temperature function ()
;This functions checks if the temperature of the cytoplasm is not too
;high. If it is the case, a node to be killed is randomly choosen
;according to the misfortunes. A "kill-node" codelet is loaded.
(let ((rvictim 0) (victim nil))
(cond
((< %temperature-threshold% (temperature))
; (terpri) (terpri) (format t " TEMP ~a" (temperature))
(setq reserve (send *cytoplasm* :secondary-cyto-nodes))
;the probability of being killed is a function of the activation
;of the node (proport. to 300 - activation)
(setq weights (mapcar 'diff300 (find-activations reserve)))
(setq rvictim (randlist weights))
(cond
(rvictim
(setq victim (nth rvictim reserve))
(cr-hang *coderack* (list 'kill-node victim) %first-urgency%)))))))
;COLLECT-MISFORTUNE FUNCTION
(defun collect-misfortune ()
;This function creates a list of the misfortunes of the secondary-
;cyto-nodes.
(let ((list nil) (cyto-nodes nil) (hap nil))
(setq current-target (send *current-target* :name))
(setq cyto-nodes (send *cytoplasm* :secondary-cyto-nodes))
(loop for node in cyto-nodes do
(setq hap (misfortune (send node :activation)
(send node :level)
(send node :status)))
(setq list (cons hap list)))
(reverse list)))
;COMPARE FUNCTION
(defun compare (element list)
;returns t if it exists in the list one number similar
;to element
(do
;;
;; May 21 09:31 1987 codelets.l Page 2
;;
((x list (cdr x)))
((null x) nil)
(if (< (sim element (car x)) 4) (return t))))
;COMPARE-B-TO-T CODELET
(defun compare-b-to-t (cyto-block)
;Compares a given block to the current target.
;If equality then it loads a replace-target codelet and frees the block
;if it is linked.
;If similar (see sim function) then it loads a decomp+ codelet.
;If digits in common (see digits-in-common function) then it loads a decompi
;codelet.
;If multiple (see multiple function) then it loads a decompx codelet.
(let (cyto-current-target current-target block stat target)
(setq target (send cyto-target :value))
(setq cyto-current-target (send *current-target* :name))
(setq current-target (send (eval cyto-current-target) :value))
(setq block (send cyto-block :value))
(setq stat (send cyto-block :status))
(cond
((or (= 0 (sim block current-target)) (= O (sim block target)))
(cond
((is-linked-to-target cyto-block) nil)
(t
(if (equal "linked" stat)
(kill-block (send (send cyto-block :upper-neighbor)
:upper-neighbor)))
(cr-hang *coderack* (list 'replace-target
cyto-block cyto-current-target) %upper-urgency%))))
((nequal "free" stat) nil)
(t
(cond
((= 1 (sim block current-target))
(cr-hang *coderack* (list 'decomp+ cyto-block cyto-current-target)
%first-urgency%))
((= 2 (sim block current-target))
(cr-hang *coderack* (list 'decomp+ cyto-block cyto-current-target)
%second-urgency%))
((= 3 (sim block current-target))
(cr-hang *coderack* (list 'decomp+ cyto-block Cyto-current-target)
%fourth-urgency%))))
(cond
((null (digits-in-common block current-target)) nil)
(t
(cr-hang *coderack* (list 'decompi cyto-block cyto-current-target)
(list 'quote (digits-in-common block current-target))) %second-urgency%)))))
;CONST-BL+ CODELET
(defun const-bl+ (cyto-block1 cyto-block2 sum)
;Creates a new block in the cytoplasm.
;Checks if the nodes are still free.
;Changes their status to "linked".
;Computes how to combine the blocks to get sum.
;Creates a new block in the cytoplasm.
;Creates a node x in the cytoplasm. Increments count to distinguish
;possible different versions of versions and targets.
;;
;; May 21 09:31 1987 codelets.l Page 3
;;
(let (stblock1 stblock2 block1 block2 lev1 lev2 lev (activation 200)
cyto-res cyto-op1 cyto-op2 cyto-op-node cyto-block-sum)
(setq stblock1 (send cyto-block1 :status))
(setq stblock2 (send cyto-block2 :status))
(setq block1 (send cyto-block1 :value))
(setq block2 (send cyto-block2 :value))
(setq lev1 (send cyto-block1 :level))
(setq lev2 (send cyto-block2 :level))
(setq lev (max lev1 lev2))
;Checks if the blocks are still free
(cond
((or (nequal "free" stblock1) (nequal "free" stblock2)) nil)
;Changes their status to "linked"
(t
(send cyto-block1 :set-status "linked")
(send cyto-block2 :set-status "linked")
;Creates a new block in the cytoplasm
(setq cyto-block-sum (concat 'cyto-block sum '-v *name-counter*))
(if (= 0 (mod sum 5)) (setq activation 250))
(if (= 0 (mod sum 10)) (setq activation 300))
(create-cyto-node activation cyto-block-sum sum "free"
(+ lev 2) "4bl" 1)
;Creates a node x in the cytoplasm
(cond
((equal sum (+ block1 block2))
(setq cyto-res (eval cyto-block-sum))
(setq cyto-op1 cyto-block1)
(setq cyto-op2 cyto-block2))
((equal sum (- block1 block2))
(setq cyto-res cyto-block1)
(setq cyto-op1 cyto-block2)
(setq cyto-op2 (eval cyto-block-sum)))
((equal sum (- block2 block1))
(setq cyto-res cyto-block2)
(setq cyto-op1 cyto-block1)
(setq cyto-op2 (eval cyto-block-sum))))
(setq cyto-op-node (concat 'plus
(min (send cyto-op1 :value) (send cyto-op2 :value)) '-
(max (send cyto-op1 :value) (send cyto-op2 :value)) '-v *name-counter*))
(create-op-node cyto-op-node cyto-res cyto-op1 cyto-op2
50 (+ lev 1))
;Increments count.
(setq *name-counter* (+ 1 *name-counter*))))))
;CONST-BLX CODELET
(defun const-blx (cyto-block1 cyto-block2 prod)
;Creates a new block in the cytoplasm.
;Checks if the nodes are still free.
;Changes their status to "linked".
;Creates a new block in the cytoplasm.
;Creates a node x in the cytoplasm. Increments count to distinguish
;possible different versions of versions and targets.
(let (stblock1 stblock2 block1 block2 lev1 lev2 lev cyto-block-prod
(activation 200) cyto-op-node)
(setq stblock1 (send cyto-block1 :status))
(setq stblock2 (send cyto-block2 :status))
;;
;; May 21 09:31 1987 codelets.l Page 4
;;
(setq block1 (send cyto-block1 :value))
(setq block2 (send cyto-block2 :value))
(setq lev1 (send cyto-block1 :level))
(setq lev2 (send cyto-block2 :level))
(setq lev (max lev1 lev2))
;Checks if the blocks are still free
(cond
((or (nequal "free" stblock1) (nequal "free" stblock2)) nil)
;Changes their status to "linked"
(t
(send cyto-block1 :set-status "linked")
(send cyto-block2 :set-status "linked")
;Creates a new block in the cytoplasm
(setq cyto-block-prod (concat 'cyto-block prod '-v *name-counter*))
(if (= 0 (mod prod 5)) (setq activation 250))
(if (= 0 (mod prod 10)) (setq activation 300))
(create-cyto-node activation cyto-block-prod prod "free"
(+ lev 2) "4bl" 1)
;Creates a node x in the cytoplasm
(setq cyto-op-node (concat 'times block1 '- block2 '-v *name-counter*))
(create-op-node cyto-op-node (eval cyto-block-prod)
cyto-block1 cyto-block2 50 (+ lev 1))
;Increments count.
(setq *name-counter* (add1 *name-counter*))))))
;CREATE-CODERACK FUNCTION
;Initialize the coderack
(defun create-coderack ()
(cr-make-coderack 'my-coderack
(list %upper-urgency% %first-urgency% %second-urgency%
%third-urgency% %fourth-urgency% %fifth-urgency% 0))
(setq *coderack* 'my-coderack))
;CREATE-CYTO-NODE CODELET
(defun create-cyto-node (activation name value status
level type success)
;Codelet which creates a new node in the cytoplasm
;Loads a link-to-pnet codelet
;Name is the name of the variable which contains the address of the node
;Loads a compare-b-to-t codelet if the node type is "2b" or "4bl".
;Updates the context if the node type is "1t" or "3dt".
(set name (make-instance 'cyto-node
:activation activation
:name name
:value value
:status status
:level level
:type type
:success success))
(format t "Node ~a created" name) (terpri)
(send *cytoplasm* :set-nodes (cons (eval name) (send *cytoplasm* :nodes)))
(cr-hang *coderack* (list 'link-to-pnet name type value)
%upper-urgency%)
(if (or (equal type "2b") (equal type "4bl")) then
(cr-hang *coderack* (list 'compare-b-to-t name) %upper-urgency%))
(if (or (equal type "1t") (equal type "3dt")) then
(update-current-target (eval name) 100)))
;;
;; May 21 09:31 1987 codelets.l Page 5
;;
;CREATE-OP-NODE CODELET
(defun create-op-node (name res op1 op2 activation level)
;Codelet which creates an operation node in the cytoplasm
;Creates links with the cyto-nodes res, op1, op2
;Updates the neighbors variable of res, op1, op2
;Updates the cytoplasm nodes variable.
(setq n1 (list res 'result))
(setq n2 (list op1 'operand))
(setq n3 (list op2 'operand))
(set name (make-instance 'cyto-node
:name name
:type "5g"
:level level
:neighbors (list n1 n2 n3)))
(format t "Node ~a created" name)(terpri)
(send *cytoplasm* :set-nodes (cons (eval name)(send *cytoplasm* :nodes)))
(send res :update-neighbors (list (list (eval name) 'result)))
(send op1 :update-neighbors (list (list (eval name) 'operand)))
(send op2 :update-neighbors (list (list (eval name) 'operand))))
;DECOMP+ CODELET
(defun decomp+ (cyto-block cyto-current-target)
;Checks if the nodes are still free
;Creates a derived target in the cytoplasm
;Changes the status of the current-target to "linked"
;Changes the status of the block to "linked"
;Changes the current-target
;Create a node + in the cytoplasm. Increments count to distinguish
;possible different versions of operations and targets.
;Loads compare codelets for all the free blocks and bricks in the cyto.
(let (stblock sttarget lev block current-target
res cyto-oper cyto-res cyto-derived-target diff cyto-op-node)
(setq stblock (send cyto-block :status))
(setq sttarget (send cyto-current-target :status))
(setq lev (send cyto-current-target :level))
(setq block (send cyto-block :value))
(setq current-target (send cyto-current-target :value))
(cond
((or (nequal "free" stblock) (nequal "free" sttarget)) nil)
((= block current-target)
(cr-hang *coderack* (list 'replace-target cyto-block
cyto-current-target) %upper-urgency%))
(t
(cond
((> block current-target)
(setq res block) (setq cyto-res cyto-block) (setq oper current-target)
(setq cyto-oper cyto-current-target))
((<= block current-target)
(setq oper block) (setq cyto-oper cyto-block) (setq res current-target)
(setq cyto-res cyto-current-target)))
(setq diff (- res oper))
(setq cyto-derived-target (concat 'cyto-target- diff '-v *name-counter*))
;Creates a derived target in the cytoplasm
(create-cyto-node 200 cyto-derived-target diff "free"
;;
;; May 21 09:31 1987 codelets.l Page 6
;;
(- lev 2) "3dt" 0)
;Changes the status of the current-target to "linked"
(send cyto-current-target :set-status "linked")
;Changes the status of the block to "linked"
(send cyto-block :set-status "linked")
;Changes the current-target
(update-current-target (eval cyto-derived-target) 50)
;Creates a node + in the cytoplasm. Increments count to distinguish
;possible different versions of operations and targets.
(setq cyto-op-node (concat 'plus oper '- diff '-v *name-counter*))
(setq *name-counter* (+ 1 *name-counter*))
(create-op-node cyto-op-node cyto-res cyto-oper
(eval cyto-derived-target) 50 (- lev 1))
;Loads compare codelets for all the free blocks and bricks in the cyto.
(loop for nn in (send *cytoplasm* :cyto-brick-block-nodes) do
(cond
((is-linked-to-target nn) nil)
(t
(cr-hang *coderack* (list 'compare-b-to-t nn) %first-urgency%))))))))
;DECOMPx CODELET
(defun decompx (cyto-block cyto-current-target)
;Checks if the nodes are still free
;Creates a derived target in the cytoplasm
;Changes the status of the current-target block to "linked"
;Changes the status of the block to "linked"
;Changes the current-target
;Create a node + in the cytoplasm. Increments count to distinguish
;possible different versions of operations and targets.
;Loads compare codelets for all the free blocks and bricks in the cyto.
(let (stblock sttarget lev oper res cyto-oper
cyto-res quot cyto-derived-target cyto-block cyto-op-node)
(setq stblock (send cyto-block :status))
(setq sttarget (send cyto-current-target :status))
(setq lev (send cyto-current-target :level))
(cond
( (or (nequal "free" stblock) (nequal "free" sttarget)) nil)
(t
(setq oper (send cyto-block :value))
(setq res (send cyto-current-target :value))
(setq cyto-oper cyto-block)
(setq cyto-res cyto-current-target)
(setq quot (/ res oper))
(setq cyto-derived-target (concat 'cyto-target- quot '-v *name-counter*))
;Creates a derived target in the cytoplasm
(create-cyto-node 200 cyto-derived-target quot "free"
(- lev 2) "3dt" 0)
;Changes the status of the current-target to "linked"
(send cyto-current-target :set-status "linked")
;Changes the status of the block to "linked"
(send cyto-block :set-status "linked")
;Changes thte current-target
(update-current-target (eval cyto-derived-target) 50)
;Creates a node x in the cytoplasm. Increments count to distinguish
;possible different versions of operations and targets.
(setq cyto-op-node (concat 'times oper '- quot '-v *name-counter*))
;;
;; May 21 09:31 1987 codelets.l Page 7
;;
(setq *name-counter* (add1 *name-counter*))
(create-op-node cyto-op-node cyto-res cyto-oper
(eval cyto-derived-target) 50 (- lev 1))
;Loads compare codelets for all the free blocks and bricks in the cyto.
(loop for nn in (send *cytoplasm* :cyto-brick-block-nodes) do
(cond
((is-linked-to-target nn) nil)
(t
(cr-hang *coderack* (list 'compare-b-to-t nn) %first-urgency%))))))))
;DECOMPI CODELET
(defun decompi (cyto-block cyto-current-target rank-list)
;Loads a Look-for-blx codelet if the rank-list has a 1 in the first or
;second position, that is if the target is equal to 10 or 100 times
;the block plus something less than 10 or 100.
;The activation variable of the block is increased. This will make it
;more probably chosen when a new block will be built.
(let ((target (send cyto-current-target :value))
(block (send cyto-block :value))
(op2 nil))
(cond
((= 1 block) nil)
((= 1 (cadr rank-list))
(setq op2 10) (send cyto-block :set-activation 200))
((= 1 (car rank-list))
(setq op2 100) (send cyto-block :set-activation 100)))
(if op2 then
(cr-hang *coderack* (list 'look-for-blx (times block op2)
block op2) %second-urgency%))))
;DECOMPOSE FUNCTION
(defun decompose (node)
;This function expands a given node using its neighbors variable.
;Used in the decoding of the result.
;The operation node is first extracted and from it the relevant
;information is searched for and printed. The function is recursived.
;Once a operation node has been listed, its listed variable is put to t.
(let ((op) (opn) (close) (operands) (op1v) (op2v) (op1n) (op2n))
(setq op (send (eval node) :neighbors))
;Considers all the neighbors (operation nodes) of the node.
(do ((x op (cdr x)))
((null x))
;Controls if the operation has not been already considered
(cond
((send (caar x) :listed) nil)
;If not yet considered it is decomposed
(t
;The listed variable of the op node is put to t.
(send (caar x) :set-listed t)
(setq opn (send (caar x) :name))
(setq close (send (caar x) :neighbors))
(setq operands (sup close (eval node)))
(setq op1v (send (car operands) :value))
(setq op2v (send (cadr operands) :value))
(setq op1n (send (car operands) :name))
(setq op2n (send (cadr operands) :name))
;;
;; May 21 09:31 1987 codelets.l Page 8
(format t "Operation ~a has been applied " opn)
(terpri) (format t "to ~a ( ~a) and to ~a ( ~a)" op1n op1v op2n op2v)
(terpri) (format t "to get ~a" (send (eval node) :name))(terpri)
(if (nequal "2b" (send (eval op1n) :type)) then (decompose op1n))
(if (nequal "2b" (send (eval op2n) :type)) then (decompose op2n)))))))
;DECREASE-INTEREST FUNCTION
(defun decrease-interest ()
;This function decreases (by 40%) the level of interest
;(activation variable) of the cyto-nodes which are free
;and secondary nodes.
(let ((list nil) (cyto-nodes nil))
(setq cyto-nodes (send *cytoplasm* :free-secondary-cyto-nodes))
(loop for node in cyto-nodes do
(setq new (times (send node :activation) 0.6))
(send node :set-activation new))))
;DIFF300 FUNCTION
(defun diff300 (val)
;used in check-temperature
(diff 300 val))
;DIGITS-IN-COMMON FUNCTION
;This function examines if two given values start with the same
;digit(s) or end with the same digit(s)
;Returns nil or a 3 dimensional vector.
;Examples: (digits-in-common 7 71) gives (0 1 0)
;Examples: (digits-in-common 7 607) gives (0 0 1)
;Examples: (digits-in-common 7 707) gives (1 0 1)
;Examples: (digits-in-common 22 222) gives (0 1 1)
(defun digits-in-common (val1 val2)
(let ((res '(0 0 0)))
(cond
((> val1 val2) (setq res nil))
(t
(if (zerop (mod (- val2 val1) 10)) then (setq res '(0 0 1)))
(if (equal val1 (*quo val2 10))
then (setq res (list 0 1 (caddr res))))
(if (equal val1 (*quo val2 100))
then (setq res (cons 1 (cdr res)))
else res)))
(cond
((equal res '(0 0 0)) (setq res nil))
(t res))))
;DISCONNECT FUNCTION
(defun disconnect (cyto-node op)
;Suppress a given block/derived-target in the cytoplasm.
;Takes the neighbors of its op-neighbor and frees them.
;Updates all the neighbors variable.
;Updates the node variable of the cytoplasm.
;Suppresses the link in the pnet by loading an "free-from-pnet"
;codelet.
(let ((close) (operands) (op1n) (op2n) (opn) (cyton))
;Frees the component blocks.
(setq close (send op :neighbors))
;;
;; May 21 09:31 1987 codelets.1 Page 9
;;
(setq operands (sup close cyto-node))
(setq op1n (send (car operands) :name))
(setq op2n (send (cadr operands) :name))
(if (equal "linked" (send (eval op1n) :status))
(send (eval op1n) :set-status "free"))
(if (equal "linked" (send (eval op2n) :status))
(send (eval op2n) :set-status "free"))
;Updates the neighbors variable.
(send (eval op1n) :suppress-neighbors op)
(send (eval op2n) :suppress-neighbors op)
(send cyto-node :suppress-neighbors op)
;Updates the node variable of the cytoplasm.
(send *cytoplasm* :suppress-node op)
(setq opn (send op :name))
(format t "Node ~a killed" opn) (terpri)
(send *cytoplasm* :suppress-node cyto-node)
(send cyto-node :set-status "killed")
(setq cyton (send cyto-node :name))
(format t "Node ~a killed" cyton) (terpri)
;Suppresses the link in the pnet by loading an "free-from-pnet"
;codelet.
(cr-hang *coderack* (list 'free-from-pnet cyto-node)
%first-urgency%)))
;ELIMINATE FUNCTION
(defun eliminate (element list)
;Eliminates from a given list the member which is the closest from
;element. For instance (eliminate 8 '(4 6 7) ) should give (4 6).
;In case of ties, the first best is eliminated.
(let ((diffmin 999))
(do
((x list (cdr x)))
((null x))
(setq diff (abs (- element (car x))))
(cond
((< diff diffmin) (setq diffmin diff) (setq min (car x)))
(t nil)))
(remove-dd min list)))
;FIND-ACTIVATIONS FUNCTION
(defun find-activations (list)
;Finds the activations of a list of cyto-nodes.
(let ((res nil)(x nil))
(do ((v list (cdr v)))
((null v) (reverse res))
(setq x (send (car v) :activation))
(setq res (cons x res)))))
;FIND-IN FUNCTION
(defun find-in (element list &optional (level 4))
;Finds in a list of nodes a node with a value similar to a given value.
;The level of similarity is optionally specified in level. Its meaning
;is relative (see sim function) , 1 being a perfect match.
;A node is randomly chosen (the weights are the activations of the nodes)
;in the list and its value is compared with
;element. If not too different the node is returned.
;;
;; May 21 09:31 1987 codelets.l Page 10
;;
(let((res1 list) (res2 (find-activations list)) (rep nil))
(do
((x (randlist res2) (randlist res2)))
((equal nil x) rep)
(setq rep (nth x res1))
(cond
((> level (sim (send rep :value) element))
(setq res2 nil))
(t (setq res1 (remove-dd rep res1))
(setq res2 (remove-dd (nth x res2) res2))
(setq rep nil))))))
;FIND-INTEREST-IN-PNET FUNCTION
(defun find-interest-in-pnet (val)
;Finds the activation of the corresponding pnode in the pnet
;and returns an urgency. If the pnode is a target the urgency
;is maximal. If it is a brick the urgency is medium. In the other
;cases, the urgency is an increasing fonction of the activation.
(let (node type urgency priority)
(cond
((< val 200)
(setq node (find-node val))
(setq priority (send node :activation))
(setq type (caar (sortcar (send node :instances) nil)))
(cond
((= val (send (send *current-target* :name) :value))
(setq urgency %upper-urgency%))
((or (equal type "1t") (equal type "3dt"))
(setq urgency %first-urgency%))
((and (nequal type "2b") (nequal type "4bl"))
(cond
((< priority 10) (setq urgency %fifth-urgency%))
((< priority 14) (setq urgency %fourth-urgency%))
((< priority 50) (setq urgency %third-urgency%))
(t (setq urgency %second-urgency%))))
(t (setq urgency %third-urgency%))))
((< val 500) (setq urgency %fifth-urgency%))
(t (setq urgency 0)))))
;FIND-NODE FUNCTION
(defun find-node (val)
;This function finds the pnode which should be associated to a given
;value (generally, the closest one)
;Similar to the code used in LINK-TO-PNET
(setq address (concat 'node- val))
(cond
((boundp address) t)
((= 0 val) (setq address 'node-1))
((< (round val) 200) (setq address (concat 'node- (round val))))
(t (setq address 'node-150)))
(eval address))
;FREE-FROM-PNET CODELET
(defun free-from-pnet (name)
;Suppresses in the pnet the link to name.
;Decreases its activation.
;;
;; May 21 09:31 1987 codelets.l Page 11
;;
(let ((pnode nil))
(setq pnode (car (send name :plinks)))
(cond
(pnode (send pnode :suppress-instances name)
(send pnode :add-activation %node-minus%)))))
;IS-LINKED-TO FUNCTION
(defun is-linked-to (cyto-block1 cyto-block2)
;Checks if two cyto-nodes are linked in the cytoplasm.
(let (lev1 lev2 st1 st2 node)
(setq lev1 (send cyto-block1 :level))
(setq lev2 (send cyto-block2 :level))
(setq st1 (send cyto-block1 :status))
(setq st2 (send cyto-block2 :status))
(cond
;If the levels are equal, they cannot be linked.
((= lev1 lev2) nil)
;If they are both free, they cannot be free neither.
((and (equal "free" st1) (equal "free" st2)) nil)
;The level of cyto-block1 will be considered the lower.
;If it is not the case they are interchanged.
(t
(when (> lev1 lev2)
(setq node cyto-block2)
(setq cyto-block2 cyto-block1)
(setq cyto-block1 node))
;If the lower cyto-block (that is cyto-block1) is free,
;once again, they cannot be linked.
(cond
((nequal "linked" (send cyto-block1 :status)) nil)
(t
;The upper neighbors of cyto-block1 are examined. If one
;of them is equal to cyto-block2, then the function will
;return "true", if not, it will return "nil".
(do
((node (send (send cyto-block1 :upper-neighbor)
:upper-neighbor)
(send (send node :upper-neighbor)
:upper-neighbor)))
((null node) nil)
(cond
((eq node cyto-block2)
(setq node nil) (return t))
((equal "free" (send node :status))
(return nil))
((equal "1t" (send node :type))
(return nil))
(t nil)))))))))
;IS-LINKED-TO-TARGET FUNCTION
(defun is-linked-to-target (cyto-block)
;This function checks if a given block/brick is linked in some way
;to the target.
(cond
((equal "linked" (send cyto-block :status))
(do
;;
;; May 21 09:31 1987 codelets.l Page 12
;;
((node cyto-block
(send (send node :upper-neighbor) :upper-neighbor)))
((null node) nil)
(cond
((equal "free" (send node :status)) (return nil))
((equal "1t" (send node :type)) (return t))
(t nil))))))
;KILL-BLOCK FUNCTION
(defun kill-block (cyto-block)
;This function is able to suppress a cyto-block in the tree structure of
;the cytoplasm. Two different cases have to be considered. First the node
;can be a free block. Then, we just have to disconnect the two blocks or
;bricks which are under it in the tree (they are linked through an
;operation node of course). If the node is not free, two different
;manipulations are needed. First, as in the first case, disconnection of
;the children but also determination of the parent and suppression of
;that parent by recursion. If the parent happens to be the target, a call
;to kill-dtarget is needed. It will proceed by going down in the tree,
;from parent to children (one of whose is always a derived-target).
;
;Disconnects the cyto-block from the nodes which are under it.
(let (op cyto-node)
(disconnect cyto-block (send cyto-block :lower-neighbor))
(cond
;If the block was free (thus no more neighbors), it is finished.
((null (send cyto-block :neighbors)) nil)
;If the block was not free, it calls op the upper operation-node.
(t
(setq op (caar (send cyto-block :neighbors)))
;It determines the upper block and if it is not the target, applies
;recursively kill-block. If it is the target, kill-dtarget is applied
;to the first derived-target (child of the target).
(setq cyto-node (send op :upper-neighbor))
(cond
((equal "4bl" (send cyto-node :type)) (kill-block cyto-node))
((equal "1t" (send cyto-node :type))
(setq cyto-node (send op :lower-dtarget-neighbor))
(kill-dtarget cyto-node)))))))
;KILL-DTARGET FUNCTION
(defun kill-dtarget (cyto-dtarget)
;This function is able to suppress a cyto-derived-target in the tree structure of
;the cytoplasm. Two different cases have to be considered. First the node
;can be a free derived-target. Then, we just have to disconnect the two nodes
;(block/brick and dtarget/target) which are above and next to it in the tree
;(they are linked through an operation node of course).
;If the node is not free, two different manipulations are needed.
;First, as in the first case, disconnection of the parent and the brother
;but also determination of the dtarget-child and suppression of it by
;recursion
;
;Disconnects the cyto-dtarget
(disconnect cyto-dtarget (send cyto-dtarget :upper-neighbor))
(let (op cyto-node)
(cond
;;
;; May 21 09:31 1987 codelets.l Page 13
;;
;If the derived-target was free (no more neighbors), it is finished.
((null (send cyto-dtarget :neighbors)) nil)
;If the derived-target was not free, it calls op the lower operation-node.
(t
(setq op (caar (send cyto-dtarget :neighbors)))
;It determines the dtarget-child and applies recursively kill-dtarget.
(setq cyto-node (send op :lower-dtarget-neighbor))
(kill-dtarget cyto-node)))))
;KILL-NODE CODELET
(defun kill-node (cyto-node)
;This codelet suppresses a block or a derived-target in the cytoplasm
;and all the dependent nodes.
;Depending on the type of the node, it uses kill-block or kill-dtarget.
;It updates the current-target and reactivates the corresponding
;pnode in the pnet (and the result pnode).
(let ((type) (pnode nil) (new-target nil) old-block)
(cond
((memq cyto-node (send *cytoplasm* :nodes))
(setq type (send cyto-node :type))
(cond
((equal type "4bl") (kill-block cyto-node))
((equal type "3dt")
;If the node to be killed is a target, its old block neighbor is
;saved in order to avoid to recreate the same association directly
;after.
(setq old-block (send cyto-node :block-neighbor))
(kill-dtarget cyto-node))
(t nil))
(setq new-target (send *cytoplasm* :find-new-target))
(update-current-target new-target 100)
(if new-target
(setq pnode (car (send (send *current-target* :name) :plinks))))
(if pnode
(cr-hang *coderack* (list 'activate %dtarget-plus% pnode t)
%upper-urgency%))
;Loads compare codelets for all the free blocks and bricks in the cyto.
(loop for nn in (send *cytoplasm* :cyto-brick-block-nodes) do
(cond
((is-linked-to-target nn) nil)
((eq old-block nn) nil)
(t
(cr-hang *coderack* (list 'compare-b-to-t nn) %second-urgency%))))))))
;LINK-TO-PNET CODELET
(defun link-to-pnet ( name type value)
;Determines the level of activation depending on the type of node.
;If the node is a derived-target, the result node in the pnet is
;reactivated via the loading of an activate codelet.
;Updates instances in the closest node of the pnet
;Loads an activate codelet
(let ((repump-yes-or-no nil) (address nil) (transact nil))
(cond ((equal "killed" (send (eval name) :status)) nil)
(t
(cond
((equal type "1t") (setq activation %target-activation%)
;;
;; May 21 09:31 1987 codelets.l Page 14
;;
(setq repump-yes-or-no t))
((equal type "2b") (setq activation %brick-activation%))
((equal type "3dt") (setq activation %dtarget-activation%)
(setq repump-yes-or-no t))
((equal type "4bl") (setq activation %block-activation%)))
(setq address (concat 'node- value))
(cond
((boundp address) (setq transact activation))
((= 0 value) (setq address 'node-1)
(setq transact 0))
((< (round value) 200)
(setq address (concat 'node- (round value)))
(setq transact (times activation (ratio value))))
(t (setq address 'node-150)
(setq transact (times activation 0.50))))
(send (eval address) :update-instances (list type name))
(send name :update-plinks (eval address))
(cr-hang *coderack* (list 'activate transact
address repump-yes-or-no) %upper-urgency%)))))
;LOOK-FOR-APPROX-BL+ CODELET
(defun look-for-approx-bl+ (res op1 op2)
;Given 3 numbers which represent an operation (res = op1 + op2)
;checks if this type of operation could be instantiated in the
;cytoplasm.
;Checks first if one of the 3 arguments is similar to ther
;current-target.
;Then eliminates in (res op1 op2) the value closest to the current-
;target.
(let (current-target values-to-find blocks rn first-value
cyto-block1 cyto-block2 n-blocks second-value v1 v2
sum)
(setq current-target (send (eval (send *current-target* :name)) :value))
(cond
((compare current-target (list res op1 op2))
(setq values-to-find (eliminate current-target (list res op1 op2)))
;Tries to find randomly a block not too far from one of the given
;value. In order to do that, chooses randomly a value and a block and
;compares them.
(setq blocks (send *cytoplasm* :cyto-brick-block-nodes))
(setq rn (random 2))
(setq first-value (nth rn values-to-find))
(setq cyto-block1 (find-in first-value blocks))
;Tries to find another block not too far from the second value.
(setq n-blocks (remove-dd cyto-block1 blocks))
(setq second-value (nth (- 1 rn) values-to-find))
(setq cyto-block2 (find-in second-value n-blocks))
;Checks if the operation should be implemented in the cytoplasm or if
;a derived target has to be defined.
;If one of the cyto-block is absent, a decomp+ codelet is loaded.
(cond
((and (null cyto-block1) (null cyto-block2)) nil)
((null cyto-block1)
(cr-hang *coderack* (list 'decomp+ cyto-block2
(send *current-target* :name)) %second-urgency%))
((null cyto-block2)
;;
;; May 21 09:31 1987 codelets.l Page 15
;;
(cr-hang *coderack* (list 'decomp+ cyto-block1
(send *current-target* :name)) %second-urgency%))
;If none is absent, a constant-bl+ codelet is loaded.
(t
(setq v1 (send cyto-block1 :value))
(setq v2 (send cyto-block2 :value))
;Determines the exact operation to implement
(cond
((member res values-to-find) (setq sum (abs (- v1 v2))))
(t (setq sum (+ v1 v2))))
;Determines the interest and the feasability of creating the new block
;by loading a "test-if-possible-and-desirable codelet".
(cr-hang *coderack* (list 'test-if-possible-and-desirable
cyto-block1 cyto-block2 "const-bl+" sum) %second-urgency%)))))))
;old method to load the codelet
; (cond
; ((< (sim sum current-target) 4)
; (cr-hang *coderack* (list 'const-bl+ cyto-block1 cyto-block2
; sum) %second-urgency%))
; (t nil)))
;LOOK-FOR-APPROX-BLX CODELET
(defun look-for-approx-blx (res op1 op2)
;Given 3 numbers which represent an operation (res = op1 x op2)
;checks if this type of operation could be instantiated in the
;cytoplasm.
;First eliminates in (res op1 op2) the value closest to the current-
;target.
(let (current-target values-to-find blocks rn first-value cyto-block1
cyto-block2 n-blocks second-value v1 v2 prod)
(setq current-target (send
(eval (send *current-target* :name)) :value))
(cond
((compare current-target (list res op1 op2))
(setq values-to-find (eliminate current-target (list res op1 op2)))
;Tries to find randomly a block not too far from one of the given
;value. In order to do that, chooses randomly a value and a block and
;compares them.
(setq blocks (send *cytoplasm* :cyto-brick-block-nodes))
(setq rn (random 2))
(setq first-value (nth rn values-to-find))
(setq cyto-block1 (find-in first-value blocks))
;Tries to find another block not too far from the second value.
(setq n-blocks (remove-dd cyto-block1 blocks))
(setq second-value (nth (- 1 rn) values-to-find))
(setq cyto-block2 (find-in second-value n-blocks))
;Checks if the operation should be implemented in the cytoplasm.
;If it is the case loads a const-blx codelet.
(cond
((or (null cyto-block1) (null cyto-block2)) nil)
(t
(setq v1 (send cyto-block1 :value))
(setq v2 (send cyto-block2 :value))
(setq prod (times v1 v2))
;Determines the interest and the feasability of creating the new block
;;
;; May 21 09:31 1987 codelets.l Page 16
;;
;by loading a "test-if-possible-and-desirable codelet".
(cr-hang *coderack* (list 'test-if-possible-and-desirable
cyto-block1 cyto-block2 "const-blx" prod) %second-urgency%)))))))
;Old method
; (cond
; ((< (sim prod current-target) 4)
; (cr-hang *coderack* (list 'const-blx cyto-block1 cyto-block2
; prod) %second-urgency%))
; (t nil)))))
;LOOK-FOR-BL+ CODELET
(defun look-for-bl+ (res op1 op2)
;Given 3 numbers which represent an operation (res = op1 + op2)
;checks if this operation could be exactly instantiated in the
;cytoplasm.
;First eliminates in (res op1 op2) the value closest to the current-
;target.
(let (current-target values-to-find node1 node2 cyto-block1
cyto-block2 v1 v2 sum st1 st2)
(setq current-target (send (eval (send *current-target* :name)) :value))
(setq values-to-find (eliminate current-target (list res op1 op2)))
;Tries to reach the target with the cyto-nodes linked to the
;pnodes corresponding to values-to-find (here op1 and op2 but could
;be more general)
(setq node1 (find-node (car values-to-find)))
(setq node2 (find-node (cadr values-to-find)))
(setq cyto-block1 (cadar (send (eval node1) :instances)))
;Here I use a kludge to take into account the fact that we can have
;op1 = op2
(setq cyto-block2 (cadar (reverse (send (eval node2) :instances))))
;Checks if cyto-block1 and cyto-block2 exist
(cond
((or (null cyto-block1)(null cyto-block2) (eq cyto-block1 cyto-block2))
(cr-hang *coderack* (list 'look-for-approx-bl+ res op1 op2)
%first-urgency%))
((or (equal "1t" (send cyto-block1 :type))
(equal "3dt" (send cyto-block1 :type)))
(cr-hang *coderack* (list 'look-for-approx-bl+ res op1 op2)
%first-urgency%))
((or (equal "1t" (send cyto-block2 :type))
(equal "3dt" (send cyto-block2 :type)))
(cr-hang *coderack* (list 'look-for-approx-bl+ res op1 op2)
%first-urgency%))
(t
(setq v1 (send cyto-block1 :value))
(setq v2 (send cyto-block2 :value))
;Determines the exact operation to implement
(cond
((= res (car values-to-find)) (setq sum (- v1 v2)))
((= res (cadr values-to-find)) (setq sum (- v2 v1)))
(t (setq sum (+ v1 v2))))
(cond
((nequal current-target sum)
(cr-hang *coderack* (list 'look-for-approx-bl+ res op1 op2)
%first-urgency%))
(t
;;
;; May 21 09:31 1987 codelets.l Page 17
;;
;Loads a "test-if-possible-and-desirable codelet" with a very high
;urgency, since the block is equal to the target.
(cr-hang *coderack*
(list 'test-if-possible-and-desirable
cyto-block1 cyto-block2 "const-bl+" sum)
%upper-urgency%)))))))
;LOOK-FOR-BLX CODELET
(defun look-for-blx (res op1 op2)
;Given 3 numbers which represent an operation (res = op1 + op2)
;checks if this operation could be exactly instantiated in the
;cytoplasm.
;First eliminates in (res op1 op2) the value closest to the current-
;target.
(setq current-target (send (eval (send *current-target* :name)) :value))
(setq values-to-find (eliminate current-target (list res op1 op2)))
;Tries to reach the target with the cyto-nodes linked to the
;pnodes corresponding to values-to-find (here op1 and op2 but could
;be more general)
(setq node1 (find-node (car values-to-find)))
(setq node2 (find-node (cadr values-to-find)))
(setq cyto-block1 (cadar (send (eval node1) :instances)))
;Here I use a kludge to take into account the fact that we can have
;op1 = op2
(setq cyto-block2 (cadar (reverse (send (eval node2) :instances))))
;Checks if cyto-block1 and cyto-block2 exist
(cond
((or (null cyto-block1)(null cyto-block2) (eq cyto-block1 cyto-block2))
(cr-hang *coderack* (list 'look-for-approx-blx res op1 op2)
%first-urgency%))
((or (equal "1t" (send cyto-block1 :type))
(equal "3dt" (send cyto-block1 :type)))
(cr-hang *coderack* (list 'look-for-approx-blx res op1 op2)
%first-urgency%))
((or (equal "1t" (send cyto-block2 :type))
(equal "3dt" (send cyto-block2 :type)))
(cr-hang *coderack* (list 'look-for-approx-blx res op1 op2)
%first-urgency%))
((nequal current-target
(times (send cyto-block1 :value) (send cyto-block2 :value)))
(cr-hang *coderack* (list 'look-for-approx-blx res op1 op2)
%first-urgency%))
(t
;Loads a "test-if-possible-and-desirable codelet" with a very high
;urgency, since the block is equal to the target.
(cr-hang *coderack*
(list 'test-if-possible-and-desirable
cyto-block1 cyto-block2 "const-blx" current-target)
%upper-urgency%))))
;LOOK-FOR-DIFF CODELET
(defun look-for-diff (trial)
;Tries to combine similar bricks to get small numbers. Trial is
;the number of trials already made.
(let (current-target blocks rn cyto-block1 value1 n-blocks
;;