Refactor past bye players list handling

This commit is contained in:
Claude Brisson
2024-03-24 14:53:07 +01:00
parent 3f0b8da68a
commit 8cd0b7d15e
3 changed files with 23 additions and 7 deletions

View File

@@ -12,7 +12,10 @@ abstract class BasePairingHelper(
) { ) {
abstract val scores: Map<ID, Pair<Double, Double>> abstract val scores: Map<ID, Pair<Double, Double>>
val historyHelper = if (pairables.first().let { it is TeamTournament.Team && it.teamOfIndividuals }) TeamOfIndividualsHistoryHelper(history) { scores } val historyHelper =
if (pairables.first().let { it is TeamTournament.Team && it.teamOfIndividuals }) TeamOfIndividualsHistoryHelper(
history
) { scores }
else HistoryHelper(history) { scores } else HistoryHelper(history) { scores }
// Extend pairables with members from all rounds // Extend pairables with members from all rounds
@@ -26,10 +29,12 @@ abstract class BasePairingHelper(
protected val sortedPairables by lazy { protected val sortedPairables by lazy {
pairables.sortedWith(::sort) pairables.sortedWith(::sort)
} }
// pairables sorted for pairing purposes // pairables sorted for pairing purposes
protected val pairingSortedPairables by lazy { protected val pairingSortedPairables by lazy {
pairables.sortedWith(::pairingSort).toMutableList() pairables.sortedWith(::pairingSort).toMutableList()
} }
// pairables sorted for pairing purposes // pairables sorted for pairing purposes
protected val nameSortedPairables by lazy { protected val nameSortedPairables by lazy {
pairables.sortedWith(::nameSort).toMutableList() pairables.sortedWith(::nameSort).toMutableList()
@@ -79,7 +84,7 @@ abstract class BasePairingHelper(
protected val Pairable.group: Int get() = _groups[id]!! protected val Pairable.group: Int get() = _groups[id]!!
protected val Pairable.drawnUpDown: Pair<Int,Int> get() = historyHelper.drawnUpDown(this) ?: Pair(0,0) protected val Pairable.drawnUpDown: Pair<Int, Int> get() = historyHelper.drawnUpDown(this) ?: Pair(0, 0)
protected val Pairable.nbBye: Int get() = historyHelper.nbPlayedWithBye(this) ?: 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.sodos: Double get() = historyHelper.sodos[id] ?: 0.0
val Pairable.cums: Double get() = historyHelper.cumScore[id] ?: 0.0 val Pairable.cums: Double get() = historyHelper.cumScore[id] ?: 0.0
fun Pairable.missedRounds(upToRound: Int, pairing: Set<ID>): Int = (1..upToRound).map { round -> fun Pairable.missedRounds(upToRound: Int, pairing: Set<ID>): 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() }.sum()
fun Pairable.eval(criterion: Criterion) = evalCriterion(this, criterion) fun Pairable.eval(criterion: Criterion) = evalCriterion(this, criterion)
open fun evalCriterion(pairable: Pairable, criterion: Criterion) = when (criterion) { open fun evalCriterion(pairable: Pairable, criterion: Criterion) = when (criterion) {
Criterion.NONE -> 0.0 Criterion.NONE -> 0.0
@@ -121,6 +129,7 @@ abstract class BasePairingHelper(
} }
return 0 return 0
} }
open fun pairingSort(p: Pairable, q: Pairable): Int { open fun pairingSort(p: Pairable, q: Pairable): Int {
for (criterion in placement.criteria) { for (criterion in placement.criteria) {
val criterionP = p.eval(criterion) val criterionP = p.eval(criterion)
@@ -141,6 +150,7 @@ abstract class BasePairingHelper(
} }
return p.fullName().compareTo(q.fullName()) return p.fullName().compareTo(q.fullName())
} }
open fun nameSort(p: Pairable, q: Pairable): Int { open fun nameSort(p: Pairable, q: Pairable): Int {
return p.fullName().compareTo(q.fullName()) return p.fullName().compareTo(q.fullName())
} }

View File

@@ -218,6 +218,14 @@ open class HistoryHelper(protected val history: List<List<Game>>, 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 // 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

View File

@@ -33,7 +33,6 @@ sealed class BaseSolver(
companion object { companion object {
val rand = Random(/* seed from properties - TODO */) val rand = Random(/* seed from properties - TODO */)
var byePlayers: MutableList<Pairable> = mutableListOf()
var weightsLogger: PrintWriter? = null var weightsLogger: PrintWriter? = null
} }
@@ -79,7 +78,7 @@ sealed class BaseSolver(
var byePlayerIndex = 0 var byePlayerIndex = 0
for (p in nameSortedPairables){ for (p in nameSortedPairables){
weightForBye = p.rank + 2*(p.main + p.rank) 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){ if (weightForBye <= minWeight){
minWeight = weightForBye minWeight = weightForBye
chosenByePlayer = p chosenByePlayer = p
@@ -87,7 +86,6 @@ sealed class BaseSolver(
// println("choose Bye: " + p.nameSeed() + " mms2 " +2*p.main+" "+ weightForBye) // println("choose Bye: " + p.nameSeed() + " mms2 " +2*p.main+" "+ weightForBye)
} }
println("Bye player : " + chosenByePlayer.fullName()) println("Bye player : " + chosenByePlayer.fullName())
byePlayers.add(chosenByePlayer)
nameSortedPairables.remove(chosenByePlayer) nameSortedPairables.remove(chosenByePlayer)
// Keep chosenByePlayer in pairingSortedPairables to be identical to opengotha // Keep chosenByePlayer in pairingSortedPairables to be identical to opengotha
pairingSortedPairables.remove(ByePlayer) pairingSortedPairables.remove(ByePlayer)