Skip to content

Commit 669ebe1

Browse files
committed
Bidirectional graph initialization
1 parent 85a922e commit 669ebe1

File tree

2 files changed

+17
-35
lines changed

2 files changed

+17
-35
lines changed

common/src/main/kotlin/com/lambda/pathing/incremental/DStarLite.kt

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,9 @@ class DStarLite(
8585

8686
fun timedOut() = (System.currentTimeMillis() - startTime) > cutoffTimeout
8787

88-
// ToDo: Check why <= needed and not <
89-
fun checkCondition() = U.topKey(Key.INFINITY) <= calculateKey(start) || rhs(start) > g(start)
88+
fun checkCondition() = U.topKey(Key.INFINITY) < calculateKey(start) || rhs(start) > g(start)
9089

91-
while (!U.isEmpty() && checkCondition() && !timedOut()) {
90+
while (checkCondition() && !timedOut()) {
9291
val u = U.top() // Get node with smallest key
9392
val kOld = U.topKey(Key.INFINITY) // Key before potential update
9493
val kNew = calculateKey(u) // Recalculate key
@@ -103,11 +102,8 @@ class DStarLite(
103102
setG(u, rhs(u)) // Set g = rhs
104103
U.remove(u) // Remove from queue, now consistent (g=rhs)
105104
// Propagate change to predecessors s
106-
// ToDo: Use predecessors
107-
graph.successors(u).forEach { (s, c) ->
108-
if (s != goal) {
109-
setRHS(s, min(rhs(s), c + g(u)))
110-
}
105+
graph.predecessors(u).forEach { (s, c) ->
106+
if (s != goal) setRHS(s, min(rhs(s), graph.cost(s, u) + g(u)))
111107
updateVertex(s)
112108
}
113109
}
@@ -118,8 +114,7 @@ class DStarLite(
118114
val gOld = g(u)
119115
setG(u, INF)
120116

121-
// ToDo: Use predecessors
122-
(graph.successors(u).keys + u).forEach { s ->
117+
(graph.predecessors(u).keys + u).forEach { s ->
123118
// If rhs(s) was based on the old g(u) path cost
124119
if (rhs(s) == graph.cost(s, u) + gOld && s != goal) {
125120
// Recalculate rhs(s) based on its *current* successors' g-values
@@ -189,20 +184,11 @@ class DStarLite(
189184
fun updateEdge(u: FastVector, v: FastVector, c: Double) {
190185
val cOld = graph.cost(u, v)
191186
graph.setCost(u, v, c)
192-
// LOG.info("Setting edge ${u.string} -> ${v.string} to $c")
193-
if (cOld > c) {
194-
if (u != goal) {
195-
setRHS(u, min(rhs(u), c + g(v)))
196-
// LOG.info("Setting RHS of ${u.string} to ${rhs(u)}")
197-
}
198-
} else if (rhs(u) == cOld + g(v)) {
199-
if (u != goal) {
200-
setRHS(u, minSuccessorCost(u))
201-
// LOG.info("Setting RHS of ${u.string} to ${rhs(u)}")
202-
}
187+
when {
188+
cOld > c -> if (u != goal) setRHS(u, min(rhs(u), c + g(v)))
189+
rhs(u) == cOld + g(v) -> if (u != goal) setRHS(u, minSuccessorCost(u))
203190
}
204191
updateVertex(u)
205-
// LOG.info("Updated vertex ${u.string}")
206192
}
207193

208194
/**
@@ -249,26 +235,20 @@ class DStarLite(
249235
/** Internal key calculation using current start and km. */
250236
private fun calculateKey(s: FastVector): Key {
251237
val minGRHS = min(g(s), rhs(s))
252-
return if (minGRHS == INF) {
253-
Key.INFINITY
254-
} else {
255-
Key(minGRHS + heuristic(start, s) + km, minGRHS)
256-
}
238+
return Key(minGRHS + heuristic(start, s) + km, minGRHS)
257239
}
258240

259241
/** Updates a vertex's state in the priority queue based on its consistency (g vs rhs). */
260242
fun updateVertex(u: FastVector) {
261243
val uInQueue = u in U
262-
val key = calculateKey(u)
263-
264244
when {
265245
// Inconsistent and in Queue: Update priority
266246
g(u) != rhs(u) && uInQueue -> {
267-
U.update(u, key)
247+
U.update(u, calculateKey(u))
268248
}
269249
// Inconsistent and not in Queue: Insert
270250
g(u) != rhs(u) && !uInQueue -> {
271-
U.insert(u, key)
251+
U.insert(u, calculateKey(u))
272252
}
273253
// Consistent and in Queue: Remove
274254
g(u) == rhs(u) && uInQueue -> {

common/src/main/kotlin/com/lambda/pathing/incremental/LazyGraph.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ class LazyGraph(
5858
}
5959

6060
/** Initializes predecessors by ensuring successors of neighboring nodes. */
61-
fun predecessors(u: FastVector): Map<FastVector, Double> {
62-
successors(u)
63-
return predecessors[u] ?: emptyMap()
64-
}
61+
fun predecessors(u: FastVector): MutableMap<FastVector, Double> =
62+
predecessors.getOrPut(u) {
63+
nodeInitializer(u).onEach { (neighbor, cost) ->
64+
successors.getOrPut(neighbor) { hashMapOf() }[u] = cost
65+
}.toMutableMap()
66+
}
6567

6668
fun removeNode(u: FastVector) {
6769
successors.remove(u)

0 commit comments

Comments
 (0)