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