1 | package agents.anac.y2012.CUHKAgent;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.List;
|
---|
6 | import java.util.Random;
|
---|
7 |
|
---|
8 | import genius.core.Bid;
|
---|
9 | import genius.core.Domain;
|
---|
10 | import genius.core.issue.Issue;
|
---|
11 | import genius.core.issue.IssueDiscrete;
|
---|
12 | import genius.core.issue.IssueInteger;
|
---|
13 | import genius.core.issue.IssueReal;
|
---|
14 | import genius.core.issue.Value;
|
---|
15 | import genius.core.issue.ValueInteger;
|
---|
16 | import genius.core.issue.ValueReal;
|
---|
17 | import 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 | */
|
---|
25 | public 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, receiveMessage the last
|
---|
205 | // cell
|
---|
206 | if (found == false) {
|
---|
207 | int i = opponentBidsStatisticsForReal.get(realIndex)
|
---|
208 | .size() - 1;
|
---|
209 | int countPerValue = opponentBidsStatisticsForReal.get(
|
---|
210 | realIndex).get(i);
|
---|
211 | if (toRemove) {
|
---|
212 | countPerValue--;
|
---|
213 | } else {
|
---|
214 | countPerValue++;
|
---|
215 | }
|
---|
216 |
|
---|
217 | opponentBidsStatisticsForReal.get(realIndex).set(i,
|
---|
218 | countPerValue);
|
---|
219 | }
|
---|
220 | realIndex++;
|
---|
221 | break;
|
---|
222 |
|
---|
223 | case INTEGER:
|
---|
224 |
|
---|
225 | IssueInteger lIssueInteger = (IssueInteger) lIssue;
|
---|
226 | int valueInteger = ((ValueInteger) v).getValue();
|
---|
227 |
|
---|
228 | int valueIndex = valueInteger
|
---|
229 | - lIssueInteger.getLowerBound(); // For ex.
|
---|
230 | // LowerBound
|
---|
231 | // index is 0,
|
---|
232 | // and the lower
|
---|
233 | // bound is 2,
|
---|
234 | // the value is
|
---|
235 | // 4, so the
|
---|
236 | // index of 4
|
---|
237 | // would be 2
|
---|
238 | // which is
|
---|
239 | // exactly 4-2
|
---|
240 | int countPerValue = opponentBidsStatisticsForInteger.get(
|
---|
241 | integerIndex).get(valueIndex);
|
---|
242 | if (toRemove) {
|
---|
243 | countPerValue--;
|
---|
244 | } else {
|
---|
245 | countPerValue++;
|
---|
246 | }
|
---|
247 | opponentBidsStatisticsForInteger.get(integerIndex).set(
|
---|
248 | valueIndex, countPerValue);
|
---|
249 | integerIndex++;
|
---|
250 | break;
|
---|
251 | }
|
---|
252 | }
|
---|
253 | } catch (Exception e) {
|
---|
254 | System.out.println("Exception in updateStatistics: "
|
---|
255 | + e.getMessage());
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | // choose a bid which is optimal for the opponent among a set of candidate
|
---|
260 | // bids.
|
---|
261 |
|
---|
262 | public Bid ChooseBid(List<Bid> candidateBids, Domain domain) {
|
---|
263 |
|
---|
264 | int upperSearchLimit = 200;// 100;
|
---|
265 |
|
---|
266 | int maxIndex = -1;
|
---|
267 | Random ran = new Random();
|
---|
268 | List<Issue> issues = domain.getIssues();
|
---|
269 | int maxFrequency = 0;
|
---|
270 | int realIndex = 0;
|
---|
271 | int discreteIndex = 0;
|
---|
272 | int integerIndex = 0;
|
---|
273 |
|
---|
274 | if (candidateBids.size() >= upperSearchLimit) {
|
---|
275 | List<Bid> bids = new ArrayList<Bid>();
|
---|
276 | for (int i = 0; i < upperSearchLimit; i++) {
|
---|
277 | int issueIndex = ran.nextInt(candidateBids.size());
|
---|
278 | bids.add(candidateBids.get(issueIndex));
|
---|
279 | }
|
---|
280 | candidateBids = bids;
|
---|
281 | }
|
---|
282 |
|
---|
283 | // this whole block of code is to find the best bid
|
---|
284 | try {
|
---|
285 |
|
---|
286 | for (int i = 0; i < candidateBids.size(); i++) {
|
---|
287 | int maxValue = 0;
|
---|
288 | realIndex = discreteIndex = integerIndex = 0;
|
---|
289 | for (int j = 0; j < issues.size(); j++) {
|
---|
290 | Value v = candidateBids.get(i).getValue(
|
---|
291 | issues.get(j).getNumber());
|
---|
292 | switch (issues.get(j).getType()) {
|
---|
293 | case DISCRETE:
|
---|
294 | if (opponentBidsStatisticsDiscrete.get(discreteIndex) != null) {
|
---|
295 | int counterPerValue = opponentBidsStatisticsDiscrete
|
---|
296 | .get(discreteIndex).get(v);
|
---|
297 | maxValue += counterPerValue;
|
---|
298 | }
|
---|
299 | discreteIndex++;
|
---|
300 | break;
|
---|
301 | case REAL:
|
---|
302 | IssueReal lIssueReal = (IssueReal) issues.get(j);
|
---|
303 | int lNumOfPossibleRealValues = lIssueReal
|
---|
304 | .getNumberOfDiscretizationSteps();
|
---|
305 | double lOneStep = (lIssueReal.getUpperBound() - lIssueReal
|
---|
306 | .getLowerBound()) / 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
|
---|
325 | .get(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 is
|
---|
341 | // 0, and
|
---|
342 | // the lower
|
---|
343 | // bound is
|
---|
344 | // 2, the
|
---|
345 | // value is
|
---|
346 | // 4, so the
|
---|
347 | // index of
|
---|
348 | // 4 would
|
---|
349 | // be 2
|
---|
350 | // which is
|
---|
351 | // exactly
|
---|
352 | // 4-2
|
---|
353 | int counterPerValue = opponentBidsStatisticsForInteger
|
---|
354 | .get(integerIndex).get(valueIndex);
|
---|
355 | maxValue += counterPerValue;
|
---|
356 | integerIndex++;
|
---|
357 | break;
|
---|
358 | }
|
---|
359 | }
|
---|
360 | if (maxValue > maxFrequency) {// choose the bid with the maximum
|
---|
361 | // maxValue
|
---|
362 | maxFrequency = maxValue;
|
---|
363 | maxIndex = i;
|
---|
364 | } else if (maxValue == maxFrequency) {// random exploration
|
---|
365 | if (ran.nextDouble() < 0.5) {
|
---|
366 | maxFrequency = maxValue;
|
---|
367 | maxIndex = i;
|
---|
368 | }
|
---|
369 | }
|
---|
370 | }
|
---|
371 |
|
---|
372 | } catch (Exception e) {
|
---|
373 | System.out.println("Exception in choosing a bid");
|
---|
374 | System.out.println(e.getMessage() + "---" + discreteIndex);
|
---|
375 | }
|
---|
376 | if (maxIndex == -1) {
|
---|
377 | return candidateBids.get(ran.nextInt(candidateBids.size()));
|
---|
378 | } else {
|
---|
379 | // here we adopt the random exploration mechanism
|
---|
380 | if (ran.nextDouble() < 0.95) {
|
---|
381 | return candidateBids.get(maxIndex);
|
---|
382 | } else {
|
---|
383 | return candidateBids.get(ran.nextInt(candidateBids.size()));
|
---|
384 | }
|
---|
385 | }
|
---|
386 | }
|
---|
387 |
|
---|
388 | /*
|
---|
389 | * return the best bid from the opponent's bidding history
|
---|
390 | */
|
---|
391 |
|
---|
392 | public Bid chooseBestFromHistory(AdditiveUtilitySpace utilitySpace) {
|
---|
393 | double max = -1;
|
---|
394 | Bid maxBid = null;
|
---|
395 | try {
|
---|
396 | for (Bid bid : bidHistory) {
|
---|
397 | if (max < utilitySpace.getUtility(bid)) {
|
---|
398 | max = utilitySpace.getUtility(bid);
|
---|
399 | maxBid = bid;
|
---|
400 | }
|
---|
401 | }
|
---|
402 | } catch (Exception e) {
|
---|
403 | System.out.println("ChooseBestfromhistory exception");
|
---|
404 | }
|
---|
405 | return maxBid;
|
---|
406 | }
|
---|
407 |
|
---|
408 | // one way to predict the concession degree of the opponent
|
---|
409 | public double concedeDegree(AdditiveUtilitySpace utilitySpace) {
|
---|
410 | int numOfBids = bidHistory.size();
|
---|
411 | HashMap<Bid, Integer> bidCounter = new HashMap<Bid, Integer>();
|
---|
412 | try {
|
---|
413 | for (int i = 0; i < numOfBids; i++) {
|
---|
414 |
|
---|
415 | if (bidCounter.get(bidHistory.get(i)) == null) {
|
---|
416 | bidCounter.put(bidHistory.get(i), 1);
|
---|
417 | } else {
|
---|
418 | int counter = bidCounter.get(bidHistory.get(i));
|
---|
419 | counter++;
|
---|
420 | bidCounter.put(bidHistory.get(i), counter);
|
---|
421 | }
|
---|
422 | }
|
---|
423 | } catch (Exception e) {
|
---|
424 | System.out.println("ChooseBestfromhistory exception");
|
---|
425 | }
|
---|
426 | // System.out.println("the opponent's toughness degree is " +
|
---|
427 | // bidCounter.size() + " divided by " +
|
---|
428 | // utilitySpace.getDomain().getNumberOfPossibleBids());
|
---|
429 | return ((double) bidCounter.size() / utilitySpace.getDomain()
|
---|
430 | .getNumberOfPossibleBids());
|
---|
431 | }
|
---|
432 |
|
---|
433 | public int getSize() {
|
---|
434 | int numOfBids = bidHistory.size();
|
---|
435 | HashMap<Bid, Integer> bidCounter = new HashMap<Bid, Integer>();
|
---|
436 | try {
|
---|
437 | for (int i = 0; i < numOfBids; i++) {
|
---|
438 |
|
---|
439 | if (bidCounter.get(bidHistory.get(i)) == null) {
|
---|
440 | bidCounter.put(bidHistory.get(i), 1);
|
---|
441 | } else {
|
---|
442 | int counter = bidCounter.get(bidHistory.get(i));
|
---|
443 | counter++;
|
---|
444 | bidCounter.put(bidHistory.get(i), counter);
|
---|
445 | }
|
---|
446 | }
|
---|
447 | } catch (Exception e) {
|
---|
448 | System.out.println("getSize exception");
|
---|
449 | }
|
---|
450 | return bidCounter.size();
|
---|
451 | }
|
---|
452 |
|
---|
453 | // Another way to predict the opponent's concession degree
|
---|
454 | public double getConcessionDegree() {
|
---|
455 | int numOfBids = bidHistory.size();
|
---|
456 | double numOfDistinctBid = 0;
|
---|
457 | int historyLength = 10;
|
---|
458 | double concessionDegree = 0;
|
---|
459 | // HashMap<Bid, Integer> bidCounter = new HashMap<Bid, Integer>();
|
---|
460 | if (numOfBids - historyLength > 0) {
|
---|
461 | try {
|
---|
462 | for (int j = numOfBids - historyLength; j < numOfBids; j++) {
|
---|
463 | if (bidCounter.get(bidHistory.get(j)) == 1) {
|
---|
464 | numOfDistinctBid++;
|
---|
465 | }
|
---|
466 | }
|
---|
467 | concessionDegree = Math
|
---|
468 | .pow(numOfDistinctBid / historyLength, 2);
|
---|
469 | } catch (Exception e) {
|
---|
470 | System.out.println("getConcessionDegree exception");
|
---|
471 | }
|
---|
472 | } else {
|
---|
473 | numOfDistinctBid = this.getSize();
|
---|
474 | concessionDegree = Math.pow(numOfDistinctBid / historyLength, 2);
|
---|
475 | }
|
---|
476 | // System.out.println("the history length is" + bidHistory.size() +
|
---|
477 | // "concessiondegree is " + concessionDegree);
|
---|
478 | return concessionDegree;
|
---|
479 | }
|
---|
480 | }
|
---|