Changeset 22 for voting/src/main/java/geniusweb
- Timestamp:
- 09/22/20 16:26:36 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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
Note:
See TracChangeset
for help on using the changeset viewer.