source: src/main/java/onetomany/bargainingchipsgame/players/Agent.java@ 309

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

General NegotiationContext

File size: 3.3 KB
Line 
1package onetomany.bargainingchipsgame.players;
2
3import java.util.concurrent.BlockingQueue;
4
5import onetomany.bargainingchipsgame.NegotiationContext;
6import onetomany.bargainingchipsgame.interactions.Offer;
7import onetomany.bargainingchipsgame.players.utilityfunction.UtilityFunction;
8
9public abstract class Agent implements Runnable
10{
11 protected String name;
12 protected UtilityFunction u;
13 protected NegotiationContext negotiationContext;
14
15 int k;
16
17 public Agent(String name, UtilityFunction u, NegotiationContext negotiationContext,
18 BlockingQueue<Offer> in, BlockingQueue<Offer> out,
19 BlockingQueue<CoordinationMessage> cin, BlockingQueue<NegotiationStatusMessage> cout)
20 {
21 this.name = name;
22 this.u = u;
23 this.negotiationContext = negotiationContext;
24 this.in = in;
25 this.out = out;
26 this.cin = cin;
27 this.cout = cout;
28 k = 0;
29 }
30
31 // Messaging from and to the opponent
32 protected BlockingQueue<Offer> in;
33 protected BlockingQueue<Offer> out;
34 // Messaging from and to the coordinator
35 protected BlockingQueue<CoordinationMessage> cin;
36 protected BlockingQueue<NegotiationStatusMessage> cout;
37
38 protected abstract void receiveOffer(Offer bundle);
39
40 protected abstract Offer sendOffer();
41
42 protected abstract Offer sendOpeningOffer();
43
44 protected abstract void receiveCoordinationMessage(CoordinationMessage cpoll);
45
46 @Override
47 public void run()
48 {
49 // A buyer may send an opening offer
50 Offer opening = do_sendOpeningOffer();
51 if (opening != null)
52 try {
53 out.put(opening);
54 } catch (InterruptedException e1) {
55 e1.printStackTrace();
56 }
57
58 while (true)
59 {
60 try
61 {
62 // Wait for either a message from coordinator or an incoming offer from seller
63 while (true)
64 {
65 CoordinationMessage cpoll = cin.poll();
66 if (cpoll != null)
67 {
68 do_receiveCoordinationMessage(cpoll);
69 break;
70 }
71
72 Offer poll = in.poll();
73 if (poll != null)
74 {
75 do_receiveOffer(poll);
76 break;
77 }
78 }
79
80 // A sync happened, so we can send out a new offer, given the new information
81 Offer sendOffer = do_sendOffer();
82 out.put(sendOffer);
83
84 if (k >= 0)
85 try {
86 Thread.sleep(500);
87 } catch (InterruptedException e) {
88 // TODO Auto-generated catch block
89 e.printStackTrace();
90 }
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();
115 System.out.println(this + " sends " + o);
116 k++;
117 return o;
118 }
119
120 protected Offer do_sendOpeningOffer()
121 {
122 Offer o = sendOpeningOffer();
123 if (o != null)
124 {
125 System.out.println(this + " sends opening offer " + o);
126 k++;
127 }
128 return o;
129 }
130
131 @Override
132 public String toString()
133 {
134 return "#" + k + ": " + name;
135 }
136
137 public String toDescription()
138 {
139 return name + " with u = " + u;
140 }
141}
Note: See TracBrowser for help on using the repository browser.