source: src/main/java/agents/anac/y2016/ngent/OpponentBidHistory.java@ 345

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