source: src/main/java/parties/simplemediator/RandomFlippingMediator.java@ 203

Last change on this file since 203 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: 4.0 KB
Line 
1package parties.simplemediator;
2
3import java.util.List;
4
5import genius.core.AgentID;
6import genius.core.Bid;
7import genius.core.Vote;
8import genius.core.actions.Action;
9import genius.core.actions.InformVotingResult;
10import genius.core.actions.OfferForVoting;
11import genius.core.actions.VoteForOfferAcceptance;
12import genius.core.issue.Issue;
13import genius.core.issue.Value;
14import genius.core.parties.AbstractNegotiationParty;
15import genius.core.parties.Mediator;
16import genius.core.parties.NegotiationInfo;
17import genius.core.protocol.DefaultMultilateralProtocol;
18import genius.core.protocol.MultilateralProtocol;
19import genius.core.protocol.SimpleMediatorBasedProtocol;
20
21/**
22 * This mediator generates random bids until all agents accept. Then, it
23 * randomly flips one issue of the current offer to generate a new offer. It
24 * keeps going until the deadline is reached.
25 * <p/>
26 * This class was adapted from Reyhan's parties.PureRandomFlippingMediator class
27 * (see svn history for details about that class), and adapted to fit into the
28 * new framework.
29 */
30public class RandomFlippingMediator extends AbstractNegotiationParty implements Mediator {
31
32 /**
33 * Holds whether the current offer is acceptable by all parties.
34 */
35 private boolean isAcceptable;
36
37 /**
38 * The most recently accepted bid.
39 */
40 private Bid lastAcceptedBid;
41
42 /**
43 * The most recently proposed bid.
44 */
45 private Bid lastProposedBid;
46
47 @Override
48 public void init(NegotiationInfo info) {
49 super.init(info);
50 isAcceptable = true;
51 }
52
53 @Override
54 public Action chooseAction(List<Class<? extends Action>> possibleActions) {
55
56 if (possibleActions.contains(OfferForVoting.class)) {
57 // check if last proposition was acceptable
58 if (isAcceptable) {
59 lastAcceptedBid = lastProposedBid;
60 }
61
62 // init acceptable to true for next round
63 isAcceptable = true;
64
65 if (lastAcceptedBid == null) {
66 lastProposedBid = generateRandomBid();
67 return new OfferForVoting(getPartyId(), lastProposedBid);
68 } else {
69 lastProposedBid = modifyLastBidRandomly();
70 return new OfferForVoting(getPartyId(), lastProposedBid);
71 }
72 } else if (possibleActions.contains(InformVotingResult.class)) {
73 return new InformVotingResult(getPartyId(), lastProposedBid, isAcceptable ? Vote.ACCEPT : Vote.REJECT);
74 }
75
76 throw new IllegalArgumentException(
77 "RandomFlippingMediator used with wrong protocol: can not handle request for " + possibleActions);
78 }
79
80 /**
81 * This method is called when an observable action is performed. Observable
82 * actions are defined in
83 * {@link MultilateralProtocol#getActionListeners(java.util.List)}
84 *
85 * @param sender
86 * The initiator of the action
87 * @param arguments
88 * The action performed
89 */
90 @Override
91 public void receiveMessage(AgentID sender, Action arguments) {
92 if (arguments instanceof VoteForOfferAcceptance) {
93 isAcceptable &= ((VoteForOfferAcceptance) arguments).getVote() == Vote.ACCEPT;
94 }
95 }
96
97 @Override
98 public Class<? extends DefaultMultilateralProtocol> getProtocol() {
99 return SimpleMediatorBasedProtocol.class;
100 }
101
102 /**
103 * modifies the most recently accepted bid by changing a single issue value.
104 *
105 * @return the modified bid
106 */
107 private Bid modifyLastBidRandomly() {
108 try {
109 List<Issue> issues = utilitySpace.getDomain().getIssues();
110 Bid modifiedBid = new Bid(lastAcceptedBid);
111 Value newValue;
112 Issue currentIssue;
113
114 int currentIndex;
115 do {
116 currentIssue = issues.get(rand.nextInt(issues.size()));
117 currentIndex = currentIssue.getNumber();
118 newValue = getRandomValue(currentIssue);
119 } while (newValue.equals(lastAcceptedBid.getValue(currentIndex)));
120
121 modifiedBid = modifiedBid.putValue(currentIndex, newValue);
122
123 return modifiedBid;
124 } catch (Exception e) {
125 System.out.println(
126 "Can not generate random bid or receiveMessage preference list " + "problem:" + e.getMessage());
127 return null;
128 }
129 }
130
131 @Override
132 public String getDescription() {
133 return "Random Flipping Mediator";
134 }
135}
Note: See TracBrowser for help on using the repository browser.