1 | package genius.core;
|
---|
2 |
|
---|
3 | import java.io.Serializable;
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Collections;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Random;
|
---|
8 |
|
---|
9 | import genius.core.bidding.BidDetails;
|
---|
10 | import genius.core.bidding.BidDetailsSorterTime;
|
---|
11 | import genius.core.bidding.BidDetailsSorterUtility;
|
---|
12 | import genius.core.bidding.BidDetailsStrictSorterUtility;
|
---|
13 | import genius.core.utility.AbstractUtilitySpace;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * This class contains the bidding history of a negotiation agent.
|
---|
17 | *
|
---|
18 | * @author Alex Dirkzwager, Mark Hendrikx, Tim Baarslag
|
---|
19 | */
|
---|
20 | public class BidHistory implements Serializable {
|
---|
21 |
|
---|
22 | /**
|
---|
23 | *
|
---|
24 | */
|
---|
25 | private static final long serialVersionUID = 1663962498632353562L;
|
---|
26 | // list used to store the bids in the order in which they are received
|
---|
27 | private List<BidDetails> bidList;
|
---|
28 | /**
|
---|
29 | * Set this boolean to true if you want to verify that two agents are
|
---|
30 | * exactly equal
|
---|
31 | */
|
---|
32 | private final boolean TEST_EQUIVALENCE = false;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Creates a bid history given an array of bids offered by the negotiation
|
---|
36 | * agent.
|
---|
37 | *
|
---|
38 | * @param bids
|
---|
39 | */
|
---|
40 | public BidHistory(List<BidDetails> bids) {
|
---|
41 | bidList = bids;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Creates an empty bid history.
|
---|
46 | */
|
---|
47 | public BidHistory() {
|
---|
48 | bidList = new ArrayList<BidDetails>();
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Returns the set of bids offered between time instances t1 and t2: (t1,
|
---|
53 | * t2].
|
---|
54 | *
|
---|
55 | * @param t1
|
---|
56 | * @param t2
|
---|
57 | * @return bids done in (t1, t2]
|
---|
58 | */
|
---|
59 | public BidHistory filterBetweenTime(double t1, double t2) {
|
---|
60 | return filterBetween(0, 1.1, t1, t2);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Returns the set of bids with a utility of at least u1 and at most u2:
|
---|
65 | * (u1, u2]. If u1 = u2, then it returns all bids with utility u1.
|
---|
66 | *
|
---|
67 | * @param minU
|
---|
68 | * minimum utility.
|
---|
69 | * @param maxU
|
---|
70 | * maximum utility.
|
---|
71 | * @return bids with a utility in (u1, u2]
|
---|
72 | */
|
---|
73 | public BidHistory filterBetweenUtility(double minU, double maxU) {
|
---|
74 | if (minU == maxU)
|
---|
75 | return filterUtility(minU);
|
---|
76 |
|
---|
77 | BidHistory bidHistory = new BidHistory();
|
---|
78 | for (BidDetails b : bidList) {
|
---|
79 | if (minU < b.getMyUndiscountedUtil()
|
---|
80 | && b.getMyUndiscountedUtil() <= maxU)
|
---|
81 | bidHistory.add(b);
|
---|
82 | }
|
---|
83 | return bidHistory;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Returns the set of bids offered between time instances t1 and t2: (t1,
|
---|
88 | * t2] and with a utility in (u1, u2].
|
---|
89 | *
|
---|
90 | * @param minU
|
---|
91 | * minimum utility.
|
---|
92 | * @param maxU
|
---|
93 | * maximum utility.
|
---|
94 | * @param minT
|
---|
95 | * minimum time.
|
---|
96 | * @param maxT
|
---|
97 | * maximum time.
|
---|
98 | * @return bids with utility (minU, maxU] made in the time (minT, maxT].
|
---|
99 | */
|
---|
100 | public BidHistory filterBetween(double minU, double maxU, double minT,
|
---|
101 | double maxT) {
|
---|
102 | BidHistory bidHistory = new BidHistory();
|
---|
103 | for (BidDetails b : bidList) {
|
---|
104 | if (minU < b.getMyUndiscountedUtil()
|
---|
105 | && b.getMyUndiscountedUtil() <= maxU && minT < b.getTime()
|
---|
106 | && b.getTime() <= maxT)
|
---|
107 | bidHistory.add(b);
|
---|
108 | }
|
---|
109 | return bidHistory;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Returns the set of bids with utility u.
|
---|
114 | *
|
---|
115 | * @param u
|
---|
116 | * utility.
|
---|
117 | * @return set of bids with utility u.
|
---|
118 | */
|
---|
119 | public BidHistory filterUtility(double u) {
|
---|
120 | BidHistory bidHistory = new BidHistory();
|
---|
121 | for (BidDetails b : bidList)
|
---|
122 | if (b.getMyUndiscountedUtil() == u)
|
---|
123 | bidHistory.add(b);
|
---|
124 | return bidHistory;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Returns the set of bids offered between time instances t1 and t2: (t1,
|
---|
129 | * t2] and with a utility in (u1, u2].
|
---|
130 | *
|
---|
131 | * @param minU
|
---|
132 | * minimum discounted utility.
|
---|
133 | * @param maxU
|
---|
134 | * maximum discounted utility.
|
---|
135 | * @param minT
|
---|
136 | * minimum time.
|
---|
137 | * @param maxT
|
---|
138 | * maximum time.
|
---|
139 | * @param utilSpace
|
---|
140 | * preference profile used to find the discounted utility.
|
---|
141 | * @return bids with discounted utility (minU, maxU] made in the time (minT,
|
---|
142 | * maxT].
|
---|
143 | */
|
---|
144 | public BidHistory discountedFilterBetween(double minU, double maxU,
|
---|
145 | double minT, double maxT, AbstractUtilitySpace utilSpace) {
|
---|
146 | BidHistory bidHistory = new BidHistory();
|
---|
147 | for (BidDetails b : bidList) {
|
---|
148 | if (minU < utilSpace.getUtilityWithDiscount(b.getBid(), b.getTime())
|
---|
149 | && utilSpace.getUtilityWithDiscount(b.getBid(),
|
---|
150 | b.getTime()) <= maxU
|
---|
151 | && minT < b.getTime() && b.getTime() <= maxT)
|
---|
152 | bidHistory.add(b);
|
---|
153 | }
|
---|
154 | return bidHistory;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Add an offered bid o the history.
|
---|
159 | *
|
---|
160 | * @param bid
|
---|
161 | * offered bid.
|
---|
162 | */
|
---|
163 | public void add(BidDetails bid) {
|
---|
164 | bidList.add(bid);
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Returns the full history.
|
---|
169 | *
|
---|
170 | * @return history
|
---|
171 | */
|
---|
172 | public List<BidDetails> getHistory() {
|
---|
173 | return bidList;
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Returns the last bid details added to the history.
|
---|
178 | *
|
---|
179 | * @return last added bid details
|
---|
180 | */
|
---|
181 | public BidDetails getLastBidDetails() {
|
---|
182 | BidDetails bid = null;
|
---|
183 | if (bidList.size() > 0) {
|
---|
184 | bid = bidList.get(bidList.size() - 1);
|
---|
185 | }
|
---|
186 | return bid;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Returns the last bid added to the history.
|
---|
191 | *
|
---|
192 | * @return last added bid, or null if no such bid.
|
---|
193 | */
|
---|
194 | public Bid getLastBid() {
|
---|
195 | BidDetails lastBidDetails = getLastBidDetails();
|
---|
196 | if (lastBidDetails == null)
|
---|
197 | return null;
|
---|
198 | return lastBidDetails.getBid();
|
---|
199 | }
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Returns the first bid stored in the history
|
---|
203 | *
|
---|
204 | * @return first bid of history
|
---|
205 | */
|
---|
206 | public BidDetails getFirstBidDetails() {
|
---|
207 | return bidList.get(0);
|
---|
208 | }
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * Returns the bid with the highest utility stored in the history.
|
---|
212 | *
|
---|
213 | * @return bid with highest utility
|
---|
214 | */
|
---|
215 | public BidDetails getBestBidDetails() {
|
---|
216 | double max = Double.NEGATIVE_INFINITY;
|
---|
217 | BidDetails bestBid = null;
|
---|
218 | for (BidDetails b : bidList) {
|
---|
219 | double utility = b.getMyUndiscountedUtil();
|
---|
220 | if (utility >= max) {
|
---|
221 | max = utility;
|
---|
222 | bestBid = b;
|
---|
223 | }
|
---|
224 | }
|
---|
225 | return bestBid;
|
---|
226 | }
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * Returns the bid with the highest discounted utility stored in the
|
---|
230 | * history.
|
---|
231 | *
|
---|
232 | * @param util
|
---|
233 | * preference profile used to determine the discounted utility of
|
---|
234 | * a bid.
|
---|
235 | * @return bid with highest utility
|
---|
236 | */
|
---|
237 | public BidDetails getBestDiscountedBidDetails(AbstractUtilitySpace util) {
|
---|
238 | double max = Double.NEGATIVE_INFINITY;
|
---|
239 | BidDetails bestBid = null;
|
---|
240 | for (BidDetails b : bidList) {
|
---|
241 | double discountedUtility = util.getUtilityWithDiscount(b.getBid(),
|
---|
242 | b.getTime());
|
---|
243 | if (discountedUtility >= max) {
|
---|
244 | max = discountedUtility;
|
---|
245 | bestBid = b;
|
---|
246 | }
|
---|
247 | }
|
---|
248 | return bestBid;
|
---|
249 | }
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * Returns the bid with the lowest utility stored in the history.
|
---|
253 | *
|
---|
254 | * @return bid with lowest utility
|
---|
255 | */
|
---|
256 | public BidDetails getWorstBidDetails() {
|
---|
257 | double min = Double.POSITIVE_INFINITY;
|
---|
258 | BidDetails worstBid = null;
|
---|
259 | for (BidDetails b : bidList) {
|
---|
260 | double utility = b.getMyUndiscountedUtil();
|
---|
261 | if (utility < min) {
|
---|
262 | min = utility;
|
---|
263 | worstBid = b;
|
---|
264 | }
|
---|
265 | }
|
---|
266 | return worstBid;
|
---|
267 | }
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Returns a list of the top N bids which the opponent has offered.
|
---|
271 | *
|
---|
272 | * @param count
|
---|
273 | * amount of N best bids.
|
---|
274 | * @return a list of bids.
|
---|
275 | */
|
---|
276 | public List<BidDetails> getNBestBids(int count) {
|
---|
277 | List<BidDetails> result = new ArrayList<BidDetails>();
|
---|
278 | List<BidDetails> sortedOpponentBids = new ArrayList<BidDetails>(
|
---|
279 | bidList);
|
---|
280 | if (TEST_EQUIVALENCE) {
|
---|
281 | Collections.sort(sortedOpponentBids,
|
---|
282 | new BidDetailsStrictSorterUtility());
|
---|
283 | } else {
|
---|
284 | Collections.sort(sortedOpponentBids, new BidDetailsSorterUtility());
|
---|
285 | }
|
---|
286 |
|
---|
287 | for (int i = 0; i < count && i < sortedOpponentBids.size(); i++) {
|
---|
288 | result.add(sortedOpponentBids.get(i));
|
---|
289 | }
|
---|
290 |
|
---|
291 | return result;
|
---|
292 | }
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * @return amount of bids stored.
|
---|
296 | */
|
---|
297 | public int size() {
|
---|
298 | return bidList.size();
|
---|
299 | }
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * @return average utility of bids stored.
|
---|
303 | */
|
---|
304 | public double getAverageUtility() {
|
---|
305 | int size = size();
|
---|
306 | if (size == 0)
|
---|
307 | return 0;
|
---|
308 | double totalUtil = 0;
|
---|
309 | for (BidDetails bid : bidList) {
|
---|
310 | totalUtil += bid.getMyUndiscountedUtil();
|
---|
311 | }
|
---|
312 | return totalUtil / size;
|
---|
313 | }
|
---|
314 |
|
---|
315 | /**
|
---|
316 | * @param utilSpace
|
---|
317 | * preference profile used to determine the discounted utility of
|
---|
318 | * a bid.
|
---|
319 | * @return average discounted utility of bids stored.
|
---|
320 | */
|
---|
321 | public double getAverageDiscountedUtility(AbstractUtilitySpace utilSpace) {
|
---|
322 | int size = size();
|
---|
323 | if (size == 0)
|
---|
324 | return 0;
|
---|
325 | double totalUtil = 0;
|
---|
326 | for (BidDetails bid : bidList) {
|
---|
327 | totalUtil += utilSpace.getUtilityWithDiscount(bid.getBid(),
|
---|
328 | bid.getTime());
|
---|
329 | }
|
---|
330 | return totalUtil / size;
|
---|
331 | }
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * Sorts the bids contained in this BidHistory object on utility.
|
---|
335 | *
|
---|
336 | * @return sorted BidHistory.
|
---|
337 | */
|
---|
338 | public BidHistory sortToUtility() {
|
---|
339 | BidHistory sortedHistory = this;
|
---|
340 | Collections.sort(sortedHistory.getHistory(),
|
---|
341 | new BidDetailsSorterUtility());
|
---|
342 | return sortedHistory;
|
---|
343 | }
|
---|
344 |
|
---|
345 | /**
|
---|
346 | * Sorts the bids contained in this BidHistory object on time.
|
---|
347 | *
|
---|
348 | * @return sorted BidHistory.
|
---|
349 | */
|
---|
350 | public BidHistory sortToTime() {
|
---|
351 | BidHistory sortedHistory = this;
|
---|
352 | Collections.sort(sortedHistory.getHistory(),
|
---|
353 | new BidDetailsSorterTime());
|
---|
354 | return sortedHistory;
|
---|
355 | }
|
---|
356 |
|
---|
357 | /**
|
---|
358 | * @return random bid from this BidHistory.
|
---|
359 | */
|
---|
360 | public BidDetails getRandom() {
|
---|
361 | return getRandom(new Random());
|
---|
362 | }
|
---|
363 |
|
---|
364 | /**
|
---|
365 | * @param rand
|
---|
366 | * random generator.
|
---|
367 | * @return random bid from this BidHistory using the given random generator.
|
---|
368 | */
|
---|
369 | public BidDetails getRandom(Random rand) {
|
---|
370 | int size = size();
|
---|
371 | if (size == 0)
|
---|
372 | return null;
|
---|
373 | int index = rand.nextInt(size);
|
---|
374 | return bidList.get(index);
|
---|
375 | }
|
---|
376 |
|
---|
377 | /**
|
---|
378 | * Checks if BidHistory (array) is empty or not.
|
---|
379 | *
|
---|
380 | * @return true if no bids are stored.
|
---|
381 | */
|
---|
382 | public boolean isEmpty() {
|
---|
383 | return bidList.isEmpty();
|
---|
384 | }
|
---|
385 |
|
---|
386 | public BidDetails getMedianUtilityBid() {
|
---|
387 | BidHistory sortedHistory = this.sortToUtility();
|
---|
388 |
|
---|
389 | BidDetails medianBid = sortedHistory.getHistory()
|
---|
390 | .get(sortedHistory.getHistory().size() / 2);
|
---|
391 |
|
---|
392 | return medianBid;
|
---|
393 | }
|
---|
394 | }
|
---|