source: src/main/java/agents/anac/y2015/AresParty/OpponentBidHistory.java@ 127

Last change on this file since 127 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: 14.8 KB
Line 
1package agents.anac.y2015.AresParty;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6import java.util.Random;
7
8import genius.core.Bid;
9import genius.core.Domain;
10import genius.core.issue.Issue;
11import genius.core.issue.IssueDiscrete;
12import genius.core.issue.IssueInteger;
13import genius.core.issue.IssueReal;
14import genius.core.issue.Value;
15import genius.core.issue.ValueInteger;
16import genius.core.issue.ValueReal;
17import genius.core.utility.AdditiveUtilitySpace;
18
19/**
20 * Operations related with the opponent's bid history. The opponent's first 100
21 * unique bids are remembered.
22 *
23 * @author Justin
24 */
25public class OpponentBidHistory {
26
27 private ArrayList<Bid> bidHistory;
28 private ArrayList<ArrayList<Integer>> opponentBidsStatisticsForReal;
29 private ArrayList<HashMap<Value, Integer>> opponentBidsStatisticsDiscrete;
30 private ArrayList<ArrayList<Integer>> opponentBidsStatisticsForInteger;
31 private int maximumBidsStored = 100;
32 private HashMap<Bid, Integer> bidCounter = new HashMap<Bid, Integer>();
33 private Bid bid_maximum_from_opponent;// the bid with maximum utility
34 // proposed by the opponent so far.
35
36 public OpponentBidHistory() {
37 this.bidHistory = new ArrayList<Bid>();
38 opponentBidsStatisticsForReal = new ArrayList<ArrayList<Integer>>();
39 opponentBidsStatisticsDiscrete = new ArrayList<HashMap<Value, Integer>>();
40 opponentBidsStatisticsForInteger = new ArrayList<ArrayList<Integer>>();
41 }
42
43 public void addBid(Bid bid, AdditiveUtilitySpace utilitySpace) {
44 if (bidHistory.indexOf(bid) == -1) {
45 bidHistory.add(bid);
46 }
47 try {
48 if (bidHistory.size() == 1) {
49 this.bid_maximum_from_opponent = bidHistory.get(0);
50 } else {
51 if (utilitySpace.getUtility(bid) > utilitySpace
52 .getUtility(this.bid_maximum_from_opponent)) {
53 this.bid_maximum_from_opponent = bid;
54 }
55 }
56 } catch (Exception e) {
57 System.out.println("error in addBid method" + e.getMessage());
58 }
59 }
60
61 public Bid getBestBidInHistory() {
62 return this.bid_maximum_from_opponent;
63 }
64
65 /**
66 * initialization
67 */
68 public void initializeDataStructures(Domain domain) {
69 try {
70 List<Issue> issues = domain.getIssues();
71 for (Issue lIssue : issues) {
72 switch (lIssue.getType()) {
73 case DISCRETE:
74 IssueDiscrete lIssueDiscrete = (IssueDiscrete) lIssue;
75 HashMap<Value, Integer> discreteIssueValuesMap = new HashMap<Value, Integer>();
76 for (int j = 0; j < lIssueDiscrete.getNumberOfValues(); j++) {
77 Value v = lIssueDiscrete.getValue(j);
78 discreteIssueValuesMap.put(v, 0);
79 }
80 opponentBidsStatisticsDiscrete.add(discreteIssueValuesMap);
81 break;
82
83 case REAL:
84 IssueReal lIssueReal = (IssueReal) lIssue;
85 ArrayList<Integer> numProposalsPerValue = new ArrayList<Integer>();
86 int lNumOfPossibleValuesInThisIssue = lIssueReal
87 .getNumberOfDiscretizationSteps();
88 for (int i = 0; i < lNumOfPossibleValuesInThisIssue; i++) {
89 numProposalsPerValue.add(0);
90 }
91 opponentBidsStatisticsForReal.add(numProposalsPerValue);
92 break;
93
94 case INTEGER:
95 IssueInteger lIssueInteger = (IssueInteger) lIssue;
96 ArrayList<Integer> numOfValueProposals = new ArrayList<Integer>();
97
98 // number of possible value when issue is integer (we should
99 // add 1 in order to include all values)
100 int lNumOfPossibleValuesForThisIssue = lIssueInteger
101 .getUpperBound()
102 - lIssueInteger.getLowerBound()
103 + 1;
104 for (int i = 0; i < lNumOfPossibleValuesForThisIssue; i++) {
105 numOfValueProposals.add(0);
106 }
107 opponentBidsStatisticsForInteger.add(numOfValueProposals);
108 break;
109 }
110 }
111 } catch (Exception e) {
112 System.out.println("EXCEPTION in initializeDataAtructures");
113 }
114 }
115
116 /**
117 * This function updates the opponent's Model by calling the
118 * updateStatistics method
119 */
120 public void updateOpponentModel(Bid bidToUpdate, Domain domain,
121 AdditiveUtilitySpace utilitySpace) {
122 this.addBid(bidToUpdate, utilitySpace);
123
124 if (bidCounter.get(bidToUpdate) == null) {
125 bidCounter.put(bidToUpdate, 1);
126 } else {
127 int counter = bidCounter.get(bidToUpdate);
128 counter++;
129 bidCounter.put(bidToUpdate, counter);
130 }
131
132 if (this.bidHistory.size() <= this.maximumBidsStored) {
133 this.updateStatistics(bidToUpdate, false, domain);
134 }
135 }
136
137 /**
138 * This function updates the statistics of the bids that were received from
139 * the opponent.
140 */
141 private void updateStatistics(Bid bidToUpdate, boolean toRemove,
142 Domain domain) {
143 try {
144 List<Issue> issues = domain.getIssues();
145
146 // counters for each type of the issues
147 int realIndex = 0;
148 int discreteIndex = 0;
149 int integerIndex = 0;
150 for (Issue lIssue : issues) {
151 int issueNum = lIssue.getNumber();
152 Value v = bidToUpdate.getValue(issueNum);
153 switch (lIssue.getType()) {
154 case DISCRETE:
155 if (opponentBidsStatisticsDiscrete == null) {
156 System.out
157 .println("opponentBidsStatisticsDiscrete is NULL");
158 } else if (opponentBidsStatisticsDiscrete
159 .get(discreteIndex) != null) {
160 int counterPerValue = opponentBidsStatisticsDiscrete
161 .get(discreteIndex).get(v);
162 if (toRemove) {
163 counterPerValue--;
164 } else {
165 counterPerValue++;
166 }
167 opponentBidsStatisticsDiscrete.get(discreteIndex).put(
168 v, counterPerValue);
169 }
170 discreteIndex++;
171 break;
172
173 case REAL:
174
175 IssueReal lIssueReal = (IssueReal) lIssue;
176 int lNumOfPossibleRealValues = lIssueReal
177 .getNumberOfDiscretizationSteps();
178 double lOneStep = (lIssueReal.getUpperBound() - lIssueReal
179 .getLowerBound()) / lNumOfPossibleRealValues;
180 double first = lIssueReal.getLowerBound();
181 double last = lIssueReal.getLowerBound() + lOneStep;
182 double valueReal = ((ValueReal) v).getValue();
183 boolean found = false;
184
185 for (int i = 0; !found
186 && i < opponentBidsStatisticsForReal.get(realIndex)
187 .size(); i++) {
188 if (valueReal >= first && valueReal <= last) {
189 int countPerValue = opponentBidsStatisticsForReal
190 .get(realIndex).get(i);
191 if (toRemove) {
192 countPerValue--;
193 } else {
194 countPerValue++;
195 }
196
197 opponentBidsStatisticsForReal.get(realIndex).set(i,
198 countPerValue);
199 found = true;
200 }
201 first = last;
202 last = last + lOneStep;
203 }
204 // If no matching value was found, update the last cell
205 if (found == false) {
206 int i = opponentBidsStatisticsForReal.get(realIndex)
207 .size() - 1;
208 int countPerValue = opponentBidsStatisticsForReal.get(
209 realIndex).get(i);
210 if (toRemove) {
211 countPerValue--;
212 } else {
213 countPerValue++;
214 }
215
216 opponentBidsStatisticsForReal.get(realIndex).set(i,
217 countPerValue);
218 }
219 realIndex++;
220 break;
221
222 case INTEGER:
223
224 IssueInteger lIssueInteger = (IssueInteger) lIssue;
225 int valueInteger = ((ValueInteger) v).getValue();
226
227 int valueIndex = valueInteger
228 - lIssueInteger.getLowerBound(); // For ex.
229 // LowerBound
230 // index is 0,
231 // and the lower
232 // bound is 2,
233 // the value is
234 // 4, so the
235 // index of 4
236 // would be 2
237 // which is
238 // exactly 4-2
239 int countPerValue = opponentBidsStatisticsForInteger.get(
240 integerIndex).get(valueIndex);
241 if (toRemove) {
242 countPerValue--;
243 } else {
244 countPerValue++;
245 }
246 opponentBidsStatisticsForInteger.get(integerIndex).set(
247 valueIndex, countPerValue);
248 integerIndex++;
249 break;
250 }
251 }
252 } catch (Exception e) {
253 System.out.println("Exception in updateStatistics: "
254 + e.getMessage());
255 }
256 }
257
258 // choose a bid which is optimal for the opponent among a set of candidate
259 // bids.
260
261 public Bid ChooseBid(List<Bid> candidateBids, Domain domain) {
262
263 int upperSearchLimit = 200;// 100;
264
265 int maxIndex = -1;
266 Random ran = new Random();
267 List<Issue> issues = domain.getIssues();
268 int maxFrequency = 0;
269 int realIndex = 0;
270 int discreteIndex = 0;
271 int integerIndex = 0;
272
273 if (candidateBids.size() >= upperSearchLimit) {
274 List<Bid> bids = new ArrayList<Bid>();
275 for (int i = 0; i < upperSearchLimit; i++) {
276 int issueIndex = ran.nextInt(candidateBids.size());
277 bids.add(candidateBids.get(issueIndex));
278 }
279 candidateBids = bids;
280 }
281
282 // this whole block of code is to find the best bid
283 try {
284
285 for (int i = 0; i < candidateBids.size(); i++) {
286 int maxValue = 0;
287 realIndex = discreteIndex = integerIndex = 0;
288 for (int j = 0; j < issues.size(); j++) {
289 Value v = candidateBids.get(i).getValue(
290 issues.get(j).getNumber());
291 switch (issues.get(j).getType()) {
292 case DISCRETE:
293 if (opponentBidsStatisticsDiscrete.get(discreteIndex) != null) {
294 int counterPerValue = opponentBidsStatisticsDiscrete
295 .get(discreteIndex).get(v);
296 maxValue += counterPerValue;
297 }
298 discreteIndex++;
299 break;
300 case REAL:
301 IssueReal lIssueReal = (IssueReal) issues.get(j);
302 int lNumOfPossibleRealValues = lIssueReal
303 .getNumberOfDiscretizationSteps();
304 double lOneStep = (lIssueReal.getUpperBound() - lIssueReal
305 .getLowerBound()) / lNumOfPossibleRealValues;
306 double first = lIssueReal.getLowerBound();
307 double last = lIssueReal.getLowerBound() + lOneStep;
308 double valueReal = ((ValueReal) v).getValue();
309 boolean found = false;
310 for (int k = 0; !found
311 && k < opponentBidsStatisticsForReal.get(
312 realIndex).size(); k++) {
313 if (valueReal >= first && valueReal <= last) {
314 int counterPerValue = opponentBidsStatisticsForReal
315 .get(realIndex).get(k);
316 maxValue += counterPerValue;
317 found = true;
318 }
319 first = last;
320 last = last + lOneStep;
321 }
322 if (found == false) {
323 int k = opponentBidsStatisticsForReal
324 .get(realIndex).size() - 1;
325 int counterPerValue = opponentBidsStatisticsForReal
326 .get(realIndex).get(k);
327 maxValue += counterPerValue;
328 }
329 realIndex++;
330 break;
331
332 case INTEGER:
333 IssueInteger lIssueInteger = (IssueInteger) issues
334 .get(j);
335 int valueInteger = ((ValueInteger) v).getValue();
336 int valueIndex = valueInteger
337 - lIssueInteger.getLowerBound(); // For ex.
338 // LowerBound
339 // index is
340 // 0, and
341 // the lower
342 // bound is
343 // 2, the
344 // value is
345 // 4, so the
346 // index of
347 // 4 would
348 // be 2
349 // which is
350 // exactly
351 // 4-2
352 int counterPerValue = opponentBidsStatisticsForInteger
353 .get(integerIndex).get(valueIndex);
354 maxValue += counterPerValue;
355 integerIndex++;
356 break;
357 }
358 }
359 if (maxValue > maxFrequency) {// choose the bid with the maximum
360 // maxValue
361 maxFrequency = maxValue;
362 maxIndex = i;
363 } else if (maxValue == maxFrequency) {// random exploration
364 if (ran.nextDouble() < 0.5) {
365 maxFrequency = maxValue;
366 maxIndex = i;
367 }
368 }
369 }
370
371 } catch (Exception e) {
372 System.out.println("Exception in choosing a bid");
373 System.out.println(e.getMessage() + "---" + discreteIndex);
374 }
375 if (maxIndex == -1) {
376 return candidateBids.get(ran.nextInt(candidateBids.size()));
377 } else {
378 // here we adopt the random exploration mechanism
379 if (ran.nextDouble() < 0.95) {
380 return candidateBids.get(maxIndex);
381 } else {
382 return candidateBids.get(ran.nextInt(candidateBids.size()));
383 }
384 }
385 }
386
387 /*
388 * return the best bid from the opponent's bidding history
389 */
390
391 public Bid chooseBestFromHistory(AdditiveUtilitySpace utilitySpace) {
392 double max = -1;
393 Bid maxBid = null;
394 try {
395 for (Bid bid : bidHistory) {
396 if (max < utilitySpace.getUtility(bid)) {
397 max = utilitySpace.getUtility(bid);
398 maxBid = bid;
399 }
400 }
401 } catch (Exception e) {
402 System.out.println("ChooseBestfromhistory exception");
403 }
404 return maxBid;
405 }
406
407 // one way to predict the concession degree of the opponent
408 public double concedeDegree(AdditiveUtilitySpace utilitySpace) {
409 int numOfBids = bidHistory.size();
410 HashMap<Bid, Integer> bidCounter = new HashMap<Bid, Integer>();
411 try {
412 for (int i = 0; i < numOfBids; i++) {
413
414 if (bidCounter.get(bidHistory.get(i)) == null) {
415 bidCounter.put(bidHistory.get(i), 1);
416 } else {
417 int counter = bidCounter.get(bidHistory.get(i));
418 counter++;
419 bidCounter.put(bidHistory.get(i), counter);
420 }
421 }
422 } catch (Exception e) {
423 System.out.println("ChooseBestfromhistory exception");
424 }
425 // System.out.println("the opponent's toughness degree is " +
426 // bidCounter.size() + " divided by " +
427 // utilitySpace.getDomain().getNumberOfPossibleBids());
428 return ((double) bidCounter.size() / utilitySpace.getDomain()
429 .getNumberOfPossibleBids());
430 }
431
432 public int getSize() {
433 int numOfBids = bidHistory.size();
434 HashMap<Bid, Integer> bidCounter = new HashMap<Bid, Integer>();
435 try {
436 for (int i = 0; i < numOfBids; i++) {
437
438 if (bidCounter.get(bidHistory.get(i)) == null) {
439 bidCounter.put(bidHistory.get(i), 1);
440 } else {
441 int counter = bidCounter.get(bidHistory.get(i));
442 counter++;
443 bidCounter.put(bidHistory.get(i), counter);
444 }
445 }
446 } catch (Exception e) {
447 System.out.println("getSize exception");
448 }
449 return bidCounter.size();
450 }
451
452 // Another way to predict the opponent's concession degree
453 public double getConcessionDegree() {
454 int numOfBids = bidHistory.size();
455 double numOfDistinctBid = 0;
456 int historyLength = 10;
457 double concessionDegree = 0;
458 // HashMap<Bid, Integer> bidCounter = new HashMap<Bid, Integer>();
459 if (numOfBids - historyLength > 0) {
460 try {
461 for (int j = numOfBids - historyLength; j < numOfBids; j++) {
462 if (bidCounter.get(bidHistory.get(j)) == 1) {
463 numOfDistinctBid++;
464 }
465 }
466 concessionDegree = Math
467 .pow(numOfDistinctBid / historyLength, 2);
468 } catch (Exception e) {
469 System.out.println("getConcessionDegree exception");
470 }
471 } else {
472 numOfDistinctBid = this.getSize();
473 concessionDegree = Math.pow(numOfDistinctBid / historyLength, 2);
474 }
475 // System.out.println("the history length is" + bidHistory.size() +
476 // "concessiondegree is " + concessionDegree);
477 return concessionDegree;
478 }
479
480 public Bid getLastOppBid() {
481 return bidHistory.get(bidHistory.size() - 1);
482 }
483}
Note: See TracBrowser for help on using the repository browser.