Skip to content

Commit d1d2302

Browse files
committed
Simplify invalidation
1 parent df12980 commit d1d2302

File tree

2 files changed

+15
-36
lines changed

2 files changed

+15
-36
lines changed

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

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,12 @@ import com.lambda.graphics.gl.Matrices.buildWorldProjection
2222
import com.lambda.graphics.gl.Matrices.withVertexTransform
2323
import com.lambda.graphics.renderer.gui.FontRenderer
2424
import com.lambda.graphics.renderer.gui.FontRenderer.drawString
25-
import com.lambda.module.modules.movement.Pathfinder
2625
import com.lambda.pathing.PathingSettings
2726
import com.lambda.util.math.Vec2d
28-
import com.lambda.util.math.div
2927
import com.lambda.util.math.minus
3028
import com.lambda.util.math.plus
3129
import com.lambda.util.math.times
3230
import com.lambda.util.world.FastVector
33-
import com.lambda.util.world.add
34-
import com.lambda.util.world.fastVectorOf
3531
import com.lambda.util.world.string
3632
import com.lambda.util.world.toCenterVec3d
3733
import kotlin.math.min
@@ -151,30 +147,18 @@ class DStarLite(
151147
*/
152148
fun invalidate(u: FastVector) {
153149
graph.neighbors(u).forEach { v ->
154-
graph.invalidated.add(v)
155-
updateEdge(u, v, INF)
156-
updateEdge(v, u, INF)
157-
}
158-
graph.invalidated.forEach { v ->
159-
val currentConnections = graph.successors(v)
160-
val actualConnections = graph.initialize(v)
161-
val changedConnections = currentConnections.filter { (succ, cost) -> cost != actualConnections[succ] }
162-
val newConnections = actualConnections.filter { (succ, _) -> succ !in currentConnections }
163-
val removedConnections = currentConnections.filter { (succ, _) -> succ !in actualConnections }
164-
changedConnections.forEach { (succ, cost) ->
165-
updateEdge(v, succ, cost)
166-
updateEdge(succ, v, cost)
167-
}
168-
newConnections.forEach { (succ, cost) ->
169-
updateEdge(v, succ, cost)
170-
updateEdge(succ, v, cost)
150+
val current = graph.successors(v)
151+
val updated = graph.nodeInitializer(v)
152+
val removed = current.filter { (w, _) -> w !in updated }
153+
removed.forEach { (w, _) ->
154+
updateEdge(v, w, INF)
155+
updateEdge(w, v, INF)
171156
}
172-
removedConnections.forEach { (succ, _) ->
173-
updateEdge(v, succ, INF)
174-
updateEdge(succ, v, INF)
157+
updated.forEach { (w, c) ->
158+
updateEdge(v, w, c)
159+
updateEdge(w, v, c)
175160
}
176161
}
177-
graph.invalidated.clear()
178162
}
179163

180164
/**
@@ -194,10 +178,6 @@ class DStarLite(
194178
if (u != goal) setRHS(u, minSuccessorCost(u))
195179
}
196180
updateVertex(u)
197-
// if (c == INF) {
198-
// graph.removeEdge(u, v)
199-
// graph.removeEdge(v, u)
200-
// }
201181
}
202182

203183
/**

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import java.util.concurrent.ConcurrentHashMap
4141
* - Additional memory overhead is based on the dynamically expanding hash maps.
4242
*/
4343
class LazyGraph(
44-
private val nodeInitializer: (FastVector) -> Map<FastVector, Double>
44+
val nodeInitializer: (FastVector) -> Map<FastVector, Double>
4545
) {
4646
val successors = ConcurrentHashMap<FastVector, MutableMap<FastVector, Double>>()
4747
val predecessors = ConcurrentHashMap<FastVector, MutableMap<FastVector, Double>>()
@@ -52,7 +52,11 @@ class LazyGraph(
5252

5353
/** Initializes a node if not already initialized, then returns successors. */
5454
fun successors(u: FastVector): MutableMap<FastVector, Double> =
55-
successors.getOrPut(u) { initialize(u).toMutableMap() }
55+
successors.getOrPut(u) {
56+
nodeInitializer(u).onEach { (neighbor, cost) ->
57+
predecessors.getOrPut(neighbor) { hashMapOf() }[u] = cost
58+
}.toMutableMap()
59+
}
5660

5761
/** Initializes predecessors by ensuring successors of neighboring nodes. */
5862
fun predecessors(u: FastVector): Map<FastVector, Double> {
@@ -78,11 +82,6 @@ class LazyGraph(
7882
}
7983
}
8084

81-
fun initialize(u: FastVector) =
82-
nodeInitializer(u).onEach { (neighbor, cost) ->
83-
predecessors.getOrPut(neighbor) { hashMapOf() }[u] = cost
84-
}
85-
8685
fun setCost(u: FastVector, v: FastVector, c: Double) {
8786
successors[u]?.put(v, c)
8887
predecessors[v]?.put(u, c)

0 commit comments

Comments
 (0)