Significantly improve EnergyCableGraph#getPath performance
This commit is contained in:
parent
5e844095ef
commit
2d1925e749
@ -8,15 +8,16 @@ import ru.dbotthepony.mc.otm.core.ifPresentK
|
|||||||
import ru.dbotthepony.mc.otm.core.math.Decimal
|
import ru.dbotthepony.mc.otm.core.math.Decimal
|
||||||
import ru.dbotthepony.mc.otm.core.math.RelativeSide
|
import ru.dbotthepony.mc.otm.core.math.RelativeSide
|
||||||
import ru.dbotthepony.mc.otm.graph.GraphNodeList
|
import ru.dbotthepony.mc.otm.graph.GraphNodeList
|
||||||
|
import java.util.PriorityQueue
|
||||||
import kotlin.math.ln
|
import kotlin.math.ln
|
||||||
|
|
||||||
class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableGraph>() {
|
class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableGraph>() {
|
||||||
val livelyNodes = ObjectOpenHashSet<EnergyCableBlockEntity.Node>()
|
val livelyNodes = HashSet<EnergyCableBlockEntity.Node>()
|
||||||
|
|
||||||
private val pathCache = Object2ObjectOpenHashMap<Pair<EnergyCableBlockEntity.Node, EnergyCableBlockEntity.Node>, Decimal?>()
|
private val pathCache = HashMap<Pair<EnergyCableBlockEntity.Node, EnergyCableBlockEntity.Node>, Decimal?>()
|
||||||
|
|
||||||
private class SearchNode(val node: EnergyCableBlockEntity.Node, target: EnergyCableBlockEntity.Node, var parent: SearchNode? = null) : Comparable<SearchNode> {
|
private class SearchNode(val node: EnergyCableBlockEntity.Node, target: EnergyCableBlockEntity.Node, var parent: SearchNode? = null) : Comparable<SearchNode> {
|
||||||
var heuristics: Double = node.position.distSqr(target.position) * 0.0001 - ln(node.energyThroughput.coerceAtMost(Decimal.LONG_MAX_VALUE).toDouble())
|
val heuristics: Double = node.position.distSqr(target.position) * 0.0001 - ln(node.energyThroughput.coerceAtMost(Decimal.LONG_MAX_VALUE).toDouble())
|
||||||
|
|
||||||
override fun compareTo(other: SearchNode): Int {
|
override fun compareTo(other: SearchNode): Int {
|
||||||
return heuristics.compareTo(other.heuristics)
|
return heuristics.compareTo(other.heuristics)
|
||||||
@ -39,13 +40,13 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
|
|||||||
// no free paths available, try to find extra one
|
// no free paths available, try to find extra one
|
||||||
// while this use A* algorithm, this is done purely for biasing search towards end point (to speed up search),
|
// while this use A* algorithm, this is done purely for biasing search towards end point (to speed up search),
|
||||||
// on small cable networks simple flooding will do just fine, if we consider overloaded cables as closed flood gates
|
// on small cable networks simple flooding will do just fine, if we consider overloaded cables as closed flood gates
|
||||||
val openNodes = ArrayList<SearchNode>()
|
val openNodes = PriorityQueue<SearchNode>()
|
||||||
val seenNodes = ObjectOpenHashSet<EnergyCableBlockEntity.Node>()
|
val seenNodes = HashSet<EnergyCableBlockEntity.Node>()
|
||||||
|
|
||||||
openNodes.add(SearchNode(a, b))
|
openNodes.add(SearchNode(a, b))
|
||||||
|
|
||||||
while (openNodes.isNotEmpty()) {
|
while (openNodes.isNotEmpty()) {
|
||||||
val first = openNodes.min()
|
val first = openNodes.remove()
|
||||||
openNodes.remove(first)
|
openNodes.remove(first)
|
||||||
|
|
||||||
if (first.node === b) {
|
if (first.node === b) {
|
||||||
|
Loading…
Reference in New Issue
Block a user