From 8cd0b7d15e88f67eea179a033eb98d5ea77df309 Mon Sep 17 00:00:00 2001 From: Claude Brisson Date: Sun, 24 Mar 2024 14:53:07 +0100 Subject: [PATCH] Refactor past bye players list handling --- .../pairgoth/pairing/BasePairingHelper.kt | 18 ++++++++++++++---- .../jeudego/pairgoth/pairing/HistoryHelper.kt | 8 ++++++++ .../pairgoth/pairing/solver/BaseSolver.kt | 4 +--- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/BasePairingHelper.kt b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/BasePairingHelper.kt index 8d4c944..14ae0d0 100644 --- a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/BasePairingHelper.kt +++ b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/BasePairingHelper.kt @@ -12,8 +12,11 @@ abstract class BasePairingHelper( ) { abstract val scores: Map> - val historyHelper = if (pairables.first().let { it is TeamTournament.Team && it.teamOfIndividuals }) TeamOfIndividualsHistoryHelper(history) { scores } - else HistoryHelper(history) { scores } + val historyHelper = + if (pairables.first().let { it is TeamTournament.Team && it.teamOfIndividuals }) TeamOfIndividualsHistoryHelper( + history + ) { scores } + else HistoryHelper(history) { scores } // Extend pairables with members from all rounds @@ -26,10 +29,12 @@ abstract class BasePairingHelper( protected val sortedPairables by lazy { pairables.sortedWith(::sort) } + // pairables sorted for pairing purposes protected val pairingSortedPairables by lazy { pairables.sortedWith(::pairingSort).toMutableList() } + // pairables sorted for pairing purposes protected val nameSortedPairables by lazy { pairables.sortedWith(::nameSort).toMutableList() @@ -79,7 +84,7 @@ abstract class BasePairingHelper( protected val Pairable.group: Int get() = _groups[id]!! - protected val Pairable.drawnUpDown: Pair get() = historyHelper.drawnUpDown(this) ?: Pair(0,0) + protected val Pairable.drawnUpDown: Pair get() = historyHelper.drawnUpDown(this) ?: Pair(0, 0) protected val Pairable.nbBye: Int get() = historyHelper.nbPlayedWithBye(this) ?: 0 @@ -93,8 +98,11 @@ abstract class BasePairingHelper( val Pairable.sodos: Double get() = historyHelper.sodos[id] ?: 0.0 val Pairable.cums: Double get() = historyHelper.cumScore[id] ?: 0.0 fun Pairable.missedRounds(upToRound: Int, pairing: Set): Int = (1..upToRound).map { round -> - if (historyHelper.playersPerRound.getOrNull(round - 1)?.contains(id) == true || round == upToRound && pairing.contains(id)) 0 else 1 + if (historyHelper.playersPerRound.getOrNull(round - 1) + ?.contains(id) == true || round == upToRound && pairing.contains(id) + ) 0 else 1 }.sum() + fun Pairable.eval(criterion: Criterion) = evalCriterion(this, criterion) open fun evalCriterion(pairable: Pairable, criterion: Criterion) = when (criterion) { Criterion.NONE -> 0.0 @@ -121,6 +129,7 @@ abstract class BasePairingHelper( } return 0 } + open fun pairingSort(p: Pairable, q: Pairable): Int { for (criterion in placement.criteria) { val criterionP = p.eval(criterion) @@ -141,6 +150,7 @@ abstract class BasePairingHelper( } return p.fullName().compareTo(q.fullName()) } + open fun nameSort(p: Pairable, q: Pairable): Int { return p.fullName().compareTo(q.fullName()) } diff --git a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/HistoryHelper.kt b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/HistoryHelper.kt index a315e1b..b95f844 100644 --- a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/HistoryHelper.kt +++ b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/HistoryHelper.kt @@ -218,6 +218,14 @@ open class HistoryHelper(protected val history: List>, scoresGetter: } } + val byePlayers by lazy { + history.flatten().mapNotNull { game -> + if (game.white == ByePlayer.id) game.black + else if (game.black == ByePlayer.id) game.white + else null + } + } + } // CB TODO - a big problem with the current naive implementation is that the team score is -for now- the sum of team members individual scores diff --git a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/solver/BaseSolver.kt b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/solver/BaseSolver.kt index 3dff369..0c6bde4 100644 --- a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/solver/BaseSolver.kt +++ b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/solver/BaseSolver.kt @@ -33,7 +33,6 @@ sealed class BaseSolver( companion object { val rand = Random(/* seed from properties - TODO */) - var byePlayers: MutableList = mutableListOf() var weightsLogger: PrintWriter? = null } @@ -79,7 +78,7 @@ sealed class BaseSolver( var byePlayerIndex = 0 for (p in nameSortedPairables){ weightForBye = p.rank + 2*(p.main + p.rank) - if (p in byePlayers) weightForBye += 1000 + if (p.id in historyHelper.byePlayers) weightForBye += 1000 if (weightForBye <= minWeight){ minWeight = weightForBye chosenByePlayer = p @@ -87,7 +86,6 @@ sealed class BaseSolver( // println("choose Bye: " + p.nameSeed() + " mms2 " +2*p.main+" "+ weightForBye) } println("Bye player : " + chosenByePlayer.fullName()) - byePlayers.add(chosenByePlayer) nameSortedPairables.remove(chosenByePlayer) // Keep chosenByePlayer in pairingSortedPairables to be identical to opengotha pairingSortedPairables.remove(ByePlayer)