source: src/main/java/genius/core/protocol/MediatorFeedbackBasedProtocol.java

Last change on this file was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 3.5 KB
Line 
1package genius.core.protocol;
2
3import java.util.ArrayList;
4import java.util.Arrays;
5import java.util.Collections;
6import java.util.HashMap;
7import java.util.List;
8import java.util.Map;
9
10import genius.core.Bid;
11import genius.core.Feedback;
12import genius.core.Vote;
13import genius.core.actions.Action;
14import genius.core.actions.GiveFeedback;
15import genius.core.actions.OfferForFeedback;
16import genius.core.parties.NegotiationParty;
17import genius.core.session.Round;
18import genius.core.session.Session;
19import genius.core.session.Turn;
20
21/**
22 * This is the protocol for the mediated feedback. This protocol has this cycle:
23 * <ol>
24 * <li>mediator proposes bid
25 * <li>participants give feedback (better, worse, same)
26 * </ol>
27 *
28 * The accepted bid is the bid from the last round that contained only "better"
29 * or "same" votes.
30 */
31public class MediatorFeedbackBasedProtocol extends MediatorProtocol {
32
33 @Override
34 public Round getRoundStructure(List<NegotiationParty> parties, Session session) {
35
36 // initialize and split parties
37 Round round = new Round();
38 NegotiationParty mediator = getMediator(parties);
39 List<NegotiationParty> otherParties = getNonMediators(parties);
40
41 // mediator places offer
42 round.addTurn(new Turn(mediator, OfferForFeedback.class));
43
44 // other parties give feedback
45 for (NegotiationParty otherParty : otherParties) {
46 round.addTurn(new Turn(otherParty, GiveFeedback.class));
47 }
48
49 return round;
50 }
51
52 @Override
53 public Map<NegotiationParty, List<NegotiationParty>> getActionListeners(List<NegotiationParty> parties) {
54
55 Map<NegotiationParty, List<NegotiationParty>> map = new HashMap<NegotiationParty, List<NegotiationParty>>();
56
57 NegotiationParty mediator = getMediator(parties);
58
59 // all other negotiating parties listen to the mediator
60 for (NegotiationParty party : getNonMediators(parties)) {
61 map.put(party, Arrays.asList(mediator));
62 }
63
64 // the mediator listens to all other negotiating parties.
65 map.put(mediator, getNonMediators(parties));
66
67 return map;
68 }
69
70 /**
71 *
72 * Returns the last offer that got accepted.
73 *
74 * @param session
75 * The complete session history up to this point
76 * @return The current agreement (the bid from the last action from the
77 * mediator that was {@link Vote#ACCEPT}), or null if no agreement
78 * yet.
79 */
80 @Override
81 public Bid getCurrentAgreement(Session session, List<NegotiationParty> parties) {
82
83 // search from last to first bid for an accepted bid.
84 List<Round> rounds = new ArrayList<Round>(session.getRounds());
85 Collections.reverse(rounds);
86 for (Round round : rounds) {
87 Bid agreement = getAcceptedBid(round);
88 if (agreement != null)
89 return agreement;
90 }
91
92 return null;
93 }
94
95 /********************* support functions ************************/
96
97 /**
98 * A round contains an accepted bid if all votes are "better" or "same".
99 *
100 * @param round
101 * the round to check
102 * @return the accepted bid this round if the bid was accepted, or null if
103 * there was no accepted bid in this round.
104 */
105 private Bid getAcceptedBid(Round round) {
106 for (Action action : round.getActions()) {
107 if (action instanceof GiveFeedback && ((GiveFeedback) action).getFeedback() == Feedback.WORSE) {
108 return null;
109 }
110 }
111 // the round is accepted. Return the accepted bid, which should be in
112 // the first turn.
113 return ((OfferForFeedback) round.getTurns().get(0).getAction()).getBid();
114
115 }
116
117}
Note: See TracBrowser for help on using the repository browser.