Skip to content

Commit

Permalink
revert bad fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
halotukozak committed Jan 24, 2024
1 parent 4aa4efb commit abc5443
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/backend/Simulation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ class Simulation(
private suspend fun nextDay() {
println("${day.updateAndGet { it + 1 }} day!")
map.growAnimals()
map.removeDeadAnimals { statisticsService.registerDeath(it.size) }
map.removeDeadAnimals { statisticsService.registerDeath(day.value, it.size) }
map.rotateAnimals()
map.moveAnimals()
map.consumePlants()
map.breedAnimals { launch { statisticsService.registerBirth(day.value) } }
map.breedAnimals { statisticsService.registerBirth(day.value) }
map.growPlants(config.plantsPerDay)

statisticsService.registerEndOfDay(day.value, plants.value.size, aliveAnimals.value.flattenValues())
Expand Down
17 changes: 4 additions & 13 deletions src/main/kotlin/backend/map/AbstractMap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ import backend.config.Config
import backend.model.Animal
import backend.model.Direction
import frontend.simulation.FamilyRoot
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import shared.*
import kotlin.collections.component1
import kotlin.collections.component2
import kotlin.collections.component3
import kotlin.coroutines.coroutineContext
import kotlin.random.Random

@Suppress("PropertyName")
Expand Down Expand Up @@ -69,13 +66,10 @@ abstract class AbstractMap(protected val config: Config) {
suspend fun rotateAnimals(callback: suspend (List<Animal>) -> Unit = {}) = updateAnimals(Animal::rotate, callback)

suspend fun removeDeadAnimals(callback: suspend (List<Animal>) -> Unit = {}) = _aliveAnimals.update { animals ->
val scope = CoroutineScope(coroutineContext)
animals.mapValuesAsync { set ->
set.partition(Animal::isDead).let { (dead, alive) ->
scope.launch {
callback(dead)
_deadAnimals.update { it + dead }
}
callback(dead)
_deadAnimals.update { it + dead }
alive
}
}
Expand Down Expand Up @@ -124,7 +118,6 @@ abstract class AbstractMap(protected val config: Config) {
}

suspend fun breedAnimals(callback: (Animal) -> Unit = {}) = _aliveAnimals.update { animals ->
val scope = CoroutineScope(coroutineContext)
animals.mapValuesAsync { set ->
(set.size >= 2).ifTake {
val (animal1, animal2) = set.max().let { it to (set - it).max() }
Expand All @@ -134,10 +127,8 @@ abstract class AbstractMap(protected val config: Config) {
config.reproductionEnergyRatio,
mutator,
).also { (parent1, parent2, child) ->
scope.launch {
familyTree.add(child.id, parent1.id, parent2.id)
callback(child)
}
familyTree.add(child.id, parent1.id, parent2.id)
callback(child)
}
}
} ?: set
Expand Down
22 changes: 11 additions & 11 deletions src/main/kotlin/backend/statistics/StatisticsService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ class StatisticsService(simulationConfig: Config) {
}

val isDeathsMetricsEnabled = simulationConfig.deaths
private val _deathMetrics by lazy { MutableCounter<Int>(range) }
private val _minDeathMetrics by lazy(::MutableMinimumMetrics)
private val _maxDeathMetrics by lazy(::MutableMaximumMetrics)
private val _avgDeathMetrics by lazy(::MutableAverageMetrics)
val deathMetrics: Counter<Int> by lazy { _deathMetrics }
private val _deathMetrics by lazy { MutableACounter<Int>(range) }
private val _minDeathMetrics by lazy(::MutableAMinimumMetrics)
private val _maxDeathMetrics by lazy(::MutableAMaximumMetrics)
private val _avgDeathMetrics by lazy(::MutableAAverageMetrics)
val deathMetrics: ACounter<Int> by lazy { _deathMetrics }
val deathTripleMetrics by lazy {
combine(_minDeathMetrics, _maxDeathMetrics, _avgDeathMetrics, ::Triple)
}
Expand Down Expand Up @@ -112,12 +112,12 @@ class StatisticsService(simulationConfig: Config) {
}
}

fun registerDeath(animals: Int) {
fun registerDeath(day: Day, animals: Int) {
if (isDeathsMetricsEnabled) {
_deathMetrics.register(animals)
_minDeathMetrics.register(animals)
_maxDeathMetrics.register(animals)
_avgDeathMetrics.register(animals)
_deathMetrics.register(day, animals)
_minDeathMetrics.register(day, animals)
_maxDeathMetrics.register(day, animals)
_avgDeathMetrics.register(day, animals)
}
}

Expand All @@ -144,7 +144,7 @@ class StatisticsService(simulationConfig: Config) {
_maxDailyAverageAgeMetrics.register(avg)
_avgDailyAverageAgeMetrics.register(avg)
}
if (isDailyAverageEnergyMetricsEnabled){
if (isDailyAverageEnergyMetricsEnabled) {
val avg = animals.map(Animal::energy).average()
_dailyAverageEnergyMetrics.register(avg)
_minDailyAverageEnergyMetrics.register(avg)
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/frontend/simulation/SimulationView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class SimulationView(simulationConfig: Config) : View() {
text = "Animals: ${it.flattenValues().size}"
}
},
separator(Orientation.VERTICAL),
label {
simulation.plants.onUpdate {
text = "Plants: ${it.size}"
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/frontend/statistics/StatisticsView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import javafx.scene.chart.NumberAxis
import javafx.scene.chart.XYChart
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import shared.truncated
import tornadofx.*
import kotlin.math.max

Expand Down Expand Up @@ -141,7 +142,7 @@ class StatisticsView(
body = vbox {
it.mapIndexed { i, genome ->
borderpane {
left = text("${i + 1}. ${genome.genome}")
left = text("${i + 1}. ${genome.genome}".truncated(40))
right = hbox {
genome.arrow?.let(::fontIcon)
text(genome.count.toString()) {
Expand Down
15 changes: 7 additions & 8 deletions src/main/kotlin/metrics/math/AMathMetrics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class MutableAMaximumMetrics : AMaximumMetrics, MutableStateFlow<Double> by Muta

override fun register(day: Day, value: Double) = update {
days[day] = days.getOrDefault(day, 0.0) + value
maxOf(it, days[day-1]?: 0.0)
maxOf(it, days[day - 1] ?: 0.0)
}
}

Expand All @@ -28,21 +28,20 @@ class MutableAMinimumMetrics : AMinimumMetrics, MutableStateFlow<Double> by Muta

override fun register(day: Day, value: Double) = update {
days[day] = days.getOrDefault(day, 0.0) + value
minOf(it, days[day-1]?: Double.MAX_VALUE)
minOf(it, days[day - 1] ?: Double.MAX_VALUE)
}
}

class MutableAAverageMetrics : AAverageMetrics, MutableStateFlow<Double> by MutableStateFlow(0.0) {
private val days = mutableMapOf<Day, Double>()
private var size = 0
private var sum = 0.0

override fun register(day: Day, value: Double) = update {
when(day) {
days.size -> days[day] = (days[day]?: 0.0) + value
days.size + 1 -> { days[day] = value }
else -> { sum -= value }
}
val oldValue = days[day]
if (oldValue == null) size++
days[day] = (oldValue ?: 0.0) + value
sum += value
sum / days.size
sum / size
}
}

0 comments on commit abc5443

Please sign in to comment.