mirror of
https://github.com/Assasinnys/ED-AStar-Galaxy-Router
synced 2025-04-13 06:47:13 +03:00
fix: change thread pool to singleThreadExecutor
This commit is contained in:
parent
0874eff533
commit
ed7e7f46ff
@ -30,21 +30,16 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
|
|||||||
private val closedList = mutableListOf<StarPoint>()
|
private val closedList = mutableListOf<StarPoint>()
|
||||||
private val stopwatch = Stopwatch()
|
private val stopwatch = Stopwatch()
|
||||||
|
|
||||||
private val threadPool = Executors.newFixedThreadPool(NUM_OF_THREADS)
|
private val threadPool = Executors.newSingleThreadExecutor()
|
||||||
private val dbList = mutableListOf<Database>()
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
openedList.add(startStarPoint)
|
openedList.add(startStarPoint)
|
||||||
repeat(NUM_OF_THREADS) {
|
|
||||||
dbList.add(Database().apply { openConnection() })
|
|
||||||
}
|
|
||||||
// println("startStarPoint=${startStarPoint.systemId64}")
|
// println("startStarPoint=${startStarPoint.systemId64}")
|
||||||
// println("finishStarPoint=${finishStarPoint.systemId64}")
|
// println("finishStarPoint=${finishStarPoint.systemId64}")
|
||||||
}
|
}
|
||||||
|
|
||||||
fun activateAStarAlgorithm()/*: Pair<Int, Int>*/ {
|
fun activateAStarAlgorithm() {
|
||||||
|
|
||||||
// findNeighbours(startStarPoint)
|
|
||||||
multithreatingFindNeighbours(startStarPoint)
|
multithreatingFindNeighbours(startStarPoint)
|
||||||
|
|
||||||
if (openedList.isEmpty()) {
|
if (openedList.isEmpty()) {
|
||||||
@ -64,7 +59,6 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
val selectedStarPoint = findStarPointWithMinCost()
|
val selectedStarPoint = findStarPointWithMinCost()
|
||||||
// findNeighbours(selectedStarPoint)
|
|
||||||
multithreatingFindNeighbours(selectedStarPoint)
|
multithreatingFindNeighbours(selectedStarPoint)
|
||||||
openedList.remove(selectedStarPoint)
|
openedList.remove(selectedStarPoint)
|
||||||
closedList.add(selectedStarPoint)
|
closedList.add(selectedStarPoint)
|
||||||
@ -97,60 +91,24 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun multithreatingFindNeighbours(starPoint: StarPoint) {
|
private fun multithreatingFindNeighbours(starPoint: StarPoint) {
|
||||||
val range = if (starPoint.isNeutronStar)
|
val maxRange = isNeutronDistance(starPoint.isNeutronStar)
|
||||||
NEUTRON_DISTANCE.div(NUM_OF_THREADS)
|
val sql = "select $C_ID64, $C_X, $C_Y, $C_Z, $C_SUBTYPE = 'Neutron Star' as isNeutronStar, " +
|
||||||
else
|
"$C_SYS_NAME, " +
|
||||||
USUAL_DISTANCE.div(NUM_OF_THREADS)
|
"sqrt((${starPoint.coords.x}-x)^2+(${starPoint.coords.y}-y)^2+(${starPoint.coords.z}-z)^2) as dist\n" +
|
||||||
val sqlList = mutableListOf<String>()
|
"from $CORRIDOR\n" +
|
||||||
val taskList = mutableListOf<Future<*>>()
|
"where sqrt((${starPoint.coords.x}-x)^2+(${starPoint.coords.y}-y)^2+(${starPoint.coords.z}-z)^2) between 0 and $maxRange" +
|
||||||
val maxJump = if (starPoint.isNeutronStar)
|
"and not $C_ID64=${starPoint.systemId64}"
|
||||||
NEUTRON_DISTANCE
|
|
||||||
else
|
|
||||||
USUAL_DISTANCE
|
|
||||||
|
|
||||||
for (i in 0 until maxJump step range) {
|
|
||||||
val sql = "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 $i and ${i.plus(
|
|
||||||
range
|
|
||||||
)}" +
|
|
||||||
"and not $C_ID64=${starPoint.systemId64}"
|
|
||||||
sqlList.add(sql)
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i in 0 until NUM_OF_THREADS) {
|
|
||||||
taskList.add(
|
|
||||||
threadPool.submit {
|
|
||||||
findNeighbours(starPoint, sqlList[i], dbList[i])
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
taskList.forEach {
|
|
||||||
it.get()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
threadPool.submit {
|
||||||
|
findNeighbours(starPoint, sql)
|
||||||
|
}.get()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun findNeighbours(starPoint: StarPoint, sql: String, db: Database) {
|
private fun findNeighbours(starPoint: StarPoint, sql: String) {
|
||||||
// checkConnection()
|
checkConnection()
|
||||||
|
|
||||||
val sw = Stopwatch().apply { start() }
|
val sw = Stopwatch().apply { start() }
|
||||||
// stopwatch.start()
|
val resultSet = database.query(sql)
|
||||||
/*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" +
|
|
||||||
"from $CORRIDOR\n" +
|
|
||||||
"where sqrt((${starPoint.coords.x}-x)^2+(${starPoint.coords.y}-y)^2+(${starPoint.coords.z}-z)^2) <= ${isNeutronDistance(
|
|
||||||
starPoint.isNeutronStar
|
|
||||||
)}" +
|
|
||||||
"and not $C_ID64=${starPoint.systemId64}"
|
|
||||||
)*/
|
|
||||||
val resultSet = db.query(sql)
|
|
||||||
// stopwatch.stopWithConsoleOutput("Query time: ")
|
|
||||||
// stopwatch.start()
|
|
||||||
|
|
||||||
while (resultSet.next()) {
|
while (resultSet.next()) {
|
||||||
with(resultSet) {
|
with(resultSet) {
|
||||||
@ -171,7 +129,6 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
|
|||||||
}
|
}
|
||||||
resultSet.close()
|
resultSet.close()
|
||||||
sw.stopWithConsoleOutput("Process time: ")
|
sw.stopWithConsoleOutput("Process time: ")
|
||||||
// stopwatch.stopWithConsoleOutput("Process time: ")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isNeutronDistance(isNeutron: Boolean) = if (isNeutron) NEUTRON_DISTANCE else USUAL_DISTANCE
|
private fun isNeutronDistance(isNeutron: Boolean) = if (isNeutron) NEUTRON_DISTANCE else USUAL_DISTANCE
|
||||||
@ -273,9 +230,6 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
|
|||||||
counter++
|
counter++
|
||||||
}
|
}
|
||||||
println("${consoleStringCounter()} Total jumps counter = $counter, distance = $fullDistance ly, replaces = $replaces, cof = ${StarPoint.NEUTRON_COF}")
|
println("${consoleStringCounter()} Total jumps counter = $counter, distance = $fullDistance ly, replaces = $replaces, cof = ${StarPoint.NEUTRON_COF}")
|
||||||
dbList.forEach { db ->
|
|
||||||
db.closeDB()
|
|
||||||
}
|
|
||||||
return counter
|
return counter
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -286,9 +240,8 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
|
|||||||
)
|
)
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val CORRIDOR = "main"
|
const val CORRIDOR = "coridor3"
|
||||||
const val NEUTRON_DISTANCE = 240
|
const val NEUTRON_DISTANCE = 240
|
||||||
const val USUAL_DISTANCE = 60
|
const val USUAL_DISTANCE = 60
|
||||||
const val NUM_OF_THREADS = 3
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user