From 1aaf6c9da43a9195904dd1d26702421b5d48c0f6 Mon Sep 17 00:00:00 2001 From: Assasinnys Date: Wed, 13 May 2020 18:29:21 +0300 Subject: [PATCH] fix: increase performance by using HashMap instead MutableList --- src/main/kotlin/elite/algorithm/AStarMain.kt | 50 +++++++++++++------- src/main/kotlin/elite/utils/ExtUtils.kt | 26 +++++----- 2 files changed, 46 insertions(+), 30 deletions(-) diff --git a/src/main/kotlin/elite/algorithm/AStarMain.kt b/src/main/kotlin/elite/algorithm/AStarMain.kt index 6bce7d0..acb1737 100644 --- a/src/main/kotlin/elite/algorithm/AStarMain.kt +++ b/src/main/kotlin/elite/algorithm/AStarMain.kt @@ -26,14 +26,15 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin private val startStarPoint: StarPoint = createStartStarPoint() - private val openedList = mutableListOf() - private val closedList = mutableListOf() + private val openedList = /*mutableListOf()*/ hashMapOf() + private val closedList = /*mutableListOf()*/ hashMapOf() private val stopwatch = Stopwatch() private val threadPool = Executors.newSingleThreadExecutor() init { - openedList.add(startStarPoint) +// openedList.add(startStarPoint) + openedList[startStarPoint.systemId64] = startStarPoint // println("startStarPoint=${startStarPoint.systemId64}") // println("finishStarPoint=${finishStarPoint.systemId64}") } @@ -47,8 +48,9 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin return } - openedList.remove(startStarPoint) - closedList.add(startStarPoint) +// openedList.remove(startStarPoint) + openedList.remove(startStarPoint.systemId64) + closedList[startStarPoint.systemId64] = startStarPoint do { @@ -60,8 +62,9 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin val selectedStarPoint = findStarPointWithMinCost() multithreatingFindNeighbours(selectedStarPoint) - openedList.remove(selectedStarPoint) - closedList.add(selectedStarPoint) +// openedList.remove(selectedStarPoint) + openedList.remove(selectedStarPoint.systemId64) + closedList[selectedStarPoint.systemId64] = selectedStarPoint } while (openedList.isNotEmpty()) @@ -70,24 +73,37 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin } private fun checkForFinish(): Boolean { - openedList.forEach { point -> - if (point == finishStarPoint) { - finishStarPoint.previousStarPoint = point - return true - } + if (openedList.containsKey(finishStarPoint.systemId64)) { + finishStarPoint.previousStarPoint = openedList[finishStarPoint.systemId64] + return true } +// openedList.forEach { id64, point -> +// if (point == finishStarPoint) { +// finishStarPoint.previousStarPoint = point +// return true +// } +// } return false } private fun findStarPointWithMinCost(): StarPoint { stopwatch.start() - return openedList.minBy { starPoint -> starPoint.costF }!!.also { nextStarPoint -> + return openedList.minBy { + it.value.costF + }!!.value.also { println( - "Min cost star point: G = ${nextStarPoint.costG}, F = ${nextStarPoint.costF}, " + - "dist = ${nextStarPoint.distance}, start = ${nextStarPoint.previousStarPoint == startStarPoint}" + "Min cost star point: G = ${it.costG}, F = ${it.costF}, " + + "dist = ${it.distance}, start = ${it.previousStarPoint == startStarPoint}" ) stopwatch.stopWithConsoleOutput("Min cost find time: ") } +// return openedList.minBy { starPoint -> starPoint.costF }!!.also { nextStarPoint -> +// println( +// "Min cost star point: G = ${nextStarPoint.costG}, F = ${nextStarPoint.costF}, " + +// "dist = ${nextStarPoint.distance}, start = ${nextStarPoint.previousStarPoint == startStarPoint}" +// ) +// stopwatch.stopWithConsoleOutput("Min cost find time: ") +// } } private fun multithreatingFindNeighbours(starPoint: StarPoint) { @@ -119,7 +135,7 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin ), getBoolean("isNeutronStar"), getDouble("dist"), getString(C_SYS_NAME), starPoint.jumpCounter.plus(1), finishStarPoint.coords ) - if (closedList.notContains(newStarPoint)) { + if (closedList.notContains(newStarPoint.systemId64)) { // openedList.addIfAbsent(newStarPoint) // openedList.smartAdd(newStarPoint) openedList.smartAdd2(newStarPoint) @@ -240,7 +256,7 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin ) companion object { - const val CORRIDOR = "coridor3" + const val CORRIDOR = "main" const val NEUTRON_DISTANCE = 240 const val USUAL_DISTANCE = 60 } diff --git a/src/main/kotlin/elite/utils/ExtUtils.kt b/src/main/kotlin/elite/utils/ExtUtils.kt index 29cf2ff..452aaad 100644 --- a/src/main/kotlin/elite/utils/ExtUtils.kt +++ b/src/main/kotlin/elite/utils/ExtUtils.kt @@ -42,33 +42,33 @@ fun MutableList.addIfAbsent(element: T) { this.add(element) } -fun MutableList.smartAdd(newStarPoint: StarPoint) { - if (this.notContains(newStarPoint)) { - this.add(newStarPoint) +fun HashMap.smartAdd(newStarPoint: StarPoint) { + if (this.notContains(newStarPoint.systemId64)) { + this[newStarPoint.systemId64] = newStarPoint } else { - val oldStarPoint = this[this.indexOf(newStarPoint)] + val oldStarPoint = this[newStarPoint.systemId64]!! // println("${consoleStringCounter()} Old star point G -> ${oldStarPoint.costG} vs ${newStarPoint.costG} <- new star point G") if (oldStarPoint.costG > newStarPoint.costG) { - this.remove(oldStarPoint) - this.add(newStarPoint) + this.remove(oldStarPoint.systemId64) + this[newStarPoint.systemId64] = newStarPoint replaces = replaces.plus(1) } } } @Synchronized -fun MutableList.smartAdd2(newStarPoint: StarPoint) { - if (this.notContains(newStarPoint)) { - this.add(newStarPoint) +fun HashMap.smartAdd2(newStarPoint: StarPoint) { + if (this.notContains(newStarPoint.systemId64)) { + this[newStarPoint.systemId64] = newStarPoint } else { - val oldStarPoint = this[this.indexOf(newStarPoint)] + val oldStarPoint = this[newStarPoint.systemId64]!! // println("${consoleStringCounter()} Old star point jumps -> ${oldStarPoint.jumpCounter} vs ${newStarPoint.jumpCounter} <- new star point jumps") if (oldStarPoint.jumpCounter > newStarPoint.jumpCounter) { - this.remove(oldStarPoint) - this.add(newStarPoint) + this.remove(oldStarPoint.systemId64) + this[newStarPoint.systemId64] = newStarPoint replaces = replaces.plus(1) } } } -fun MutableList.notContains(element: T) = !this.contains(element) \ No newline at end of file +fun HashMap.notContains(key: K) = !this.contains(key) \ No newline at end of file