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