From 3e8d0b9c30fe40a46f870589da849958a39bf70e Mon Sep 17 00:00:00 2001 From: Assasinnys Date: Tue, 9 Jun 2020 18:56:52 +0300 Subject: [PATCH] feat: change main formula, work only with neutron stars --- src/main/kotlin/elite/algorithm/AStarMain.kt | 14 ++--- src/main/kotlin/elite/algorithm/StarPoint.kt | 4 +- .../kotlin/elite/alternative/AStarMainFile.kt | 53 ++++++++++--------- 3 files changed, 36 insertions(+), 35 deletions(-) diff --git a/src/main/kotlin/elite/algorithm/AStarMain.kt b/src/main/kotlin/elite/algorithm/AStarMain.kt index cd703f3..4eb9171 100644 --- a/src/main/kotlin/elite/algorithm/AStarMain.kt +++ b/src/main/kotlin/elite/algorithm/AStarMain.kt @@ -13,7 +13,7 @@ import elite.utils.* import java.sql.ResultSet import java.util.concurrent.Executors -//TODO check for neutron as a second star +//TODO check for neutron as a second star -> [future 0%] //TODO check neighbors StarPoint's for better way (lower cost) [ready 50%] class AStarMain(private val startSystem: String, private val finishSystem: String) { @@ -93,7 +93,7 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin "sqrt((${starPoint.coords.x}-x)^2+(${starPoint.coords.y}-y)^2+(${starPoint.coords.z}-z)^2) as dist\n" + "from $CORRIDOR\n" + "where sqrt((${starPoint.coords.x}-x)^2+(${starPoint.coords.y}-y)^2+(${starPoint.coords.z}-z)^2) " + - "between 0 and ${maxRange.plus(jumpModifier.minus(1).times(60))}" + + "between 0 and ${maxRange.plus(jumpModifier.minus(1).times(USUAL_DISTANCE))}" + "and not $C_ID64=${starPoint.systemId64}" // threadPool.submit { @@ -103,7 +103,7 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin private fun findNeighbours(starPoint: StarPoint, sql: String, jumpModifier: Int) { checkConnection() - var smartAdd = false + var isNoResult = true val sw = Stopwatch().apply { start() } val resultSet = database.query(sql) @@ -117,11 +117,11 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin val newStarPoint = StarPoint( starPoint, getLong(C_ID64), coords, isNeutronStar, getDouble("dist"), - getString(C_SYS_NAME), starPoint.jumpCounter.plus(1), finishStarPoint.coords + getString(C_SYS_NAME), starPoint.jumpCounter.plus(jumpModifier), finishStarPoint.coords ) if (closedList.notContains(newStarPoint.systemId64)) { openedList.smartAdd2(newStarPoint) - smartAdd = true + isNoResult = false } } } @@ -129,7 +129,7 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin resultSet.close() sw.stopWithConsoleOutput("Process time: ") - if (!smartAdd) { + if (isNoResult) { multithreatingFindNeighbours(starPoint, jumpModifier.plus(1)) } } @@ -232,7 +232,7 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin starPoint = starPoint.previousStarPoint!! counter++ } - println("${consoleStringCounter()} Total jumps counter = $counter, distance = $fullDistance ly, replaces = $replaces, cof = ${StarPoint.NEUTRON_COF}") + println("${consoleStringCounter()} Total jumps counter = ${finishStarPoint.previousStarPoint?.jumpCounter}, distance = $fullDistance ly, replaces = $replaces, cof = ${StarPoint.NEUTRON_COF}") return counter } diff --git a/src/main/kotlin/elite/algorithm/StarPoint.kt b/src/main/kotlin/elite/algorithm/StarPoint.kt index f6f2581..3a2e55d 100644 --- a/src/main/kotlin/elite/algorithm/StarPoint.kt +++ b/src/main/kotlin/elite/algorithm/StarPoint.kt @@ -24,7 +24,7 @@ class StarPoint( val previousCostG = previousStarPoint?.costG ?: 0.0 val costGForStarPoint = if (isNeutronStar) - DISTANCE_MODIFIER.minus(distance).minus(NEUTRON_COF) + DISTANCE_MODIFIER.div(distance) // DISTANCE_MODIFIER.div(distance).minus(NEUTRON_COF) else DISTANCE_MODIFIER.minus(distance) @@ -67,7 +67,7 @@ class StarPoint( } companion object { - const val DISTANCE_MODIFIER = 100 + const val DISTANCE_MODIFIER = 10000 // 10k const val NEUTRON_COF = 0 // Default } diff --git a/src/main/kotlin/elite/alternative/AStarMainFile.kt b/src/main/kotlin/elite/alternative/AStarMainFile.kt index 34d1e75..f6389af 100644 --- a/src/main/kotlin/elite/alternative/AStarMainFile.kt +++ b/src/main/kotlin/elite/alternative/AStarMainFile.kt @@ -6,7 +6,7 @@ import elite.replaces import elite.utils.* import java.io.File -//TODO check for neutron as a second star +//TODO check for neutron as a second star -> [future 0%] //TODO check neighbors StarPoint's for better way (lower cost) [ready 50%] class AStarMainFile { @@ -23,15 +23,16 @@ class AStarMainFile { private val stopwatch = Stopwatch() init { - openedList[startStarPoint.systemId64] = startStarPoint +// openedList[startStarPoint.systemId64] = startStarPoint + closedList[startStarPoint.systemId64] = startStarPoint } fun activateAStarAlgorithm() { - findNeighbours(startStarPoint) + findNeighbours(startStarPoint, 1) + +// openedList.remove(startStarPoint.systemId64) - openedList.remove(startStarPoint.systemId64) - closedList[startStarPoint.systemId64] = startStarPoint if (openedList.isEmpty()) { println("${consoleStringCounter()}Unable to complete task. No neighbors found near startStarPoint. =(") @@ -47,7 +48,7 @@ class AStarMainFile { } val selectedStarPoint = findStarPointWithMinCost() - findNeighbours(selectedStarPoint) + findNeighbours(selectedStarPoint, 1) openedList.remove(selectedStarPoint.systemId64) closedList[selectedStarPoint.systemId64] = selectedStarPoint @@ -73,11 +74,12 @@ class AStarMainFile { "Min cost star point: G = ${it.costG}, F = ${it.costF}, " + "dist = ${it.distance}, start = ${it.previousStarPoint == startStarPoint}" ) - stopwatch.stopWithConsoleOutput("Min cost find time: ") + stopwatch.stopWithConsoleOutput("openList size = ${openedList.size}; Min cost find time: ") } } - private fun findNeighbours(starPoint: StarPoint) { + private fun findNeighbours(starPoint: StarPoint, jumpModifier: Int) { + var isNoResult = true val reader = file.bufferedReader().lines() reader.forEach { line -> val sArray = line.split(" ") @@ -94,35 +96,34 @@ class AStarMainFile { coords.z, starPoint.coords.z ) + val maxRange = NEUTRON_DISTANCE.plus(jumpModifier.minus(1).times(USUAL_DISTANCE)) + val isNeutronStar = sArray[4].toBoolean() - if (starPoint.isNeutronStar && distance <= NEUTRON_DISTANCE) { + if ((isNeutronStar || coords == finishStarPoint.coords) && + distance <= maxRange + ) { val newStarPoint = StarPoint( starPoint, sArray[0].toLong(), coords, - sArray[4].toBoolean(), + isNeutronStar, distance, null, - starPoint.jumpCounter.plus(1), - finishStarPoint.coords - ) - if (closedList.notContains(newStarPoint.systemId64)) - openedList.smartAdd2(newStarPoint) - - } else if (!starPoint.isNeutronStar && distance <= USUAL_DISTANCE) { - val newStarPoint = StarPoint( - starPoint, sArray[0].toLong(), - coords, - sArray[4].toBoolean(), - distance, - null, - starPoint.jumpCounter.plus(1), + starPoint.jumpCounter.plus(jumpModifier), finishStarPoint.coords ) - if (closedList.notContains(newStarPoint.systemId64)) + if (closedList.notContains(newStarPoint.systemId64) && newStarPoint != starPoint) { openedList.smartAdd2(newStarPoint) + isNoResult = false + } } } + + if (isNoResult) { + reader.close() + findNeighbours(starPoint, jumpModifier.plus(1)) + } + reader.close() } @@ -146,7 +147,7 @@ class AStarMainFile { counter++ } // db.closeDB() - println("${consoleStringCounter()} Total jumps counter = $counter, distance = $fullDistance ly, replaces = $replaces, cof = ${StarPoint.NEUTRON_COF}") + println("${consoleStringCounter()} Total jumps counter = ${finishStarPoint.previousStarPoint?.jumpCounter}, distance = $fullDistance ly, replaces = $replaces, cof = ${StarPoint.NEUTRON_COF}") return counter }