mirror of
https://github.com/Assasinnys/ED-AStar-Galaxy-Router
synced 2025-04-13 06:47:13 +03:00
fix: increase performance by using HashMap instead MutableList
This commit is contained in:
parent
ed7e7f46ff
commit
1aaf6c9da4
@ -26,14 +26,15 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
|
|||||||
private val startStarPoint: StarPoint = createStartStarPoint()
|
private val startStarPoint: StarPoint = createStartStarPoint()
|
||||||
|
|
||||||
|
|
||||||
private val openedList = mutableListOf<StarPoint>()
|
private val openedList = /*mutableListOf<StarPoint>()*/ hashMapOf<Long, StarPoint>()
|
||||||
private val closedList = mutableListOf<StarPoint>()
|
private val closedList = /*mutableListOf<StarPoint>()*/ hashMapOf<Long, StarPoint>()
|
||||||
private val stopwatch = Stopwatch()
|
private val stopwatch = Stopwatch()
|
||||||
|
|
||||||
private val threadPool = Executors.newSingleThreadExecutor()
|
private val threadPool = Executors.newSingleThreadExecutor()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
openedList.add(startStarPoint)
|
// openedList.add(startStarPoint)
|
||||||
|
openedList[startStarPoint.systemId64] = startStarPoint
|
||||||
// println("startStarPoint=${startStarPoint.systemId64}")
|
// println("startStarPoint=${startStarPoint.systemId64}")
|
||||||
// println("finishStarPoint=${finishStarPoint.systemId64}")
|
// println("finishStarPoint=${finishStarPoint.systemId64}")
|
||||||
}
|
}
|
||||||
@ -47,8 +48,9 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
openedList.remove(startStarPoint)
|
// openedList.remove(startStarPoint)
|
||||||
closedList.add(startStarPoint)
|
openedList.remove(startStarPoint.systemId64)
|
||||||
|
closedList[startStarPoint.systemId64] = startStarPoint
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
|
||||||
@ -60,8 +62,9 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
|
|||||||
|
|
||||||
val selectedStarPoint = findStarPointWithMinCost()
|
val selectedStarPoint = findStarPointWithMinCost()
|
||||||
multithreatingFindNeighbours(selectedStarPoint)
|
multithreatingFindNeighbours(selectedStarPoint)
|
||||||
openedList.remove(selectedStarPoint)
|
// openedList.remove(selectedStarPoint)
|
||||||
closedList.add(selectedStarPoint)
|
openedList.remove(selectedStarPoint.systemId64)
|
||||||
|
closedList[selectedStarPoint.systemId64] = selectedStarPoint
|
||||||
|
|
||||||
} while (openedList.isNotEmpty())
|
} while (openedList.isNotEmpty())
|
||||||
|
|
||||||
@ -70,24 +73,37 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun checkForFinish(): Boolean {
|
private fun checkForFinish(): Boolean {
|
||||||
openedList.forEach { point ->
|
if (openedList.containsKey(finishStarPoint.systemId64)) {
|
||||||
if (point == finishStarPoint) {
|
finishStarPoint.previousStarPoint = openedList[finishStarPoint.systemId64]
|
||||||
finishStarPoint.previousStarPoint = point
|
return true
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// openedList.forEach { id64, point ->
|
||||||
|
// if (point == finishStarPoint) {
|
||||||
|
// finishStarPoint.previousStarPoint = point
|
||||||
|
// return true
|
||||||
|
// }
|
||||||
|
// }
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun findStarPointWithMinCost(): StarPoint {
|
private fun findStarPointWithMinCost(): StarPoint {
|
||||||
stopwatch.start()
|
stopwatch.start()
|
||||||
return openedList.minBy { starPoint -> starPoint.costF }!!.also { nextStarPoint ->
|
return openedList.minBy {
|
||||||
|
it.value.costF
|
||||||
|
}!!.value.also {
|
||||||
println(
|
println(
|
||||||
"Min cost star point: G = ${nextStarPoint.costG}, F = ${nextStarPoint.costF}, " +
|
"Min cost star point: G = ${it.costG}, F = ${it.costF}, " +
|
||||||
"dist = ${nextStarPoint.distance}, start = ${nextStarPoint.previousStarPoint == startStarPoint}"
|
"dist = ${it.distance}, start = ${it.previousStarPoint == startStarPoint}"
|
||||||
)
|
)
|
||||||
stopwatch.stopWithConsoleOutput("Min cost find time: ")
|
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) {
|
private fun multithreatingFindNeighbours(starPoint: StarPoint) {
|
||||||
@ -119,7 +135,7 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
|
|||||||
), getBoolean("isNeutronStar"), getDouble("dist"),
|
), getBoolean("isNeutronStar"), getDouble("dist"),
|
||||||
getString(C_SYS_NAME), starPoint.jumpCounter.plus(1), finishStarPoint.coords
|
getString(C_SYS_NAME), starPoint.jumpCounter.plus(1), finishStarPoint.coords
|
||||||
)
|
)
|
||||||
if (closedList.notContains(newStarPoint)) {
|
if (closedList.notContains(newStarPoint.systemId64)) {
|
||||||
// openedList.addIfAbsent(newStarPoint)
|
// openedList.addIfAbsent(newStarPoint)
|
||||||
// openedList.smartAdd(newStarPoint)
|
// openedList.smartAdd(newStarPoint)
|
||||||
openedList.smartAdd2(newStarPoint)
|
openedList.smartAdd2(newStarPoint)
|
||||||
@ -240,7 +256,7 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
|
|||||||
)
|
)
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val CORRIDOR = "coridor3"
|
const val CORRIDOR = "main"
|
||||||
const val NEUTRON_DISTANCE = 240
|
const val NEUTRON_DISTANCE = 240
|
||||||
const val USUAL_DISTANCE = 60
|
const val USUAL_DISTANCE = 60
|
||||||
}
|
}
|
||||||
|
@ -42,33 +42,33 @@ fun <T> MutableList<T>.addIfAbsent(element: T) {
|
|||||||
this.add(element)
|
this.add(element)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun MutableList<StarPoint>.smartAdd(newStarPoint: StarPoint) {
|
fun HashMap<Long, StarPoint>.smartAdd(newStarPoint: StarPoint) {
|
||||||
if (this.notContains(newStarPoint)) {
|
if (this.notContains(newStarPoint.systemId64)) {
|
||||||
this.add(newStarPoint)
|
this[newStarPoint.systemId64] = newStarPoint
|
||||||
} else {
|
} 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")
|
// println("${consoleStringCounter()} Old star point G -> ${oldStarPoint.costG} vs ${newStarPoint.costG} <- new star point G")
|
||||||
if (oldStarPoint.costG > newStarPoint.costG) {
|
if (oldStarPoint.costG > newStarPoint.costG) {
|
||||||
this.remove(oldStarPoint)
|
this.remove(oldStarPoint.systemId64)
|
||||||
this.add(newStarPoint)
|
this[newStarPoint.systemId64] = newStarPoint
|
||||||
replaces = replaces.plus(1)
|
replaces = replaces.plus(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun MutableList<StarPoint>.smartAdd2(newStarPoint: StarPoint) {
|
fun HashMap<Long, StarPoint>.smartAdd2(newStarPoint: StarPoint) {
|
||||||
if (this.notContains(newStarPoint)) {
|
if (this.notContains(newStarPoint.systemId64)) {
|
||||||
this.add(newStarPoint)
|
this[newStarPoint.systemId64] = newStarPoint
|
||||||
} else {
|
} 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")
|
// println("${consoleStringCounter()} Old star point jumps -> ${oldStarPoint.jumpCounter} vs ${newStarPoint.jumpCounter} <- new star point jumps")
|
||||||
if (oldStarPoint.jumpCounter > newStarPoint.jumpCounter) {
|
if (oldStarPoint.jumpCounter > newStarPoint.jumpCounter) {
|
||||||
this.remove(oldStarPoint)
|
this.remove(oldStarPoint.systemId64)
|
||||||
this.add(newStarPoint)
|
this[newStarPoint.systemId64] = newStarPoint
|
||||||
replaces = replaces.plus(1)
|
replaces = replaces.plus(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> MutableList<T>.notContains(element: T) = !this.contains(element)
|
fun <K, V> HashMap<K, V>.notContains(key: K) = !this.contains(key)
|
Loading…
x
Reference in New Issue
Block a user