Ignore:
Timestamp:
09/22/20 16:26:36 (4 years ago)
Author:
bart
Message:

Minor fixes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • voting/src/main/java/geniusweb/voting/CollectedVotes.java

    r21 r22  
    44import java.util.HashMap;
    55import java.util.HashSet;
    6 import java.util.List;
    76import java.util.Map;
    87import java.util.Set;
    9 import java.util.stream.Collectors;
    108
    119import geniusweb.actions.PartyId;
     
    3129         *
    3230         * @param votesMap the {@link Votes} done for each party
    33          * @param power  the power of each party. The power is an integer number. In
    34          *               the voting process, the party powers add up to form the
    35          *               total voting power. This set may contain parties that are
    36          *               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.
    3735         */
    3836        public CollectedVotes(Map<PartyId, Votes> votesMap,
     
    132130         * @param votes a list of all votes for a particular bid. The parties that
    133131         *              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).
    139135         */
    140136        protected Set<PartyId> getMaxPowerGroup(Set<Vote> votes) {
     
    142138                Integer maxpower = 0;
    143139
    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;
    156145                        }
    157146                }
     
    169158         *         input size.
    170159         */
    171         protected Set<Set<PartyId>> getConsensusGroups(List<Vote> votes) {
     160        protected Set<Set<PartyId>> getConsensusGroups(Set<Vote> votes) {
    172161                Set<Set<PartyId>> groups = new HashSet<>();
    173162
     
    177166                for (ImmutableList<Vote> voteList : allVotePermutations) {
    178167                        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)) {
    181171                                groups.add(parties);
    182172                        }
     
    210200                }
    211201                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;
    212217        }
    213218
Note: See TracChangeset for help on using the changeset viewer.