forked from klutometis/aima
-
Notifications
You must be signed in to change notification settings - Fork 0
/
online-dfs-non-deterministic.scm
executable file
·211 lines (198 loc) · 7.93 KB
/
online-dfs-non-deterministic.scm
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
#!/usr/bin/env chicken-scheme
;; [[file:~/prg/scm/aima/aima.org::*4.11c][4\.11c:1]]
(include "online-navigation.scm")
(define (maybe-update-untried! untried state)
(unless (hash-table-exists? untried state)
(hash-table-set!
untried
state
(list->stack state))))
(define (maybe-update-labels! labels state)
(unless (hash-table-exists? labels state)
(hash-table-set!
labels
state
(gensym))))
(define (maybe-update-result! result previous-state previous-action state)
(hash-table-update!
result
previous-state
(lambda (state->action)
(hash-table-set! state->action state previous-action)
state->action)
(lambda () (make-hash-table))))
(define (maybe-update-unbacktracked! unbacktracked backtrack? previous-state state)
(unless backtrack?
(hash-table-update!
unbacktracked
state
(lambda (backtracks)
(stack-push! backtracks previous-state)
backtracks)
(lambda () (make-stack)))))
(define (maybe-update-coordinates! coordinates previous-state previous-action state time)
;; We could probably set this as a state variable
;; and avoid the lookup.
(let* ((previous-coordinate
(hash-table-ref coordinates previous-state))
(previous-point (coordinate-point previous-coordinate)))
(if (hash-table-exists? coordinates state)
(let* ((coordinate (hash-table-ref coordinates state))
(point (coordinate-point coordinate)))
(when (< (coordinate-time coordinate) time)
(let ((delta-x
(- (point-x point)
(+ (point-x previous-point)
(point-x previous-action))))
(delta-y
(- (point-y point)
(+ (point-y previous-point)
(point-y previous-action)))))
(hash-table-walk coordinates
(lambda (state old-coordinate)
(when (< (coordinate-time old-coordinate) time)
;; (debug ;; (coordinate-time old-coordinate)
;; ;; time
;; delta-x
;; delta-y)
(coordinate-time-set! old-coordinate time)
(let ((old-point (coordinate-point old-coordinate)))
(coordinate-point-set!
old-coordinate
(make-point (- (point-x old-point) delta-x)
(- (point-y old-point) delta-y))))))))))
(hash-table-set!
coordinates
state
(make-coordinate
(make-point (+ (point-x previous-point)
(point-x previous-action))
(+ (point-y previous-point)
(point-y previous-action)))
time)))))
(define (maybe-update-origin! coordinates state time)
(unless (hash-table-exists? coordinates state)
(hash-table-set!
coordinates
state
(make-coordinate origin time))))
(define (untried? untried state)
(stack-empty? (hash-table-ref/default untried state (make-stack))))
(define (unbacktracked? unbacktracked state)
(stack-empty? (hash-table-ref/default unbacktracked state (make-stack))))
(define (make-agent-online-dfs start next-frame)
(make-agent
start
0
(let ((coordinates (make-hash-table))
(labels (make-hash-table))
;; state->state->[[action_0, p_0], ..., [action_n, p_n]]
(result (make-hash-table))
;; state->action->[[state_0, p_0], ..., [state_n, p_n]]
(state->action->state (make-hash-table))
(untried (make-hash-table))
(unbacktracked (make-hash-table))
(previous-state #f)
(previous-action #f)
(backtrack? #f)
(time 0)
(start #f)
(goal #f))
(define (reset-state! state)
(set! goal state)
(set! start #f)
(set! previous-state #f)
(set! previous-action #f)
(set! backtrack? #f)
(set! untried (make-hash-table))
(set! unbacktracked (make-hash-table))
(inc! time)
;; (if previous-state
;; (hash-table-update!
;; result
;; previous-state
;; (lambda (state->action)
;; (hash-table-set! state->action state previous-action)
;; state->action)
;; (lambda () (make-hash-table))))
)
(define (maybe-write-agent-as-png state score)
(unless (zero? (hash-table-size result))
(write-agent-as-png (next-frame)
state
coordinates
labels
result
untried
unbacktracked
previous-state
previous-action
start
goal
score)))
(lambda (state goal? score)
(unless start
(set! start state))
(maybe-write-agent-as-png state score)
(maybe-update-untried! untried state)
(maybe-update-labels! labels state)
(if previous-state
(begin
;; (hash-table-update!
;; state->action->state
;; previous-state
;; (lambda (action->state)
;; (let* ((expected-state
;; (hash-table-ref/default
;; action->state
;; previous-action
;; #f))
;; (navigation-error?
;; (and expected-state
;; (equal? expected-state state))))
;; (if expected-state
;; (when navigation-error?
;; (debug 'navigation-error))
;; (hash-table-set!
;; action->state
;; previous-action
;; state)))
;; action->state)
;; (lambda () (make-hash-table)))
(maybe-update-result! result previous-state previous-action state)
(maybe-update-unbacktracked! unbacktracked backtrack? previous-state state)
(maybe-update-coordinates! coordinates previous-state previous-action state time))
;; We've been teleported to some previously unknown
;; location; let's designate it as the origin.
(maybe-update-origin! coordinates state time))
(if goal?
(begin
(debug "Goal found -- ONLINE-DFS-AGENT")
(reset-state! state)
zero-motion)
(begin
(if (untried? untried state)
(if (unbacktracked? unbacktracked state)
(begin
;; (set! previous-action stop)
;; (error "Goal not found -- AGENT-ONLINE-DFS")
(set! previous-action (list-ref state (random (length state))))
(debug "Goal not found -- AGENT-ONLINE-DFS"))
(let* ((backtrack (stack-pop! (hash-table-ref unbacktracked state)))
(backtrack-action
(hash-table-ref (hash-table-ref result state) backtrack)))
(debug 'backtrack)
(set! backtrack? #t)
(set! previous-action backtrack-action)
(set! previous-action (list-ref state (random (length state))))))
(begin
(set! backtrack? #f)
(set! previous-action (stack-pop! (hash-table-ref untried state)))))
(set! previous-state state)
previous-action))))))
(simulate-navigation make-agent-online-dfs
n-points: 10
n-steps: 100
p-slippage: 0
animation-file: "slippage.avi")
;; 4\.11c:1 ends here