source: src/main/java/agents/anac/y2014/BraveCat/necessaryClasses/BidHistory.java

Last change on this file was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 6.2 KB
Line 
1package agents.anac.y2014.BraveCat.necessaryClasses;
2
3import java.io.Serializable;
4import java.util.ArrayList;
5import java.util.Collections;
6import java.util.List;
7import java.util.Random;
8
9import genius.core.Bid;
10import genius.core.bidding.BidDetails;
11import genius.core.bidding.BidDetailsSorterTime;
12import genius.core.bidding.BidDetailsSorterUtility;
13import genius.core.utility.AbstractUtilitySpace;
14import genius.core.utility.AdditiveUtilitySpace;
15
16public class BidHistory implements Serializable {
17 private List<Bid> opponentNLastBidHistory;
18 public int numberOfTotalBidsInBidHistory = 20;
19 public int numberOfUniqueBidsInBidHistory = 0;
20 private static final long serialVersionUID = 1663962498632353562L;
21 private List<BidDetails> bidList;
22 private final boolean TEST_EQUIVALENCE = false;
23
24 public BidHistory(List<BidDetails> bids) {
25 this.bidList = bids;
26 opponentNLastBidHistory = new ArrayList();
27 }
28
29 public BidHistory() {
30 this.bidList = new ArrayList();
31 opponentNLastBidHistory = new ArrayList();
32 }
33
34 public BidHistory filterBetweenTime(double t1, double t2) {
35 return filterBetween(0.0D, 1.1D, t1, t2);
36 }
37
38 public BidHistory filterBetweenUtility(double minU, double maxU) {
39 if (minU == maxU) {
40 return filterUtility(minU);
41 }
42 BidHistory bidHistory = new BidHistory();
43 for (BidDetails b : this.bidList) {
44 if ((minU < b.getMyUndiscountedUtil())
45 && (b.getMyUndiscountedUtil() <= maxU))
46 bidHistory.add(b);
47 }
48 return bidHistory;
49 }
50
51 public BidHistory filterBetween(double minU, double maxU, double minT,
52 double maxT) {
53 BidHistory bidHistory = new BidHistory();
54 for (BidDetails b : this.bidList) {
55 if ((minU < b.getMyUndiscountedUtil())
56 && (b.getMyUndiscountedUtil() <= maxU)
57 && (minT < b.getTime()) && (b.getTime() <= maxT))
58 bidHistory.add(b);
59 }
60 return bidHistory;
61 }
62
63 public BidHistory filterUtility(double u) {
64 BidHistory bidHistory = new BidHistory();
65 for (BidDetails b : this.bidList)
66 if (b.getMyUndiscountedUtil() == u)
67 bidHistory.add(b);
68 return bidHistory;
69 }
70
71 public BidHistory discountedFilterBetween(double minU, double maxU,
72 double minT, double maxT, AdditiveUtilitySpace utilSpace) {
73 BidHistory bidHistory = new BidHistory();
74 for (BidDetails b : this.bidList) {
75 if ((minU < utilSpace.getUtilityWithDiscount(b.getBid(),
76 b.getTime()))
77 && (utilSpace.getUtilityWithDiscount(b.getBid(),
78 b.getTime()) <= maxU)
79 && (minT < b.getTime())
80 && (b.getTime() <= maxT))
81 bidHistory.add(b);
82 }
83 return bidHistory;
84 }
85
86 public void add(BidDetails bid) {
87 if (!this.opponentNLastBidHistory.contains(bid.getBid()))
88 numberOfUniqueBidsInBidHistory++;
89 this.opponentNLastBidHistory.add(bid.getBid());
90 if (this.opponentNLastBidHistory.size() == numberOfTotalBidsInBidHistory) {
91 Bid tempBid = this.opponentNLastBidHistory.remove(0);
92 if (!this.opponentNLastBidHistory.contains(tempBid))
93 numberOfUniqueBidsInBidHistory--;
94 }
95 this.bidList.add(bid);
96 }
97
98 public List<BidDetails> getHistory() {
99 return this.bidList;
100 }
101
102 public BidDetails getLastBidDetails() {
103 BidDetails bid = null;
104 if (this.bidList.size() > 0) {
105 bid = (BidDetails) this.bidList.get(this.bidList.size() - 1);
106 }
107 return bid;
108 }
109
110 public Bid getLastBid() {
111 BidDetails lastBidDetails = getLastBidDetails();
112 if (lastBidDetails == null)
113 return null;
114 return lastBidDetails.getBid();
115 }
116
117 public BidDetails getFirstBidDetails() {
118 return (BidDetails) this.bidList.get(0);
119 }
120
121 public BidDetails getBestBidDetails() {
122 double max = (-1.0D / 0.0D);
123 BidDetails bestBid = null;
124 for (BidDetails b : this.bidList) {
125 double utility = b.getMyUndiscountedUtil();
126 if (utility >= max) {
127 max = utility;
128 bestBid = b;
129 }
130 }
131 return bestBid;
132 }
133
134 public BidDetails getBestDiscountedBidDetails(AbstractUtilitySpace util) {
135 double max = (-1.0D / 0.0D);
136 BidDetails bestBid = null;
137 for (BidDetails b : this.bidList) {
138 double discountedUtility = util.getUtilityWithDiscount(b.getBid(),
139 b.getTime());
140 if (discountedUtility >= max) {
141 max = discountedUtility;
142 bestBid = b;
143 }
144 }
145 return bestBid;
146 }
147
148 public BidDetails getWorstBidDetails() {
149 double min = (1.0D / 0.0D);
150 BidDetails worstBid = null;
151 for (BidDetails b : this.bidList) {
152 double utility = b.getMyUndiscountedUtil();
153 if (utility < min) {
154 min = utility;
155 worstBid = b;
156 }
157 }
158 return worstBid;
159 }
160
161 public List<BidDetails> getNBestBids(int count) {
162 List result = new ArrayList();
163 List sortedOpponentBids = new ArrayList(this.bidList);
164
165 Collections.sort(sortedOpponentBids, new BidDetailsSorterUtility());
166
167 for (int i = 0; (i < count) && (i < sortedOpponentBids.size()); i++) {
168 result.add((BidDetails) sortedOpponentBids.get(i));
169 }
170
171 return result;
172 }
173
174 public int size() {
175 return this.bidList.size();
176 }
177
178 public double getAverageUtility() {
179 int size = size();
180 if (size == 0)
181 return 0.0D;
182 double totalUtil = 0.0D;
183 for (BidDetails bid : this.bidList) {
184 totalUtil = bid.getMyUndiscountedUtil();
185 }
186 return totalUtil / size;
187 }
188
189 public double getAverageDiscountedUtility(AdditiveUtilitySpace utilSpace) {
190 int size = size();
191 if (size == 0)
192 return 0.0D;
193 double totalUtil = 0.0D;
194 for (BidDetails bid : this.bidList) {
195 totalUtil = utilSpace.getUtilityWithDiscount(bid.getBid(),
196 bid.getTime());
197 }
198 return totalUtil / size;
199 }
200
201 public BidHistory sortToUtility() {
202 BidHistory sortedHistory = this;
203 Collections.sort(sortedHistory.getHistory(),
204 new BidDetailsSorterUtility());
205 return sortedHistory;
206 }
207
208 public BidHistory sortToTime() {
209 BidHistory sortedHistory = this;
210 Collections
211 .sort(sortedHistory.getHistory(), new BidDetailsSorterTime());
212 return sortedHistory;
213 }
214
215 public BidDetails getRandom() {
216 return getRandom(new Random());
217 }
218
219 public BidDetails getRandom(Random rand) {
220 int size = size();
221 if (size == 0)
222 return null;
223 int index = rand.nextInt(size);
224 return (BidDetails) this.bidList.get(index);
225 }
226
227 public boolean isEmpty() {
228 return this.bidList.isEmpty();
229 }
230}
Note: See TracBrowser for help on using the repository browser.