Merge pull request #4 from MieuxVoter/issue-3
Prepare for a more exhaustive test-suite, using JSONpull/8/head
commit
ba83ad8224
@ -0,0 +1,41 @@
|
|||||||
|
package fr.mieuxvoter.mj;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
|
||||||
|
public class TallyWithDefaultGrade extends Tally implements TallyInterface {
|
||||||
|
|
||||||
|
protected Integer defaultGrade = 0;
|
||||||
|
|
||||||
|
public TallyWithDefaultGrade(ProposalTallyInterface[] proposalsTallies, BigInteger amountOfJudges, Integer defaultGrade) {
|
||||||
|
super(proposalsTallies, amountOfJudges);
|
||||||
|
this.defaultGrade = defaultGrade;
|
||||||
|
fillWithDefaultGrade();
|
||||||
|
}
|
||||||
|
|
||||||
|
public TallyWithDefaultGrade(ProposalTallyInterface[] proposalsTallies, Long amountOfJudges, Integer defaultGrade) {
|
||||||
|
super(proposalsTallies, amountOfJudges);
|
||||||
|
this.defaultGrade = defaultGrade;
|
||||||
|
fillWithDefaultGrade();
|
||||||
|
}
|
||||||
|
|
||||||
|
public TallyWithDefaultGrade(ProposalTallyInterface[] proposalsTallies, Integer amountOfJudges, Integer defaultGrade) {
|
||||||
|
super(proposalsTallies, amountOfJudges);
|
||||||
|
this.defaultGrade = defaultGrade;
|
||||||
|
fillWithDefaultGrade();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void fillWithDefaultGrade() {
|
||||||
|
int amountOfProposals = getAmountOfProposals();
|
||||||
|
for (int i = 0 ; i < amountOfProposals ; i++) {
|
||||||
|
ProposalTallyInterface proposal = getProposalsTallies()[i];
|
||||||
|
BigInteger amountOfJudgments = proposal.getAmountOfJudgments();
|
||||||
|
BigInteger missingAmount = this.amountOfJudges.subtract(amountOfJudgments);
|
||||||
|
int missingSign = missingAmount.compareTo(BigInteger.ZERO);
|
||||||
|
assert(0 <= missingSign); // ERROR: More judgments than judges!
|
||||||
|
if (0 < missingSign) {
|
||||||
|
proposal.getTally()[this.defaultGrade] = proposal.getTally()[this.defaultGrade].add(missingAmount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
Do add your test cases in the JSON file.
|
||||||
|
|
||||||
|
Some of the sample tallies were made using python.
|
||||||
|
|
||||||
|
```python
|
||||||
|
import numpy as np
|
||||||
|
def randofsum_unbalanced(s, n):
|
||||||
|
# Where s = sum (e.g. 40 in your case) and n is the output array length (e.g. 4 in your case)
|
||||||
|
r = np.random.rand(n)
|
||||||
|
a = np.array(np.round((r/np.sum(r))*s,0),dtype=int)
|
||||||
|
while np.sum(a) > s:
|
||||||
|
a[np.random.choice(n)] -= 1
|
||||||
|
while np.sum(a) < s:
|
||||||
|
a[np.random.choice(n)] += 1
|
||||||
|
return a
|
||||||
|
```
|
@ -0,0 +1,54 @@
|
|||||||
|
[
|
||||||
|
|
||||||
|
{
|
||||||
|
"title": "Few participants",
|
||||||
|
"participants": 3,
|
||||||
|
"tallies": [
|
||||||
|
[1, 1, 1],
|
||||||
|
[1, 0, 2],
|
||||||
|
[3, 0, 0],
|
||||||
|
[2, 0, 1],
|
||||||
|
[0, 3, 0]
|
||||||
|
],
|
||||||
|
"ranks": [
|
||||||
|
3,
|
||||||
|
1,
|
||||||
|
5,
|
||||||
|
4,
|
||||||
|
2
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title": "Thousands of participants",
|
||||||
|
"participants": 37000,
|
||||||
|
"tallies": [
|
||||||
|
[11142, 6970, 4040, 1968, 9888, 2992],
|
||||||
|
[10141, 8971, 4043, 1965, 8884, 2996],
|
||||||
|
[14141, 8971, 1043, 1965, 7884, 2996]
|
||||||
|
],
|
||||||
|
"ranks": [
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
3
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title": "Millions of participants",
|
||||||
|
"participants": 72327456,
|
||||||
|
"tallies": [
|
||||||
|
[5272679, 19797001, 10732688, 9612936, 1379840, 16886281, 8646031],
|
||||||
|
[16354546, 11690342, 9451800, 14245973, 817593, 12461162, 7306040],
|
||||||
|
[9849171, 17970690, 14276861, 4606692, 16404594, 6760147, 2459301],
|
||||||
|
[2645563, 12907474, 1278331, 22843261, 8025412, 8964952, 15662463],
|
||||||
|
[16293252, 12277630, 38348, 14929905, 11087753, 10634266, 7066302]
|
||||||
|
],
|
||||||
|
"ranks": [
|
||||||
|
3,
|
||||||
|
4,
|
||||||
|
5,
|
||||||
|
1,
|
||||||
|
2
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
]
|
Loading…
Reference in new issue