-
Notifications
You must be signed in to change notification settings - Fork 0
/
implement-effects.lisp
190 lines (180 loc) · 9.93 KB
/
implement-effects.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
; I'm separating out this function because it causes compiler errors.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This procedure executes the first action specified by arg action-name
;; of the best plan found in go!, updates *real-clock* and *TED-clock*,
;; and refreshes the actual new current state *curr-state-node* after
;; gathering new facts that may be available after the action taken.
;; (The assumption is that some facts associated with objects at a
;; particular point will not become known to the agent till it gets to that
;; point, and so the new state will in general be richer in facts than the
;; anticipated state. After executing the action, the action along with
;; its real resulting state, real duration, expected duration, and
;; *real-clock* and *TED-clock* is logged in *real-history* and
;; *TED-history*, respectively.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Daphne: Revised 10/18/2012 to handle new representation of terms
; Daphne: Revised Dec. 2009 to handle new representation of wff-htable
(defun implement-effects (action-name)
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(let* ((the-action (action-type action-name))
(action (eval action-name)); the agent's action concept
(expected-duration (op-time-required action))
(name (op-name action)); name of operator instantiated by action
(name.actual-str (concatenate 'string (string name) ".ACTUAL"))
; If 'name.actual' has no value yet, we assign it a copy of
; (eval name) as value (i.e., the actual operator is the same
; as the conceptual one); but change the op-name field.
(name.actual
(if (find-symbol name.actual-str)
(find-symbol name.actual-str)
; Build the required variant.
(name-of-actual-operator name.actual-str (eval name)))
)
(op.actual (eval name.actual)); presumed to exist at this point
(pars (op-pars op.actual))
(par-values (op-pars action))
(is-first-iter 'T)
(is-terminated 'NIL)
(par 'NIL)
(par-value 'NIL)
(stopconds.actual (op.actual-stopconds op.actual))
(adds.actual (op.actual-adds op.actual))
(deletes.actual (op.actual-deletes op.actual))
tempdeletes adds deletes stopconds implied-facts bindings
new-state deletes-copy new-wff-htable new-terms
)
; Substitute the parameter values for the parameters in the termination
; conditions and the actual effects.
(while pars
(setq par (pop pars))
(setq par-value (pop par-values))
(setq adds.actual (subst par-value par adds.actual))
(setq deletes.actual (subst par-value par deletes.actual))
(setq stopconds.actual (subst par-value par stopconds.actual))
)
; We can look at the "local-value" of the state node previously
; calculated in lookahead, since the effects contributing to
; "state-value" are the same in both the model and actual versions
; of each internal action operator.
(setq *total-value*
(incf *total-value* (+ (op-value action)
(state-node-local-value *curr-state-node*))))
; Reset *event-timer* to start tracking duration of the selected action.
(setq *event-timer* 0)
; Execute the action iteration by iteration while its termination
; conditions are all not true in TED's KB.
(while (or (eq 'T is-first-iter) (eq is-terminated 'NIL))
(incf *event-timer* 1)
(if (eq 'T is-first-iter)
(setq is-first-iter 'NIL)
; For each subsequent (after first) iteration, check whether any
; of the termination conditions of the selected action are true
; according to TED's KB.
(progn
(handleExtOps)
; Above line commented out only for opp (i.e., normal) runs
(setq stopconds (mapcar #'simplify-value stopconds.actual))
(if (evaluable-func (car stopconds))
(setq stopconds (simplify-value stopconds)))
(if (eq 'T
(eval (cons 'memb
(list (quote 'T)
(list 'quote
(mapcar
#'(lambda (x)
(evalFunctionPredicate x))
stopconds)))))
)
(setq is-terminated 'T))
)
)
; Evaluate and apply effects to the world KB and TED's KB only if the
; selected action's termination conditions are all false according
; to TED's KB.
(when (eq 'NIL is-terminated)
; Evaluate and simplify the actual adds.
(setq adds (mapcar #'simplify-value adds.actual))
(if (evaluable-func (car adds))
(setq adds (simplify-value adds))
)
; Evaluate and simplify the actual deletes.
(setq deletes (mapcar #'simplify-value deletes.actual))
(if (evaluable-func (car deletes))
(setq deletes (simplify-value deletes)))
(setq deletes (set-differencef deletes adds))
; Find all the bindings of variables in actual deletes so
; we can make sure all actual deletes will be removed.
(setq bindings (remove-if #'degenerate-binding
(all-bindings-of-goals-to-fact-htable deletes
(state-node-wff-htable *curr-state-node*)
(state-node-terms *curr-state-node*))))
(setq bindings (remove-duplicates bindings :test #'equal))
; Apply all bindings found to get all actual deletes.
(when (and (not (equal '(T) bindings)) (not (null bindings)))
(setq tempdeletes 'NIL)
(dolist (u bindings)
(setq deletes-copy deletes)
(dolist (b u)
(setq deletes-copy
(subst (cdr b) (car b) deletes-copy)) )
(setq tempdeletes (unionf deletes-copy tempdeletes))
)
(setq tempdeletes (remove-duplicates tempdeletes :test #'equal))
(setq tempdeletes (mapcar #'simplify-value tempdeletes))
(setq deletes (set-differencef tempdeletes adds))
)
; Apply actual effects to the world KB after further inferences.
(remove_list_of_tuples_from_hashtable deletes *protected-facts* 'NIL)
(add_list_of_tuples_to_hashtable adds *protected-facts* 'NIL)
(setq *world-facts* (all-inferences *protected-facts*
*general-knowledge* *inference-limit*))
(add_htable_to_hashtable *protected-facts* *world-facts* 'NIL)
; Apply actual effects to TED's KB.
(setq new-terms (state-node-terms *curr-state-node*))
(setq new-wff-htable (state-node-wff-htable *curr-state-node*))
(setq new-terms
(remove_term_list_from_term_list
(remove_list_of_tuples_from_hashtable deletes new-wff-htable 'T)
new-terms))
(setq new-terms
(merge_term_list_with_term_list
(add_list_of_tuples_to_hashtable adds new-wff-htable 'T)
new-terms))
(setf (state-node-terms *curr-state-node*) new-terms)
; Modify *curr-state-node* to update the agent's beliefs
; in light of the locally evident facts (and beliefs that
; have evidently become false).
(setq new-state (copy_construct_hashtable
(notice-new-local-facts *curr-state-node*)))
(setq *states* (cons new-state (cdr *states*)))
; After executing for an iteration, the action along with its
; real resulting state, real elapsed duration, expected duration,
; and *real-clock* is logged in *real-history*.
(push (list (cons the-action new-state) *event-timer*
expected-duration *real-clock*) *real-history*)
; After the very first iteration of execution, the action along
; with its real resulting state, real elapsed duration, expected
; duration, and *TED-clock* is logged in *TED-history*. Only the
; start and (later) the end, and not each iteration of execution,
; of the action will be logged in *TED-history* to reflect that
; TED does not know in advance what the real duration of an action
; will surely be.
(if (= 1 *event-timer*)
(push (list (cons the-action new-state)
*event-timer* expected-duration *TED-clock*)
*TED-history*))
)
; Increment *real-clock* and *TED-clock* after each iteration of
; execution.
(incf *real-clock* 1)
(incf *TED-clock* 1)
); end of executing the action iteration by iteration
; Log the end of the action in *TED-history* to indicate TED's awareness
; of its termination.
(when (> *event-timer* 2)
(push (list (cons the-action new-state) (- *event-timer* 1)
expected-duration (- *TED-clock* 2)) *TED-history*)
)
)
); end of implement-effects