This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository pollen. See https://gitlab.nuiton.org/chorem/pollen.git commit b3885e28d584a5c7ac19d7cfcd8901f911c41756 Author: Sylvain Bavencoff <bavencoff@codelutin.com> Date: Fri Sep 8 17:13:15 2017 +0200 correction de le méthode de condorcet (ref #140) --- .../CondorcetVoteCountingStrategy.java | 27 ++- .../CondorcetVoteCountingStrategyTest.java | 224 +++++++++++++++------ 2 files changed, 181 insertions(+), 70 deletions(-) diff --git a/pollen-votecounting-condorcet/src/main/java/org/chorem/pollen/votecounting/CondorcetVoteCountingStrategy.java b/pollen-votecounting-condorcet/src/main/java/org/chorem/pollen/votecounting/CondorcetVoteCountingStrategy.java index a4125830..a2d122d7 100644 --- a/pollen-votecounting-condorcet/src/main/java/org/chorem/pollen/votecounting/CondorcetVoteCountingStrategy.java +++ b/pollen-votecounting-condorcet/src/main/java/org/chorem/pollen/votecounting/CondorcetVoteCountingStrategy.java @@ -54,9 +54,11 @@ public class CondorcetVoteCountingStrategy extends AbstractVoteCountingStrategy for (Voter voter : voters) { // add this voter votes to result - addVoterChoices(voter, scores, detailResult); + addVoterChoices(voter, detailResult); } + resolveBattles(detailResult, scores); + // order scores (using their value) and return result return orderByValues(scores.values(), detailResult); } @@ -76,9 +78,7 @@ public class CondorcetVoteCountingStrategy extends AbstractVoteCountingStrategy return voteForChoices; } - protected void addVoterChoices(Voter voter, - Map<String, ChoiceScore> scores, - CondorcetDetailResult detailResult) { + protected void addVoterChoices(Voter voter, CondorcetDetailResult detailResult) { if (log.isDebugEnabled()) { log.debug("Start count for voter " + voter.getVoterId()); @@ -89,8 +89,6 @@ public class CondorcetVoteCountingStrategy extends AbstractVoteCountingStrategy String choiceIdX = voteForChoiceX.getChoiceId(); - double score = 0; - for (VoteForChoice voteForChoiceY : voter.getVoteForChoices()) { String choiceIdY = voteForChoiceY.getChoiceId(); @@ -104,14 +102,10 @@ public class CondorcetVoteCountingStrategy extends AbstractVoteCountingStrategy if (compare < 0) { // X wins over Y; - score += voterWeight; addBattle(detailResult, choiceIdX, choiceIdY, voterWeight); + addBattle(detailResult, choiceIdY, choiceIdX, -voterWeight); } } - - // store new score for this choice - ChoiceScore choiceScore = scores.get(choiceIdX); - choiceScore.addScoreValue(score); } } @@ -132,4 +126,15 @@ public class CondorcetVoteCountingStrategy extends AbstractVoteCountingStrategy battle.addScoreValue(score); } + protected void resolveBattles(CondorcetDetailResult detailResult, Map<String, ChoiceScore> scores) { + + for (CondorcetBattle battle : detailResult.getBattles()) { + + ChoiceScore choiceScore = scores.get(battle.getOpponentId()); + choiceScore.addScoreValue(battle.getScore().signum()); + + } + + } + } diff --git a/pollen-votecounting-condorcet/src/test/java/org/chorem/pollen/votecounting/CondorcetVoteCountingStrategyTest.java b/pollen-votecounting-condorcet/src/test/java/org/chorem/pollen/votecounting/CondorcetVoteCountingStrategyTest.java index d20f22ca..59eb5c0a 100644 --- a/pollen-votecounting-condorcet/src/test/java/org/chorem/pollen/votecounting/CondorcetVoteCountingStrategyTest.java +++ b/pollen-votecounting-condorcet/src/test/java/org/chorem/pollen/votecounting/CondorcetVoteCountingStrategyTest.java @@ -71,10 +71,16 @@ public class CondorcetVoteCountingStrategyTest { public void simpleVotecount() throws Exception { // Simple poll (all weight to 1) - // 1 (a=1 b=2 c=null) - // 2 (a=1 b=3 c=2) - // 3 (a=1 b=null c=2) - // Result (a=6 b=1 c=2) + // a b c + // 1 1 2 + // 2 1 3 2 + // 3 1 2 + // combats + // a X -3 -3 + // b 3 X 1 + // c 3 -1 X + //-------------------- + // Result 2 -2 0 Set<Voter> voters = new SimpleVoterBuilder(). newVoter("1", 1.). @@ -97,9 +103,9 @@ public class CondorcetVoteCountingStrategyTest { assertThat(result.getScores()) .isNotNull() .containsExactlyInAnyOrder( - ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(6.0), 0), - ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(1.0), 2), - ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(2.0), 1)) + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(2.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(-2.0), 2), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(0.0), 1)) .isSortedAccordingTo(ChoiceScore::compareTo); } @@ -107,10 +113,16 @@ public class CondorcetVoteCountingStrategyTest { public void simpleVotecount0() throws Exception { // Simple poll (all weight to 1) - // 1 (a=1 b=2 c=null) - // 2 (a=3 b=2 c=1) - // 3 (a=1 b=null c=2) - // Result (a=4 b=2 c=3) + // a b c + // 1 1 2 + // 2 3 2 1 + // 3 1 2 + // combats + // a X -1 -1 + // b 1 X 1 + // c 1 -1 X + //-------------------- + // Result 2 -2 0 Set<Voter> voters = new SimpleVoterBuilder(). newVoter("1", 1.). @@ -133,9 +145,9 @@ public class CondorcetVoteCountingStrategyTest { assertThat(result.getScores()) .isNotNull() .containsExactlyInAnyOrder( - ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(4.0), 0), - ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(2.0), 2), - ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(3.0), 1)) + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(2.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(-2.0), 2), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(0.0), 1)) .isSortedAccordingTo(ChoiceScore::compareTo); } @@ -143,10 +155,16 @@ public class CondorcetVoteCountingStrategyTest { public void simpleVotecount2() throws Exception { // Simple poll (all weight to 1) - // 1 (a=1 b=2 c=null) - // 2 (a=1 b=2 c=1) - // 3 (a=null b=null c=2) - // Result (a=3 b=1 c=3) + // a b c + // 1 1 2 + // 2 1 2 1 + // 3 2 + // combats a b c + // a X -2 0 + // b 2 X 1 + // c 0 -1 X + //-------------------- + // Result 1 -2 1 Set<Voter> voters = new SimpleVoterBuilder(). newVoter("1", 1.). @@ -169,9 +187,9 @@ public class CondorcetVoteCountingStrategyTest { assertThat(result.getScores()) .isNotNull() .containsExactlyInAnyOrder( - ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(3.0), 0), - ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(1.0), 1), - ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(3.0), 0)) + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(1.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(-2.0), 1), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(1.0), 0)) .isSortedAccordingTo(ChoiceScore::compareTo); } @@ -179,10 +197,16 @@ public class CondorcetVoteCountingStrategyTest { public void simpleVotecount3() throws Exception { // Simple poll (all weight to 1) - // 1 (a=1 b=null c=null) - // 2 (a=null b=1 c=null) - // 3 (a=null b=null c=1) - // Result (a=2 b=2 c=2) + // a b c + // 1 1 + // 2 1 + // 3 1 + // combats + // a X 0 0 + // b 0 X 0 + // c 0 0 X + //-------------------- + // Result 0 0 0 Set<Voter> voters = new SimpleVoterBuilder(). newVoter("1", 1.). @@ -205,9 +229,9 @@ public class CondorcetVoteCountingStrategyTest { assertThat(result.getScores()) .isNotNull() .containsExactlyInAnyOrder( - ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(2.0), 0), - ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(2.0), 0), - ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(2.0), 0)) + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(0.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(0.0), 0), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(0.0), 0)) .isSortedAccordingTo(ChoiceScore::compareTo); } @@ -215,10 +239,16 @@ public class CondorcetVoteCountingStrategyTest { public void weightedVotecount1() throws Exception { // poll with weighted vote - // 1 (x2) (a=1 b=null c=null) - // 2 (x1) (a=null b=1 c=null) - // 3 (x1) (a=null b=1 c=2) - // Result (a=2 b=2 c=1) + // a b c + // 1 1 x 2 + // 2 1 + // 3 1 2 + // combats + // a X 0 -1 + // b 0 X -2 + // c 1 2 X + //-------------------- + // Result 1 1 -2 Set<Voter> voters = new SimpleVoterBuilder(). newVoter("1", 2.). @@ -241,9 +271,9 @@ public class CondorcetVoteCountingStrategyTest { assertThat(result.getScores()) .isNotNull() .containsExactlyInAnyOrder( - ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(4.0), 0), - ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(4.0), 0), - ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(1.0), 1)) + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(1.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(1.0), 0), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(-2.0), 1)) .isSortedAccordingTo(ChoiceScore::compareTo); } @@ -251,10 +281,16 @@ public class CondorcetVoteCountingStrategyTest { public void weightedVotecount2() throws Exception { // poll with weighted vote - // 1 (x2) (a=1 b=null c=null) - // 2 (x1) (a=null b=1 c=null) - // 3 (x3) (a=null b=2 c=1) - // Result (a=2 b=4 c=3) + // a b c + // 1 1 x 2 + // 2 1 + // 3 2 1 x 3 + // combats + // a X 2 1 + // b -2 X 2 + // c -1 -2 X + //-------------------- + // Result -2 0 2 Set<Voter> voters = new SimpleVoterBuilder(). newVoter("1", 2.). @@ -277,28 +313,46 @@ public class CondorcetVoteCountingStrategyTest { assertThat(result.getScores()) .isNotNull() .containsExactlyInAnyOrder( - ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(4.0), 2), - ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(5.0), 1), - ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(6.0), 0)) + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(-2.0), 2), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(0.0), 1), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(2.0), 0)) .isSortedAccordingTo(ChoiceScore::compareTo); } @Test public void listVotecount() throws Exception { - // A B C - // G1U1 1 2 3 * 2 - // G1U2 1 2 2 - // G1U3 3 2 1 - // Result G1 6 4 1 - // Vote G1 1 2 3 - - // G2U1 1 2 3 * 2 - // G2U2 3 2 1 - // G2U3 3 1 2 - // Result G2 4 5 3 - // Vote G1 2 1 3 * 2 - - // Result 4 5 0 + // a b c + // G1U1 1 2 3 * 2 + // G1U2 1 2 2 + // G1U3 3 2 1 + //-------------------- + // combats + // a X -2 -2 + // b 2 X -1 + // c 2 1 X + //-------------------- + // Result G1 2 0 -2 + // Vote G1 1 2 3 + + // G2U1 1 2 3 * 2 + // G2U2 3 2 1 + // G2U3 3 1 2 + //-------------------- + // combats + // A X 0 0 + // B 0 X -2 + // C 0 2 X + //-------------------- + // Result G2 0 1 -1 + // Vote G2 2 1 3 * 2 + + //-------------------- + // combats + // A X 1 -3 + // B -1 X -3 + // C 3 3 X + //-------------------- + // Result 0 2 -2 ListOfVoter voters = ListOfVoter.newVoter(null, 1, Sets.newHashSet( ListOfVoter.newVoter("G1", 1, Sets.newHashSet( @@ -336,9 +390,61 @@ public class CondorcetVoteCountingStrategyTest { assertThat(result.getMainResult()).isNotNull(); assertThat(result.getMainResult().getScores()).isNotNull() .containsExactlyInAnyOrder( - ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(4.0), 1), - ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(5.0), 0), - ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(0.0), 2)) + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(0.0), 1), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(2.0), 0), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(-2.0), 2)) + .isSortedAccordingTo(ChoiceScore::compareTo); + } + + @Test + public void simpleVotecount4() throws Exception { + + // Simple poll (all weight to 1) see issue #140 + // a b c + // 1 1 2 3 + // 2 1 2 3 + // 3 1 2 3 + // 4 3 1 2 + // 4 3 1 2 + // combats + // a X -1 -1 + // b 1 X -5 + // c 1 5 X + //-------------------- + // Result 2 0 -2 + + Set<Voter> voters = new SimpleVoterBuilder(). + newVoter("1", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, 2.). + addVoteForChoice(CHOICE_C, 3.). + newVoter("2", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, 2.). + addVoteForChoice(CHOICE_C, 3.). + newVoter("3", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, 2.). + addVoteForChoice(CHOICE_C, 3.). + newVoter("4", 1.). + addVoteForChoice(CHOICE_A, 3.). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, 2.). + newVoter("5", 1.). + addVoteForChoice(CHOICE_A, 3.). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, 2.). + getVoters(); + + VoteCountingResult result = strategy.votecount(voters); + + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(2.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(0.0), 1), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(-2.0), 2)) .isSortedAccordingTo(ChoiceScore::compareTo); } -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.