[316] | 1 | package bargainingchips.players;
|
---|
[315] | 2 |
|
---|
[325] | 3 | import java.util.List;
|
---|
| 4 |
|
---|
| 5 | import java.util.ArrayList;
|
---|
[315] | 6 | import java.util.concurrent.BlockingQueue;
|
---|
| 7 |
|
---|
[316] | 8 | import bargainingchips.NegotiationContext;
|
---|
| 9 | import bargainingchips.actions.Offer;
|
---|
[323] | 10 | import bargainingchips.actions.OfferBy;
|
---|
[340] | 11 | import bargainingchips.messaging.coordination.CoordinationMessage;
|
---|
| 12 | import bargainingchips.messaging.status.StatusMessage;
|
---|
[328] | 13 | import bargainingchips.outcomes.Outcome;
|
---|
[316] | 14 | import bargainingchips.utilityfunctions.UtilityFunction;
|
---|
[315] | 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 | */
|
---|
[340] | 19 | public abstract class Agent implements Runnable // no comma here
|
---|
[315] | 20 | {
|
---|
[331] | 21 | protected final String name;
|
---|
[315] | 22 | protected UtilityFunction u;
|
---|
| 23 | protected NegotiationContext negotiationContext;
|
---|
| 24 |
|
---|
[338] | 25 | protected int k;
|
---|
[315] | 26 |
|
---|
| 27 | public Agent(String name, UtilityFunction u, NegotiationContext negotiationContext,
|
---|
[323] | 28 | BlockingQueue<Offer> in, BlockingQueue<OfferBy> out,
|
---|
[315] | 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;
|
---|
[323] | 43 | protected BlockingQueue<OfferBy> out;
|
---|
[315] | 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 |
|
---|
[328] | 50 | protected abstract void receiveOutcome(Outcome outcome);
|
---|
| 51 |
|
---|
[315] | 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 {
|
---|
[323] | 67 | out.put(new OfferBy(name, opening));
|
---|
[315] | 68 | } catch (InterruptedException e1) {
|
---|
| 69 | e1.printStackTrace();
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | while (true)
|
---|
| 73 | {
|
---|
| 74 | try
|
---|
| 75 | {
|
---|
[325] | 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 | */
|
---|
[315] | 81 | while (true)
|
---|
| 82 | {
|
---|
[325] | 83 | List<CoordinationMessage> coordinationMessages = new ArrayList<CoordinationMessage>();
|
---|
| 84 | int messageNo = cin.drainTo(coordinationMessages);
|
---|
| 85 |
|
---|
| 86 | if (messageNo > 0)
|
---|
[315] | 87 | {
|
---|
[325] | 88 | for (CoordinationMessage cm: coordinationMessages)
|
---|
| 89 | do_receiveCoordinationMessage(cm);
|
---|
[315] | 90 | break;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[325] | 93 | List<Offer> offers = new ArrayList<Offer>();
|
---|
| 94 | int offersNo = in.drainTo(offers);
|
---|
| 95 |
|
---|
| 96 | if (offersNo > 0)
|
---|
[315] | 97 | {
|
---|
[325] | 98 | for (Offer o: offers)
|
---|
[328] | 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);
|
---|
[315] | 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();
|
---|
[323] | 112 | out.put(new OfferBy(name, sendOffer));
|
---|
[315] | 113 |
|
---|
| 114 | // Pause
|
---|
[331] | 115 | Thread.sleep(500);
|
---|
| 116 | // Thread.sleep(500 + (int)(Math.random() * 1000));
|
---|
[315] | 117 | }
|
---|
| 118 | catch (InterruptedException e) {
|
---|
| 119 | e.printStackTrace();
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[328] | 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 |
|
---|
[315] | 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 | }
|
---|
[331] | 173 |
|
---|
[339] | 174 | public String getAgentName()
|
---|
[331] | 175 | {
|
---|
| 176 | return name;
|
---|
| 177 | }
|
---|
[315] | 178 | }
|
---|