source: src/main/java/bargainingchips/players/Agent.java@ 316

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

new packages complete

File size: 3.3 KB
Line 
1package bargainingchips.players;
2
3import java.util.concurrent.BlockingQueue;
4
5import bargainingchips.NegotiationContext;
6import bargainingchips.actions.Offer;
7import bargainingchips.utilityfunctions.UtilityFunction;
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 */
12public abstract class Agent implements Runnable
13{
14 protected String name;
15 protected UtilityFunction u;
16 protected NegotiationContext negotiationContext;
17
18 int k;
19
20 public Agent(String name, UtilityFunction u, NegotiationContext negotiationContext,
21 BlockingQueue<Offer> in, BlockingQueue<Offer> out,
22 BlockingQueue<CoordinationMessage> cin, BlockingQueue<StatusMessage> cout)
23 {
24 this.name = name;
25 this.u = u;
26 this.negotiationContext = negotiationContext;
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;
39 protected BlockingQueue<StatusMessage> cout;
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
49 protected abstract StatusMessage sendStatusMessage();
50
51 @Override
52 public void run()
53 {
54 // A buyer may send an opening offer
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
89 // Pause
90 Thread.sleep(500);
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.