source: src/main/java/onetomany/bargainingchipsgame/players/BuyerSubnegotiator.java@ 284

Last change on this file since 284 was 284, checked in by Tim Baarslag, 5 years ago

Coordinator has WishList
OutcomeSpace

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