source: geniuswebcore/geniusweb/voting/votingevaluators/LargestAgreement.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.1 KB
RevLine 
[88]1from typing import Optional, Dict, FrozenSet
2
3from geniusweb.actions.PartyId import PartyId
4from geniusweb.inform.Agreements import Agreements
5from geniusweb.issuevalue.Bid import Bid
6from geniusweb.voting.CollectedVotes import CollectedVotes
7from geniusweb.voting.VotingEvaluator import VotingEvaluator
8
9
10class LargestAgreement(VotingEvaluator):
11 '''
12 Take the first possible agreement, and take the largest possible agreement.
13 Stop after first agreement.
14 '''
15
16 def __init__(self ) :
17 '''
18 @param votes the votes collected so far.
19 YOU MUST USE THE DEFAULT VALUE.
20 '''
21 self._allVotes = CollectedVotes({},{});
22 self._maxAgreements:Optional[Agreements ] = None
23
24 def getVotes(self):
25 return self._allVotes
26
27 def getAgreements(self)->Agreements :
28 if self._maxAgreements == None:
29 self._maxAgreements = self._collectVotes()
30 return self._maxAgreements #type:ignore
31
32 def isFinished(self)-> bool:
33 parties = set(self._allVotes.getVotes().keys())
34 for party in self.getAgreements().getMap().keys():
35 parties.remove(party)
36 return len(parties) < 2
37
38 def create(self, votes:CollectedVotes ) ->VotingEvaluator :
39 newagree= LargestAgreement()
40 # bit hacky, because we can not have overloaded constructor
41 # like in java
42 newagree._allVotes=votes
43 return newagree
44
45 def _collectVotes(self)->Agreements:
46 '''
47 @return the agreements that were reached from the given votes, using the
48 greedy algorithm picking the largets votesets.
49 '''
50 agreement = Agreements()
51
52 agrees:Dict[Bid, FrozenSet[PartyId]] = self._allVotes.getMaxAgreements()
53 if len(agrees)==0:
54 return agreement; # none
55
56 # find the bid with max group power
57 maxpower = -9999999999999
58 for bid in agrees:
59 power = self._allVotes.getTotalPower(agrees[bid])
60 if power>maxpower:
61 maxpower=power
62 maxbid = bid
63 agreemap = { party:maxbid for party in agrees[maxbid]}
64 agreement = agreement.With(Agreements(agreemap))
65 return agreement
66
67 def __hash__(self):
68 return hash(self._allVotes)
69
70 def __eq__(self, other):
71 return isinstance(other, self.__class__) and\
72 self._allVotes == other._allVotes
73
74 def __repr__(self):
75 return "LargestAgreement";
Note: See TracBrowser for help on using the repository browser.