comparison src/sim.tp @ 53:fbeedb3aa239

Add heuristic for evaluating non-terminal states. Cull to 8 states based on heuristic rather than just a single one based on score.
author Mike Pavone <pavone@retrodev.com>
date Sun, 15 Jul 2012 21:42:46 -0700
parents b0d89ee419c1
children a37ceb0a4f5c
comparison
equal deleted inserted replaced
52:b0d89ee419c1 53:fbeedb3aa239
57 id <- ("R" byte: 0) 57 id <- ("R" byte: 0)
58 string <- "R" 58 string <- "R"
59 x <- 0 59 x <- 0
60 y <- 0 60 y <- 0
61 isrobot <- { true } 61 isrobot <- { true }
62 navigable <- { false }
62 eq <- :other { id = (other id) } 63 eq <- :other { id = (other id) }
63 collected <- 0 64 collected <- 0
64 heldBreath <- 0 65 heldBreath <- 0
65 razors <- 0 66 razors <- 0
66 busted <- false 67 busted <- false
129 new <- :in_grid in_width in_height { 130 new <- :in_grid in_width in_height {
130 _nextGrid <- #[] 131 _nextGrid <- #[]
131 _robot <- null 132 _robot <- null
132 _ended <- false 133 _ended <- false
133 _maxmoves <- in_width * in_height 134 _maxmoves <- in_width * in_height
134 135 _heuristicValid <- false
136 _heuristic <- 0
135 _succeeded <- false 137 _succeeded <- false
136 ret <- #{ 138 ret <- #{
137 grid <- in_grid 139 grid <- in_grid
138 width <- in_width 140 width <- in_width
139 height <- in_height 141 height <- in_height
187 cur append: el 189 cur append: el
188 } 190 }
189 } 191 }
190 cur 192 cur
191 } 193 }
194 distanceFrom:to <- :x y celltype {
195 //print: "calculating distance from " . x . ", " . y . " to " . celltype . "\n"
196 moves <- validMoves: x y
197 curdist <- 0
198 visited <- _nextGrid
199 foreach: grid :idx el {
200 visited set: idx false
201 }
202 notfound <- true
203 while: { if: notfound { (moves length) > 0 } } do: {
204 nextmoves <- #[]
205 curdist <- curdist + 1
206 foreach: moves :idx move {
207 curpos <- move index
208 if: (not: (visited get: curpos)) {
209 if: ((grid get: curpos) eq: celltype) {
210 notfound <- false
211 } else: {
212 visited set: curpos true
213 foreach: (validMoves: (calcX: curpos) (calcY: curpos)) :idx move {
214 nextmoves append: move
215 }
216 }
217 }
218 }
219 moves <- nextmoves
220 }
221 curdist
222 }
192 getRobot <- { _robot } 223 getRobot <- { _robot }
193 updatePos <- :obj Index { 224 updatePos <- :obj Index {
194 obj x!: (calcX: Index) 225 obj x!: (calcX: Index)
195 obj y!: (calcY: Index) 226 obj y!: (calcY: Index)
196 } 227 }
199 flooding <- 0 230 flooding <- 0
200 waterproof <- 10 231 waterproof <- 10
201 moves <- #[] 232 moves <- #[]
202 score <- 0 233 score <- 0
203 maxScore <- { score + (lambdaCount - (_robot collected)) * 25 + lambdaCount * 50 } 234 maxScore <- { score + (lambdaCount - (_robot collected)) * 25 + lambdaCount * 50 }
235 heuristic <- {
236 if: (not: _heuristicValid) {
237 dest <- if: (_robot collected) = lambdaCount {
238 cellTypes openLift
239 } else: {
240 cellTypes lambda
241 }
242 _heuristic <- score - (distanceFrom: (_robot x) (_robot y) to: dest)
243 _heuristicValid <- true
244 }
245 _heuristic
246 }
204 addPoints <- :points { score <- score + points } 247 addPoints <- :points { score <- score + points }
205 ended <- {_ended} 248 ended <- {_ended}
206 succeeded <- {_succeeded} 249 succeeded <- {_succeeded}
207 succeeded! <- { 250 succeeded! <- {
208 _ended <- true 251 _ended <- true
254 abort <- { 297 abort <- {
255 _ended <- true 298 _ended <- true
256 addPoints: (_robot collected) * 25 299 addPoints: (_robot collected) * 25
257 } 300 }
258 advance <- :roboCmd { 301 advance <- :roboCmd {
302 _heuristicValid <- false
259 if: roboCmd = "A" { 303 if: roboCmd = "A" {
260 moves append: roboCmd 304 moves append: roboCmd
261 abort 305 abort
262 } 306 }
263 307