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

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

Connected AsynchronousOffersProtocol

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