Skip to content

Commit 2a048a2

Browse files
committed
Slight code improvements
1 parent 2bf4a18 commit 2a048a2

File tree

2 files changed

+6
-14
lines changed

2 files changed

+6
-14
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class DStarLite(
103103
setG(u, rhs(u)) // Set g = rhs
104104
U.remove(u) // Remove from queue, now consistent (g=rhs)
105105
// Propagate change to predecessors s
106+
// ToDo: Use predecessors
106107
graph.successors(u).forEach { (s, c) ->
107108
if (s != goal) {
108109
setRHS(s, min(rhs(s), c + g(u)))
@@ -117,6 +118,7 @@ class DStarLite(
117118
val gOld = g(u)
118119
setG(u, INF)
119120

121+
// ToDo: Use predecessors
120122
(graph.successors(u).keys + u).forEach { s ->
121123
// If rhs(s) was based on the old g(u) path cost
122124
if (rhs(s) == graph.cost(s, u) + gOld && s != goal) {
@@ -278,11 +280,7 @@ class DStarLite(
278280

279281
/** Computes min_{s' in Succ(s)} (c(s, s') + g(s')). */
280282
private fun minSuccessorCost(s: FastVector) =
281-
graph.successors(s)
282-
.mapNotNull { (s1, cost) ->
283-
if (cost == INF) null else cost + g(s1)
284-
}
285-
.minOrNull() ?: INF
283+
graph.successors(s).minOfOrNull { (s1, cost) -> cost + g(s1) } ?: INF
286284

287285
fun buildDebugInfoRenderer(config: PathingSettings) {
288286
if (!config.renderGraph) return

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,15 @@ package com.lambda.pathing.dstar
2020
/**
2121
* Represents the Key used in the D* Lite algorithm.
2222
* It's a pair of comparable values, typically Doubles or Ints.
23-
* Comparison is done lexicographically as described in Field D*[cite: 142].
23+
* Comparison is done lexicographically as described in Field D*.
2424
*/
2525
data class Key(val first: Double, val second: Double) : Comparable<Key> {
26-
override fun compareTo(other: Key): Int {
27-
val firstCompare = this.first.compareTo(other.first)
28-
if (firstCompare != 0) {
29-
return firstCompare
30-
}
31-
return this.second.compareTo(other.second)
32-
}
26+
override fun compareTo(other: Key) =
27+
compareValuesBy(this, other, { it.first }, { it.second })
3328

3429
override fun toString() = "(%.3f, %.3f)".format(first, second)
3530

3631
companion object {
37-
// Represents an infinite key
3832
val INFINITY = Key(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY)
3933
}
4034
}

0 commit comments

Comments
 (0)