Skip to content

Commit 31a1351

Browse files
committed
updates for distance functions, retraction, and tracking implementations
1 parent 8df8491 commit 31a1351

File tree

2 files changed

+138
-47
lines changed

2 files changed

+138
-47
lines changed

techne-atms.lisp

+5-6
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
(informant nil)
5858
(consequence nil)
5959
(antecedents nil))
60-
60+
6161
(defun print-just (just stream ignore)
6262
(declare (ignore ignore))
6363
(format stream "<~A ~D>" (just-informant just)
@@ -154,7 +154,6 @@
154154
(push (create-env atms (list node)) (tms-node-label node)))
155155
node)
156156

157-
158157
(defun assume-node (node &aux atms)
159158
(unless (tms-node-assumption? node)
160159
(setq atms (tms-node-atms node))
@@ -196,7 +195,7 @@
196195
(justify-node informant
197196
(atms-contra-node (tms-node-atms (car nodes)))
198197
nodes))
199-
198+
200199
;;; Label updating
201200

202201
(defun propagate (just antecedent envs &aux new-envs)
@@ -223,7 +222,7 @@
223222
(rplaca new-envs nil)))
224223
(setq new-envs (delete nil new-envs :TEST #'eq))
225224
(unless new-envs (return-from update nil))))
226-
225+
227226
(defun update-label (node new-envs &aux envs)
228227
(setq envs (tms-node-label node))
229228
(do ((new-envs new-envs (cdr new-envs)))
@@ -278,7 +277,7 @@
278277
(defun supporting-antecedent? (nodes env)
279278
(dolist (node nodes t) (unless (in-node? node env) (return nil))))
280279

281-
280+
282281
(defun remove-node (node &aux atms)
283282
(if (tms-node-consequences node)
284283
(error "Can't remove node with consequences"))
@@ -293,7 +292,7 @@
293292
(dolist (env (tms-node-label node))
294293
(setf (env-nodes env)
295294
(delete node (env-nodes env) :test #'eq :count 1))))
296-
295+
297296
;;; Creating and extending environments.
298297

299298
(defun create-env (atms assumptions &aux e)

techne-psm.lisp

+133-41
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,165 @@
11
;;; The Problem Solver Module for Techne.
2-
;;; Contains the interface described in techne/papers/caise11-tms/caise-tms.tex
2+
;;; Contains the interface described in papers/caise11-tms/caise-tms.tex
33
;;; E.g. ASK, TELL, UNTELL
44

55
;;; Load the ATMS
6-
(load "techne-atms.dx64fsl") ; Clozure
6+
(load "/Users/nernst/Dropbox/research/src/tms/techne-atms.dx32fsl") ; Clozure
77
;(load "techne-atms.fasl") ; SBCL
88

99
;;; Data storage
1010
;;; TODO: figure out why defclass is preferred for REKB over defstruct
11-
(defvar *goals* nil)
12-
(defvar *da* nil)
13-
(defvar *tasks* nil)
14-
(defvar *opts* nil) ; optional goals
15-
(defvar *assm-count* 0) ; how many assumptions have we been told? The computational limit is ~100? TODO verify this.
11+
(defvar *goals* ())
12+
(defvar *assumptions* ())
13+
(defvar *opts* ()) ; optional goals
14+
(defvar *assm-count* 0) ; how many assumptions have we been told? The computational limit is ~300
15+
(defvar *mands* ()) ; use equal to compare strings (?)
16+
(defvar *assm-form* ()) ; hold labelled formulae
17+
(defvar *labels* (make-hash-table :test 'equalp)) ;case insensitive
18+
(defvar *impl-repo* nil) ; hold the sets of implementations
1619

1720
;;;Distance functions
18-
(defun min-effort (initial-solution new-solutions)
19-
" The solution which differs in the fewest tasks and is shortest."
21+
(defun min-effort (new-solutions)
22+
" The solution which differs in the fewest tasks"
23+
;; TODO add minimize set size of new solution
24+
(let ((existing (getf (first *impl-repo*) :TASKS )) (smallest (first new-solutions)))
25+
;;assume use last set of impl
26+
(loop for soln in new-solutions
27+
if (< (length (set-difference soln existing)) (length (set-difference smallest existing)))
28+
do (setf smallest soln)
29+
finally (return smallest))))
2030
)
2131

22-
(defun max-fam (initial-solution new-solutions)
32+
(defun max-fam (new-solutions)
2333
"The solution which differs in the fewest tasks from the previous one"
24-
)
25-
26-
(defun simple (initial-solution new-solutions)
27-
"The shortest new solution"
28-
)
29-
30-
;;; TELLs
34+
;; maximize intersection
35+
(let ((existing (getf (first *impl-repo*) :TASKS )) (biggest (first new-solutions)))
36+
;;assume use last set of impl
37+
(loop for soln in new-solutions
38+
;do (print existing)
39+
if (> (length (intersection existing soln)) (length (intersection existing biggest)))
40+
do (setf biggest soln)
41+
finally (return biggest))))
42+
43+
(defun simple (new-solutions)
44+
"The shortest new solution, if a match, determined non-deterministically"
45+
(let ((sorted (sort new-solutions #'shorterp)))
46+
(first sorted)))
47+
48+
(defun shorterp (list1 list2)
49+
(< (length list1) (length list2)))
50+
51+
;; track implemented tasks
52+
(defun mark-implemented (tasks &optional (name "") )
53+
"Take a list of tasks which the user marked as selected"
54+
;; TODO: verify they are actually a valid solution?
55+
;; TODO: store old goals .. where do they go.
56+
(push (list :name name :tasks tasks :date (get-universal-time)) *impl-repo*))
57+
58+
;;; TELL/UNTELL
3159
(defun declare-atomic (atom label sort rekb)
3260
"Add the pair (ATOM LABEL) to the symbol table"
33-
; was it a task?
34-
(if (eql sort :TASK)
35-
(setf ?assume t) ;TODO also add this to list of goals etc.
36-
(setf ?assume nil))
37-
(tms-create-node rekb label :ASSUMPTIONP ?assume))
38-
39-
(defun assrt-formula (consequent ants sort label rekb) ;TODO ant/cons should be a single arg formula
40-
"If equivalent formula not yet asserted, add to set of LABELLED SORTED FORMULA."
41-
(justify-node label consequent ants)
42-
)
43-
44-
(defun assrt-opt (goal rekb) ; changed from assert to assrt to avoid conflict with lisp-unit
45-
"Flag ATOM as attractive."
46-
)
47-
48-
;UNTELLs
49-
(defun undeclare-atomic (atom rekb)
61+
;; if a task or DA, we justify with an assumption to indicate defeasibility
62+
(if (or (eql sort :DA) (eql sort :TASK))
63+
(progn
64+
(let ((ass-node (tms-create-node rekb label :ASSUMPTIONP nil)))
65+
(let ((fakenode (tms-create-node rekb (concatenate 'string "fake-" label) :ASSUMPTIONP t)))
66+
(incf *assm-count*)
67+
(justify-node (concatenate 'string "fake-just-" label) ass-node (list fakenode))
68+
;(setf *assumptions* (nconc *assumptions* (list label))) ; the name
69+
(setf (gethash (concatenate 'string "fake-" label) *labels*) fakenode)
70+
;(nconc *assumption-refs* (list fakenode))) ; the actual node
71+
ass-node)))
72+
(progn ;; otherwise, just a simple node
73+
(setf *goals* (nconc *goals* (list label)))
74+
(tms-create-node rekb label :ASSUMPTIONP nil))))
75+
76+
(defun undeclare-atomic (label rekb)
5077
"Remove ATOM and any FORMULAs with ATOM as member from REKB."
51-
)
78+
;; see DeKleer's ATMS paper for the default logic reasoning behind this
79+
;; lookup the label in the list of DA or TASKs
80+
(unless (label-in-table-p (concatenate 'string "fake-" label))
81+
(error "\"~A\" is not a valid atom" label))
82+
(let ((x (gethash (concatenate 'string "fake-" label) *labels*)))
83+
(justify-node (concatenate 'string "retracted-" label) (contradiction rekb) (list x))
84+
(remhash (concatenate 'string "fake-" label) *labels* )))
85+
86+
(defun assert-formula (consequent ants sort label rekb)
87+
"If equivalent formula not yet asserted, add to set of LABELLED SORTED FORMULA."
88+
;;TODO ant/cons should be a single arg formula
89+
;; create a fake node that will be used to retract the formula.
90+
(when (eq sort :DA)
91+
(let ((ass-node (tms-create-node rekb (concatenate 'string "fakeform-" label) :ASSUMPTIONP t)))
92+
;; call the ATMS justification method from fake-node + ants to consequent
93+
;; add this node to the table for future retraction
94+
(setf (gethash (concatenate 'string "fakeform-" label) *labels*) ass-node)
95+
(let ((new-ants (append (list ass-node) ants)))
96+
(justify-node label consequent new-ants)))))
5297

5398
(defun retract-formula (label rekb)
5499
"Remove formula identified by LABEL from REKB."
55-
)
100+
(unless (label-in-table-p (concatenate 'string "fakeform-" label))
101+
(error "\"~A\" is not a valid formula" label))
102+
(let ((x (gethash (concatenate 'string "fakeform-" label) *labels*)))
103+
(justify-node (concatenate 'string "retractedform-" label) (contradiction rekb) (list x))
104+
(remhash (concatenate 'string "fakeform-" label) *labels* )))
105+
106+
(defun assert-opt (goal rekb)
107+
"Flag ATOM as attractive."
108+
;; todo: restrict so can only be performed on goals
109+
(setf *opts* (nconc *opts* (list goal))))
56110

57111
(defun retract-opt (goal rekb)
58112
"Remove the optional status of this goal"
59-
)
113+
(setf *opts* (remove goal *opts*)))
114+
115+
(defun assert-mandatory (goal rekb mandatory?)
116+
"Flag ATOM as mandatory or not mandatory in solution."
117+
;; get the goal name
118+
;; (setq goal-name (node-string goal))
119+
;; (if mandatory?
120+
;; (setq value goal)
121+
;; (setq value nil))
122+
;; (setf (gethash goal-name *mand*) value)
123+
;; )
124+
(setf *mands* (nconc *mands* (list goal))))
125+
;; add to the list, hash is quicker but don't think it will matter
126+
127+
(defun retract-mand (goal rekb)
128+
"Remove the optional status of this goal"
129+
(setf *mands* (remove goal *mands*)))
60130

61131
;ASKs
62132
(defun ask-sort (label rekb)
63133
"Return the sort of the given label"
64134
)
65135

66-
(defun are-goals-entailed? (goals rekb)
136+
(defun is-solution-p (rekb)
137+
"Check whether all mandatory goals are satisfied by a common explanation"
138+
)
139+
140+
(defun are-goals-achieved-from-p (goals rekb)
141+
"For the given list of goals, are all the mandatory goals explained by that set?"
142+
)
143+
144+
(defun are-goals-entailed-p (goals rekb)
67145
"Are the set of goals entailed by the rekb?
68146
entailment of a set of goals mean they are consistent w/ all of at
69147
least 1 of the other goal's contexts"
70-
71148
(setf goal (pop goals)) ;get environments for each goal
72149
(setf entailed? nil) ; TODO use LET instead of setf?
73150
(dolist (env (tms-node-label goal)) ;env of first goal (tms-node-label goal)
74151
(setf congruent? t)
75152
(dolist (other goals) ; compare to remaining goals
76153
(setf all-false? t)
77154
(dolist (other-env (tms-node-label other))
78-
(if (not (not (compare-env env other-env))) ; if EQ,:S21,:S12 continue
155+
(when (not (not (compare-env env other-env))) ; if EQ,:S21,:S12 continue
79156
(setf all-false? nil)))
80157
;; looped all envs in that goal, should we continue?
81158
(if all-false?
82159
(setf congruent? nil))) ;TODO add short-circuit to prevent eval of all goals.
83160
(if congruent?
84-
(setf entailed? t))) ;at least one env in the other goals matches this env. TODO again short-circuit would be nice.
161+
(setf entailed? t))) ;at least one env in the other goals matches this env.
162+
;;TODO again short-circuit would be nice.
85163
entailed?)
86164

87165
(defun min-change-task-entailing (goals tasks dist_fn rekb)
@@ -134,3 +212,17 @@
134212
(defun contradiction (rekb)
135213
"return the TMS contradiction special node"
136214
(tms-create-node rekb 'CONTRADICTION :CONTRADICTORYP t))
215+
216+
(defun print-rekb (rekb)
217+
"print the values in the rekb"
218+
(print *assumptions*)
219+
(print-atms-statistics rekb))
220+
221+
(defun print-assumption-hash (rekb)
222+
"print the keys of the assumption nodes added for retraction"
223+
(loop for key being the hash-keys of *labels*
224+
do (print key)))
225+
226+
(defun label-in-table-p (label)
227+
"try to find the given label, suitably prepended with 'fake-' or 'fakeform-', in the dictionary"
228+
(nth-value 1 (gethash label *labels*)))

0 commit comments

Comments
 (0)