-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMazeWorldFinal.krf
250 lines (209 loc) · 7.57 KB
/
MazeWorldFinal.krf
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
(in-microtheory MazeWorldMt)
(genlMt MazeWorldMt HTNPlanner)
(genlMt Session-ReasonerPlanMt MazeWorldMt)
;;; axiom vocab
(isa isStart UnaryPredicate)
(arity isStart 1)
(arg1Isa isStart mazeBlock)
(comment isStart "(isStart ?x) means that the agent starts the maze from ?x.")
(isa hasAgent UnaryPredicate)
(arity hasAgent 1)
(arg1Isa hasAgent mazeBlock)
(comment hasAgent "(hasAgent ?x) means the agent is at mazeBlock ?x.")
(isa keyCount UnaryPredicate)
(arity keyCount 1)
(arg1Isa keyCount NonNegativeInteger)
(comment keyCount "(keyCount ?x) means the agent has ?x keys.")
(isa coordinate TernaryPredicate)
(arity coordinate 3)
(arg1Isa coordinate mazeBlock)
(arg2Isa coordinate NonNegativeInteger)
(arg3Isa coordinate NonNegativeInteger)
(comment coordinate "(coordinate ?x ?y ?z) means the mazeBlock ?x is located at (?y,?z).")
(isa isWalk UnaryPredicate)
(arity isWalk 1)
(arg1Isa isWalk mazeBlock)
(comment isWalk "(isWalk ?x) means mazeBlock ?x is traversible by the agent.")
(isa hasKey UnaryPredicate)
(arity hasKey 1)
(arg1Isa hasKey mazeBlock)
(comment hasKey "(hasKey ?x) means mazeBlock ?x is contains a key for the door.")
(isa isExit UnaryPredicate)
(arity isExit 1)
(arg1Isa isExit mazeBlock)
(comment isExit "(isExit ?x) means mazeBlock ?x is the exit mazeBlock.")
(isa finalPath UnaryPredicate)
(arity finalPath 1)
(arg1Isa finalPath List)
(comment finalPath "(finalPath ?x) ?x is an ordered list of mazeBlocks leading through the maze to the exit.")
;;; Horn Clauses
(<== (connected ?blockA ?blockB)
(isWalk ?blockA)
(isWalk ?blockB)
(coordinate ?blockA ?xA ?yA)
(coordinate ?blockB ?xB ?yA)
(evaluate ?x (DifferenceFn ?xA ?xB))
(evaluate ?y (AbsoluteValueFn ?x))
(equals ?y 1))
(<== (connected ?blockA ?blockB)
(isWalk ?blockA)
(isWalk ?blockB)
(coordinate ?blockA ?xA ?yA)
(coordinate ?blockB ?xA ?yB)
(evaluate ?x (DifferenceFn ?yA ?yB))
(evaluate ?y (AbsoluteValueFn ?x))
(equals ?y 1))
(<== (isDeadEnd ?block)
(evaluate ?set (TheClosedRetrievalSetOf ?block (connected ?block ?x)))
(evaluate ?len (CardinalityFn ?set))
(equals ?len 1))
;this gives us our end condition, and it should be updated as we traverse the maze. If these are all simultaneously true, we immediately stop recursion
(<== (solutionReached)
(totalKeyNumber ?x)
(keyCount ?y)
(equals ?x ?y)
(isExit ?exitBlock)
(hasAgent ?exitBlock))
;;; complex actions
(isa mazeAct ComplexActionPredicate)
(arity mazeAct 1)
(arg1Isa mazeAct List)
(comment mazeAct "(mazeAct ?l) recursively solves the maze in the current context and eventually saves ?l, the list of actions it took to solve the maze.")
(isa beginRecursion ComplexActionPredicate)
(arity beginRecursion 0)
(comment beginRecursion "(beginRecursion) calls (initializeWM).")
(isa recurseAgain ComplexActionPredicate)
(arity recurseAgain 0)
(comment recurseAgain "(recurseAgain) calls (mazeAct (TheList)).")
(isa initializeWM ComplexActionPredicate)
(arity initializeWM 0)
(comment initializeWM "(initializeWM) calls (mazeAct (TheList)) after it reads microtheory information into working memory.")
;;; complex action real definition
;grab a key from the mazeBlock you're in
(preconditionForMethod
(and
(uninferredSentence (ist-Information aMAZEingMt (solutionReached)))
(wmOnly (ist-Information aMAZEingMt (hasAgent ?x)))
(ist-Information aMAZEingMt (hasKey ?x))
(ist-Information aMAZEingMt (keyCount ?y))
(evaluate ?newList (JoinListsFn ?actList (TheList ?x)))
(localAgent ?sr)
(newUniqueTask MazeAct ?task)
)
(methodForAction
(mazeAct ?actList)
(actionSequence
(TheList
(evaluate ?z (PlusFn ?y 1))
(doUntell (ist-Information aMAZEingMt (keyCount ?y)))
(doTell (ist-Information aMAZEingMt (keyCount ?z)))
(doUntell (ist-Information aMAZEingMt (hasKey ?x)))
(doEnqueue ?sr ?task
(mazeAct ?newList))))))
;move to an adjacent mazeBlock, and don't backtrack
(preconditionForMethod
(and
(uninferredSentence (ist-Information aMAZEingMt (solutionReached)))
(wmOnly (ist-Information aMAZEingMt (hasAgent ?x)))
(wmOnly (ist-Information aMAZEingMt (uninferredSentence (hasKey ?x))))
;(ist-Information aMAZEingMt (connected ?x ?y))
;(evaluate ?last (LastInListFn ?actList))
;(different ?y ?last)
(numAnswers 1 (getNextBlock ?x ?y ?actList))
(evaluate ?newList (JoinListsFn ?actList (TheList ?x)))
(localAgent ?sr)
(wmOnly (ist-Information aMAZEingMt (evaluate ?agentSet (TheClosedRetrievalSetOf ?agent (hasAgent ?agent)))))
(evaluate ?longth (CardinalityFn ?agentSet))
(newUniqueTask MazeAct ?task))
(methodForAction
(mazeAct ?actList)
(actionSequence
(TheList
(doTell (ist-Information aMAZEingMt (hasAgent ?y)))
(doUntell (ist-Information aMAZEingMt (hasAgent ?x)))
(doEnqueue ?sr ?task
(mazeAct ?newList))))))
(<== (getNextBlock ?curr ?next ?list)
(uninferredSentence (ist-Information aMAZEingMt (isDeadEnd ?curr)))
(ist-Information aMAZEingMt (connected ?curr ?next))
(evaluate ?last (LastInListFn ?list))
(different ?last ?next))
(<== (getNextBlock ?curr ?next ?list)
(ist-Information aMAZEingMt (isDeadEnd ?curr))
(ist-Information aMAZEingMt (connected ?curr ?next)))
;(<== (getNextBlock ?curr ?next ?list)
; (evaluate ?len (LengthOfListFn ?list))
; (equals ?len 0)
; (ist-Information aMAZEingMt (connected ?curr ?next)))
;move to an adjacent mazeBlock and backtrack because you're in a dead end
;(preconditionForMethod
; (and
; (uninferredSentence (ist-Information aMAZEingMt (solutionReached)))
; (wmOnly (ist-Information aMAZEingMt (hasAgent ?x)))
; (uninferredSentence (ist-Information aMAZEingMt (hasKey ?x)))
; (ist-Information aMAZEingMt (connected ?x ?y))
; (ist-Information aMAZEingMt (isDeadEnd ?x))
; (evaluate ?newList (JoinListsFn ?actList (TheList ?x)))
; (localAgent ?sr)
; (newUniqueTask MazeAct ?task))
; (methodForAction
; (mazeAct ?actList)
; (actionSequence
; (TheList
; (doTell (ist-Information aMAZEingMt (hasAgent ?y)))
; (doUntell (ist-Information aMAZEingMt (hasAgent ?x)))
; (doEnqueue ?sr ?task
; (mazeAct ?newList))))))
;this is the same as the no backtrack one, essentially.
;that one fails if the list is empty since it has no last element to compare with the square you want to move to
;hence, this one does the same thing but can only be called when the list of visited squares is empty
(preconditionForMethod
(and
(evaluate ?len (LengthOfListFn ?actList))
(equals ?len 0)
(wmOnly (ist-Information aMAZEingMt (hasAgent ?x)))
(ist-Information aMAZEingMt (connected ?x ?y))
(evaluate ?newList (JoinListsFn ?actList (TheList ?x)))
(localAgent ?sr)
(newUniqueTask MazeAct ?task))
(methodForAction
(mazeAct ?actList)
(actionSequence
(TheList
(doTell (ist-Information aMAZEingMt (hasAgent ?y)))
; (doUntell (ist-Information aMAZEingMt (hasAgent ?x)))
(doEnqueue ?sr ?task
(mazeAct ?newList))))))
;If all goals are satisfied, at end of maze, do nothing
(preconditionForMethod
(ist-Information aMAZEingMt (solutionReached))
(methodForAction
(mazeAct ?actList)
(actionSequence
(TheList
(doTell (ist-Information aMAZEingMt (finalPath ?actList)))))))
;to start everything, we need something with no preconditions
(preconditionForMethod
(true)
(methodForAction
(beginRecursion)
(actionSequence
(TheList
(initializeWM)))))
(preconditionForMethod
(isStart ?s)
(methodForAction
(initializeWM)
(actionSequence
(TheList
(doTell (ist-Information aMAZEingMt (hasAgent ?s)))
(doTell (ist-Information aMAZEingMt (keyCount 0)))
(doPauseAgenda)
(mazeAct (TheList))))))
(preconditionForMethod
(true)
(methodForAction
(recurseAgain)
(actionSequence
(TheList
(mazeAct (TheList))))))