source: geniuswebcore/geniusweb/voting/votingevaluators/LargestAgreementsLoop.py@ 88

Last change on this file since 88 was 88, checked in by Bart Vastenhouw, 3 years ago

Added python SimpleRunner GUI

File size: 2.3 KB
Line 
1from geniusweb.inform.Agreements import Agreements
2from geniusweb.voting.CollectedVotes import CollectedVotes
3from geniusweb.voting.VotingEvaluator import VotingEvaluator
4
5
6class LargestAgreementsLoop (VotingEvaluator):
7 '''
8 {@link #getAgreements()} collects all possible {@link Agreements}. We're
9 finished only when {@link #getAgreements()} covers all except possibly 1
10 party.
11
12 The agreements is a list of {@link Bid}s and a maximum-sized list of
13 {@link PartyId}s that placed a satisfied vote for that bid. maximum-sized
14 means that there is no larger subset of votes for that bid. There may be
15 other subsets of equal size for the bid. If there are no agreements at all
16 for a bid, the bid is not placed in the map.
17 '''
18
19 def __init__(self) :
20 self._allVotes = CollectedVotes({},{})
21 self._maxAgreements = None
22
23
24 def getAgreements(self)->Agreements:
25 if self._maxAgreements == None:
26 self._maxAgreements = self._collectAgreements()
27 return self._maxAgreements;
28
29 def isFinished(self)->bool:
30 parties = set(self._allVotes.keys())
31 parties= set([ party for party in parties
32 if not party in self.getAgreements().getMap().keys()])
33 return len(parties) < 2
34
35 def create(self, votes:CollectedVotes ) -> "LargestAgreementsLoop" :
36 res= LargestAgreementsLoop()
37 res._allVotes = votes
38 return res
39
40 def _collectAgreements(self)-> Agreements :
41 '''
42 @return the agreements that were reached from the given votes, using the
43 greedy algorithm picking the largets votesets.
44 '''
45 remainingvotes = self._allVotes;
46 newagreements = Agreements()
47 while True:
48 agrees = remainingvotes.getMaxAgreements()
49 if len(agrees)==0:
50 break;
51 # find the bid with max group power
52 maxbid = max(agrees.keys(),
53 key=lambda bid: self._allVotes.getTotalPower(agrees[bid]))
54
55 maxparties = agrees[maxbid]
56
57 agreemap = {party:maxbid for party in maxparties }
58 newagreements = newagreements.With(Agreements(agreemap))
59 remainingvotes = remainingvotes.Without(maxparties)
60 return newagreements
61
62 def __hash__(self):
63 '''
64 WARNING you must manually set prime=37 here to fix collision with
65 {@link LargestAgreement}.
66 '''
67 return 37*hash(self._allVotes)
68
69 def __eq__(self, other):
70 return isinstance(other, self.__class__) and\
71 self._allVotes == other._allVotes
72
73 def __repr__(self):
74 return "LargestAgreementsLoop";
75
Note: See TracBrowser for help on using the repository browser.