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