[32] | 1 | package geniusweb.voting.votingevaluators;
|
---|
| 2 |
|
---|
| 3 | import java.util.Collections;
|
---|
| 4 | import java.util.Comparator;
|
---|
| 5 | import java.util.HashSet;
|
---|
| 6 | import java.util.Map;
|
---|
| 7 | import java.util.Set;
|
---|
| 8 |
|
---|
| 9 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
| 10 | import com.fasterxml.jackson.annotation.JsonIgnore;
|
---|
| 11 |
|
---|
| 12 | import geniusweb.actions.PartyId;
|
---|
| 13 | import geniusweb.inform.Agreements;
|
---|
| 14 | import geniusweb.issuevalue.Bid;
|
---|
| 15 | import geniusweb.voting.CollectedVotes;
|
---|
| 16 | import geniusweb.voting.VotingEvaluator;
|
---|
| 17 |
|
---|
| 18 | /**
|
---|
| 19 | * Take the first possible agreement, and take the largest possible agreement.
|
---|
| 20 | * Stop after first agreement.
|
---|
| 21 | */
|
---|
| 22 | public 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 LargestAgreementsLoop(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 | }
|
---|