Add benchmarking functionality

main
Bryson Zimmerman 11 months ago
parent 3370f6215e
commit fe4f33f070

@ -4,6 +4,7 @@ import technology.zim.data.Directions
import technology.zim.data.Tile import technology.zim.data.Tile
import technology.zim.data.TileHeap import technology.zim.data.TileHeap
import technology.zim.data.TileNavigatedArray import technology.zim.data.TileNavigatedArray
import kotlin.Int
import kotlin.math.abs import kotlin.math.abs
//A* pathfinder backed by an array to improve efficiency //A* pathfinder backed by an array to improve efficiency
@ -12,7 +13,7 @@ import kotlin.math.abs
//and https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Comparator.html //and https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Comparator.html
object ArrayBackedPathfinder { object ArrayBackedPathfinder {
val gVals = TileNavigatedArray<Int>(World.sizeX, World.sizeY, false) var gVals = TileNavigatedArray<Int>(World.sizeX, World.sizeY, false)
//work along the path, marking tiles with VISITED along the way //work along the path, marking tiles with VISITED along the way
//if marking with visited is too expensive, just make the path and finalize it //if marking with visited is too expensive, just make the path and finalize it
fun generatePath(start: Tile, end: Tile) { fun generatePath(start: Tile, end: Tile) {
@ -25,7 +26,7 @@ object ArrayBackedPathfinder {
println("Ouroboros detected") println("Ouroboros detected")
return return
} }
gVals = TileNavigatedArray<Int>(World.sizeX, World.sizeY, false)
val frontier = TileHeap(end, this::fValue) val frontier = TileHeap(end, this::fValue)
//Prime the things //Prime the things
@ -55,7 +56,7 @@ object ArrayBackedPathfinder {
//At this point, a path is found //At this point, a path is found
markPath(start, end) markPath(start, end)
println("Path found!") //println("Path found!")
} }
fun markPath(start: Tile, end:Tile) { fun markPath(start: Tile, end:Tile) {

@ -1,6 +1,7 @@
package technology.zim package technology.zim
import technology.zim.data.Tile import technology.zim.data.Tile
import java.io.File
import java.text.NumberFormat import java.text.NumberFormat
import java.util.Locale import java.util.Locale
import kotlin.time.measureTime import kotlin.time.measureTime
@ -20,41 +21,60 @@ class HierarchicalPathfinding {
companion object { companion object {
@JvmStatic @JvmStatic
fun main(args: Array<String>) { fun main(args: Array<String>) {
val n = 1000 val benchmarking = false
println("Building maze") val ns = arrayListOf(50, 100, 250, 500, 750, 1000)
val buildMazeTime = measureTime { val iterations = 10
buildMaze(n) val file = File("performance.csv")
file.writeText("n, build, bfs, astar-array, astar-hashmap\n")
if(benchmarking) {
for (n in ns) {
for (i in 0 until iterations) {
doTheThing(n, file)
}
}
} }
else
doTheThing(500, file)
/*
val numberFormat = NumberFormat.getInstance(Locale.US)
println(World.toString())
println(numberFormat.format(n*n))
println("Maze build time: ${numberFormat.format(buildMazeTime.inWholeMilliseconds)} ms")
println("BFS Pathfinder time: ${numberFormat.format(bfsPathfinderTime.inWholeMilliseconds)}ms")
println("Array-Backed Pathfinder time: ${numberFormat.format(arrayBackedPathfinderTime.inWholeMilliseconds)}ms")
println("HashMap-Backed Pathfinder time: ${numberFormat.format(mapBackedPathfinderTime.inWholeMilliseconds)}ms")
*/
}
println("Pathfinding") fun doTheThing(n:Int, file:File) {
World.clear()
val bfsPathfinderTime = measureTime { var buildMazeTime = measureTime {
BFSPathfinder.generatePath(Tile(0, 0), Tile(n-1, n-1)) buildMaze(n)
}
var bfsPathfinderTime = measureTime {
BFSPathfinder.generatePath(Tile(0, 0), Tile(n - 1, n - 1))
} }
val arrayBackedPathfinderTime = measureTime { var arrayBackedPathfinderTime = measureTime {
ArrayBackedPathfinder.generatePath(Tile(0, 0), Tile(n - 1, (n - 1))) ArrayBackedPathfinder.generatePath(Tile(0, 0), Tile(n - 1, n - 1))
} }
val mapBackedPathfinderTime = measureTime { var mapBackedPathfinderTime = measureTime {
MapBackedPathfinder.generatePath(Tile(0, 0), Tile(n - 1, (n - 1))) MapBackedPathfinder.generatePath(Tile(0, 0), Tile(n - 1, n - 1))
} }
file.appendText("${n},${buildMazeTime.inWholeMilliseconds},${bfsPathfinderTime.inWholeMilliseconds},${arrayBackedPathfinderTime.inWholeMilliseconds},${mapBackedPathfinderTime.inWholeMilliseconds}\n")
val numberFormat = NumberFormat.getInstance(Locale.US)
println(World.toString())
println(numberFormat.format(n*n))
println("Maze build time: ${numberFormat.format(buildMazeTime.inWholeMilliseconds)} ms")
println("BFS Pathfinder time: ${numberFormat.format(bfsPathfinderTime.inWholeMilliseconds)}ms")
println("Array-Backed Pathfinder time: ${numberFormat.format(arrayBackedPathfinderTime.inWholeMilliseconds)}ms")
println("HashMap-Backed Pathfinder time: ${numberFormat.format(mapBackedPathfinderTime.inWholeMilliseconds)}ms")
} }
fun buildMaze(n: Int) { fun buildMaze(n: Int) {
println("Building world") //println("Building world")
World.setSize(n, n) World.setSize(n, n)
println("Start") //println("Start")
try { try {
MazeFinder.primsAlgorithm() MazeFinder.primsAlgorithm()

@ -23,7 +23,7 @@ object MapBackedPathfinder {
println("Ouroboros detected") println("Ouroboros detected")
return return
} }
gVals.clear()
val frontier = TileHeap(end, this::fValue) val frontier = TileHeap(end, this::fValue)
//Prime the things //Prime the things
@ -52,7 +52,7 @@ object MapBackedPathfinder {
} while( current != end) } while( current != end)
//At this point, a path is found //At this point, a path is found
println("Path found!") //println("Path found!")
markPath(start, end) markPath(start, end)
} }

@ -26,6 +26,7 @@ object MazeFinder {
} }
fun primsAlgorithm() { fun primsAlgorithm() {
frontier.clear()
//Prime the graph with the first connection, which marks the first visited Tiles //Prime the graph with the first connection, which marks the first visited Tiles
val startingTile = World.getRandomLocation() val startingTile = World.getRandomLocation()
val connectorTile = startingTile.getAdjacentTiles(false).random() val connectorTile = startingTile.getAdjacentTiles(false).random()
@ -62,7 +63,7 @@ object MazeFinder {
//println("--------------------------------------------") //println("--------------------------------------------")
} }
println("prim") //println("prim")
} }
private fun addToManifest(current: Tile) { private fun addToManifest(current: Tile) {

@ -20,7 +20,7 @@ import technology.zim.data.TileNavigatedArray
@Suppress("unused") @Suppress("unused")
object World { object World {
//Default size should be 10 //Default size should be 10
val tiles = TileNavigatedArray<TileProperties>() var tiles = TileNavigatedArray<TileProperties>()
var sizeX = 10 //Default size var sizeX = 10 //Default size
var sizeY = 10 var sizeY = 10
const val ANSI_RESET = "\u001B[0m" const val ANSI_RESET = "\u001B[0m"
@ -54,6 +54,15 @@ object World {
sizeX = x sizeX = x
sizeY = y sizeY = y
tiles.resize(x, y) tiles.resize(x, y)
tiles.forEachIndexed {
i, t ->
tiles.set(i, TileProperties(0))
}
}
fun clear() {
tiles = TileNavigatedArray<TileProperties>()
setSize(sizeX, sizeY)
} }
override fun toString(): String { override fun toString(): String {

Loading…
Cancel
Save