Skip to content

Commit

Permalink
add genome metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
halotukozak committed Jan 15, 2024
1 parent eede139 commit 00d1626
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 11 deletions.
14 changes: 14 additions & 0 deletions src/main/kotlin/backend/model/Genome.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ package backend.model
import kotlin.random.Random

class Genome(val genes: List<Gen>, startPos: Int? = null) : Iterator<Gen> {
override fun toString(): String = genes.joinToString(", ")

override fun equals(other: Any?): Boolean = when {
this === other -> true
other !is Genome -> false
genes != other.genes -> false
else -> true
}

private var curr = startPos ?: Random.nextInt(genes.size)

Expand All @@ -17,6 +25,12 @@ class Genome(val genes: List<Gen>, startPos: Int? = null) : Iterator<Gen> {

val frequencyMap by lazy { this.genes.groupingBy { it }.eachCount() }

override fun hashCode(): Int {
var result = genes.hashCode()
result = 31 * result + curr
return result
}

companion object {
fun random(size: Int): Genome = Genome(List(size) { Gen.random() })
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/backend/statistics/StatisticsService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package backend.statistics
import backend.config.Config
import backend.model.Animal
import backend.model.Gen
import backend.model.Genome
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import metrics.Day
Expand Down Expand Up @@ -85,6 +86,10 @@ class StatisticsService(simulationConfig: Config) {
private val _genCollector by lazy { MutableCollector<Gen>(range) }
val genCollector: Collector<Gen> by lazy { _genCollector }

val isGenomeCollectorEnabled = simulationConfig.genomes
private val _genomeCollector by lazy { MutableCollector<Genome>(range) }
val genomeCollector: Collector<Genome> by lazy { _genomeCollector }

fun registerBirth(day: Day) {
if (isBirthsMetricsEnabled) {
_birthMetrics.register(day, 1)
Expand Down Expand Up @@ -131,6 +136,9 @@ class StatisticsService(simulationConfig: Config) {
.groupBy({ it.first }, { it.second })
.map { (key, values) -> key to values.sum() }
)
if (isGenomeCollectorEnabled)
_genomeCollector.register(
animals.map(Animal::genome).groupingBy { it }.eachCount().toList().sortedByDescending { it.second }) //extractFromHere
}
}

Expand Down
35 changes: 24 additions & 11 deletions src/main/kotlin/frontend/statistics/StatisticsView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,40 @@ class StatisticsView(
if (isDailyAverageAgeMetricsEnabled) {
linechart("Daily Average Energy Over Time", NumberAxis(), NumberAxis()) {
normalize()
series("age", dailyAverageAgeMetrics){
series("age", dailyAverageAgeMetrics) {
tripleLegend("age", dailyAverageAgeTriple)
}
}
}
}
}
if (isGenCollectorEnabled)
if (isGenCollectorEnabled || isGenomeCollectorEnabled)
tab("Gens") {
vbox {
piechart("Present") {
animated = false
genCollector.onUpdate {
data.setAll(it.toList().lastOrNull()?.second?.sortedBy { it.first }?.map { (gen, count) ->
PieChart.Data(gen.toString(), count.toDouble()) //todo to view model
})
if (isGenCollectorEnabled) {
piechart("Present") {
animated = false
genCollector.onUpdate {
data.setAll(it.toList().lastOrNull()?.second?.sortedBy { it.first }?.map { (gen, count) ->
PieChart.Data(gen.toString(), count.toDouble()) //todo to view model
})
}
}
linechart("Gens", NumberAxis(), NumberAxis()) {
normalize()
multiseries(Gen.entries.map { it.name }, genCollector)
}
}
linechart("Gens", NumberAxis(), NumberAxis()) {
normalize()
multiseries(Gen.entries.map { it.name }, genCollector)
if (isGenomeCollectorEnabled) {
label("Top 10 genomes")
tableview {
topGenomes.onUpdate {
items.setAll(it)
}
readonlyColumn("Genome", GenomeColumn::genome)
readonlyColumn("Count", GenomeColumn::count)
readonlyColumn("Diff", GenomeColumn::diff)
}
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/main/kotlin/frontend/statistics/StatisticsViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
package frontend.statistics

import backend.model.Genome
import backend.statistics.StatisticsService
import frontend.components.ViewModel
import javafx.scene.paint.Color
import kotlinx.coroutines.flow.map
import tornadofx.*

class StatisticsViewModel(val statisticsService: StatisticsService, private val maxPlants: Int) : ViewModel() {
fun Number.ofAllPlants() = this.toDouble() * 100 / maxPlants

val topGenomes = statisticsService.genomeCollector.map {
it.takeLast(2).let { (previous, current) ->
current.second.take(10).map { (genome, count) ->
val previousCount = previous.second.firstOrNull { it.first == genome }?.second ?: 0
GenomeColumn(genome, count, previousCount)
}
}

}
}

class GenomeColumn(val genome: Genome, val count: Int, previousCount: Int) {
val diff = when {
count > previousCount + 3 -> Color.GREEN
count > previousCount -> Color.LIGHTGREEN
count == previousCount -> Color.BLACK
count < previousCount - 3 -> Color.RED
else -> Color.ORANGE
}.toProperty()
}

0 comments on commit 00d1626

Please sign in to comment.