Skip to content

Commit cc433cb

Browse files
committed
Vertex invalidation approach
1 parent a6f39f6 commit cc433cb

File tree

4 files changed

+55
-32
lines changed

4 files changed

+55
-32
lines changed

common/src/main/kotlin/com/lambda/command/commands/PathCommand.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,18 @@ import com.lambda.util.world.string
3131

3232
object PathCommand : LambdaCommand(
3333
name = "path",
34-
usage = "path <markDirty>",
34+
usage = "path <invalidate | reset | update>",
3535
description = "Move through world"
3636
) {
3737
override fun CommandBuilder.create() {
38-
required(literal("markDirty")) {
38+
required(literal("invalidate")) {
3939
required(integer("X", -30000000, 30000000)) { x ->
4040
required(integer("Y", -64, 255)) { y ->
4141
required(integer("Z", -30000000, 30000000)) { z ->
4242
execute {
4343
val dirty = fastVectorOf(x().value(), y().value(), z().value())
44-
Pathfinder.graph.markDirty(dirty)
45-
// Pathfinder.dStar.updateGraph()
46-
this@PathCommand.info("Marked ${dirty.string} as dirty")
44+
Pathfinder.dStar.invalidate(dirty)
45+
this@PathCommand.info("Invalidated ${dirty.string}")
4746
}
4847
}
4948
}

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ class DStarLite(
7777
U.insertOrUpdate(goal, calculateKey(goal))
7878
}
7979

80-
private fun g(u: FastVector): Double = gMap[u] ?: INF
80+
fun g(u: FastVector): Double = gMap[u] ?: INF
8181
private fun setG(u: FastVector, g: Double) { gMap[u] = g }
8282

83-
private fun rhs(u: FastVector): Double = rhsMap[u] ?: INF
83+
fun rhs(u: FastVector): Double = rhsMap[u] ?: INF
8484
private fun setRHS(u: FastVector, rhs: Double) { rhsMap[u] = rhs }
8585

8686
/**
@@ -100,7 +100,7 @@ class DStarLite(
100100
*
101101
* Then u is removed from the queue and reinserted if it is inconsistent.
102102
*/
103-
fun updateVertex(u: FastVector) {
103+
private fun updateVertex(u: FastVector) {
104104
if (u != goal) {
105105
var tmp = INF
106106
graph.predecessors(u).forEach { (pred, cost) ->
@@ -117,15 +117,12 @@ class DStarLite(
117117
}
118118
}
119119

120-
fun updateGraph() {
121-
graph.dirtyNodes.forEach { u ->
122-
if (u != goal) {
123-
gMap.remove(u)
124-
rhsMap.remove(u)
125-
}
126-
updateVertex(u)
120+
fun invalidate(u: FastVector) {
121+
val affectedNodes = graph.getNeighbors(u) + u
122+
affectedNodes.forEach { v ->
123+
graph.invalidate(v)
124+
updateVertex(v)
127125
}
128-
graph.dirtyNodes.clear()
129126
}
130127

131128
/**
@@ -265,6 +262,17 @@ class DStarLite(
265262
}
266263
}
267264

265+
override fun toString() = buildString {
266+
appendLine("Nodes:")
267+
graph.nodes.forEach { appendLine(" ${it.string} g: ${g(it)} rhs: ${rhs(it)}") }
268+
appendLine("Edges:")
269+
graph.nodes.forEach { u ->
270+
graph.successors(u).forEach { (v, cost) ->
271+
appendLine(" ${u.string} -> ${v.string}: c: $cost")
272+
}
273+
}
274+
}
275+
268276
companion object {
269277
private const val INF = Double.POSITIVE_INFINITY
270278
}

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,18 @@ class LazyGraph(
7575
return predecessors[u] ?: emptyMap()
7676
}
7777

78-
fun remove(u: FastVector) {
79-
successors.remove(u)
80-
successors.values.forEach { it.remove(u) }
81-
predecessors.remove(u)
82-
predecessors.values.forEach { it.remove(u) }
78+
fun invalidate(u: FastVector) {
79+
val neighbors = getNeighbors(u)
80+
(neighbors + u).forEach { v ->
81+
successors.remove(v)
82+
predecessors.remove(v)
83+
predecessors.values.forEach { predMap ->
84+
predMap.remove(v)
85+
}
86+
}
8387
}
8488

85-
fun markDirty(u: FastVector) {
86-
dirtyNodes.add(u)
87-
val preds = predecessors[u]?.keys ?: emptySet()
88-
val succs = successors[u]?.keys ?: emptySet()
89-
remove(u)
90-
preds.forEach { remove(it) }
91-
succs.forEach { remove(it) }
92-
dirtyNodes.addAll(preds)
93-
dirtyNodes.addAll(succs)
94-
}
89+
fun getNeighbors(u: FastVector): Set<FastVector> = successors(u).keys + predecessors(u).keys
9590

9691
/** Returns the cost of the edge from u to v (or ∞ if none exists) */
9792
fun cost(u: FastVector, v: FastVector): Double = successors(u)[v] ?: Double.POSITIVE_INFINITY

common/src/test/kotlin/DStarLiteTest.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import com.lambda.pathing.dstar.DStarLite
22
import com.lambda.pathing.dstar.LazyGraph
33
import com.lambda.util.world.FastVector
44
import com.lambda.util.world.fastVectorOf
5+
import com.lambda.util.world.string
6+
import com.lambda.util.world.toBlockPos
57
import com.lambda.util.world.x
68
import com.lambda.util.world.y
79
import com.lambda.util.world.z
@@ -71,7 +73,7 @@ class DStarLiteTest {
7173
}
7274

7375
@Test
74-
fun testUpdateStart3D() {
76+
fun testUpdateStart() {
7577
val graph = createLazy3DGridGraph()
7678
var start = fastVectorOf(0, 0, 0)
7779
val goal = fastVectorOf(2, 2, 2)
@@ -91,4 +93,23 @@ class DStarLiteTest {
9193
assertEquals(start, updatedPath.first(), "Updated path must start at updated position")
9294
assertEquals(goal, updatedPath.last(), "Updated path must end at the goal")
9395
}
96+
97+
@Test
98+
fun testInvalidateVertex() {
99+
val graph = createLazy3DGridGraph()
100+
val start = fastVectorOf(0, 0, 0)
101+
val goal = fastVectorOf(2, 2, 2)
102+
val dstar = DStarLite(graph, start, goal, ::heuristic)
103+
fun List<FastVector>.string() = joinToString(" -> ") { "[${it.string} g=${dstar.g(it)} rhs=${dstar.rhs(it)}]" }
104+
105+
println("Computing shortest path from ${start.string} to ${goal.string}")
106+
dstar.computeShortestPath()
107+
println(dstar.path().string())
108+
val invalidate = fastVectorOf(1, 0, 0)
109+
println("Invalidating vertex ${invalidate.string}")
110+
dstar.invalidate(invalidate)
111+
println("Computing shortest path from ${start.string} to ${goal.string}")
112+
dstar.computeShortestPath()
113+
println(dstar.path().string())
114+
}
94115
}

0 commit comments

Comments
 (0)