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.utilityfunctions.UtilityFunction;
|
---|
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 | */
|
---|
16 | public 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,
|
---|
25 | BlockingQueue<Offer> in, BlockingQueue<OfferBy> out,
|
---|
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;
|
---|
40 | protected BlockingQueue<OfferBy> out;
|
---|
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 {
|
---|
62 | out.put(new OfferBy(name, opening));
|
---|
63 | } catch (InterruptedException e1) {
|
---|
64 | e1.printStackTrace();
|
---|
65 | }
|
---|
66 |
|
---|
67 | while (true)
|
---|
68 | {
|
---|
69 | try
|
---|
70 | {
|
---|
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 | */
|
---|
76 | while (true)
|
---|
77 | {
|
---|
78 | List<CoordinationMessage> coordinationMessages = new ArrayList<CoordinationMessage>();
|
---|
79 | int messageNo = cin.drainTo(coordinationMessages);
|
---|
80 |
|
---|
81 | if (messageNo > 0)
|
---|
82 | {
|
---|
83 | for (CoordinationMessage cm: coordinationMessages)
|
---|
84 | do_receiveCoordinationMessage(cm);
|
---|
85 | break;
|
---|
86 | }
|
---|
87 |
|
---|
88 | List<Offer> offers = new ArrayList<Offer>();
|
---|
89 | int offersNo = in.drainTo(offers);
|
---|
90 |
|
---|
91 | if (offersNo > 0)
|
---|
92 | {
|
---|
93 | for (Offer o: offers)
|
---|
94 | do_receiveOffer(o);
|
---|
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();
|
---|
101 | out.put(new OfferBy(name, sendOffer));
|
---|
102 |
|
---|
103 | // Pause
|
---|
104 | // Thread.sleep(500);
|
---|
105 | Thread.sleep(500 + (int)(Math.random() * 1000));
|
---|
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 | }
|
---|