source: src/main/java/bargainingchips/players/history/BidEventHistory.java@ 340

Last change on this file since 340 was 340, checked in by Tim Baarslag, 4 years ago

Change BilateralProtocol loop to avoid busy wait; note that this fixes only a part of the busy wait problem.

Package structure now in line with latest Acumex discussions.

Pinpointed error avoidance in Agent.

File size: 5.4 KB
Line 
1package bargainingchips.players.history;
2
3import java.util.List;
4
5import java.util.ArrayList;
6import java.util.Collections;
7import java.util.Iterator;
8import java.util.Random;
9
10import bargainingchips.Bundle;
11import bargainingchips.OutcomeSpace;
12import bargainingchips.actions.Bid;
13
14/**
15 *
16 * BidEventHistory is the list of {@link BidEvent}s (including bid, its utility [for the agent], and time) that
17 * an agent stores for any further use.
18 * It is adapted from {@link /NegotiatorGUI/src/main/java/agents/anac/y2011/Nice_Tit_for_Tat/BidHistory.java}
19 *
20 *
21 * @author Faria Nassiri-Mofakham
22 *
23 */
24public class BidEventHistory implements Iterable<BidEvent>
25{
26 List<BidEvent> history;
27 private final boolean TEST_EQUIVALENCE = false;
28
29 public BidEventHistory()
30 {
31 this.history = new ArrayList<BidEvent>();
32 }
33
34 public BidEventHistory(BidEventHistory h)
35 {
36 this.history = new ArrayList<BidEvent>(h.getHistory());
37 }
38
39 /**
40 * Pretends all bids in the domain were made on t = 0.
41 */
42 public BidEventHistory(OutcomeSpace s)
43 {
44 this();
45 for (Bundle b : s.getAllBids())
46 history.add(new BidEvent(b,0, 0));
47 }
48
49 public BidEventHistory filterBetweenTime(double minT, double maxT)
50 {
51 return filterBetween(0, 1, minT, maxT);
52 }
53
54 public BidEventHistory filterBetween(double minU, double maxU, double minT, double maxT)
55 {
56 BidEventHistory bidEventHistory = new BidEventHistory();
57 for (BidEvent e : history)
58 {
59 if (minU < e.getMyUtility() &&
60 e.getMyUtility() <= maxU &&
61 minT < e.getTime() &&
62 e.getTime() <= maxT)
63 bidEventHistory.add(e);
64 }
65 return bidEventHistory;
66 }
67
68 public void add(BidEvent e)
69 {
70 history.add(e);
71 }
72
73 public BidEvent getLastBidEvent()
74 {
75 if (history.isEmpty())
76 return null;
77
78 BidEvent bidEvent = history.get(size() - 1);
79 return bidEvent;
80 }
81
82 public BidEvent getFirstBidEvent()
83 {
84 if (history.isEmpty())
85 return null;
86
87 BidEvent bidEvent = history.get(0);
88 return bidEvent;
89 }
90
91 public Bundle getLastBid()
92 {
93 BidEvent lastBidEvent = getLastBidEvent();
94 if (lastBidEvent == null)
95 return null;
96 return lastBidEvent.getBid();
97 }
98
99 public Bundle getSecondLastBid()
100 {
101 if (history.size() < 2)
102 return null;
103
104 return history.get(history.size() - 2).getBid();
105 }
106
107 public int size()
108 {
109 return history.size();
110 }
111
112 public List<BidEvent> getHistory()
113 {
114 return history;
115 }
116
117 /**
118 * Get the {@link BidDetails} of the {@link Bid} with utility closest to u.
119 */
120 public BidEvent getBidEventOfUtility(double u)
121 {
122 double minDistance = -1;
123 BidEvent closestBid = null;
124 for (BidEvent e : history)
125 {
126 double utility = e.getMyUtility();
127 if (Math.abs(utility - u) <= minDistance || minDistance == -1)
128 {
129 minDistance = Math.abs(utility - u);
130 closestBid = e;
131 }
132 }
133 return closestBid;
134 }
135
136 public double getMaximumUtility()
137 {
138 double max = -1;
139 for (BidEvent b : history)
140 {
141 double utility = b.getMyUtility();
142 if (utility >= max || max == -1)
143 max = utility;
144 }
145 return max;
146 }
147
148 public double getMinimumUtility()
149 {
150 double min = -1;
151 for (BidEvent b : history)
152 {
153 double utility = b.getMyUtility();
154 if (utility <= min || min == -1)
155 min = utility;
156 }
157 return min;
158 }
159
160 /**
161 * Gets the best bid for me.
162 */
163 public Bundle getBestBid()
164 {
165 BidEvent bestBidEvent = getBestBidEvent();
166 if (bestBidEvent == null)
167 return null;
168 return bestBidEvent.getBid();
169 }
170
171 /**
172 * Gets the details of the best bid for me.
173 */
174 public BidEvent getBestBidEvent()
175 {
176 double max = -1;
177 BidEvent bestBid = null;
178 for (BidEvent b : history)
179 {
180 double utility = b.getMyUtility();
181 if (utility >= max || max == -1)
182 {
183 max = utility;
184 bestBid = b;
185 }
186 }
187 return bestBid;
188 }
189
190 /**
191 * Gets the history part of the best n (or less) bids for me.
192 */
193 public BidEventHistory getBestBidHistory(int n)
194 {
195 BidEventHistory copySortedToUtility = getCopySortedToUtility();
196 BidEventHistory best = new BidEventHistory();
197 int i = 0;
198 for (BidEvent e : copySortedToUtility)
199 {
200 best.add(e);
201 i++;
202 if (i >= n)
203 break;
204 }
205 return best;
206 }
207
208 public BidEvent getRandom()
209 {
210 int size = size();
211 if (size == 0)
212 return null;
213 int index = (new Random()).nextInt(size);
214 return history.get(index);
215 }
216
217 public BidEvent getRandom(Random r)
218 {
219 int size = size();
220 if (size == 0)
221 return null;
222 int index = r.nextInt(size);
223 return history.get(index);
224 }
225
226 public double getAverageUtility()
227 {
228 int size = size();
229 if (size == 0)
230 return 0;
231 double totalUtil = 0;
232 for (BidEvent e : history)
233 totalUtil += e.getMyUtility();
234 return totalUtil / size;
235 }
236
237 public void sortToUtility()
238 {
239 if (TEST_EQUIVALENCE) {
240 Collections.sort(history, new BidEventStrictSorterUtility());
241 } else {
242 Collections.sort(history, new BidEventSorterUtility());
243 }
244 }
245
246 public BidEventHistory getCopySortedToUtility()
247 {
248 BidEventHistory bidEventHistoryCopy = new BidEventHistory(this);
249 bidEventHistoryCopy.sortToUtility();
250 return bidEventHistoryCopy;
251 }
252
253 public Bid getNextBid()
254 {
255 Bid b=null;
256
257
258
259 return b;
260 }
261
262 @Override
263 public String toString()
264 {
265 return "" + history;
266 }
267
268 public Iterator<BidEvent> iterator()
269 {
270 return history.iterator();
271 }
272}
Note: See TracBrowser for help on using the repository browser.