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