From 931e3266ff2ad5bd9feb5dc723ec037259f0ce35 Mon Sep 17 00:00:00 2001 From: Quentin Rendu Date: Mon, 16 Oct 2023 18:46:07 +0200 Subject: [PATCH] Correct pairingSort function --- .../kotlin/org/jeudego/pairgoth/pairing/Solver.kt | 13 +++++-------- api-webapp/src/test/kotlin/BasicTests.kt | 10 ++++++---- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/Solver.kt b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/Solver.kt index 766e0e0..e2af397 100644 --- a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/Solver.kt +++ b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/Solver.kt @@ -79,7 +79,7 @@ sealed class Solver( val criterionP = p.eval(criterion) val criterionQ = q.eval(criterion) if (criterionP != criterionQ) { - return (criterionP * 100 - criterionQ * 100).toInt() + return (criterionQ * 100 - criterionP * 100).toInt() } } return 0 @@ -89,7 +89,7 @@ sealed class Solver( val criterionP = p.eval(criterion) val criterionQ = q.eval(criterion) if (criterionP != criterionQ) { - return (criterionP * 100 - criterionQ * 100).toInt() + return (criterionQ * 1e6 - criterionP * 1e6).toInt() } } if (p.rating == q.rating) { @@ -174,14 +174,11 @@ sealed class Solver( val matching = KolmogorovWeightedPerfectMatching(graph, ObjectiveSense.MAXIMIZE) val solution = matching.matching - fun gamesSort(p1:Pairable, p2:Pairable) = 0.5*(p1.place + p2.place) - - var sorted = solution.map{ + val sorted = solution.map{ listOf(graph.getEdgeSource(it), graph.getEdgeTarget(it)) - }.sortedBy { gamesSort(it[0],it[1])} + }.sortedWith(compareBy({ 0.5*(it[0].place + it[1].place) }, { min(it[0].place, it[1].place) })) - - var result = sorted.flatMap { games(white = it[0], black = it[1]) } + val result = sorted.flatMap { games(white = it[0], black = it[1]) } return result diff --git a/api-webapp/src/test/kotlin/BasicTests.kt b/api-webapp/src/test/kotlin/BasicTests.kt index 683c948..abb05fe 100644 --- a/api-webapp/src/test/kotlin/BasicTests.kt +++ b/api-webapp/src/test/kotlin/BasicTests.kt @@ -237,11 +237,12 @@ class BasicTests: TestBase() { costs.add(parts[1].toDouble()) } + val tmp_pair = if (name1 > name2) Pair(name1,name2) else Pair(name2,name1) // Add to map if (count == 1) { - map1[Pair(name1, name2)] = costs + map1[tmp_pair] = costs } else { - map2[Pair(name1, name2)] = costs + map2[tmp_pair] = costs } } count += 1 @@ -253,9 +254,10 @@ class BasicTests: TestBase() { // Check if key exists in both if (map2.containsKey(key)) { // Compare values + //logger.info("Comparing $key") if (value != map2[key] && diff_found==false) { // Key exists but values differ - print key - logger.info("Difference found at $key") + logger.info("Difference found at $key : $value") logger.info("baseDuplicateGameCost = "+value!![0].toString()+" "+map2[key]!![0].toString()) logger.info("baseRandomCost = "+value!![1].toString()+" "+map2[key]!![1].toString()) logger.info("baseBWBalanceCost = "+value!![2].toString()+" "+map2[key]!![2].toString()) @@ -318,7 +320,7 @@ class BasicTests: TestBase() { val players_np = TestAPI.get("/api/tour/$id_np/part").asArray() logger.info(players_np.toString().slice(0..50) + "...") var games_np = TestAPI.post("/api/tour/$id_np/pair/1", Json.Array("all")).asArray() - // logger.info("games for round 1: {}", games_np.toString()) + logger.info("games for round 1: {}", games_np.toString()) // logger.info("Compare weights with itself") assertTrue(compare_weights("weights.txt", "weights.txt"), "Weights not equal to itselft")