Changeset 22 for voting


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

Minor fixes

Location:
voting
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • voting/pom.xml

    r21 r22  
    66        <groupId>geniusweb</groupId>
    77        <artifactId>voting</artifactId>
    8         <version>1.5.0</version> <!-- must equal ${geniusweb.version} -->
     8        <version>1.5.1</version> <!-- must equal ${geniusweb.version} -->
    99        <packaging>jar</packaging>
    1010
     
    1717                <passwd>${env.ARTIFACTORY_PASS}</passwd>
    1818                <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>
    2020        </properties>
    2121
  • 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
  • voting/src/test/java/geniusweb/voting/CollectedVotesTest.java

    r21 r22  
    22
    33import static org.junit.Assert.assertEquals;
     4import static org.junit.Assert.assertTrue;
    45import static org.mockito.Mockito.mock;
    56
     
    3536        private final PartyId partyQ = new PartyId("partyQ");
    3637        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);
    4648
    4749        private Map<PartyId, Integer> allpowers = new HashMap<>();
     
    7375                allpowers.put(partyQ, 1);
    7476                allpowers.put(partyR, 1);
     77                allpowers.put(partyS, 1);
    7578        }
    7679
     
    156159                                allpowers);
    157160
    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)));
    160163                // should give 1 solution: both parties join in.
    161164                System.out.println(consensus);
     
    169172                                allpowers);
    170173
    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)));
    173176                // should give 1 solution: P and Q join in. 4 is unreachable.
    174177                System.out.println(consensus);
     
    182185                                allpowers);
    183186
    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)));
    186189                // should give 4 solutions: P+Q, P+R, Q+R and P+Q+R
    187190                System.out.println(consensus);
     
    200203                                allpowers);
    201204
    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)));
    204207                // should give solutions P+Q and P+Q+R
    205208                System.out.println(consensus);
     
    244247        }
    245248
     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
    246300}
  • voting/src/test/java/geniusweb/voting/votingevaluators/LargestAgreementsLoopTest.java

    r21 r22  
    7474        @Test
    7575        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)));
    8384                // party 1,2,4 vote for A, party 1,2 vote for B, party 3,4 vote for C.
    8485                // the biggest vote is P,Q,S
Note: See TracChangeset for help on using the changeset viewer.