1 | package genius.core.protocol;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 |
|
---|
5 | import genius.core.actions.Action;
|
---|
6 | import genius.core.actions.Offer;
|
---|
7 | import genius.core.parties.NegotiationParty;
|
---|
8 | import genius.core.session.Round;
|
---|
9 | import genius.core.session.Session;
|
---|
10 | import genius.core.session.Turn;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Implementation of an alternating offer protocol using majority voting. This
|
---|
14 | * is essentially equal to the {@link AlternatingMultipleOffersProtocol} but now
|
---|
15 | * an offer is accepted if the majority (instead of all) accept.
|
---|
16 | * <p/>
|
---|
17 | * Protocol:
|
---|
18 | *
|
---|
19 | * <pre>
|
---|
20 | * Round 1 (offers): Each agent makes an offer
|
---|
21 | * Round 2 (voting): Each agent votes for each offer on the table
|
---|
22 | *
|
---|
23 | * The offer that is supported by the most parties, will stay on the table.
|
---|
24 | * If a new offer has more supporting parties, it overwrites the old offer.
|
---|
25 | * This protocol always has some agreement.
|
---|
26 | *
|
---|
27 | * When deadline reached, the most recent agreement will be considered the final agreement.
|
---|
28 | * </pre>
|
---|
29 | *
|
---|
30 | * @author David Festen
|
---|
31 | * @author Reyhan
|
---|
32 | * @author W.Pasman removed all code except acceptedOffer and extend existing
|
---|
33 | * AMOP
|
---|
34 | */
|
---|
35 | public class AlternatingMajorityConsensusProtocol extends AlternatingMultipleOffersProtocol {
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Holds the number of parties that voted for the most recently accepted
|
---|
39 | * offer.
|
---|
40 | */
|
---|
41 | private int mostRecentlyAcceptedOfferVoteCount;
|
---|
42 | /**
|
---|
43 | * Holds the most recently accepted offer. i.e. The offer with the most
|
---|
44 | * support
|
---|
45 | */
|
---|
46 | private Offer mostRecentlyAcceptedOffer;
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public boolean isFinished(Session session, List<NegotiationParty> parties) {
|
---|
50 | if (isVotingRound(session)) {
|
---|
51 | Round votingRound = session.getMostRecentRound();
|
---|
52 | Round offerRound = session.getRounds().get(session.getRoundNumber() - 2);
|
---|
53 | acceptedOffer(votingRound, offerRound);
|
---|
54 | }
|
---|
55 |
|
---|
56 | // if everyone accepts a vote, we're done, otherwise continue
|
---|
57 | return mostRecentlyAcceptedOfferVoteCount == parties.size();
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * returns the first offer with more support than the current one, or null
|
---|
62 | * if no such offer.
|
---|
63 | *
|
---|
64 | * @param votingRound
|
---|
65 | * the round with the voting (expected number of turns is agent#
|
---|
66 | * * agent#)
|
---|
67 | * @param offerRound
|
---|
68 | * the round with the offers (expected number of turns is agent#)
|
---|
69 | * @return The best offer on the table
|
---|
70 | */
|
---|
71 | protected Offer acceptedOffer(Round votingRound, Round offerRound) {
|
---|
72 | allActionsAreOffers(offerRound);
|
---|
73 | int numOffers = offerRound.getTurns().size();
|
---|
74 | List<Turn> turns = votingRound.getTurns();
|
---|
75 | List<Action> voteActions = offerRound.getActions();
|
---|
76 |
|
---|
77 | int availableOfferRounds = Math.min(numOffers, numOffers > 0 ? turns.size() / numOffers : 0);
|
---|
78 |
|
---|
79 | for (int offerNumber = 0; offerNumber < availableOfferRounds; offerNumber++) {
|
---|
80 | // update the maxNumberOfVotes we got
|
---|
81 | int votes = nrOfVotes(numOffers, turns, offerNumber);
|
---|
82 | // if enough votes, accept bid
|
---|
83 | if (votes > mostRecentlyAcceptedOfferVoteCount) {
|
---|
84 | mostRecentlyAcceptedOffer = (Offer) voteActions.get(offerNumber);
|
---|
85 | mostRecentlyAcceptedOfferVoteCount = votes;
|
---|
86 | System.out.println(" New most recently accepted bid (votes=" + mostRecentlyAcceptedOfferVoteCount
|
---|
87 | + "/" + numOffers + "): " + mostRecentlyAcceptedOffer);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | return mostRecentlyAcceptedOffer;
|
---|
92 | }
|
---|
93 |
|
---|
94 | }
|
---|