source: src/main/java/parties/AlternatingMultipleOffers/RandomAmopParty.java@ 346

Last change on this file since 346 was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 2.8 KB
Line 
1package parties.AlternatingMultipleOffers;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.Random;
6
7import genius.core.AgentID;
8import genius.core.Bid;
9import genius.core.actions.Accept;
10import genius.core.actions.Action;
11import genius.core.actions.Offer;
12import genius.core.actions.OfferForVoting;
13import genius.core.actions.Reject;
14import genius.core.parties.AbstractNegotiationParty;
15import genius.core.protocol.AlternatingMultipleOffersProtocol;
16import genius.core.protocol.DefaultMultilateralProtocol;
17
18public class RandomAmopParty extends AbstractNegotiationParty {
19
20 private final static float P_ACCEPT = 0.3f;
21
22 private List<Bid> receivedBids = new ArrayList<Bid>();
23 private Random rnd = new Random();
24
25 enum Phase {
26 OFFER, VOTE
27 };
28
29 private Phase phase = Phase.OFFER;
30
31 /**
32 * We need to keep track of the number of votes we already did.
33 */
34 private int votingIndex = 0;
35
36 @Override
37 public Action chooseAction(List<Class<? extends Action>> possibleActions) {
38 if (possibleActions.contains(OfferForVoting.class)) {
39 Bid bid = generateRandomBid();
40 addOffer(bid);
41 return new OfferForVoting(getPartyId(), bid);
42 }
43 if (possibleActions.contains(Accept.class)) {
44 setPhase(Phase.VOTE);
45 if (votingIndex >= receivedBids.size()) {
46 throw new IllegalStateException("Received more requests for vote (" + (votingIndex + 1)
47 + ") than number of received offers (" + receivedBids.size() + ")!");
48 }
49 Bid bid = receivedBids.get(votingIndex++);
50
51 if (rnd.nextFloat() < P_ACCEPT) {
52 return new Accept(getPartyId(), bid);
53 } else {
54 return new Reject(getPartyId(), bid);
55 }
56 }
57 throw new IllegalStateException("Unknown action request " + possibleActions);
58 }
59
60 /**
61 * Try to track which phase we are in...
62 *
63 * @param newPhase
64 * the phase that we supposedly are in now.
65 */
66 private void setPhase(Phase newPhase) {
67 if (phase != newPhase) {
68 phase = newPhase;
69 if (phase == Phase.OFFER) {
70 // entered new offer round
71 if (receivedBids.size() != votingIndex) {
72 System.out.println("Warning: entered new Offer round but we voted only on " + votingIndex
73 + " of the " + receivedBids);
74 }
75 votingIndex = 0;
76 receivedBids.clear();
77 }
78 }
79 }
80
81 private void addOffer(Bid bid) {
82 setPhase(Phase.OFFER);
83 receivedBids.add(bid);
84 }
85
86 @Override
87 public void receiveMessage(AgentID sender, Action action) {
88 if (action instanceof OfferForVoting) {
89 addOffer(((Offer) action).getBid());
90 } else if (action instanceof Accept || action instanceof Reject) {
91 setPhase(Phase.VOTE);
92 } else {
93 System.out.println("Warning: ignoring unknown message " + action);
94 }
95 }
96
97 @Override
98 public Class<? extends DefaultMultilateralProtocol> getProtocol() {
99 return AlternatingMultipleOffersProtocol.class;
100 }
101
102 @Override
103 public String getDescription() {
104 return "Random AMOP Party";
105 }
106
107}
Note: See TracBrowser for help on using the repository browser.