- Timestamp:
- 09/22/20 16:26:36 (4 years ago)
- Location:
- voting
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
voting/pom.xml
r21 r22 6 6 <groupId>geniusweb</groupId> 7 7 <artifactId>voting</artifactId> 8 <version>1.5. 0</version> <!-- must equal ${geniusweb.version} -->8 <version>1.5.1</version> <!-- must equal ${geniusweb.version} --> 9 9 <packaging>jar</packaging> 10 10 … … 17 17 <passwd>${env.ARTIFACTORY_PASS}</passwd> 18 18 <jackson-2-version>2.9.10</jackson-2-version> 19 <geniusweb.version>1.5. 0</geniusweb.version>19 <geniusweb.version>1.5.1</geniusweb.version> 20 20 </properties> 21 21 -
voting/src/main/java/geniusweb/voting/CollectedVotes.java
r21 r22 4 4 import java.util.HashMap; 5 5 import java.util.HashSet; 6 import java.util.List;7 6 import java.util.Map; 8 7 import java.util.Set; 9 import java.util.stream.Collectors;10 8 11 9 import geniusweb.actions.PartyId; … … 31 29 * 32 30 * @param votesMap the {@link Votes} done for each party 33 * @param power the power of each party. The power is an integer number. In34 * the voting process, the party powers add up to form the35 * total voting power. This set may contain parties that are36 * not in newmap.31 * @param power the power of each party. The power is an integer number. 32 * In the voting process, the party powers add up to form 33 * the total voting power. This set may contain parties that 34 * are not in newmap. 37 35 */ 38 36 public CollectedVotes(Map<PartyId, Votes> votesMap, … … 132 130 * @param votes a list of all votes for a particular bid. The parties that 133 131 * placed the votes are called 'voters' 134 * @return a subset of the voters for which holds (1) the min-power 135 * condition of all these voters is satisfied (2) the power of this 136 * subset of voters is maximal (no other satisfied set of voters 137 * with bigger power exists) maximum-power that placed votes with 138 * holding conditions.This algorithm works in quadratic time. 132 * @return a consensusgroup with maximum power, so there is no other 133 * consensus group that has more power (but there may be other 134 * groups with same power). 139 135 */ 140 136 protected Set<PartyId> getMaxPowerGroup(Set<Vote> votes) { … … 142 138 Integer maxpower = 0; 143 139 144 for (Vote vote : votes) { 145 int target = vote.getMinPower(); 146 147 // find parties that would join if we reach that power 148 Set<PartyId> group = votes.stream() 149 .filter(v -> target >= v.getMinPower()) 150 .map(v -> v.getActor()).collect(Collectors.toSet()); 151 // check what the group's power is 152 Integer groupPower = getTotalPower(group); 153 if (groupPower >= target && groupPower > maxpower) { 154 maxgroup = group; 155 maxpower = groupPower; 140 for (Set<PartyId> parties : getConsensusGroups(votes)) { 141 Integer power = getTotalPower(parties); 142 if (power > maxpower) { 143 maxgroup = parties; 144 maxpower = power; 156 145 } 157 146 } … … 169 158 * input size. 170 159 */ 171 protected Set<Set<PartyId>> getConsensusGroups( List<Vote> votes) {160 protected Set<Set<PartyId>> getConsensusGroups(Set<Vote> votes) { 172 161 Set<Set<PartyId>> groups = new HashSet<>(); 173 162 … … 177 166 for (ImmutableList<Vote> voteList : allVotePermutations) { 178 167 Set<PartyId> parties = getParties(voteList); 179 if (parties.size() >= 2 180 && getTotalPower(parties) >= getMinPower(voteList)) { 168 Integer totalpower = getTotalPower(parties); 169 if (parties.size() >= 2 && totalpower >= getMinPower(voteList) 170 && totalpower <= getMaxPower(voteList)) { 181 171 groups.add(parties); 182 172 } … … 210 200 } 211 201 return max; 202 } 203 204 /** 205 * 206 * @param votes a list of {@link Vote}s on one Bid. All parties must be 207 * known 208 * @return the maximum voting power needed for this group, so that all can 209 * accept. This is equal to the min of the vote.getMaxPower 210 */ 211 private Integer getMaxPower(ImmutableList<Vote> votes) { 212 int min = Integer.MAX_VALUE; 213 for (Vote vote : votes) { 214 min = Math.min(min, vote.getMaxPower()); 215 } 216 return min; 212 217 } 213 218 -
voting/src/test/java/geniusweb/voting/CollectedVotesTest.java
r21 r22 2 2 3 3 import static org.junit.Assert.assertEquals; 4 import static org.junit.Assert.assertTrue; 4 5 import static org.mockito.Mockito.mock; 5 6 … … 35 36 private final PartyId partyQ = new PartyId("partyQ"); 36 37 private final PartyId partyR = new PartyId("partyR"); 37 38 private final Vote votePA2 = new Vote(partyP, bidA, 2); 39 private final Vote voteQA2 = new Vote(partyQ, bidA, 2); 40 private final Vote voteQB3 = new Vote(partyQ, bidB, 3); 41 private final Vote voteQB2 = new Vote(partyQ, bidB, 2); 42 private final Vote voteRA2 = new Vote(partyR, bidA, 2); 43 private final Vote voteRA3 = new Vote(partyR, bidA, 3); 44 private final Vote voteRA4 = new Vote(partyR, bidA, 4); 45 private final Vote voteRB2 = new Vote(partyR, bidB, 2); 38 private final PartyId partyS = new PartyId("partyS"); 39 40 private final Vote votePA2 = new Vote(partyP, bidA, 2, 9); 41 private final Vote voteQA2 = new Vote(partyQ, bidA, 2, 9); 42 private final Vote voteQB3 = new Vote(partyQ, bidB, 3, 9); 43 private final Vote voteQB2 = new Vote(partyQ, bidB, 2, 9); 44 private final Vote voteRA2 = new Vote(partyR, bidA, 2, 9); 45 private final Vote voteRA3 = new Vote(partyR, bidA, 3, 9); 46 private final Vote voteRA4 = new Vote(partyR, bidA, 4, 9); 47 private final Vote voteRB2 = new Vote(partyR, bidB, 2, 9); 46 48 47 49 private Map<PartyId, Integer> allpowers = new HashMap<>(); … … 73 75 allpowers.put(partyQ, 1); 74 76 allpowers.put(partyR, 1); 77 allpowers.put(partyS, 1); 75 78 } 76 79 … … 156 159 allpowers); 157 160 158 Set<Set<PartyId>> consensus = cv 159 .getConsensusGroups(Arrays.asList(votePA2, voteQA2));161 Set<Set<PartyId>> consensus = cv.getConsensusGroups( 162 new HashSet<>(Arrays.asList(votePA2, voteQA2))); 160 163 // should give 1 solution: both parties join in. 161 164 System.out.println(consensus); … … 169 172 allpowers); 170 173 171 Set<Set<PartyId>> consensus = cv 172 .getConsensusGroups(Arrays.asList(votePA2, voteQA2, voteRA4));174 Set<Set<PartyId>> consensus = cv.getConsensusGroups( 175 new HashSet<>(Arrays.asList(votePA2, voteQA2, voteRA4))); 173 176 // should give 1 solution: P and Q join in. 4 is unreachable. 174 177 System.out.println(consensus); … … 182 185 allpowers); 183 186 184 Set<Set<PartyId>> consensus = cv 185 .getConsensusGroups(Arrays.asList(votePA2, voteQA2, voteRA2));187 Set<Set<PartyId>> consensus = cv.getConsensusGroups( 188 new HashSet<>(Arrays.asList(votePA2, voteQA2, voteRA2))); 186 189 // should give 4 solutions: P+Q, P+R, Q+R and P+Q+R 187 190 System.out.println(consensus); … … 200 203 allpowers); 201 204 202 Set<Set<PartyId>> consensus = cv 203 .getConsensusGroups(Arrays.asList(votePA2, voteQA2, voteRA4));205 Set<Set<PartyId>> consensus = cv.getConsensusGroups( 206 new HashSet<>(Arrays.asList(votePA2, voteQA2, voteRA4))); 204 207 // should give solutions P+Q and P+Q+R 205 208 System.out.println(consensus); … … 244 247 } 245 248 249 @Test 250 public void getConsensusGroupMaxPower() { 251 Vote P = new Vote(partyP, bidA, 2, 3); 252 Vote Q = new Vote(partyQ, bidA, 2, 3); 253 Vote R = new Vote(partyR, bidA, 2, 3); 254 Vote S = new Vote(partyS, bidA, 2, 3); 255 Map<PartyId, Votes> allvotes = new HashMap<>(); 256 allvotes.put(partyP, new Votes(partyP, Arrays.asList(P))); 257 allvotes.put(partyQ, new Votes(partyQ, Arrays.asList(Q))); 258 allvotes.put(partyR, new Votes(partyR, Arrays.asList(R))); 259 allvotes.put(partyS, new Votes(partyS, Arrays.asList(S))); 260 261 CollectedVotes cv = new CollectedVotes(allvotes, allpowers); 262 Set<Set<PartyId>> consensus = cv.getConsensusGroups( 263 new HashSet<Vote>(Arrays.asList(P, Q, R, S))); 264 System.out.println(consensus); 265 // check that all are size 2 or 3. 266 for (Set<PartyId> set : consensus) { 267 assertTrue(set.size() == 2 || set.size() == 3); 268 } 269 270 } 271 272 @Test 273 public void getConsensusGroupMaxPowerModifiedPartyPowers() { 274 // party R gets power 2. 275 Vote P = new Vote(partyP, bidA, 2, 3); 276 Vote Q = new Vote(partyQ, bidA, 2, 3); 277 Vote R = new Vote(partyR, bidA, 2, 3); 278 Map<PartyId, Votes> allvotes = new HashMap<>(); 279 allvotes.put(partyP, new Votes(partyP, Arrays.asList(P))); 280 allvotes.put(partyQ, new Votes(partyQ, Arrays.asList(Q))); 281 allvotes.put(partyR, new Votes(partyR, Arrays.asList(R))); 282 283 Map<PartyId, Integer> powers = new HashMap<PartyId, Integer>(); 284 powers.put(partyP, 1); 285 powers.put(partyQ, 1); 286 powers.put(partyR, 2); 287 288 CollectedVotes cv = new CollectedVotes(allvotes, powers); 289 Set<Set<PartyId>> consensus = cv 290 .getConsensusGroups(new HashSet<Vote>(Arrays.asList(P, Q, R))); 291 System.out.println(consensus); 292 // R has power 2 now. That means groups of size 3 are not posisble. 293 // check that all are size 2. 294 for (Set<PartyId> set : consensus) { 295 assertTrue(set.size() == 2); 296 } 297 298 } 299 246 300 } -
voting/src/test/java/geniusweb/voting/votingevaluators/LargestAgreementsLoopTest.java
r21 r22 74 74 @Test 75 75 public void testCollectVotes() { 76 Votes vote1AB = new Votes(party1, 77 Arrays.asList(new Vote(party1, a, 2), new Vote(party1, b, 2))); 78 Votes vote2AB = new Votes(party2, 79 Arrays.asList(new Vote(party2, a, 2), new Vote(party2, b, 2))); 80 Votes vote3C = new Votes(party3, Arrays.asList(new Vote(party3, c, 2))); 81 Votes vote4AC = new Votes(party4, 82 Arrays.asList(new Vote(party4, a, 2), new Vote(party4, c, 2))); 76 Votes vote1AB = new Votes(party1, Arrays 77 .asList(new Vote(party1, a, 2, 9), new Vote(party1, b, 2, 9))); 78 Votes vote2AB = new Votes(party2, Arrays 79 .asList(new Vote(party2, a, 2, 9), new Vote(party2, b, 2, 9))); 80 Votes vote3C = new Votes(party3, 81 Arrays.asList(new Vote(party3, c, 2, 9))); 82 Votes vote4AC = new Votes(party4, Arrays 83 .asList(new Vote(party4, a, 2, 9), new Vote(party4, c, 2, 9))); 83 84 // party 1,2,4 vote for A, party 1,2 vote for B, party 3,4 vote for C. 84 85 // the biggest vote is P,Q,S
Note:
See TracChangeset
for help on using the changeset viewer.