mirror of
https://github.com/Assasinnys/ED-AStar-Galaxy-Router
synced 2025-04-12 14:37:13 +03:00
feat: add multithreating support for DB version
This commit is contained in:
parent
ef739464b8
commit
e05b23db9f
@ -13,6 +13,9 @@ import elite.pojo.Coordinates
|
||||
import elite.replaces
|
||||
import elite.utils.*
|
||||
import java.sql.ResultSet
|
||||
import java.util.concurrent.Executors
|
||||
import java.util.concurrent.Future
|
||||
import java.util.concurrent.FutureTask
|
||||
|
||||
//TODO check for neutron as a second star
|
||||
//TODO check neighbors StarPoint's for better way (lower cost) [ready 50%]
|
||||
@ -27,6 +30,11 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
|
||||
private val closedList = mutableListOf<StarPoint>()
|
||||
private val stopwatch = Stopwatch()
|
||||
|
||||
private val threadPool = Executors.newFixedThreadPool(2)
|
||||
|
||||
val db1 = Database().apply { openConnection() }
|
||||
val db2 = Database().apply { openConnection() }
|
||||
|
||||
init {
|
||||
openedList.add(startStarPoint)
|
||||
// println("startStarPoint=${startStarPoint.systemId64}")
|
||||
@ -35,7 +43,8 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
|
||||
|
||||
fun activateAStarAlgorithm()/*: Pair<Int, Int>*/ {
|
||||
|
||||
findNeighbours(startStarPoint)
|
||||
// findNeighbours(startStarPoint)
|
||||
multithreatingFindNeighbours(startStarPoint)
|
||||
|
||||
if (openedList.isEmpty()) {
|
||||
println("${consoleStringCounter()}Unable to complete task. No neighbors found near startStarPoint. =(")
|
||||
@ -54,7 +63,8 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
|
||||
}
|
||||
|
||||
val selectedStarPoint = findStarPointWithMinCost()
|
||||
findNeighbours(selectedStarPoint)
|
||||
// findNeighbours(selectedStarPoint)
|
||||
multithreatingFindNeighbours(selectedStarPoint)
|
||||
openedList.remove(selectedStarPoint)
|
||||
closedList.add(selectedStarPoint)
|
||||
|
||||
@ -77,17 +87,57 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
|
||||
private fun findStarPointWithMinCost(): StarPoint {
|
||||
stopwatch.start()
|
||||
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}")
|
||||
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 findNeighbours(starPoint: StarPoint) {
|
||||
checkConnection()
|
||||
private fun multithreatingFindNeighbours(starPoint: StarPoint) {
|
||||
var firstRange = 0 to 30
|
||||
var secondRange = 30 to 60
|
||||
if (starPoint.isNeutronStar) {
|
||||
firstRange = 0 to 120
|
||||
secondRange = 120 to 240
|
||||
}
|
||||
val sql1 = "select $C_ID64, $C_X, $C_Y, $C_Z, $C_SUBTYPE = 'Neutron Star' as isNeutronStar, " +
|
||||
"$C_SYS_NAME, " +
|
||||
"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 ${firstRange.first} and ${firstRange.second}" +
|
||||
"and not $C_ID64=${starPoint.systemId64}"
|
||||
val sql2 = "select $C_ID64, $C_X, $C_Y, $C_Z, $C_SUBTYPE = 'Neutron Star' as isNeutronStar, " +
|
||||
"$C_SYS_NAME, " +
|
||||
"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 ${secondRange.first} and ${secondRange.second}" +
|
||||
"and not $C_ID64=${starPoint.systemId64}"
|
||||
|
||||
stopwatch.start()
|
||||
val resultSet = database.query(
|
||||
val taskList = mutableListOf<Future<*>>()
|
||||
taskList.add(
|
||||
threadPool.submit {
|
||||
findNeighbours(starPoint, sql1, db1)
|
||||
}
|
||||
)
|
||||
taskList.add(
|
||||
threadPool.submit {
|
||||
findNeighbours(starPoint, sql2, db2)
|
||||
}
|
||||
)
|
||||
|
||||
taskList.forEach {
|
||||
it.get()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private fun findNeighbours(starPoint: StarPoint, sql: String, db: Database) {
|
||||
// checkConnection()
|
||||
val sw = Stopwatch().apply { start() }
|
||||
// stopwatch.start()
|
||||
/*val resultSet = database.query(
|
||||
"select $C_ID64, $C_X, $C_Y, $C_Z, $C_SUBTYPE = 'Neutron Star' as isNeutronStar, " +
|
||||
"$C_SYS_NAME, " +
|
||||
"sqrt((${starPoint.coords.x}-x)^2+(${starPoint.coords.y}-y)^2+(${starPoint.coords.z}-z)^2) as dist\n" +
|
||||
@ -96,11 +146,10 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
|
||||
starPoint.isNeutronStar
|
||||
)}" +
|
||||
"and not $C_ID64=${starPoint.systemId64}"
|
||||
)
|
||||
stopwatch.stopWithConsoleOutput("Query time: ")
|
||||
stopwatch.start()
|
||||
|
||||
val sw2 = Stopwatch()
|
||||
)*/
|
||||
val resultSet = db.query(sql)
|
||||
// stopwatch.stopWithConsoleOutput("Query time: ")
|
||||
// stopwatch.start()
|
||||
|
||||
while (resultSet.next()) {
|
||||
with(resultSet) {
|
||||
@ -111,17 +160,17 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
|
||||
), getBoolean("isNeutronStar"), getDouble("dist"),
|
||||
getString(C_SYS_NAME), starPoint.jumpCounter.plus(1), finishStarPoint.coords
|
||||
)
|
||||
sw2.start()
|
||||
if (closedList.notContains(newStarPoint)) {
|
||||
// openedList.addIfAbsent(newStarPoint)
|
||||
// openedList.smartAdd(newStarPoint)
|
||||
openedList.smartAdd2(newStarPoint)
|
||||
}
|
||||
sw2.stopWithConsoleOutput("Add to openList time: ")
|
||||
|
||||
}
|
||||
}
|
||||
resultSet.close()
|
||||
stopwatch.stopWithConsoleOutput("Process time: ")
|
||||
sw.stopWithConsoleOutput("Process time: ")
|
||||
// stopwatch.stopWithConsoleOutput("Process time: ")
|
||||
}
|
||||
|
||||
private fun isNeutronDistance(isNeutron: Boolean) = if (isNeutron) NEUTRON_DISTANCE else USUAL_DISTANCE
|
||||
@ -208,6 +257,7 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
|
||||
|
||||
private fun printTheFoundPath(): Int {
|
||||
database.closeDB()
|
||||
threadPool.shutdownNow()
|
||||
var starPoint = finishStarPoint.previousStarPoint!!
|
||||
var counter = 0
|
||||
var fullDistance = 0.0
|
||||
@ -222,6 +272,8 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
|
||||
counter++
|
||||
}
|
||||
println("${consoleStringCounter()} Total jumps counter = $counter, distance = $fullDistance ly, replaces = $replaces, cof = ${StarPoint.NEUTRON_COF}")
|
||||
db1.closeDB()
|
||||
db2.closeDB()
|
||||
return counter
|
||||
}
|
||||
|
||||
@ -232,7 +284,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
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ import kotlin.system.exitProcess
|
||||
//TODO check neighbors StarPoint's for better way (lower cost) [ready 50%]
|
||||
|
||||
class AStarMainFile {
|
||||
private val coords = Coordinates(-2275.3125, -1140.5312, 1284.9688)
|
||||
/* private val coords = Coordinates(-2275.3125, -1140.5312, 1284.9688)
|
||||
private val finishStarPoint = StarPoint(null, 216165812715L, coords, true, 0.0, null, 0, coords)
|
||||
|
||||
private val startStarPoint =
|
||||
@ -146,7 +146,7 @@ class AStarMainFile {
|
||||
// db.closeDB()
|
||||
println("${consoleStringCounter()} Total jumps counter = $counter, distance = $fullDistance ly, replaces = $replaces, cof = ${StarPoint.NEUTRON_COF}")
|
||||
return counter
|
||||
}
|
||||
}*/
|
||||
|
||||
companion object {
|
||||
const val NEUTRON_DISTANCE = 240
|
||||
|
@ -56,6 +56,7 @@ fun MutableList<StarPoint>.smartAdd(newStarPoint: StarPoint) {
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun MutableList<StarPoint>.smartAdd2(newStarPoint: StarPoint) {
|
||||
if (this.notContains(newStarPoint)) {
|
||||
this.add(newStarPoint)
|
||||
|
Loading…
x
Reference in New Issue
Block a user