-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathonline-dfs.scm
executable file
·182 lines (169 loc) · 6.59 KB
/
online-dfs.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
#!/usr/bin/env chicken-scheme
;; [[file:~/prg/scm/aima/aima.org::*Online%20DFS][Online-DFS: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)
(not (stack-empty? (hash-table-ref/default untried state (make-stack)))))
(define (unbacktracked? unbacktracked state)
(not (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))
(result (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))
(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)))
(define (maybe-set-start! state)
(unless start
(set! start state)))
(define (set-unbacktracked! state)
(let* ((backtrack (stack-pop! (hash-table-ref unbacktracked state)))
(backtrack-action
(hash-table-ref (hash-table-ref result state) backtrack)))
(set! backtrack? #t)
(set! previous-action backtrack-action)))
(define (set-untried! state)
(set! backtrack? #f)
(set! previous-action (stack-pop! (hash-table-ref untried state))))
(lambda (state goal? score)
(maybe-set-start! state)
;; (maybe-write-agent-as-png state score)
(maybe-update-untried! untried state)
(maybe-update-labels! labels state)
(if previous-state
(begin
(maybe-update-result! result previous-state previous-action state)
(maybe-update-unbacktracked! unbacktracked backtrack? previous-state state)
;; We could probably set this as a state variable
;; and avoid the lookup.
(maybe-update-coordinates! coordinates previous-state previous-action state time))
;; We've been teleported to some previously unknow
;; 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)
(set-untried! state)
(if (unbacktracked? unbacktracked state)
(set-unbacktracked! state)
(begin
(set! previous-action stop)
(error "Goal not found -- AGENT-ONLINE-DFS"))))
(set! previous-state state)
previous-action))))))
;; (simulate-navigation 50 100 "online-dfs.avi" make-agent-online-dfs)
(simulate-navigation make-agent-online-dfs
n-points: 10
n-steps: 100
;; animation-file: "online-dfs.avi"
)
;; Online-DFS:1 ends here