Skip to content

Commit

Permalink
Simulation: Save Stat support (#12663)
Browse files Browse the repository at this point in the history
* Changes

* Default to not using avgStat

* Make print a float

* Make statTurns a list
rename sumStat
better summation for popsum

* Fix potential nullpointer

* Add .add()

* And another place to use .add()
  • Loading branch information
itanasi authored Dec 16, 2024
1 parent 7918123 commit 2963d47
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions core/src/com/unciv/logic/simulation/MutableInt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class MutableInt(var value: Int = 0) {
fun inc() { ++value }
fun get(): Int { return value }
fun set(newValue: Int) { value = newValue }
fun add(addend: Int) { value += addend }

override fun toString(): String {
return value.tr()
Expand Down
39 changes: 33 additions & 6 deletions core/src/com/unciv/logic/simulation/Simulation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ class Simulation(
private val newGameInfo: GameInfo,
val simulationsPerThread: Int = 1,
private val threadsNumber: Int = 1,
private val maxTurns: Int = 500
private val maxTurns: Int = 500,
private val statTurns: List<Int> = listOf()
) {
private val maxSimulations = threadsNumber * simulationsPerThread
val civilizations = newGameInfo.civilizations.filter { it.civName != Constants.spectator }.map { it.civName }
private val majorCivs = newGameInfo.civilizations.filter { it.civName != Constants.spectator }.filter { it.isMajorCiv() }.size
private var startTime: Long = 0
var steps = ArrayList<SimulationStep>()
var numWins = mutableMapOf<String, MutableInt>()
var sumStat = mutableMapOf<String, MutableMap<Int, MutableInt>>()
private var winRateByVictory = HashMap<String, MutableMap<String, MutableInt>>()
private var winTurnByVictory = HashMap<String, MutableMap<String, MutableInt>>()
private var avgSpeed = 0f
Expand All @@ -40,6 +42,8 @@ class Simulation(
init{
for (civ in civilizations) {
this.numWins[civ] = MutableInt(0)
for (turn in statTurns)
this.sumStat.getOrPut(civ) { mutableMapOf() }[turn] = MutableInt(0)
winRateByVictory[civ] = mutableMapOf()
for (victory in UncivGame.Current.gameInfo!!.ruleset.victories.keys)
winRateByVictory[civ]!![victory] = MutableInt(0)
Expand All @@ -60,12 +64,23 @@ class Simulation(
jobs.add(launch(CoroutineName("simulation-${threadId}")) {
repeat(simulationsPerThread) {
val gameInfo = GameStarter.startNewGame(GameSetupInfo(newGameInfo))
gameInfo.simulateMaxTurns = maxTurns
gameInfo.simulateUntilWin = true
gameInfo.nextTurn()
for (turn in statTurns) {
gameInfo.simulateMaxTurns = turn
gameInfo.nextTurn()
val step = SimulationStep(gameInfo)
if (step.victoryType != null)
break
saveStat(gameInfo)
}
// check if Victory
var step = SimulationStep(gameInfo)
if (step.victoryType == null) {
gameInfo.simulateMaxTurns = maxTurns
gameInfo.nextTurn()
}

val step = SimulationStep(gameInfo)
println("First: ${gameInfo.civilizations.first().civName}")
step = SimulationStep(gameInfo) // final game state

if (step.victoryType != null) {
step.winner = step.currentPlayer
Expand Down Expand Up @@ -94,6 +109,16 @@ class Simulation(
println("Simulation step ($stepCounter/$maxSimulations)")
}

@Synchronized
fun saveStat(gameInfo: GameInfo) {
val turn = gameInfo.turns
for (civ in gameInfo.civilizations.filter { it.civName != Constants.spectator }) {
val popsum = civ.cities.sumOf { it.population.population }
//println("$civ $popsum")
sumStat[civ.civName]!![turn]!!.add(popsum)
}
}

@Synchronized
fun print(){
getStats()
Expand All @@ -109,7 +134,7 @@ class Simulation(
if (it.winner != null) {
numWins[it.winner!!]!!.inc()
winRateByVictory[it.winner!!]!![it.victoryType]!!.inc()
winTurnByVictory[it.winner!!]!![it.victoryType]!!.set(winTurnByVictory[it.winner!!]!![it.victoryType]!!.get() + it.turns)
winTurnByVictory[it.winner!!]!![it.victoryType]!!.add(it.turns)
}
}
totalTurns = steps.sumOf { it.turns }
Expand Down Expand Up @@ -151,6 +176,8 @@ class Simulation(
outString += "$victory: $winsTurns "
}
outString += "avg turns\n"
for (turn in statTurns)
outString += "avgStat (@$turn): ${sumStat[civ]!![turn]!!.value.toFloat()/numSteps}\n"
}
outString += "\nAverage speed: %.1f turns/s \n".format(avgSpeed)
outString += "Average game duration: $avgDuration\n"
Expand Down

0 comments on commit 2963d47

Please sign in to comment.