source: src/main/java/agents/anac/y2015/cuhkagent2015/OpponentBidHistory.java@ 346

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