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