Browse Source

Merge pull request #8 from MieuxVoter/feat-default-static-grade

feat: allow setting a static default grade
feat-median-default-grade
Dominique Merle 1 year ago
committed by GitHub
parent
commit
6770108f4c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      README.md
  2. 34
      src/main/java/fr/mieuxvoter/mj/ProposalTally.java
  3. 2
      src/main/java/fr/mieuxvoter/mj/ProposalTallyAnalysis.java
  4. 10
      src/test/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberatorTest.java
  5. 40
      src/test/resources/assertions.json

8
README.md

@ -44,6 +44,10 @@ assert(1 == result.getProposalResults()[1].getRank()); // Proposal B
Got more than 2³² judges? Use a `Long[]` in a `ProposalTally`.
Got even more than that ? Use `BigInteger`s !
Want to set a static default grade ? Use a `TallyWithDefaultGrade` instead of a `Tally`.
## Roadmap
@ -55,9 +59,9 @@ Got more than 2³² judges? Use a `Long[]` in a `ProposalTally`.
- [x] Release v0.1.0
- [ ] Guess the amount of judges
- [ ] Allow defining a default grade
- [ ] Static Grade (configurable)
- [x] Static Grade (configurable)
- [ ] Median Grade
- [ ] Normalization (using smallest common multiple)
- [ ] Normalization (using least common multiple)
- [ ] Release v0.2.0
- [ ] Publish on package repositories
- [ ] Gradle

34
src/main/java/fr/mieuxvoter/mj/ProposalTally.java

@ -9,17 +9,33 @@ public class ProposalTally implements ProposalTallyInterface {
// Should we allow this as well?
//public ProposalTally() {}
public ProposalTally(String[] tally) {
setTally(tally);
}
public ProposalTally(Integer[] tally) {
setTally(tally);
}
public ProposalTally(Long[] tally) {
setTally(tally);
}
public ProposalTally(BigInteger[] tally) {
setTally(tally);
}
public void setTally(String[] tally) {
int tallyLength = tally.length;
BigInteger[] bigTally = new BigInteger[tallyLength];
for (int i = 0 ; i < tallyLength ; i++) {
bigTally[i] = BigInteger.valueOf(tally[i]);
bigTally[i] = new BigInteger(tally[i]);
}
setTally(bigTally);
}
public ProposalTally(Long[] tally) {
public void setTally(Integer[] tally) {
int tallyLength = tally.length;
BigInteger[] bigTally = new BigInteger[tallyLength];
for (int i = 0 ; i < tallyLength ; i++) {
@ -27,9 +43,14 @@ public class ProposalTally implements ProposalTallyInterface {
}
setTally(bigTally);
}
public ProposalTally(BigInteger[] tally) {
setTally(tally);
public void setTally(Long[] tally) {
int tallyLength = tally.length;
BigInteger[] bigTally = new BigInteger[tallyLength];
for (int i = 0 ; i < tallyLength ; i++) {
bigTally[i] = BigInteger.valueOf(tally[i]);
}
setTally(bigTally);
}
public void setTally(BigInteger[] tally) {
@ -48,7 +69,6 @@ public class ProposalTally implements ProposalTallyInterface {
@Override
public void moveJudgments(Integer fromGrade, Integer intoGrade) {
// this.tally[intoGrade] += this.tally[fromGrade];
this.tally[intoGrade] = this.tally[intoGrade].add(this.tally[fromGrade]);
this.tally[fromGrade] = BigInteger.ZERO;
}

2
src/main/java/fr/mieuxvoter/mj/ProposalTallyAnalysis.java

@ -92,7 +92,6 @@ public class ProposalTallyAnalysis {
this.medianGrade = grade;
this.contestationGroupSize = tallyBeforeCursor;
this.medianGroupSize = gradeTally;
// this.adhesionGroupSize = this.totalSize - this.contestationGroupSize - this.medianGroupSize;
this.adhesionGroupSize = this.totalSize.subtract(this.contestationGroupSize).subtract(this.medianGroupSize);
} else {
if (1 == gradeTally.compareTo(BigInteger.ZERO)) { // 0 < gradeTally
@ -108,7 +107,6 @@ public class ProposalTallyAnalysis {
this.contestationGrade = contestationGrade;
this.adhesionGrade = adhesionGrade;
// this.secondMedianGroupSize = Math.max(this.contestationGroupSize, this.adhesionGroupSize);
this.secondMedianGroupSize = this.contestationGroupSize.max(this.adhesionGroupSize);
this.secondMedianGroupSign = 0;
// if (this.contestationGroupSize < this.adhesionGroupSize) {

10
src/test/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberatorTest.java

@ -68,8 +68,16 @@ class MajorityJudgmentDeliberatorTest {
}
tallies[i] = new ProposalTally(tally);
}
String mode = datum.getString("mode", "None");
TallyInterface tally;
if ("StaticDefault".equalsIgnoreCase(mode)) {
tally = new TallyWithDefaultGrade(tallies, amountOfParticipants, datum.getInt("default"));
} else {
tally = new Tally(tallies, amountOfParticipants);
}
DeliberatorInterface mj = new MajorityJudgmentDeliberator();
TallyInterface tally = new Tally(tallies, amountOfParticipants);
ResultInterface result = mj.deliberate(tally);
assertNotNull(result);

40
src/test/resources/assertions.json

@ -49,6 +49,46 @@
1,
2
]
},
{
"title": "Static Default Grade",
"participants": 10,
"mode": "StaticDefault",
"default": 0,
"tallies": [
[ 2, 2, 2, 2, 2 ],
[ 1, 2, 2, 2, 2 ],
[ 0, 0, 4, 0, 0 ],
[ 0, 0, 0, 0, 2 ],
[ 0, 0, 0, 1, 1 ]
],
"ranks": [
1,
1,
3,
4,
5
]
},
{
"title": "Static Default Grade to Passable",
"participants": 10,
"mode": "StaticDefault",
"default": 2,
"tallies": [
[ 2, 2, 2, 2, 2 ],
[ 2, 2, 1, 2, 2 ],
[ 0, 0, 4, 0, 0 ],
[ 0, 0, 0, 0, 2 ],
[ 0, 0, 0, 1, 1 ]
],
"ranks": [
4,
4,
3,
1,
2
]
}
]
Loading…
Cancel
Save