comparison src/sim.tp @ 62:ff2b38518a58

Updated heuristic
author Mike Pavone <pavone@retrodev.com>
date Mon, 16 Jul 2012 04:03:03 -0700
parents 7d4e51b4769a
children aa822c683e28
comparison
equal deleted inserted replaced
61:f851895ea67a 62:ff2b38518a58
30 ret <- #{ 30 ret <- #{
31 id <- (idStr byte: 0) 31 id <- (idStr byte: 0)
32 string <- idStr 32 string <- idStr
33 isrobot <- { false } 33 isrobot <- { false }
34 eq <- :other { id = (other id) } 34 eq <- :other { id = (other id) }
35 navigable <- { cannav } 35 navigable <- cannav
36 } 36 }
37 typedict set: (ret id) ret 37 typedict set: (ret id) ret
38 ret 38 ret
39 } 39 }
40 #{ 40 #{
177 string <- { 177 string <- {
178 name . "(" . idx . ")" 178 name . "(" . idx . ")"
179 } 179 }
180 }} 180 }}
181 here <- calcIndex: x y 181 here <- calcIndex: x y
182 //TODO: Add wait move when rocks are in motion 182 cur <- #[(amove: here "A") (amove: here "W")]
183 //(amove: here "W")
184 cur <- #[(amove: here "A")]
185 up <- amove: (calcIndex: x y + 1) "U" 183 up <- amove: (calcIndex: x y + 1) "U"
186 down <- amove: (calcIndex: x y - 1) "D" 184 down <- amove: (calcIndex: x y - 1) "D"
187 left <- amove: (calcIndex: x - 1 y) "L" 185 left <- amove: (calcIndex: x - 1 y) "L"
188 right <- amove: (calcIndex: x + 1 y) "R" 186 right <- amove: (calcIndex: x + 1 y) "R"
189 foreach: #[up down left right] :idx el { 187 foreach: #[up down left right] :idx el {
192 } 190 }
193 } 191 }
194 cur 192 cur
195 } 193 }
196 distanceFrom:to <- :x y celltype { 194 distanceFrom:to <- :x y celltype {
197 //print: "calculating distance from " . x . ", " . y . " to " . celltype . "\n" 195 //debugLog: "calculating distance from " . x . ", " . y . " to " . celltype . "\n"
196 if: (celltype eq: (cellTypes closedLift)) {
197 celltype navigable!: true
198 }
198 moves <- validMoves: x y 199 moves <- validMoves: x y
199 curdist <- 0 200 curdist <- 0
200 visited <- _nextGrid 201 visited <- _nextGrid
201 foreach: grid :idx el { 202 foreach: grid :idx el {
202 visited set: idx false 203 visited set: idx false
203 } 204 }
204 notfound <- true 205 notfound <- true
205 while: { if: notfound { (moves length) > 0 } } do: { 206 while: { if: notfound { (moves length) > 0 } } do: {
206 nextmoves <- #[] 207 nextmoves <- #[]
207 curdist <- curdist + 1 208 curdist <- curdist + 1
209 //debugLog: "moves at distance " . curdist . "\n"
208 foreach: moves :idx move { 210 foreach: moves :idx move {
209 curpos <- move index 211 curpos <- move index
212 //debugLog: "" . move . " " . (grid get: curpos) . "\n"
210 if: (not: (visited get: curpos)) { 213 if: (not: (visited get: curpos)) {
211 if: ((grid get: curpos) eq: celltype) { 214 if: ((grid get: curpos) eq: celltype) {
212 notfound <- false 215 notfound <- false
213 } else: { 216 } else: {
214 visited set: curpos true 217 visited set: curpos true
215 foreach: (validMoves: (calcX: curpos) (calcY: curpos)) :idx move { 218 foreach: (validMoves: (calcX: curpos) (calcY: curpos)) :idx move {
219
216 nextmoves append: move 220 nextmoves append: move
217 } 221 }
218 } 222 }
219 } 223 }
220 } 224 }
221 moves <- nextmoves 225 moves <- nextmoves
222 } 226 }
223 curdist 227 if: (celltype eq: (cellTypes closedLift)) {
228 celltype navigable!: false
229 }
230 if: notfound {
231 -1
232 } else: {
233 curdist
234 }
224 } 235 }
225 getRobot <- { _robot } 236 getRobot <- { _robot }
226 updatePos <- :obj Index { 237 updatePos <- :obj Index {
227 obj x!: (calcX: Index) 238 obj x!: (calcX: Index)
228 obj y!: (calcY: Index) 239 obj y!: (calcY: Index)
235 score <- 0 246 score <- 0
236 maxScore <- { score + (lambdaCount - (_robot collected)) * 25 + lambdaCount * 50 } 247 maxScore <- { score + (lambdaCount - (_robot collected)) * 25 + lambdaCount * 50 }
237 heuristic <- { 248 heuristic <- {
238 if: (not: _heuristicValid) { 249 if: (not: _heuristicValid) {
239 dest <- if: (_robot collected) = lambdaCount { 250 dest <- if: (_robot collected) = lambdaCount {
240 cellTypes openLift 251 dist <- (distanceFrom: (_robot x) (_robot y) to: (cellTypes openLift))
252 if: dist < 0 {
253 //debugLog: "open lift unreachable\n"
254 _heuristic <- (_robot collected) * 50 - (moves length)
255 } else: {
256 //debugLog: "open lift unreachable at distance" . dist . "\n"
257 _heuristic <- (_robot collected) * 75 - dist - (moves length)
258 }
241 } else: { 259 } else: {
242 cellTypes lambda 260 mult <- 0
243 } 261 liftdist <- (distanceFrom: (_robot x) (_robot y) to: (cellTypes closedLift))
244 _heuristic <- score - (distanceFrom: (_robot x) (_robot y) to: dest) 262 if: liftdist < 0 {
263 mult <- 50
264 } else: {
265 mult <- 75
266 }
267 lambdadist <- (distanceFrom: (_robot x) (_robot y) to: (cellTypes lambda))
268 if: lambdadist < 0 {
269 //debugLog: "lambda unreachable with lift multilier " . mult . "\n"
270 _heuristic <- score
271 } else: {
272 //debugLog: "lambda reachable at distance " . lambdadist . " lift multilier " . mult . "\n"
273 _heuristic <- (_robot collected) * mult - lambdadist - (moves length)
274 }
275 }
276 //_heuristic <- (_robot collected) * 75 - (distanceFrom: (_robot x) (_robot y) to: (cellTypes openLift) - (moves length)
245 _heuristicValid <- true 277 _heuristicValid <- true
246 } 278 }
247 _heuristic 279 _heuristic
248 } 280 }
249 addPoints <- :points { score <- score + points } 281 addPoints <- :points { score <- score + points }
307 abort <- { 339 abort <- {
308 _ended <- true 340 _ended <- true
309 addPoints: (_robot collected) * 25 341 addPoints: (_robot collected) * 25
310 } 342 }
311 advance <- :roboCmd { 343 advance <- :roboCmd {
312 _heuristicValid <- false 344 if: roboCmd = "?" {
313 if: roboCmd = "A" { 345 os write: 2 "valid moves: "
314 moves append: roboCmd 346 valid <- validMoves: (_robot x) (_robot y)
315 abort 347 foreach: valid :idx el {
316 } 348 os write: 2 (el cmd)
349 }
350 os write: 2 "\n"
351 } else: {
352 if: roboCmd = "h" {
353 os write: 2 "heuristic: " . heuristic . "\n"
354 } else: {
355 _heuristicValid <- false
356 if: roboCmd = "A" {
357 moves append: roboCmd
358 abort
359 }
317 360
318 if: (not: _ended) { 361 if: (not: _ended) {
319 _robot doCmd: roboCmd 362 _robot doCmd: roboCmd
320 score <- score - 1 363 score <- score - 1
321 moves append: roboCmd 364 moves append: roboCmd
322 doUpdate: 365 doUpdate:
323 checkForDeath: 366 checkForDeath:
324 swapGrids: 367 swapGrids:
325 if: (moves length) >= _maxmoves { 368 if: (moves length) >= _maxmoves {
326 abort 369 abort
370 }
371 }
327 } 372 }
328 } 373 }
329 self 374 self
330 } 375 }
331 printGrid <- { 376 printGrid <- {