source: voting/src/main/java/geniusweb/voting/votingevaluators/LargestAgreement.java@ 52

Last change on this file since 52 was 52, checked in by ruud, 14 months ago

Fixed small issues in domaineditor.

File size: 2.6 KB
Line 
1package geniusweb.voting.votingevaluators;
2
3import java.util.Collections;
4import java.util.Comparator;
5import java.util.HashSet;
6import java.util.Map;
7import java.util.Set;
8
9import com.fasterxml.jackson.annotation.JsonCreator;
10import com.fasterxml.jackson.annotation.JsonIgnore;
11
12import geniusweb.actions.PartyId;
13import geniusweb.inform.Agreements;
14import geniusweb.issuevalue.Bid;
15import geniusweb.voting.CollectedVotes;
16import geniusweb.voting.VotingEvaluator;
17
18/**
19 * Take the first possible agreement, and take the largest possible agreement.
20 * Stop after first agreement.
21 */
22public class LargestAgreement implements VotingEvaluator {
23 @JsonIgnore
24 private final CollectedVotes allVotes;
25
26 // caching
27 @JsonIgnore
28 private Agreements maxAgreements = null;
29
30 @JsonCreator
31 public LargestAgreement() {
32 this(new CollectedVotes(Collections.emptyMap(),
33 Collections.emptyMap()));
34 }
35
36 protected LargestAgreement(CollectedVotes votes) {
37 this.allVotes = votes;
38 }
39
40 @Override
41 public Agreements getAgreements() {
42 if (maxAgreements == null)
43 maxAgreements = collectVotes();
44 return maxAgreements;
45 }
46
47 @Override
48 public boolean isFinished() {
49 HashSet<PartyId> parties = new HashSet<PartyId>(
50 allVotes.getVotes().keySet());
51 parties.removeAll(getAgreements().getMap().keySet());
52 return parties.size() < 2;
53 }
54
55 @Override
56 public VotingEvaluator create(CollectedVotes votes) {
57 return new LargestAgreement(votes);
58 }
59
60 /**
61 * @return the agreements that were reached from the given votes, using the
62 * greedy algorithm picking the largets votesets.
63 */
64 protected Agreements collectVotes() {
65 Agreements agreement = new Agreements();
66
67 Map<Bid, Set<PartyId>> agrees = allVotes.getMaxAgreements();
68 if (agrees.isEmpty())
69 return agreement;// none
70
71 // find the one with max group power
72 Bid maxbid = agrees.keySet().stream().max(Comparator
73 .comparingInt(bid -> allVotes.getTotalPower(agrees.get(bid))))
74 .get();
75 agreement = agreement.with(new Agreements(maxbid, agrees.get(maxbid)));
76 return agreement;
77 }
78
79 @Override
80 public int hashCode() {
81 final int prime = 31;
82 int result = 1;
83 result = prime * result
84 + ((allVotes == null) ? 0 : allVotes.hashCode());
85 return result;
86 }
87
88 @Override
89 public boolean equals(Object obj) {
90 if (this == obj)
91 return true;
92 if (obj == null)
93 return false;
94 if (getClass() != obj.getClass())
95 return false;
96 LargestAgreement other = (LargestAgreement) obj;
97 if (allVotes == null) {
98 if (other.allVotes != null)
99 return false;
100 } else if (!allVotes.equals(other.allVotes))
101 return false;
102 return true;
103 }
104
105 @Override
106 public String toString() {
107 return "LargestAgreement";
108 }
109}
Note: See TracBrowser for help on using the repository browser.