source: src/main/java/bargainingchips/players/Agent.java@ 326

Last change on this file since 326 was 325, checked in by Tim Baarslag, 5 years ago

Wait multiple messages from coordinator or an incoming offer from seller.

There may be multiple messages waiting. The agent should process them all.

The agent can choose itself how to handle multiple messages (e.g. only use the last one).

File size: 4.0 KB
RevLine 
[316]1package bargainingchips.players;
[315]2
[325]3import java.util.List;
4
5import java.util.ArrayList;
[315]6import java.util.concurrent.BlockingQueue;
7
[316]8import bargainingchips.NegotiationContext;
9import bargainingchips.actions.Offer;
[323]10import bargainingchips.actions.OfferBy;
[316]11import bargainingchips.utilityfunctions.UtilityFunction;
[315]12/**
13 * An agent typically has a buyer or seller role and can send an receive offers.
14 * An agent has a utility function for evaluating any proposal in a given negotiation context.
15 */
16public abstract class Agent implements Runnable
17{
18 protected String name;
19 protected UtilityFunction u;
20 protected NegotiationContext negotiationContext;
21
22 int k;
23
24 public Agent(String name, UtilityFunction u, NegotiationContext negotiationContext,
[323]25 BlockingQueue<Offer> in, BlockingQueue<OfferBy> out,
[315]26 BlockingQueue<CoordinationMessage> cin, BlockingQueue<StatusMessage> cout)
27 {
28 this.name = name;
29 this.u = u;
30 this.negotiationContext = negotiationContext;
31 this.in = in;
32 this.out = out;
33 this.cin = cin;
34 this.cout = cout;
35 k = 0;
36 }
37
38 // Messaging from and to the opponent
39 protected BlockingQueue<Offer> in;
[323]40 protected BlockingQueue<OfferBy> out;
[315]41 // Messaging from and to the coordinator
42 protected BlockingQueue<CoordinationMessage> cin;
43 protected BlockingQueue<StatusMessage> cout;
44
45 protected abstract void receiveOffer(Offer bundle);
46
47 protected abstract Offer sendOffer();
48
49 protected abstract Offer sendOpeningOffer();
50
51 protected abstract void receiveCoordinationMessage(CoordinationMessage cpoll);
52
53 protected abstract StatusMessage sendStatusMessage();
54
55 @Override
56 public void run()
57 {
58 // A buyer may send an opening offer
59 Offer opening = do_sendOpeningOffer();
60 if (opening != null)
61 try {
[323]62 out.put(new OfferBy(name, opening));
[315]63 } catch (InterruptedException e1) {
64 e1.printStackTrace();
65 }
66
67 while (true)
68 {
69 try
70 {
[325]71 /* Wait for the messages from coordinator or an incoming offer from seller.
72 * There may be multiple messages waiting. The agent should process them all.
73 * The agent can choose itself how to handle multiple messages (e.g. only
74 * use the last one).
75 */
[315]76 while (true)
77 {
[325]78 List<CoordinationMessage> coordinationMessages = new ArrayList<CoordinationMessage>();
79 int messageNo = cin.drainTo(coordinationMessages);
80
81 if (messageNo > 0)
[315]82 {
[325]83 for (CoordinationMessage cm: coordinationMessages)
84 do_receiveCoordinationMessage(cm);
[315]85 break;
86 }
87
[325]88 List<Offer> offers = new ArrayList<Offer>();
89 int offersNo = in.drainTo(offers);
90
91 if (offersNo > 0)
[315]92 {
[325]93 for (Offer o: offers)
94 do_receiveOffer(o);
[315]95 break;
96 }
97 }
98
99 // A sync happened, so we can send out a new offer, given the new information
100 Offer sendOffer = do_sendOffer();
[323]101 out.put(new OfferBy(name, sendOffer));
[315]102
103 // Pause
[325]104// Thread.sleep(500);
105 Thread.sleep(500 + (int)(Math.random() * 1000));
[315]106 }
107 catch (InterruptedException e) {
108 e.printStackTrace();
109 }
110 }
111 }
112
113 private void do_receiveCoordinationMessage(CoordinationMessage cpoll)
114 {
115 System.out.println(this + " received coordination msg " + cpoll);
116 receiveCoordinationMessage(cpoll);
117 }
118
119 protected void do_receiveOffer(Offer o)
120 {
121 System.out.println(this + " received " + o
122 + (o.isBid() ? (", with util = " + u.getUtility(o.getBundle())) : "")
123 );
124 receiveOffer(o);
125 }
126
127 protected Offer do_sendOffer()
128 {
129 Offer o = sendOffer();
130 System.out.println(this + " sends " + o);
131 k++;
132 return o;
133 }
134
135 protected Offer do_sendOpeningOffer()
136 {
137 Offer o = sendOpeningOffer();
138 if (o != null)
139 {
140 System.out.println(this + " sends opening offer " + o);
141 k++;
142 }
143 return o;
144 }
145
146 @Override
147 public String toString()
148 {
149 return "#" + k + ": " + name;
150 }
151
152 public String toDescription()
153 {
154 return name + " with u = " + u;
155 }
156}
Note: See TracBrowser for help on using the repository browser.