-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlearner.lisp
1531 lines (1380 loc) · 47.3 KB
/
learner.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
;;; -*- Mode: LISP; Syntax: Common-lisp; Package: Meta-aqua; Base: 10 -*-
(in-package :metaaqua)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;
;;;; The Meta-AQUA Introspective Multistrategy Learning System
;;;; Version 6
;;;;
;;;; Copyright (C) 1996 Michael T. Cox (mcox25@covad.net)
;;;;
;;;;
;;;; File: learner.lisp
;;;;
;;;;
;;;; *******************************************************
;;;
;;; This program 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 1, or (at your option) any later
;;; version.
;;;
;;; This program 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 this program; if not, write to the Free Software Foundation, Inc., 675
;;; Mass Ave, Cambridge, MA 02139, USA. In emacs type C-h C-w to view license.
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;
;;;; LEARN AND REVIEW PHASE
;;;; AND LEARNING STRATEGIES
;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Support Functions
;;;
;;;
;;; Predicate not-trivial-frame-p returns t if the input frame is a trivial
;;; frame, nil otherwise. A frame is trivial if it has only a status slot and a
;;; truth slot. Many (most?) entity frames suit this characterization. This
;;; predicate is used during EBG.
;;;
(defun not-trivial-frame-p (frame)
(if (remove
*status-slot*
(remove
*truth-slot*
(f.role-list frame)))
t)
)
(defun test-p (list)
(if (not (equal 'instance-of (first list)))
(let ((found nil))
(dolist (each-item (rest list))
(if (and (not (not-trivial-frame-p each-item))
(not (attribute-value-p each-item)))
(setf found t)))
found))
)
;;;
;;; Function remove-truth removes truth-slot value fillers from all decendents
;;; of the given frame. The use of this function above may not be necessary
;;; because of the conditional check of not-trivial-frame-p.
;;;
(defun remove-truth (frame)
(f.traverse-frame
frame
(lambda (current-frame parent role facet-name level)
(if (and (not (visited-p current-frame *traverse-marker*))
(equal role *truth-slot*)
(equal facet-name *value-facet*))
(f.remove-filler! parent *truth-slot*))))
frame
)
;;;
;;; Function change-status alters the status slot of fillers from all
;;; decendents of the given frame. The argument new-status replaces the old
;;; value if there was one. A frame's status changes when it is acquired as a
;;; case, generalized, or otherwise brought into a program from input. The
;;; changes is usually from story-instance to learned.
;;;
;;; |||||| Should we change the status if a frame is a predefined instance?
;;; Or an attribute value?
;;;
(defun change-status (frame new-status)
(f.traverse-frame
frame
(lambda (current-frame parent role facet-name level)
(if (and (non-attribute-val-status-p
current-frame
role
facet-name)
(not (equal current-frame *question*)))
(f.put! new-status parent *status-slot*))))
frame
)
;;;
;;; Predicate non-attribute-status-filler-p returns t when the current-frame is
;;; a filler for a status slot other than the status-slot filler
;;; 'attribute-value.0 (which signifies that the parent is an attribute filler
;;; itself, such as in.0 or learned.0). Such values are not subject to change.
;;; Visited frames have already been changed.
;;;
(defun non-attribute-val-status-p (current-frame role facet-name)
(do-break non-attribute-val-status-p)
(and (not (visited-p current-frame *traverse-marker*))
(equal role *status-slot*)
(equal facet-name *value-facet*)
(not (equal *attribute-value*
current-frame)))
)
(defun remove-status (frame)
(f.traverse-frame
frame
(lambda (current-frame parent role facet-name level)
(if (and
(non-attribute-val-status-p
current-frame
role
facet-name)
(not (equal current-frame *question*)))
(f.remove-filler! parent *status-slot*))))
frame
)
;;;
;;; ||||| Note that funcall may be appropriate to use
;;; because of the problems encountered below.
;(defun test (p1 p2)
; (format t "~%The number is ~s." (eval p2))
; )
;
;(dolist (each-num '(1 2 3 4)) (test each-num '(+ 1 p1)))
; |||||
; Function old-review used this. What will be done?
;
(defun retrieve-case-from-META-XP (concept)
(first (get-model *World-Model*))
)
;;;
;;; Function adjust-path changes the path-list so that as f.traverse-frame
;;; traverses the node network, the path-list contracts to remain pointing
;;; into the net. A call of f.chase-path will then be able to get to the
;;; current node being operated on.
;;;
(defun adjust-path (path-list level)
(cond ((equal level *prev-level*)
(butlast path-list))
((> level *prev-level*)
path-list)
((< level *prev-level*)
(dotimes (x (+ 1 (- *prev-level* level)))
(setf path-list (butlast path-list)))
path-list))
)
;;;
;;; Explanation-Based Generalization (EBG) Learning Strategy
;;;
;;;
;;; |||||Note that in the current version this will produce extra slots if the
;;; ones in the xp do not have as many slots as the definition of a frame.
;;;
;;; NOTE that this is not the current definition used by the program. Look
;;; below for the right one.
;;;
;(defun generalize-node (current-frame parent role facet-name level parent-struct)
; (cond ((not (equal level 0))
; (setf *path-list* (append
; (adjust-path *path-list* level)
; `((,role ,facet-name)) ))
; (setf *prev-level* level)
; (when (and (not (visited-p current-frame))
; ;; The frame-body call makes sure that this is not a terminal.
; (frame-body current-frame)
; (not-trivial-frame-p current-frame))
; (f.unify current-frame
; ;; |||||Literals are not handled properly below.
; (let ((new-frame
; (frame-def
; (apply #'f.chase-path
; *new-struct*
; *path-list*))))
; (if (or (null new-frame)
; ;; |||||| Eventually we want to unify all items in a list.
; (frame-list-p (f.get parent role facet-name)))
; *nil*
; (if (listp new-frame)
; (if (attribute-value-p (frame-type new-frame))
; new-frame
; (if (literal-p (frame-type new-frame))
; (f.set-literal
; (f.instantiate-frame (*FRAME* *literal*))
; (symbol-value new-frame))
; (f.instantiate-frame new-frame)))
; (f.instantiate-frame (list new-frame))))))
; (format-if
; (and *Debug-On* (f.where-bound current-frame))
; t
; "~%Backpointers of ~s is ~s.~%Location is ~s-~s-~s.~%"
; current-frame
; (f.where-bound current-frame)
; parent role facet-name)
; )))
; (when *Debug-On*
; (format t "~%*path-list*: ~s." *path-list*)
; (format t "~%*prev-level*: ~s.~%" *prev-level*))
; )
(defun generalize-node (current-frame parent role facet-name level parent-struct)
;;;(if (isa-p 'fridge (list current-frame))
;;; (do-break generalize-node))
(cond ((not (equal level 0))
(setf *path-list* (append
(adjust-path *path-list* level)
`((,role ,facet-name)) ))
(setf *prev-level* level)
(when (and (not (visited-p current-frame *traverse-marker*))
;;; (not (tmxp-filter current-frame))
;; The frame-body call makes sure that this is not a terminal.
(frame-body current-frame)
(not-trivial-frame-p current-frame))
(f.unify
(apply #'f.chase-path *new-struct* *path-list*)
;; ||||||Literals are not handled properly below.
;; ||||||Is it because f.set-literal returns the value rather than the literal? [cox 24apr94]
(let ((new-frame (frame-def current-frame)))
(if (or (null new-frame)
;; |||||| Eventually we want to unify all items in a list.
(frame-list-p (f.get parent role facet-name)))
*nil*
(if (listp new-frame)
(if (attribute-value-p (frame-type new-frame))
new-frame
(if (literal-p (frame-type new-frame))
(f.set-literal
(f.instantiate-frame (*FRAME* *literal*))
(symbol-value new-frame))
(f.instantiate-frame new-frame)))
(f.instantiate-frame (list new-frame)))))
:notify-but-no-break)
(if (and *Debug-On* (f.where-bound current-frame))
(format t "~%Backpointers of ~s is ~s.~%Location is ~s-~s-~s.~%"
current-frame
(f.where-bound current-frame)
parent role facet-name))
)))
(when *Debug-On*
(format t "~%*path-list*: ~s." *path-list*)
(format t "~%*prev-level*: ~s.~%" *prev-level*))
)
;;;
;;; Function do-ebg should really be doing this.
;;;
(defun perform-EBG-on (xp)
;; Set-up for function generalize-node.
;; |||||Why is this not done with let?
(setf *path-list* nil)
(setf *prev-level* 0)
;;; (setf *new-struct*
;;; (f.instantiate-frame (*FRAME* (frame-type xp))))
(setf *new-struct* xp)
(do-break perform-EBG-on)
(f.traverse-frame
(f.instantiate-frame (*FRAME* (frame-type xp)))
#'generalize-node nil)
;; ||||| Really need to just make sure that tokens are unified
;; after fixing backptr problem, instead of the following hack.
(f.unify (f.chase-path *new-struct* 'consequent 'object)
(f.chase-path *new-struct*
'main-precondition
*co-domain-slot*
*co-domain-slot*
*co-domain-slot*))
(remove-truth *new-struct*)
)
;(defun count-nodes (current-frame parent role facet-name level)
; (if (not (visited-p current-frame *traverse-marker*))
; (setf *x* (+ 1 *x*)))
; )
;
;(defun find-it (current-frame parent role facet-name level)
; (if (equal 'drug (frame-type current-frame))
; (break))
; )
;
;
;(setf *x* 0)
(defvar *path-list* nil)
(defvar *prev-level* 0)
(defvar *new-struct* nil)
(defun perform-EBG-on (xp)
; (with-character-style (*Style*)
(format
*aqua-window*
"~%~%Performing EBG on explanation ~s."
xp)
;; Set-up for function generalize-node.
;; |||||Why is this not done with let?
(setf *path-list* nil)
(setf *prev-level* 0)
(setf *new-struct*
(f.instantiate-frame (frame-def xp)))
(do-break perform-EBG-on)
(f.traverse-frame xp #'generalize-node nil)
;; ||||| Really need to just make sure that tokens are unified
;; after fixing backptr problem, instead of the following hack.
(f.unify (f.chase-path *new-struct* 'conseq 'object)
(f.chase-path *new-struct*
'main-precondition
*co-domain-slot*
*co-domain-slot*
*co-domain-slot*)
:notify-but-no-break)
(let ((generalized-xp
(change-status
(remove-truth *new-struct*)
*learned*)))
(format
*aqua-window*
"~% resulting in general explanation ~s.~%"
generalized-xp)
generalized-xp)
; )
)
;;;;
;;;; Review - Learn Phase
;;;;
;;;
;;; ||||| NOTE the perform-EBG-on function that really does the EBG in
;;; function specialize. Change soon. 18 May 93.
;;;
(defun do-EBG (concept)
(format
*aqua-window*
"~%Execute EBG on ~s."
concept)
)
;;; ||||||Must complete.
(defun new-concept (concept)
t)
;;;
;;; |||||| WIll this involve a differentiate plan?
;;;
;;; Function differentiate creates subgoals to achieve a given
;;; knowledge-differentiation-goal. If either of the objects to be
;;; differentiated are new concepts, then we spawn a sub-goal to expand them.
;;; The subgoal will have a higher priority than the following organization
;;; goal so that the concepts will be ready to be indexed.
;;;
(defun differentiate (differentiate-goal)
(let* ((g-actor (goal-actor differentiate-goal))
(g-object (goal-object differentiate-goal))
(obj1 (f.get *domain-slot* g-object))
(obj2 (f.get *co-domain-slot* g-object))
(goal-priority (f.get differentiate-goal 'priority))
(higher-priority
(inverse-trans
(+ 2 (trans goal-priority))))
)
(do-break differentiate)
(if (new-concept obj1)
(spawn-sub-goal
g-actor
obj1
knowledge-expansion-goal.0
higher-priority
differentiate-goal))
(if (new-concept obj2)
(spawn-sub-goal
g-actor
obj2
knowledge-expansion-goal.0
higher-priority
differentiate-goal))
(spawn-sub-goal
g-actor
`(reindex-with-respect-to
(,*domain-slot* (,*value-facet* ,obj1))
(,*co-domain-slot* (,*value-facet* ,obj2)))
knowledge-reorganization-goal.0
(inverse-trans
(- (trans higher-priority)
1))
differentiate-goal)
;; Also need to place these subgoals on the subgoal slot of the differentiation goal.
)
)
;(defun generalize (xp-token imxp role-literal)
; (let ((generalized-xp (perform-ebg-on xp-token)))
; (break)
; (f.unify generalized-xp
; (f.get imxp
; (symbol-value
; role-literal))))
; )
;;;
;;; Function generalize calls ebg on the xp token. The resultant generalized
;;; explanation is then unified with m-prime. M-prime is the node in the imxp
;;; that registers the new explanation created during learning.
;;;
;;; If various generalization routine were available to choose from, this
;;; function might decide which was most appropriate. At least it would have
;;; the appropriate algorithm passed to it.
;;;
(defun generalize (xp-token memory-item)
(let ((generalized-xp ;Generalize the token
(perform-ebg-on ;using EBG.
xp-token)))
(do-break generalize)
;; Reversed order or arguments below [cox 30jun95]
(f.unify
memory-item ;with the imxp node m' or m.
generalized-xp ;Unify the result
))
)
;;;
;;; Function abstract currently is used to handle the bad constraints on the
;;; value of at-locations on a bark. This is done by abstracting to the common
;;; parents of what was expected (the constraint) to happen and what actually
;;; happened.
;;;
;;; ||||||Note that this is passed =A1 and =Anomaly as sibling1 and sibling2
;;; respectively. Which-sibling will then be assigned from sibling2 and A1
;;; (sibling1) will never be used in the function.
;;;
(defun abstract (sibling1 sibling2)
(let ((which-sibling ;Find the parameter which
(cond ((isa-p 'anomaly ;represents the original anomaly.
(list sibling1))
sibling1)
((isa-p 'anomaly
(list sibling2))
sibling2)
)))
(do-break abstract)
(if which-sibling
(let* ((action (f.get which-sibling 'action))
(path (first (symbol-value (f.get which-sibling 'paths))))
;; ||||| Hack: f.chase-path needs to be modified rather than having the
;; following conditional. See comments on f.chase-path.
(constraint (let ((temp
(if (listp path)
(apply #'f.chase-path
(frame-def action)
path))))
(if (var-binding-p temp)
(f.get (frame-def action)
(var->role temp))
temp)))
(actual
(f.get which-sibling 'actual-outcome)
; (if (listp path)
; (apply #'f.chase-path
; (cons
; action
; path)))
)
(common-parent (f.lowest-common-ancestor
(frame-type constraint)
(frame-type actual))))
; (format
; *aqua-window*
; "~%~%Concept: ~s~%Slots: ~s~%Constraint: ~s~%"
; (first (get-abstraction action))
; (symbol-value (f.get which-sibling 'paths))
; constraint)
; (format
; *aqua-window*
; (str-concat
; "Actual Filler: ~s "
; "= ~s~%Common-Parent: ~s~%")
; actual
; (get-abstraction
; (first
; (get-abstraction
; (first (get-abstraction actual)))))
; common-parent)
(if (listp path)
; (with-character-style (*Style*)
(cond (common-parent
(format
*aqua-window*
"~%Perform abstraction to ~s~% on conceptual definition of ~s.~%"
common-parent
(first (get-abstraction action))
)
(set (frame-type action)
(apply #'f.modify
`(,(frame-type action)
,common-parent
,@path)))
;; The action this learning was about is interesting,
;; because it is newly learned.
(setf
(get (first (get-abstraction action))
'personally-interesting)
t)
common-parent) ; return value if successful.
(t
(format
*aqua-window*
"Abstraction fails: No common parent.")))
; )
(format *aqua-window*
"~%Error in function ABSTRACT.~%")))))
)
;;;
;;; Predicate current-frame-more-general-than returns t if the current frame is
;;; more general than the frame it is compared to, nil otherwise.
;;;
(defun current-frame-more-general-than (frame-compared-to current-frame)
(and (not (eq (frame-type
frame-compared-to)
(frame-type
current-frame)))
(isa-p (frame-type
current-frame)
(list
frame-compared-to)))
)
;;;
;;; Function get-corresponding-frame returns the frame in the new structure
;;; being created by function replace-with-general-filler corresponding to the
;;; corresponding-filler argument. If the corresponding-filler is nil or a
;;; non-list, then it is returned. The real problem solved by this function is
;;; the case when corresponding-filler is a list. That is, the current-frame
;;; may be a list element in a filler of a role of the parent frame. So the
;;; problem is to find the location in the list where the current frame exists,
;;; and then to return the corresponding frame from the list represented by
;;; corresponding filler.
;;;
;;; parent -> (frame (role (facet-name (q current-frame r s))))
;;; corresponding-filler -> (a b c d)
;;; (get-corresponding-frame
;;; current-frame parent role facet-name corresponding-filler)
;;; -> b
;;;
(defun get-corresponding-frame
(current-frame parent role facet-name corresponding-filler)
(cond ((null corresponding-filler)
nil)
((not (listp corresponding-filler))
corresponding-filler)
(t
(let ((list-filler (f.get parent role facet-name)))
(nth (- (length list-filler)
(length (member current-frame
list-filler)))
corresponding-filler))))
)
;;;
;;; Function replace-with-general-filler is the function passed to
;;; f.traverse-frame by function merge-concepts. As each sub-frame (the
;;; current-frame parameter) is encountered during the traversal, it checks to
;;; see if the sub-frame is more general than the corresponding sub-frame in
;;; the new concept (global *new-struct*) being constructed. If this is true,
;;; it replaces the more specific frame with the more general one. This is
;;; performed, not only on the current frame, but all locations where the
;;; current frame is bound. This information is obtained from the specific
;;; frame's back-pointer list by calling f.where-bound.
;;;
(defun replace-with-general-filler (current-frame parent role facet-name level parent-struct)
(cond ((not (equal level 0))
(setf *path-list* (append
(adjust-path *path-list* level)
`((,role ,facet-name)) ))
(setf *prev-level* level)
(let ((corresponding-frame
(get-corresponding-frame
current-frame parent role facet-name
(apply #'f.chase-path
*new-struct*
*path-list*))))
(when (and (not (visited-p
current-frame
*traverse-marker*))
(current-frame-more-general-than
corresponding-frame
current-frame))
(let ((general-frame
(f.copy-instantiated-frame
current-frame
;;; #'tmxp-filter
)))
(dolist (each-back-ptr
(f.where-bound
corresponding-frame))
(f.put!
general-frame
(first each-back-ptr)
(second each-back-ptr)
(third each-back-ptr))
))))
))
)
;;;
;;; Function merge-concepts takes two concepts and returns the most general
;;; unification of the two. It works by unifying copies of the two in order to
;;; produce a copy that is guaranteed to contain all slots and facets of both
;;; concepts. Subsequently, the new concept is traversed twice; once for each
;;; parent concept, and any facet that is more general than a corresponding
;;; filler in the unified concept is put on the new frame in its place.
;;; Finally, all truth slots are removed. The global variables *path-list*,
;;; *prev-level*, and *new-struct* are used by function
;;; replace-with-general-filler to manipulate the new frame as the traversal is
;;; performed.
;;;
(defun merge-concepts (concept1 concept2)
(do-break merge-concepts)
(setf *path-list* nil)
(setf *prev-level* 0)
(setf *new-struct*
(f.unify (f.copy-instantiated-frame
(remove-status concept1)
;;; #'tmxp-filter
)
(f.copy-instantiated-frame
(remove-status concept2)
;;; #'tmxp-filter
)))
(f.traverse-frame concept1 #'replace-with-general-filler nil)
(f.traverse-frame concept2 #'replace-with-general-filler nil)
(let ((generalized-xp
(change-status
(remove-truth *new-struct*)
*learned*)))
(format
*aqua-window*
"~% resulting in general explanation ~s.~%"
generalized-xp)
generalized-xp)
)
;;;
;;; Function index-new-xp indexes a newly generalized explanation pattern in
;;; memory.
;;;
;;; The function do-index will return an index to the memory when it stores the
;;; item, unless a similar item already exists in memory. If this occurs,
;;; do-index will return the similar item. Therefore, the first cond clause
;;; represents a successful store of a new memory; whereas, the second clause
;;; is the case of finding the item already in memory. This case is covered in
;;; Cox (1994), when Meta-AQUA forgets the detection explanation, but is
;;; reminded of it as it tries to store a newly generalized explanation from
;;; the story.
;;;
(defun index-new-xp (generalized-xp)
(let ((new-index
(do-index generalized-xp ;Index the because-xp off dogs barking at containers.
'xp-type.0
(f.get generalized-xp
*explains-node*)
t
)))
(do-break index-new-xp)
(cond ((isa-p 'index (list new-index))
(format
*aqua-window*
(str-concat
"~%~%Indexing new explanation "
"with index ~s.~%")
new-index)
new-index)
(t
(format
*aqua-window*
"~%Indexing aborted because of reminding.~%")
(format
*aqua-window*
(str-concat
"~%Generalizing the similar memories "
"instead of storing separate items.~%"))
;;; (break "index-new-xp")
(let ((merged-xp
(merge-concepts generalized-xp new-index)))
;; Otherwise new-index is the conflicting memory item from the memory-list.
(do-index merged-xp
'xp-type.0
(f.get merged-xp
*explains-node*)
t
t ; Forced indexing true
(list
(f.chase-path
merged-xp
'explains
'domain '(to relation))))
)
)) )
)
;;;
;;; Function list-relations returns a list of relation frames along a path list
;;; starting at the current frame. The first element in the path list provides
;;; a pointer into the current frame, determining the slot from which to
;;; extract the current relation. The filler of this slot then provides the
;;; next frame from which to recursively extract more relations.
;;;
(defun list-relations (current-frame path-list)
"Extract a list of relations along a path list starting with the current frame."
(cond ((null path-list)
nil)
(t
(cons
(f.make-relation
current-frame
(first path-list))
(list-relations
(f.get current-frame
(first path-list))
(rest path-list)))))
)
;;;
;;; Function specialize is currently used to learn from the expectation
;;; failure. It differentiates the indices used to retrieved the incorrectly
;;; expected explanation and the actual explanation for the dog barking events.
;;; The differentiation is accomplished by indexing the generalized-xp using
;;; relations obtained from the anomaly path. Then the function removes the
;;; index for the old-xp. Finally, with the help of a small hack, the old-xp is
;;; reindexed using the anomaly path.
;;;
;;; The old-index parameter is the overly general index that incorrectly
;;; retrieved the defensive-bark explanation (local variable old-xp). The
;;; generalized version of the proper explanation is passed to the function as
;;; parameter generalized-xp. The last three function parameters are from the
;;; IMXP: (=i =anomaly =m-prime)
;;;
;;; ||||||To perform the index differentiation, an algorithm must be developed
;;; that reindexes the two explanations with respect to each other and the
;;; existing indexes (in the case of a novel situation there is no index
;;; though). The current algorithm depends on the anomaly, rather than the xps
;;; themselves. The small hack mentioned above must go.
;;;
(defun specialize (learning-node old-index anomaly generalized-xp)
(let* ((anomalous-concept ; e.g., Copy of dog-barks or hit.
(f.copy-instantiated-frame
(f.get anomaly 'action)))
(anomaly-path ; e.g., (TO DOMAIN) or
(second ; (INSTRUMENTAL-SCENE TO DOMAIN)
(symbol-value
(f.get anomaly 'paths))))
(index-type (f.get old-index 'type)) ; e.g., xp-type.0
(index-relation (f.get old-index ; e.g., an actor frame
'relation)))
(do-break specialize)
(setf (get (frame-type anomalous-concept) ; The action this learning was about is
'personally-interesting) ; interesting because it is newly learned.
t) ; |||||| But need to make this more principled.
;; Index the xp off dogs barking at
;; containers or people hitting what?
(print-indexing-event
(do-index generalized-xp
index-type
(f.get generalized-xp
*explains-node*) ; Pass some explained relation
t ; Non-destructive as true
nil ; Forced-indexing as false
(list-relations
(f.chase-path generalized-xp
*explains-node*
*domain-slot*)
(butlast anomaly-path))))
(print-specialization-event old-index) ; Simple program feedback.
(do-index nil index-type index-relation) ; Removes the current indexing of old-xp.
;; ||||| Hack to get old threaten-xp indexed off
;; animate objects in the "to" slot.
(f.put-all!
(f.instantiate-frame animate-object) ; Filler
(f.get anomalous-concept 'to) ; Frame
*domain-slot*) ; Slot
;; The old threatening bark explanation is indexed off
;; dogs barking at animate objects.
(do-index (first ; Old-xp, e.g., xp-defensive-bark.X or xp-injury-hit.Y
(f.get old-index
*co-domain-slot*))
index-type
(f.get anomalous-concept
*actor-slot*
*relation-facet*)
t
t ; Forced indexing [cox 27jun95]
(list-relations anomalous-concept
(butlast anomaly-path)))
)
)
(defun print-indexing-event (memory-index
&optional
(stream *aqua-window*))
"Program feedback during re-indexing."
; (with-character-style (*Style*)
(format
stream
"~%~%Indexing new explanation with index ~s.~%"
memory-index)
; )
)
(defun print-specialization-event (memory-index
&optional
(stream *aqua-window*))
"Program feedback during re-indexing."
; (with-character-style (*Style*)
(format
stream
"~%~%Execute specialize on ~s.~%"
memory-index)
; )
)
;;;
;;; BLAME ASSIGNMENT
;;;
;;;
;;; Function l.strategy-decision uses the outcome of the verify stage (||||||
;;; NOTE that it may also use the outcome of the generation-phase) as an index
;;; to retrieve an introspective explanation of the reasoning trace. If the
;;; memory retrieval is successful, then the index is formally represented as a
;;; frame and then stored in a newly created knowledge state frame. This state
;;; frame is then recorded as the decision basis for the learning strategy
;;; decision. Since many strategies may be chosen as part of an overall
;;; learning plan, the function does not choose a meaningful value to return as
;;; the result of the function. Explanation.0 is returned as a generic value. I
;;; suppose that we are using introspective explanation with the IMXP in
;;; further learning steps, so the value is appropriate, but many algorithms
;;; may be selected at a lower level, so it may be misleading too..
;;;
(defun l.strategy-decision (decision-basis reasoning-failure learning-node k-goal)
(let* ((imxp-candidates
(retrieve-xps
reasoning-failure))
(k-state (f.instantiate-frame
knowledge-state)))
(do-break l.strategy-decision)
(cond (imxp-candidates
(f.unify
(make-index reasoning-failure ; Result should be of xp-type.0
imxp-candidates)
(f.get k-state
'believed-item)))
(t
(format *aqua-window*
"~%No introspective XP retrieved.~%")))
(f.put! (list k-state)
(f.get
decision-basis
'knowledge)
'members))
'explanation.0) ; Return value
;;;
;;; STRATEGY EXECUTION
;;;
;;;
;;; ||||| Because l.runstrategy is performed multiple times, only the last
;;; f.put! will have an effect. Should add each one to a list instead.
;;;
(defun l.runstrategy (learning-node strategy-choice parameters &optional add-break?)
(do-break l.runstrategy)
(case strategy-choice
(generalization.0
(if add-break?
(add-break generalize))
(f.put! (list (apply #'generalize
parameters))
(f.get learning-node
'main-result)
'members)
(if add-break?
(un-break generalize))
'generalize.0)
(abstraction.0
(if add-break?
(add-break abstract))
(f.put! (list (apply #'abstract
parameters))
(f.get learning-node
'main-result)
'members)
(if add-break?
(un-break abstract))
'abstract.0)
(specialization.0
(if add-break?
(add-break specialize))
(f.put! (list (apply #'specialize
(cons learning-node parameters)))
(f.get learning-node
'main-result)
'members)
(if add-break?
(un-break specialize))
'specialize.0)
(conditionalization.0
(if add-break?
(add-break index-new-xp))
(f.put! (list (apply #'index-new-xp
parameters))