Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simulation: Save Stat support #12663

Merged
merged 9 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
37 changes: 32 additions & 5 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 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
Loading