-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdstarlite.lua
775 lines (645 loc) · 19.5 KB
/
dstarlite.lua
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
--SHIM
verbose = false --{} --this is a map of features to be verbose about
mode = 'production' -- production or development
if mode == 'development' then
require( 'PQueue' )
require( 'debugger' )
require( 'profiler' )
require "socket"
_inspect = require( 'inspect' )
inspect = function( ... )
print( _inspect( unpack( arg ) ) )
end
local iowrite = io.write
io = {write = function( ... )
if not verbose then return end
iowrite( unpack( arg ) )
end}
else
cc_include( 'PQueue' )
function pause() end
profiler = {start = function() end, stop = function() end}
socket = {gettime = function() return 0 end}
inspect = function() end
if not term then
local iowrite = io.write
term = {
write = function( ... )
if not verbose then return end
iowrite( unpack( arg ) )
end
}
end
io = term
end
-- CC doesn't seem to have math.huge. Or maybe it is sometimes math.inf?
if not math.huge then
math.huge = 1 / 0
end
function DumpQueue(q)
if not verbose then return end
local i = 1
for key, s in q.nodes() do
dprint( i..': '..'['..key[1]..','..key[2]..']', s.v[1], s.v[2], s.v[3], s.v[4] )
i = i + 1
end
end
function DumpNode(s)
if not verbose then return end
print( '<'..s.v[1]..','..s.v[2]..','.. s.v[3]..','.. s.v[4]..'>' )
end
function dlog( ... )
if not verbose then return end
inspect( unpack( arg ) )
end
function dprint( ... )
if not verbose then return end
print( unpack( arg ) )
end
function panic()
dprint( "Stuff broke and I don't know what to do about it" )
if verbose then
pause()
end
end
function LogPrintMap( m, goal, start )
if not ( verbose and verbose.map ) then
return
end
PrintMap( m, goal, start )
end
--Not likely very useful in 3D
function PrintMap(m, goal, start)
local minx, miny, maxx, maxy = math.huge, math.huge, -math.huge, -math.huge
for i, n in m.each() do
if n.v[1] < minx then
minx = n.v[1]
end
if n.v[1] > maxx then
maxx = n.v[1]
end
if n.v[2] < miny then
miny = n.v[2]
end
if n.v[2] > maxy then
maxy = n.v[2]
end
end
local robotNode = goal
local path = {}
while goal do
path[ goal ] = true
goal = goal.tree
end
path[ start ] = true -- temporary hack - seems like start should already be there
local z = 0
local dirs = {[0] = '^', '>', 'v', '<'}
for y = maxy, miny, -1 do
if y == maxy then
io.write( ' ' )
for x = minx, maxx do
local c = math.mod( math.abs( x ), 10 )
io.write( c )
end
io.write( '\n\n' )
end
for x = minx, maxx do
local n, pn = nil, nil
for d=0, 3 do
pn = m.get( {x, y, z, d} )
if pn then
n = pn
if path[ pn ] then
break
end
end
end
if x == minx then
local c = math.mod( math.abs( y ), 10 )
io.write( c..' ' )
end
if n then
local c = '0'
if n.cost == math.huge then
c = '#'
elseif n.g == math.huge then
c = '!'
else
--c = math.mod( math.floor(n.g), 10 )
c = '.'
end
if path[ n ] then
if n.cost == math.huge then
c = '%'
elseif n == robotNode then
c = 'T'
else
c = dirs[ n.v[4] ]
end
end
if n == start then
c = 'X'
elseif n == goal then
c = 'S'
end
io.write( c )
else
io.write( ' ' )
end
end
io.write( '\n' )
end
end
--/SHIM
function NodeMap()
local nodes = {}
local self
function tcopy(t)
tt = {}
for k,v in pairs(t) do
tt[ k ] = v
end
--TODO: i need a deep copy function.
tt.v = {tt.v[1], tt.v[2], tt.v[3], tt.v[4]}
return tt
end
self = {
add = function( n, val, noexpand )
if false and val.cost and val.cost == math.huge and not noexpand then
n = tcopy( n )
for d = 0, 3 do
n[4] = d
self.add( n, val, true )
end
return
end
if nodes[ n[1] ] == nil then
nodes[ n[1] ] = {}
end
if nodes[ n[1] ][ n[2] ] == nil then
nodes[ n[1] ][ n[2] ] = {}
end
if nodes[ n[1] ][ n[2] ][ n[3] ] == nil then
nodes[ n[1] ][ n[2] ][ n[3] ] = {}
end
nodes[ n[1] ][ n[2] ][ n[3] ][ n[4] ] = val
end,
hasKey = function( n )
return self.get( n ) ~= nil
end,
remove = function( n )
nodes[ n[1] ][ n[2] ][ n[3] ][ n[4] ] = nil
--yep, nodes grows with containers forever
end,
get = function( n )
return nodes[ n[1] ]
and nodes[ n[1] ][ n[2] ]
and nodes[ n[1] ][ n[2] ][ n[3] ]
and nodes[ n[1] ][ n[2] ][ n[3] ][ n[4] ]
end,
each = function()
local fnodes = {}
for xi, x in pairs( nodes ) do
for yi, y in pairs( x ) do
for zi, z in pairs( y ) do
for di, d in pairs( z ) do
table.insert( fnodes, d )
end
end
end
end
return ipairs( fnodes )
end,
}
return self
end
local NodeMetatable = {}
NodeMetatable.__index = NodeMetatable
NodeMetatable.__metatable = NodeMetatable
function NodeMetatable:serialize()
return self.v[1]..','
..self.v[2]..','
..self.v[3]..','
..self.v[4]..','
..self.g..','
..self.rhs..','
..self.cost
end
function NodeMetatable:unserialize( s )
local v = {}
s:gsub( '([^,]*)', function(m) table.insert( v,tonumber(m) ) end )
local n = Node( v )
n.g = v[5]
n.rhs = v[6]
n.cost = v[7]
return n
end
function Node( v )
if type(v) == 'string' then
return NodeMetatable:unserialize( v )
end
if v == nil then v = {0,0,0,0} end
local n = {
rhs = math.huge,
g = math.huge,
tree = nil,
cost = 1,
oldcost = 1,
v = {v[1], v[2], v[3], v[4]},
}
setmetatable( n, NodeMetatable )
return n
end
function Unit( n )
return n
end
function VectorAdd( v1, v2 )
local vs = {
v1[1] + v2[1],
v1[2] + v2[2],
v1[3] + v2[3],
v1[4] + v2[4],
}
if vs[4] > 3 then vs[4] = vs[4] - 4 end
return vs
end
function VectorSub( v1, v2 )
local vs = {
v1[1] - v2[1],
v1[2] - v2[2],
v1[3] - v2[3],
v1[4] - v2[4],
}
if vs[4] < 0 then vs[4] = vs[4] + 4 end
return vs
end
function Obstacle( v )
local n = Node( v )
n.cost = math.huge
return n
end
--helper function, because to make a node blocked I have
--to show it blocked in all "direction" dimensions...
--I don't like that, I need a better map structure.
-- Needs to remove existing nodes, and replace *all*
-- directions with the new obstacle. If there was a
-- node already, its RHS etc. values need to be maintained.
function makeObstacle( v, map, cost )
for dir = 0, 3 do
local ov = {v[1], v[2], v[3], dir}
local existing = map.get( ov )
if existing then
existing.oldcost = existing.cost
existing.cost = cost
--for s in Succ( existing ) do
-- if s.tree == existing then
-- end
--end
else
local node = Node( ov )
node.cost = cost
map.add( node.v, node )
end
end
end
function Distance( a, b )
return math.abs( a[1] - b[1] )
+ math.abs( a[2] - b[2] )
+ math.abs( a[3] - b[3] )
end
function TrueDistance( a, b )
local d1, d2, d3 = math.abs( a[1] - b[1] ), math.abs( a[2] - b[2] ), math.abs( a[3] - b[3] )
local hyp = math.sqrt( d1 * d1 + d2 * d2 )
return math.sqrt( hyp * hyp + d3 * d3 )
end
function DStarLite( start, goal, map, onmove )
local U, km, CalculateKey, CompareKey
local iter = 0
if map == nil then
map = NodeMap()
end
function DN(s) DumpNode(s) end
function DQ() DumpQueue( U ) end
-- Koenig reverses start and goal which seems
-- totally stupid to do in code, but to help me
-- understand, I'm doing it too.
goal, start = Node( start ), Node( goal )
-- start, goal = Node( start ), Node( goal )
last = nil
function Succ( s )
-- TODO: Randomize this. I think it helps.
s = s.v
local r = { [0]=1, 2, 3, 0}
local l = { [0]=3, 0, 1, 2}
local dx, dy = 0, 0
if s[4] == 0 or s[4] == 2 then
dy = 1
else
dx = 1
end
local vertices = {
{s[1] + dx, s[2] + dy, s[3], s[4] }, -- move north or east
{s[1] - dx, s[2] - dy, s[3], s[4] }, -- move south or west
--{s[1], s[2], s[3] + 1, s[4] }, -- move up
--{s[1], s[2], s[3] - 1, s[4] }, -- move down
{s[1], s[2], s[3], l[ s[4] ]}, -- turn left
{s[1], s[2], s[3], r[ s[4] ]}, -- turn right
}
local vertices = {
{s[1]+1, s[2], s[3],s[4]},
{s[1]-1, s[2], s[3],s[4]},
{s[1], s[2]+1, s[3],s[4]},
{s[1], s[2]-1, s[3],s[4]},
{s[1], s[2], s[3]+1,s[4]},
{s[1], s[2], s[3]-1,s[4]},
}
local nodes = {}
local i, diff = 1, 1
while vertices[i] do
local n = map.get( vertices[i] )
if not n then
n = Node( vertices[i] )
end
table.insert( nodes, n )
i = i + diff
end
local ni = 0
return function()
ni = ni + 1
return nodes[ ni ]
end
end
function Pred( s )
return Succ( s )
end
function updaterhs( n )
n.rhs = math.huge
n.tree = nil
for s in Succ( n ) do
local cost = Cost( n, s )
if n.rhs > s.g + cost then
n.rhs = s.g + cost
n.tree = s
end
end
UpdateVertex( n )
end
--s2 is current... at least now
function Heuristic( s1, s2 )
local dx1 = s2.v[1] - goal.v[1]
local dy1 = s2.v[2] - goal.v[2]
local dz1 = s2.v[3] - goal.v[3]
local dx2 = start.v[1] - goal.v[1]
local dy2 = start.v[2] - goal.v[2]
local dz2 = start.v[3] - goal.v[3]
local cross = math.abs( dx1 * dy2 - dx2 * dy1 )
cross = cross + math.abs( dx1 * dz2 - dx2 * dz1 )
-- the cross product nudging needs to be small enough to not
-- encourage the turtle to turn a lot, but big enough
-- to reduce expanded nodes.
--return TrueDistance( s1.v, s2.v ) + cross * .05
-- I think for this to be feasible, I need to randomize vertices
return Distance( s1.v, s2.v ) --+ cross * .0000000001
end
function Cost( s, sn )
local reverse = 0
if s.v[4] ~= sn.v[4] then
-- turning is cheap but not free (takes time, not fuel)
return .5
end
--include a penalty for driving in reverse (for looks)
local dv = {s.v[1] - sn.v[1], s.v[2] - sn.v[2]}
if dv[1] < 0 and s.v[4] == 1 then
reverse = .1
elseif dv[2] < 0 and s.v[4] == 0 then
reverse = .1
elseif dv[2] > 0 and s.v[4] == 2 then
reverse = .1
elseif dv[1] > 0 and s.v[4] == 3 then
reverse = .1
end
reverse=0
--return Distance( s.v, sn.v ) + reverse + sn.cost
return ( sn.cost + s.cost ) / 2
end
function MakeKeyCalculator( h )
return function( s )
--for k in s do print( k ) end
--print( 'DOING', s, s.g, s.rhs )
local k2 = math.min( s.g, s.rhs )
return { k2 + h( goal, s ) + km, k2 }
end
end
function Initialize()
km = 0
start.g = math.huge
start.rhs = 0
CalculateKey = MakeKeyCalculator( Heuristic )
CompareKey = function( k1, k2 )
local kd1 = k1[1] - k2[1]
if math.abs(kd1) < 0.00001 then
kd1 = k1[2] - k2[2]
if math.abs(kd1) < 0.00001 then return 0 end
return kd1
end
return kd1
end
U = PQueue( CompareKey )
U.insert( start, CalculateKey( start ) )
map.add( goal.v, goal )
map.add( start.v, start )
end
function MinSucc( u )
local mincost = math.huge
local minsucc = nil
for s in Succ( u ) do
local cost = Cost( u, s )
if ( cost + s.g ) < mincost then
mincost = cost + s.g
minsucc = s
end
end
return minsucc, mincost
end
function UpdateCost( u, newcost )
local tmpold, tmpnew, minsucc, mincost
local oldcost = u.cost
u.cost = newcost
for s in Succ( u ) do
u.cost = oldcost
tmpold = Cost( u, s )
u.cost = newcost
tmpnew = Cost( u, s )
if Distance( u.v, start.v ) > 0 then
if tmpold > tmpnew then
u.rhs = math.min( u.rhs, tmpnew + u.g )
elseif u.rhs == (tmpold + s.g) then
minsucc, mincost = MinSucc( u )
u.rhs = mincost
u.tree = minsucc
end
end
end
UpdateVertex( u )
for s in Succ( u ) do
u.cost = oldcost
tmpold = Cost( u, s )
u.cost = newcost
tmpnew = Cost( u, s )
if Distance( s.v, start.v ) > 0 then
if tmpold > tmpnew then
s.rhs = math.min( s.rhs, tmpnew + u.g )
elseif s.rhs == ( tmpold + u.g ) then
minsucc, mincost = MinSucc( s )
s.rhs = mincost
s.tree = minsucc
end
end
UpdateVertex( s )
end
end
function UpdateVertex( u )
local cont = U.contains( u )
if u.g ~= u.rhs and cont then
U.update( u, CalculateKey( u ) )
elseif u.g ~= u.rhs and not cont then
map.add( u.v, u )
U.insert( u, CalculateKey( u ) )
elseif u.g == u.rhs and cont then
U.remove( u )
end
end
function ComputeShortestPath()
local iters = 0
dlog( 'Driving to: ' )
DN( start )
while U.topKey() and ( CompareKey( U.topKey(), CalculateKey( goal ) ) < 0 or goal.rhs ~= goal.g ) do
iters = iters + 1
local u = U.top()
local kold = U.topKey()
local knew = CalculateKey( u )
if CompareKey( kold, knew ) < 0 then
U.update( u, knew )
elseif u.g > u.rhs then
u.g = u.rhs
U.remove( u )
for s in Pred( u ) do
-- using distance as my state comparision.
-- it COULD actually be different than distance...
local c = Cost( u, s )
if Distance( s.v, start.v ) ~= 0 and s.rhs > c + u.g then
s.rhs = c + u.g
UpdateVertex( s )
s.tree = u
if u.tree == s then
pause()
end
end
end
else
local gold = u.g
u.g = math.huge
--I don't know. The pseudocode shows
--this part going over Pred() + u. But
--I didn't see that in the c implementation.
local preds = { u }
for s in Pred( u ) do
table.insert( preds, s )
end
for i, s in pairs( preds ) do
if Distance( s.v, start.v ) > 0 and s.tree == u then
updaterhs( s )
end
end
end
end
local seendis = {}
local seendat = {}
local path = {}
local cur = goal
while cur do
local k = cur.v[1]..','..cur.v[2]..','..cur.v[3]
if seendat[ cur ] or seendis[k] then
dprint( "Holy shit what's going on?" )
dprint( 'km: ', km )
LogPrintMap( map, nil, start )
panic()
end
seendis[k] = true
seendat[ cur ] = true
table.insert( path, cur )
cur = cur.tree
end
return path
end
-- try to move to v, which is move i and expected cost cost
function TestMove( v, cost, i )
local firstmove = i == 1
local isstart = Distance( start.v, VectorAdd( goal.v, v ) ) == 0
if not isstart and not firstmove and math.random() < 0.2 then
return math.huge
end
return cost
end
function TraversePath( path )
local len = table.getn( path )
local lasti = nil
local costOld
local move = onmove or TestMove
for i=1, len do
local n = path[ i ]
lasti = i
DN( n )
if i > 1 then
costOld = Cost( path[ i - 1 ], path[ i ] )
else
costOld = path[ i ].cost
end
local cost = move( VectorSub( n.v, goal.v ), n.cost, i )
if cost ~= n.cost then
dprint( 'obstacle' )
makeObstacle( n.v, map, cost )
break
end
goal = path[ i ]
end
return lasti
end
function Main()
local origGoal = goal
local last = goal
Initialize()
math.randomseed( 27 )
while start and Distance( start.v, goal.v ) > 0 do
iter = iter + 1
dprint( 'Attempt ', iter )
local now = socket.gettime()
local path = ComputeShortestPath()
dprint( 'Time to compute: ', socket.gettime() - now )
-- if start.rhs == infinity there is no known path=
local lasti = TraversePath( path )
LogPrintMap( map, last, start )
if Distance( start.v, goal.v ) > 0 then
km = km + Heuristic( last, goal )
last = goal
--for all the cells we went through...?
for i = lasti, 1, -1 do
local newcost = path[ i ].cost
path[ i ].cost = path[ i ].oldcost
UpdateCost( path[ i ], newcost )
path[ i ].oldcost = path[ i ].cost
end
end
end
end
Main()
end
updateTime = 0
removeTime = 0
if mode == 'development' then
s = {0,0,0,0}
g = {16, 15, 0, 0}
DStarLite( s, g, nil )
dprint( 'Total time in PQueue.insert: ', updateTime )
dprint( 'Total time in PQueue.remove: ', removeTime )
end