-
Notifications
You must be signed in to change notification settings - Fork 2
/
assignment-trees.lisp
1982 lines (1912 loc) · 118 KB
/
assignment-trees.lisp
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
;======================================================
; COMPUTING DEFEAT STATUSES
(in-package "OSCAR")
#| This fixes COMPUTE-STATUS-ASSIGNMENTS. |#
(proclaim '(special *use-reductio* *assignment-tree* *altered-links* *tree-trace*
*tree-number* *triangle-number* *assignment-trace* *discards*
*creations* *affected-nodes* *potential-supportees*
*potential-link-supportees* *potential-defeatees* *potential-link-defeatees*))
(defvar *assignment-trace* nil)
(defvar *tree-trace* nil)
(defvar *discards* nil)
(defvar *creations* nil)
(defun print-assignment-tree (x stream depth)
(declare (ignore depth))
(princ "#<assignment-tree " stream) (princ (tree-number x) stream) (princ ">" stream))
(defstruct (assignment-tree (:print-function print-assignment-tree)
(:conc-name nil))
(tree-number 1)
(parent-tree nil)
(node-UDs nil)
(node-MPUDs nil)
(link-UDs nil)
(link-MPUDs nil)
(triangle-sets nil))
(defun print-triangle-set (x stream depth)
(declare (ignore depth))
(princ "#<triangle-set " stream) (princ (triangle-number x) stream) (princ ">" stream))
(defstruct (triangle-set (:print-function print-triangle-set)
(:conc-name nil))
(triangle-number 0)
(triangle-domain nil)
(assignment-trees nil))
#| The assignment-tree with number n: |#
(defunction assignment-tree (n)
(catch 'number
(find-tree n *assignment-tree*)))
(defunction find-tree (n assignment-tree)
(cond ((eql (tree-number assignment-tree) n)
(throw 'number assignment-tree))
(t (dolist (triangle (triangle-sets assignment-tree))
(dolist (tree (assignment-trees triangle))
(find-tree n tree))))))
#| The triangle-set with number n: |#
(defunction triangle-set (n)
(catch 'number
(find-triangle n *assignment-tree*)))
(defunction find-triangle (n assignment-tree)
(dolist (triangle (triangle-sets assignment-tree))
(cond ((eql (triangle-number triangle) n)
(throw 'number triangle))
(t (dolist (tree (assignment-trees triangle))
(find-triangle n tree))))))
(defun display-assignment-tree (&optional tree (depth 0) stream)
(when (null tree) (setf tree *assignment-tree*))
(indent depth stream) (princ "assignment-tree " stream) (princ (tree-number tree) stream)
(princ ":" stream) (terpri stream)
(print-list (node-UDs tree) 4 (+ 1 depth) stream) (terpri stream)
(dolist (triangle (triangle-sets tree))
(display-triangle triangle (+ 1 depth) stream)))
#|
(defun display-assignment-tree (&optional tree (depth 0) stream)
(when (null tree) (setf tree *assignment-tree*))
(indent depth stream) (princ "assignment-tree " stream) (princ (tree-number tree) stream)
(princ ":" stream) (terpri stream)
(print-list
(mapcar #'(lambda (x y) (list (car x) (cdr x) (cdr y)))
(node-UDs tree) (node-MPUDs tree))
; (node-UDs tree)
5 (+ 1 depth) stream) (terpri stream)
(dolist (triangle (triangle-sets tree))
(display-triangle triangle (+ 1 depth) stream)))
|#
(defun display-triangle (triangle depth stream)
(indent depth stream) (princ "triangle " stream) (princ (triangle-number triangle) stream)
(princ ":" stream) (terpri stream)
(print-list (triangle-domain triangle) 5 (1+ depth) stream) (terpri stream)
(dolist (tree (assignment-trees triangle))
(display-assignment-tree tree (+ 3 depth) stream)))
#| This computes the number of branches in an assignment-tree. |#
(defunction size-of-tree (tree)
(If (null (triangle-sets tree)) 1
(apply #'+ (mapcar #'size-of-triangle (triangle-sets tree)))))
(defunction size-of-triangle (triangle)
(apply #'+ (mapcar #'size-of-tree (assignment-trees triangle))))
#| This toggles tree tracing on and off. |#
(defunction tree-trace ()
(cond ((not (boundp '*tree-trace*)) (setf *tree-trace* t))
(t
(if *tree-trace* (setf *tree-trace* nil) (setf *tree-trace* t)))))
#| This computes minimal triangle-sets. This makes the generating link the
first member of the triangle-domain. |#
(defunction compute-triangle-domains (tree)
; (setf *tree* tree)
(let ((Y nil))
(dolist (as (node-UDs tree))
(dolist (L (consequent-links (car as)))
(when (and (not (member L Y))
(not (tree-link-UD L tree))
(every #'(lambda (b)
(or (eq b (car as))
(tree-node-UD b tree)))
(support-link-basis L)))
(push L Y))))
(when Y
(let* ((triangle-domains nil))
(dolist (L Y)
(let* ((triangle (inference/defeat-ancestors (list L) tree)))
(when (mem L triangle)
(dolist (td triangle-domains)
(when (mem L td)
(pull td triangle-domains)))
(when (not (some
#'(lambda (td) (mem (mem1 td) triangle))
triangle-domains))
(push (cons L (remove L triangle)) triangle-domains)))))
triangle-domains))))
#| node-UDs is a list of either dotted pairs (node . val) or (node val . cycle). |#
(defunction tree-node-UD* (node tree)
(let ((assoc (assoc node (node-UDs tree))))
(when assoc
(cond ((numberp (cdr assoc)) assoc)
(t
(let ((cycle (cddr assoc)))
(cond ((not (eql cycle *cycle*))
(cons node
(cons
(adjust-for-decay (cadr assoc) (expt *temporal-decay* (- *cycle* cycle)))
*cycle*)))
(t assoc))))))))
(defunction tree-node-MPUD* (node tree)
(let ((assoc (assoc node (node-MPUDs tree))))
(when assoc
(cond ((numberp (cdr assoc)) assoc)
(t
(let ((cycle (cddr assoc)))
(cond ((not (eql cycle *cycle*))
(cons node
(cons
(adjust-for-decay (cadr assoc) (expt *temporal-decay* (- *cycle* cycle)))
*cycle*)))
(t assoc))))))))
(defunction tree-link-UD* (link tree)
(let ((assoc (assoc link (link-UDs tree))))
(when assoc
(cond ((numberp (cdr assoc)) assoc)
(t
(let ((cycle (cddr assoc)))
(cond ((not (eql cycle *cycle*))
(cons link
(cons
(adjust-for-decay (cadr assoc) (expt *temporal-decay* (- *cycle* cycle)))
*cycle*)))
(t assoc))))))))
(defunction tree-link-MPUD* (link tree)
(let ((assoc (assoc link (link-MPUDs tree))))
(when assoc
(cond ((numberp (cdr assoc)) assoc)
(t
(let ((cycle (cddr assoc)))
(cond ((not (eql cycle *cycle*))
(cons link
(cons
(adjust-for-decay (cadr assoc) (expt *temporal-decay* (- *cycle* cycle)))
*cycle*)))
(t assoc))))))))
#| If node has an assigned status in assignment-tree or one of its ancestors,
this returns (node.status). |#
(defunction tree-node-UD (node assignment-tree)
(if assignment-tree
(or (tree-node-UD* node assignment-tree)
(tree-node-UD node (parent-tree assignment-tree)))))
#| If node has an assigned status in assignment-tree or one of its ancestors,
this returns (node.status). |#
(defunction tree-node-MPUD (node assignment-tree)
(if assignment-tree
(or (tree-node-MPUD* node assignment-tree)
(tree-node-MPUD node (parent-tree assignment-tree)))))
#| If link has an assigned status in assignment-tree or one of its ancestors,
this returns (link.status). |#
(defunction tree-link-UD (link assignment-tree)
(if assignment-tree
(or (tree-link-UD* link assignment-tree)
(tree-link-UD link (parent-tree assignment-tree)))))
#| If link has an assigned status in assignment-tree or one of its ancestors,
this returns (link.status). |#
(defunction tree-link-MPUD (link assignment-tree)
(if assignment-tree
(or (tree-link-MPUD* link assignment-tree)
(tree-link-MPUD link (parent-tree assignment-tree)))))
;(defunction assoc-value (assoc)
; (when assoc
; (cond ((listp (cdr assoc)) (cadr assoc))
; (t (cdr assoc)))))
(defunction assoc-value (assoc)
(when assoc
(cond ((listp (cdr assoc))
(let ((cycle (cddr assoc)))
(when (not (eql cycle *cycle*))
(setf (cdr assoc)
(cons
(adjust-for-decay (cadr assoc) (expt *temporal-decay* (- *cycle* cycle)))
*cycle*))))
(cadr assoc))
(t (cdr assoc)))))
(defunction make-node-assoc (node value)
(cond ((temporal-node node) (cons node (cons value *cycle*)))
(t (cons node value))))
(defunction make-link-assoc (link value)
(cond ((temporal-link link) (cons link (cons value *cycle*)))
(t (cons link value))))
#| X is a list of support-links. This finds the list of support-links that are inference/defeat-ancestors
and are not assigned values by tree or assignment. |#
(defunction inference/defeat-ancestors (X tree &optional ancestors)
(let ((new-ancestors nil))
(dolist (L X)
(dolist (d (support-link-defeaters L))
(dolist (L* (support-links d))
(when (and (not (member L* new-ancestors))
(not (member L* ancestors))
(not (tree-link-UD L* tree)))
(push L* new-ancestors))))
(dolist (b (support-link-basis L))
(dolist (L* (support-links b))
(when (and (not (member L* new-ancestors))
(not (member L* ancestors))
(not (tree-link-UD L* tree)))
(push L* new-ancestors)))))
(cond ((null new-ancestors) ancestors)
(t
(inference/defeat-ancestors
new-ancestors tree (append new-ancestors ancestors))))))
;; ----------------------------------------------------------------------
(defunction compute-assignment-tree (&optional assignment parent-tree (indent-level 0))
(when (null assignment)
(setf *tree-number* 0)
(setf *triangle-number* 0)
(dolist (node *inference-graph*)
(setf (undefeated-degree-of-support node) nil))
(dolist (L *support-links*)
(setf (defeating-assignment-trees L) nil))
(let* ((initial-nodes
(subset
#'(lambda (N)
(or (null (support-links N))
(some
#'(lambda (L) (null (support-link-basis L)))
(support-links N))))
*inference-graph*))
(link-assignment nil))
(dolist (N initial-nodes)
(dolist (L (support-links N))
(when (null (support-link-basis L))
(push (cons L (maximal-degree-of-support N)) link-assignment))))
(let ((node-assignment
(mapcar #'(lambda (N) (cons N (maximal-degree-of-support N)))
initial-nodes)))
(multiple-value-bind
(link-UDs link-MPUDs node-UDs node-MPUDs)
(dual-assignment-closure
link-assignment link-assignment node-assignment node-assignment nil nil nil)
(setf assignment (vector link-UDs link-MPUDs node-UDs node-MPUDs))))))
(when *tree-trace*
(indent indent-level) (princ "assignment-tree ")
(princ (1+ *tree-number*))
(princ ": ") (terpri) (print-list (mem2 assignment) 5 (+ 1 indent-level)) (terpri))
(let* ((tree (make-assignment-tree
:tree-number (incf *tree-number*)
:parent-tree parent-tree
:node-UDs (elt assignment 2)
:node-MPUDs (elt assignment 3)
:link-UDs (elt assignment 0)
:link-MPUDs (elt assignment 1)))
(triangles
(compute-triangle-domains tree)))
(setf (triangle-sets tree)
(mapcar
#'(lambda (triangle)
(when *tree-trace*
(indent (+ 1 indent-level)) (princ "triangle ")
(princ (1+ *triangle-number*)) (princ ": ") (terpri)
(print-list triangle 5 (+ 2 indent-level)) (terpri))
(compute-triangle-set triangle tree indent-level))
triangles))
(dolist (assoc (elt assignment 0))
(when (>< 0.0 (assoc-value assoc))
(push tree (defeating-assignment-trees (car assoc)))
(push (car assoc) *altered-links*)))
(when *display?* (push tree *creations*))
tree))
#| This is the closure under the rules for extending assignments of the result of adding
a new list of support-link assignments arb-UD arb-MPUD to the assignments generated by tree
and the background-assignment assignment. arb is a vector of two a-lists, arb-UD and arb-MPUD.
The optional assignment is a vector of four a-lists -- node-UDs, node-MPUD's, link-UDs, link-MPUDs.
It is assumed that the domain of arb consists of support-links not previously assigned values.
This returns eight values -- link-UDs link-MPUDs node-UDs node-MPUDs and the list of newly
assigned nodes and newly-assigned links, or nil if the closure is inconsistent. |#
(defunction assignment-closure
(arb-UD arb-MPUD tree &optional link-UDs link-MPUDs node-UDs node-MPUDs)
; (setf au arb-UD am arb-MPUD tr tree lu link-uds lm link-mpuds nu node-uds nm node-mpuds) ; (break)
;; (step (assignment-closure au am tr lu lm nu nm))
(catch 'consistency-check
(closure-with-consistency-check
arb-UD arb-MPUD tree nil nil link-UDs link-MPUDs node-UDs node-MPUDs nil nil nil nil)))
#| nodes is the list of newly-assigned-nodes. If D1 and D2 are not nil, only nodes in D2 and links in D1
have values computed. This returns eight values -- link-UDs link-MPUDs node-UDs node-MPUDs and
the list of newly assigned nodes and newly-assigned links. |#
(defunction closure-with-consistency-check
(arb-UD arb-MPUD tree D1 D2 link-UDs link-MPUDs node-UDs node-MPUDs nodes1 nodes2 links1 links2)
(cond
((null arb-UD)
(if tree
(values
(append (link-UDs tree) link-UDs)
(append (link-MPUDs tree) link-MPUDs)
(append (node-UDs tree) node-UDs)
(append (node-MPUDs tree) node-MPUDs)
nodes1 nodes2 links1 links2)
(values link-UDs link-MPUDs node-UDs node-MPUDs nodes1 nodes2 links1 links2)))
(t
(multiple-value-bind
(dictated-link-UDs dictated-link-MPUDs dictated-node-UDs dictated-node-MPUDs
ud-nodes mpud-nodes ud-links mpud-links)
(dictated-values arb-UD arb-MPUD tree D1 D2 link-UDs link-MPUDs node-UDs node-MPUDs)
(closure-with-consistency-check
dictated-link-UDs dictated-link-MPUDs tree D1 D2
(append arb-UD link-UDs) (append arb-MPUD link-MPUDs)
(append dictated-node-UDs node-UDs) (append dictated-node-MPUDs node-MPUDs)
(append ud-nodes nodes1) (append mpud-nodes nodes2)
(append ud-links links1) (append mpud-links links2))))))
#| This is the closure simultaneously of a set of link-assignments and a set of node-
assignments. If D1 and D2 are not nil, only nodes in D2 and links in D1 have values computed.
This returns eight values -- link-UDs link-MPUDs node-UDs node-MPUDs nodes links. |#
(defunction dual-assignment-closure
(arb-link-UD arb-link-MPUD arb-node-UD arb-node-MPUD tree
&optional D1 D2 ass-link-UD ass-link-MPUD ass-UD ass-MPUD)
; (setf aul arb-link-UD aml arb-link-MPUD aun arb-node-UD amn arb-node-MPUD tr tree
; d1* d1 d2* d2 alu ass-link-UD alm ass-link-MPUD au ass-UD am ass-MPUD) ; (break)
;; (step (dual-assignment-closure aul aml aun amn tr d1* d2* alu alm au am))
(catch 'consistency-check
(dual-closure-with-consistency-check
arb-link-UD arb-link-MPUD arb-node-UD arb-node-MPUD tree D1 D2
ass-link-UD ass-link-MPUD ass-UD ass-MPUD)))
(defunction dual-closure-with-consistency-check
(arb-link-UD arb-link-MPUD arb-node-UD arb-node-MPUD tree
D1 D2 link-UDs link-MPUDs node-UDs node-MPUDs)
(multiple-value-bind
(dictated-link-UDs dictated-link-MPUDs)
(dictated-values-from-nodes
arb-node-UD arb-node-MPUD tree D1 link-UDs link-MPUDs node-UDs node-MPUDs nil nil)
(closure-with-consistency-check
(append dictated-link-UDs arb-link-UD) (append dictated-link-MPUDs arb-link-MPUD)
tree D1 D2 link-UDs link-MPUDs (append arb-node-UD node-UDs)
(append arb-node-MPUD node-MPUDs) (domain arb-node-MPUD) (domain arb-node-UD)
(domain arb-link-MPUD) (domain arb-link-UD))))
#| This returns eight values -- dictated-link-UDs dictated-link-MPUDs dictated-node-UDs
dictated-node-MPUDs and the lists of nodes and links having newly dictated values. If D1 and D2
are not nil, only nodes in D2 and links in D1 have values computed. |#
(defunction dictated-values (arb-UD arb-MPUD tree D1 D2 link-UD link-MPUD node-UD node-MPUD)
; (setf au arb-UD am arb-MPUD tr tree lu link-ud lm link-mpud nu node-ud nm node-mpud) ; (break)
;; (step (dictated-values au am tr nil nil lu lm nu nm))
(multiple-value-bind
(dictated-node-UDs dictated-node-MPUDs ud-nodes mpud-nodes)
(dictated-values-from-support-links
arb-UD arb-MPUD tree D2 link-UD link-MPUD node-UD node-MPUD)
(dictated-values-from-nodes
dictated-node-UDs dictated-node-MPUDs tree D1 link-UD
link-MPUD node-UD node-MPUD ud-nodes mpud-nodes)))
(defunction dictated-values-from-support-links
(arb-UD arb-MPUD tree D2 link-UD link-MPUD node-UDs node-MPUDs)
; (setf au arb-UD am arb-MPUD tr tree d d2 lu link-ud lm link-mpud nu node-UDs nm node-MPUDs)
;; (step (dictated-values-from-support-links au am tr d lu lm nu nm))
(let ((dictated-node-UDs nil)
(dictated-node-MPUDs nil)
(ud-nodes nil)
(mpud-nodes nil))
(dolist (link (union (domain arb-UD) (domain arb-MPUD)))
(let ((node (support-link-target link)))
(when
(or (null D2) (member node D2))
(let ((unassigned-link-UDs nil)
(unassigned-link-MPUDs nil)
(link-UDs nil)
(link-MPUDs nil))
(dolist (L (support-links node))
(let ((assoc1 (link-assoc-UD L link-UD arb-UD tree))
(assoc2 (link-assoc-MPUD L link-MPUD arb-MPUD tree)))
(cond (assoc1 (push (assoc-value assoc1) link-UDs))
(t (push L unassigned-link-UDs)))
(cond (assoc2 (push (assoc-value assoc2) link-MPUDs))
(t (push L unassigned-link-MPUDs)))))
(let ((new-UD (maximum0 link-UDs))
(new-MPUD (maximum0 link-MPUDs))
(max-link-strength
(maximum0 (mapcar #'support-link-maximal-strength
(union unassigned-link-UDs unassigned-link-MPUDs)))))
(cond ((>< new-UD new-MPUD)
(when (>>= new-UD max-link-strength)
(let ((node-UD
(assoc-value (node-assoc-UD node dictated-node-UDs node-UDs tree)))
(node-MPUD
(assoc-value (node-assoc-MPUD node dictated-node-MPUDs node-MPUDs tree))))
(cond (node-UD
(if (or (not (>< node-UD new-UD)) (not (>< node-MPUD new-MPUD)))
(throw 'consistency-check nil)
(progn
(push node ud-nodes)
(push node mpud-nodes))))
(t
(push (make-node-assoc node new-UD) dictated-node-UDs)
(push (make-node-assoc node new-MPUD) dictated-node-MPUDs)
(push node ud-nodes)
(push node mpud-nodes))))))
(t
(when (>>= new-UD max-link-strength)
(let ((node-UD
(assoc-value (node-assoc-UD node dictated-node-UDs node-UDs tree))))
(cond (node-UD
(if (not (>< node-UD new-UD))
(throw 'consistency-check nil)
(push node ud-nodes)))
(t
(push (make-node-assoc node new-UD) dictated-node-UDs)
(push node ud-nodes)))))
(when (>>= new-UD max-link-strength)
(let ((node-MPUD
(assoc-value (node-assoc-MPUD node dictated-node-MPUDs node-MPUDs tree))))
(cond (node-MPUD
(if (not (>< node-MPUD new-MPUD))
(throw 'consistency-check nil)
(push node mpud-nodes)))
(t
(push (make-node-assoc node new-MPUD) dictated-node-MPUDs)
(pushnew node mpud-nodes))))))))))))
(values dictated-node-UDs dictated-node-MPUDs ud-nodes mpud-nodes)))
(defunction dictated-values-from-nodes
(arb-UD arb-MPUD tree D1 link-UD link-MPUD node-UD node-MPUD ud-nodes mpud-nodes)
; (setf au arb-UD am arb-MPUD tr tree d d1 lu link-ud lm link-mpud nu node-ud nm node-mpud un ud-nodes mn mpud-nodes)
;; (step (dictated-values-from-nodes au am tr d lu lm nu nm un mn))
(let ((dictated-link-UDs nil)
(dictated-link-MPUDs nil)
(ud-links nil)
(mpud-links nil))
(dolist (node (union (domain arb-UD) (domain arb-MPUD)))
(let* ((UD (assoc-value (assoc node arb-UD)))
(MPUD (assoc-value (assoc node arb-MPUD))))
;; ==========================================================
(dolist (link (consequent-links node))
(when (or (null D1) (member link D1))
(let ((basis-UDs (if UD (list UD)))
(basis-MPUDs (if MPUD (list MPUD)))
(defeater-UDs nil)
(defeater-MPUDs nil)
(unassigned-basis-UDs nil)
(unassigned-basis-MPUDs nil)
(unassigned-defeater-UDs nil)
(unassigned-defeater-MPUDs nil)
(new-UD nil)
(new-MPUD nil))
(cond ((eql MPUD 0.0) (setf new-UD 0.0 new-MPUD 0.0))
((eql UD 0.0) (setf new-UD 0.0)))
(when new-UD
(let ((link-UD (assoc-value (link-assoc-UD link dictated-link-UDs link-UD tree))))
(cond (link-UD
(if (not (>< link-UD new-UD))
(throw 'consistency-check nil)
(progn
(pushnew link ud-links)
(pushnew link mpud-links))))
(t
(push (make-link-assoc link new-UD) dictated-link-UDs)
(pushnew link ud-links)
(pushnew link mpud-links)))))
(when new-MPUD
(let ((link-MPUD (assoc-value (link-assoc-MPUD link dictated-link-MPUDs link-MPUD tree))))
(cond (link-MPUD
(if (not (>< link-MPUD new-MPUD))
(throw 'consistency-check nil)
(pushnew link mpud-links)))
(t
(push (make-link-assoc link new-MPUD) dictated-link-MPUDs)
(pushnew link mpud-links)))))
(when (null new-MPUD)
(dolist (b (support-link-basis link))
(when (and (null new-UD) (or (null UD) (not (eq b node))))
(let ((assoc (node-assoc-UD b node-UD arb-UD tree)))
(cond (assoc (push (assoc-value assoc) basis-UDs))
(t (push b unassigned-basis-UDs)))))
(when (or (null MPUD) (not (eq b node)))
(let ((assoc (node-assoc-MPUD b node-MPUD arb-MPUD tree)))
(cond (assoc (push (assoc-value assoc) basis-MPUDs))
(t (push b unassigned-basis-MPUDs))))))
(let* ((temp
(or (eq (support-link-rule link) *temporal-projection*)
(eq (support-link-rule link) *causal-implication*)
(and (not (keywordp (support-link-rule link)))
(temporal? (support-link-rule link)))))
(beta (if (null new-UD)
(if temp
(* (support-link-reason-strength link) (minimum0 basis-UDs))
(minimum (cons (support-link-reason-strength link) basis-UDs)))))
(beta*
(if temp
(* (support-link-reason-strength link) (minimum0 basis-MPUDs))
(minimum (cons (support-link-reason-strength link) basis-MPUDs)))))
(dolist (d (support-link-defeaters link))
(let ((value (assoc-value (node-assoc-UD d node-UD arb-UD tree))))
(cond (value
(when (or (>>= value beta*)
(some
#'(lambda (b)
(>>= value (maximal-degree-of-support b)))
unassigned-basis-MPUDs))
(setf new-MPUD 0.0) (setf new-UD 0.0) (return))
(push value defeater-UDs))
(t (push d unassigned-defeater-UDs)))))
(when (null new-UD)
(dolist (d (support-link-defeaters link))
(let ((value (assoc-value (node-assoc-MPUD d node-MPUD arb-MPUD tree))))
(cond (value
(when (or (>>= value beta)
(some
#'(lambda (b)
(>>= value (maximal-degree-of-support b)))
unassigned-basis-MPUDs))
(setf new-UD 0.0) (return))
(push value defeater-MPUDs))
(t (push d unassigned-defeater-MPUDs))))))
(cond (new-UD
(let ((link-UD (assoc-value (link-assoc-UD link dictated-link-UDs link-UD tree))))
(cond (link-UD
(if (not (>< link-UD new-UD))
(throw 'consistency-check nil)
(progn
(pushnew link ud-links)
(pushnew link mpud-links))))
(t
(push (make-link-assoc link new-UD) dictated-link-UDs)
(pushnew link ud-links)
(pushnew link mpud-links)))))
((and
(null unassigned-basis-UDs)
(every #'(lambda (delta*) (<< delta* beta)) defeater-MPUDs)
(every #'(lambda (d) (<< (maximal-degree-of-support d) beta))
unassigned-defeater-MPUDs))
(let ((link-UD (assoc-value (link-assoc-UD link dictated-link-UDs link-UD tree))))
(cond (link-UD
(if (not (>< link-UD beta))
(throw 'consistency-check nil)
(pushnew link ud-links)))
(t
(push (make-link-assoc link beta) dictated-link-UDs)
(pushnew link ud-links))))))
(cond (new-MPUD
(let ((link-MPUD
(assoc-value (link-assoc-MPUD link dictated-link-MPUDs link-MPUD tree))))
(cond (link-MPUD
(if (not (>< link-MPUD new-MPUD))
(throw 'consistency-check nil)
(pushnew link mpud-links)))
(t
(push (make-link-assoc link new-MPUD) dictated-link-MPUDs)
(pushnew link mpud-links)))))
((and
(null unassigned-basis-MPUDs)
(every #'(lambda (delta) (<< delta beta*)) defeater-UDs)
(every #'(lambda (d) (<< (maximal-degree-of-support d) beta*))
unassigned-defeater-UDs))
(let ((link-MPUD
(assoc-value (link-assoc-MPUD link dictated-link-MPUDs link-MPUD tree))))
(cond (link-MPUD
(if (not (>< link-MPUD beta*))
(throw 'consistency-check nil)
(pushnew link mpud-links)))
(t
(push (make-link-assoc link beta*) dictated-link-MPUDs)
(pushnew link mpud-links)))))))))))
;; ==========================================================
(dolist (link (node-defeatees node))
(when (or (null D1) (member link D1))
(let ((basis-UDs nil)
(basis-MPUDs nil)
(new-UD nil)
(new-MPUD nil)
(unassigned-basis-UDs nil)
(unassigned-basis-MPUDs nil)
(unassigned-defeater-UDs nil)
(unassigned-defeater-MPUDs nil)
(UD*
(if (and UD
(or (eq (support-link-rule link) *temporal-projection*)
(eq (support-link-rule link) *causal-implication*)
(and (not (keywordp (support-link-rule link)))
(temporal? (support-link-rule link)))))
(/ UD (support-link-reason-strength link)) UD))
(MPUD*
(if (and MPUD
(or (eq (support-link-rule link) *temporal-projection*)
(eq (support-link-rule link) *causal-implication*)
(and (not (keywordp (support-link-rule link)))
(temporal? (support-link-rule link)))))
(/ MPUD (support-link-reason-strength link)) MPUD)))
(cond ((and UD (>>= UD (support-link-reason-strength link)))
(setf new-MPUD 0.0) (setf new-UD 0.0))
((and MPUD (>>= MPUD (support-link-reason-strength link)))
(setf new-UD 0.0))
(t
(dolist (b (support-link-basis link))
(let ((value (assoc-value (node-assoc-MPUD b node-MPUD arb-MPUD tree))))
(cond (value
(when (>>= UD* value) (setf new-MPUD 0.0 new-UD 0.0) (return))
(push value basis-MPUDs))
(t
(when (>>= UD* (maximal-degree-of-support b))
(setf new-UD 0.0 new-MPUD 0.0) (return))
(push b unassigned-basis-mpuds)))))))
(when (null new-UD)
(dolist (b (support-link-basis link))
(let ((value (assoc-value (node-assoc-UD b node-UD arb-UD tree))))
(cond (value
(when (>>= MPUD* value) (setf new-UD 0.0) (return))
(push value basis-UDs))
(t
(when (>>= MPUD* (maximal-degree-of-support b))
(setf new-UD 0.0) (return))
(push b unassigned-basis-uds))))))
(when (null new-MPUD)
(dolist (b (support-link-basis link))
(let ((value (assoc-value (node-assoc-MPUD b node-MPUD arb-MPUD tree))))
(cond (value
(when (>>= UD* value) (setf new-MPUD 0.0) (return))
(push value basis-MPUDs))
(t
(when (>>= UD* (maximal-degree-of-support b))
(setf new-MPUD 0.0) (return))
(push b unassigned-basis-mpuds))))))
(cond (new-UD
(let ((link-UD (assoc-value (link-assoc-UD link dictated-link-UDs link-UD tree))))
(cond (link-UD
(if (not (>< link-UD new-UD))
(throw 'consistency-check nil)
(pushnew link ud-links)))
(t
(push (make-link-assoc link new-UD) dictated-link-UDs)
(pushnew link ud-links)))))
((null unassigned-basis-UDs)
(let ((beta
(if (or (eq (support-link-rule link) *temporal-projection*)
(eq (support-link-rule link) *causal-implication*)
(and (not (keywordp (support-link-rule link)))
(temporal? (support-link-rule link))))
(* (support-link-reason-strength link) (minimum0 basis-UDs))
(minimum (cons (support-link-reason-strength link) basis-UDs)))))
(when
(and
(every
#'(lambda (d)
(let ((assoc
(node-assoc-MPUD d node-MPUD arb-MPUD tree)))
(or (null assoc) (<< (assoc-value assoc) beta)
(<< (maximal-degree-of-support d) beta))))
(support-link-defeaters link))
(every #'(lambda (d) (<< (maximal-degree-of-support d) beta))
unassigned-defeater-MPUDs))
(let ((link-UD (assoc-value (link-assoc-UD link dictated-link-UDs link-UD tree))))
(cond (link-UD
(if (not (>< link-UD beta))
(throw 'consistency-check nil)
(pushnew link ud-links)))
(t
(push (make-link-assoc link beta) dictated-link-UDs)
(pushnew link ud-links))))))))
(cond (new-MPUD
(let ((link-MPUD
(assoc-value (link-assoc-MPUD link dictated-link-MPUDs link-MPUD tree))))
(cond (link-MPUD
(if (not (>< link-MPUD new-MPUD)) (throw 'consistency-check nil)))
(t
(push (make-link-assoc link new-MPUD) dictated-link-MPUDs)))))
((null unassigned-basis-MPUDs)
(let ((beta*
(if (or (eq (support-link-rule link) *temporal-projection*)
(eq (support-link-rule link) *causal-implication*)
(and (not (keywordp (support-link-rule link)))
(temporal? (support-link-rule link))))
(* (support-link-reason-strength link) (minimum0 basis-MPUDs))
(minimum (cons (support-link-reason-strength link) basis-MPUDs)))))
(when
(and
(every
#'(lambda (d)
(or (eq d node)
(let ((assoc
(node-assoc-UD d node-UD arb-UD tree)))
(or (null assoc) (<< (assoc-value assoc) beta*)
(<< (maximal-degree-of-support d) beta*)))))
(support-link-defeaters link))
(every #'(lambda (d) (<< (maximal-degree-of-support d) beta*))
unassigned-defeater-UDs))
(let ((link-MPUD
(assoc-value (link-assoc-MPUD link dictated-link-MPUDs link-MPUD tree))))
(cond (link-MPUD
(if (not (>< link-MPUD beta*))
(throw 'consistency-check nil)
(pushnew link mpud-links)))
(t
(push (make-link-assoc link beta*) dictated-link-MPUDs)
(pushnew link mpud-links)))))))))))
;; ==========================================================
))
(values dictated-link-UDs dictated-link-MPUDs arb-UD arb-MPUD ud-nodes
mpud-nodes ud-links mpud-links)))
#| This finds and returns an association of a node in either UDs, UD, or tree if there is one. |#
(defunction node-assoc-UD (node UDs UD tree)
(let ((assoc (assoc node UDs)))
(when (null assoc)
(setf assoc (assoc node UD))
(when (and (null assoc) tree)
(setf assoc (tree-node-UD node tree))))
assoc))
#| This finds and returns an association of a node in either MPUDs, MPUD, or tree if there is one. |#
(defunction node-assoc-MPUD (node MPUDs MPUD tree)
(let ((assoc (assoc node MPUDs)))
(when (null assoc)
(setf assoc (assoc node MPUD))
(when (and (null assoc) tree)
(setf assoc (tree-node-MPUD node tree))))
assoc))
#| This finds and returns an association of a support-link in either UDs, UD, or tree if there is one. |#
(defunction link-assoc-UD (node UDs UD tree)
(let ((assoc (assoc node UDs)))
(when (null assoc)
(setf assoc (assoc node UD))
(when (and (null assoc) tree)
(setf assoc (tree-link-UD node tree))))
assoc))
#| This finds and returns an association of a support-link in either MPUDs, MPUD, or tree if there is one. |#
(defunction link-assoc-MPUD (node MPUDs MPUD tree)
(let ((assoc (assoc node MPUDs)))
(when (null assoc)
(setf assoc (assoc node MPUD))
(when (and (null assoc) tree)
(setf assoc (tree-link-MPUD node tree))))
assoc))
;; ----------------------------------------------------------------------
(defunction compute-triangle-set (triangle tree &optional (indent-level 0))
(let ((triangle-set
(make-triangle-set
:triangle-number (incf *triangle-number*)
:triangle-domain triangle
:assignment-trees
(mapcar
#'(lambda (ass)
(let ((link-UDs (setdifference (elt ass 0) (link-UDs tree)))
(link-MPUDs (setdifference (elt ass 1) (link-MPUDs tree)))
(node-UDs (setdifference (elt ass 2) (node-UDs tree)))
(node-MPUDs (setdifference (elt ass 3) (node-MPUDs tree))))
(compute-assignment-tree
(vector link-UDs link-MPUDs node-UDs node-MPUDs) tree (+ 4 indent-level))))
(compute-assignments-to-domain tree triangle)))))
(when *display?* (push triangle-set *creations*))
triangle-set))
;; ----------------------------------------------------------------------
#| triangle is a triangle-domain. |#
(defunction compute-assignments-to-domain (tree triangle)
; (setf tr tree ia triangle) ; (break)
;; (step (compute-assignments-to-domain tr ia))
(let* ((UDs nil)
(MPUDs nil)
(link
(find-if
#'(lambda (L)
(setf UDs nil)
(setf MPUDs nil)
(dolist (b (support-link-basis L))
(let ((UD (assoc-value (tree-node-UD b tree))))
(cond (UD (push UD UDs) (push (assoc-value (tree-node-MPUD b tree)) MPUDs))
(t (setf UDs nil) (setf MPUDs nil) (return)))))
UDs)
triangle)))
(when (null link)
(setf link (car triangle))
(setf UDs
(mapcar
#'(lambda (b)
(maximum0
(mapcar
#'(lambda (L)
(let ((tl (assoc-value (tree-link-UD L tree)))) (or tl 0.0))) (support-links b))))
(support-link-basis link)))
(setf MPUDs
(mapcar
#'(lambda (b)
(maximum0
(mapcar
#'(lambda (L)
(let ((tl (assoc-value (tree-link-MPUD L tree)))) (or tl 0.0))) (support-links b))))
(support-link-basis link))))
(let* ((temp
(or (eq (support-link-rule link) *temporal-projection*)
(eq (support-link-rule link) *causal-implication*)
(and (not (keywordp (support-link-rule link)))
(temporal? (support-link-rule link)))))
(beta (if temp
(* (support-link-reason-strength link) (minimum UDs))
(minimum (cons (support-link-reason-strength link) UDs))))
(beta* (if temp
(* (support-link-reason-strength link) (minimum MPUDs))
(minimum (cons (support-link-reason-strength link) MPUDs)))))
(multiple-value-bind
(link-UDs1 link-MPUDs1 node-UDs1 node-MPUDs1 ud-nodes1 mpud-nodes1)
(if (and (not (zerop beta)) (not (zerop beta*)))
(assignment-closure (list (cons link beta)) (list (cons link beta*)) tree))
(multiple-value-bind
(link-UDs3 link-MPUDs3 node-UDs3 node-MPUDs3 ud-nodes3 mpud-nodes3)
(assignment-closure (list (cons link 0.0)) (list (cons link 0.0)) tree)
(let ((assignments
(cond
(link-UDs1
(cond
(link-UDs3
(append
(expand-assignment
ud-nodes1 mpud-nodes1 link-UDs1 link-MPUDs1
node-UDs1 node-MPUDs1 triangle tree)
(expand-assignment
ud-nodes3 mpud-nodes3 link-UDs3 link-MPUDs3
node-UDs3 node-MPUDs3 triangle tree)))
(t
(expand-assignment
ud-nodes1 mpud-nodes1 link-UDs1 link-MPUDs1
node-UDs1 node-MPUDs1 triangle tree))))
(link-UDs3
(expand-assignment
ud-nodes3 mpud-nodes3 link-UDs3 link-MPUDs3
node-UDs3 node-MPUDs3 triangle tree)))))
(or assignments
(multiple-value-bind
(link-UDs2 link-MPUDs2 node-UDs2 node-MPUDs2 ud-nodes2 mpud-nodes2)
(if (not (zerop beta*))
(assignment-closure (list (cons link 0.0)) (list (cons link beta*)) tree))
(expand-assignment
ud-nodes2 mpud-nodes2 link-UDs2 link-MPUDs2
node-UDs2 node-MPUDs2 triangle tree)))))))))
#| assignment is a pair (link-assignment node-assignment). Nodes is the list of newly assigned nodes.
Candidate-links is the list of previous candidate-links that have not yet been assigned. |#
(defunction expand-assignment
(ud-nodes mpud-nodes link-UDs link-MPUDs node-UDs node-MPUDs triangle tree)
; (setf u ud-nodes m mpud-nodes lu link-uds lm link-mpuds nu node-uds nm node-mpuds tr triangle t* tree)
;; (step (expand-assignment u m lu lm nu nm tr t*))
(let ((candidate-links
(subset #'(lambda (L) (or (not (assoc L link-UDs)) (not (assoc L link-MPUDs)))) triangle)))
(cond
((null candidate-links)
(list (vector link-UDs link-MPUDs node-UDs node-MPUDs)))
(t
(let* ((UDs nil)
(MPUDs nil)
(link
(find-if
#'(lambda (L)
(setf UDs nil)
(setf MPUDs nil)
(dolist (b (support-link-basis L))
(let ((UD (assoc-value (node-assoc-UD b node-UDs nil tree))))
(cond (UD (push UD UDs)
(push (assoc-value (node-assoc-MPUD b node-MPUDs nil tree)) MPUDs))
(t (setf UDs nil) (setf MPUDs nil) (return)))))
UDs)
candidate-links)))
; (intersection candidate-links
; (unionmapcar+ #'consequent-links (union ud-nodes mpud-nodes))))))
(when (null link)
(setf link (car candidate-links))
(setf UDs
(mapcar
#'(lambda (b)
(maximum0
(mapcar
#'(lambda (L)
(let ((tl (assoc-value (tree-link-UD L tree)))) (or tl 0.0))) (support-links b))))
(support-link-basis link)))
(setf MPUDs
(mapcar
#'(lambda (b)
(maximum0
(mapcar
#'(lambda (L)
(let ((tl (assoc-value (tree-link-MPUD L tree)))) (or tl 0.0))) (support-links b))))
(support-link-basis link))))
(let* ((temp
(or (eq (support-link-rule link) *temporal-projection*)
(eq (support-link-rule link) *causal-implication*)
(and (not (keywordp (support-link-rule link)))
(temporal? (support-link-rule link)))))
(beta (if temp
(* (support-link-reason-strength link) (minimum UDs))
(minimum (cons (support-link-reason-strength link) UDs))))
(beta* (if temp
(* (support-link-reason-strength link) (minimum MPUDs))
(minimum (cons (support-link-reason-strength link) MPUDs)))))
(multiple-value-bind
(link-UDs1 link-MPUDs1 node-UDs1 node-MPUDs1 ud-nodes1 mpud-nodes1)
(if (and (not (zerop beta)) (not (zerop beta*)))
(assignment-closure
(list (cons link beta)) (list (cons link beta*)) tree link-UDs link-MPUDs node-UDs node-MPUDs))
(multiple-value-bind
(link-UDs3 link-MPUDs3 node-UDs3 node-MPUDs3 ud-nodes3 mpud-nodes3)
(assignment-closure
(list (cons link 0.0)) (list (cons link 0.0)) tree link-UDs link-MPUDs node-UDs node-MPUDs)
(let ((assignments
(cond
(link-UDs1
(cond
(link-UDs3
(append
(expand-assignment
; ud-nodes1 mpud-nodes1
(append ud-nodes ud-nodes1) (append mpud-nodes mpud-nodes1)
link-UDs1 link-MPUDs1 node-UDs1 node-MPUDs1 triangle tree)
(expand-assignment
; ud-nodes3 mpud-nodes3
(append ud-nodes ud-nodes3) (append mpud-nodes mpud-nodes3)
link-UDs3 link-MPUDs3 node-UDs3 node-MPUDs3 triangle tree)))
(t
(expand-assignment
; ud-nodes1 mpud-nodes1
(append ud-nodes ud-nodes1) (append mpud-nodes mpud-nodes1)
link-UDs1 link-MPUDs1 node-UDs1 node-MPUDs1 triangle tree))))
(link-UDs3
(expand-assignment
; ud-nodes3 mpud-nodes3
(append ud-nodes ud-nodes3) (append mpud-nodes mpud-nodes3)
link-UDs3 link-MPUDs3 node-UDs3 node-MPUDs3 triangle tree)))))
(or assignments
(multiple-value-bind
(link-UDs2 link-MPUDs2 node-UDs2 node-MPUDs2 ud-nodes2 mpud-nodes2)
(if (not (zerop beta*))
(assignment-closure
(list (cons link 0.0))
(list (cons link beta*)) tree link-UDs link-MPUDs node-UDs node-MPUDs))
(if link-UDs2
(expand-assignment
; ud-nodes2 mpud-nodes2
(append ud-nodes ud-nodes2) (append mpud-nodes mpud-nodes2)
link-UDs2 link-MPUDs2 node-UDs2 node-MPUDs2 triangle tree)))))))))))))
;======================================================
; UPDATING-ASSIGNMENT-TREES