1 | package agents.ai2014.group11;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.Map.Entry;
|
---|
6 |
|
---|
7 | import genius.core.Bid;
|
---|
8 | import genius.core.issue.Issue;
|
---|
9 | import genius.core.issue.Value;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * This class saves the history of bids done by a single opponent.
|
---|
13 | */
|
---|
14 | public class OpponentBidHistory {
|
---|
15 |
|
---|
16 | private ArrayList<BidSequence> bids;
|
---|
17 |
|
---|
18 | public OpponentBidHistory() {
|
---|
19 | bids = new ArrayList<OpponentBidHistory.BidSequence>();
|
---|
20 | }
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Save a sequence of bids
|
---|
24 | * @param previousBid
|
---|
25 | * @param newBid
|
---|
26 | */
|
---|
27 | public void add(Bid previousBid, Bid newBid) {
|
---|
28 | bids.add(new BidSequence(previousBid, newBid));
|
---|
29 | }
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * @return the amount of bids in this history
|
---|
33 | */
|
---|
34 | public int getSize() {
|
---|
35 | return bids.size();
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Try to determine what kind of strategy the opponent is using.
|
---|
40 | *
|
---|
41 | * This is done by checking the difference in values between the
|
---|
42 | * opponent's current and the opponent's own last offer,
|
---|
43 | * and between the opponent's current and the overall last offer.
|
---|
44 | *
|
---|
45 | * @return the strategy the opponent is most likely using.
|
---|
46 | */
|
---|
47 | public BidModificationStrategy getMostLikelyStrategy() {
|
---|
48 | HashMap<BidModificationStrategy, Integer> counts = new HashMap<OpponentBidHistory.BidModificationStrategy, Integer>();
|
---|
49 |
|
---|
50 | if (bids.size() < 2) {
|
---|
51 | return BidModificationStrategy.UNKNOWN;
|
---|
52 | } else {
|
---|
53 | for (int i = 1; i < bids.size(); i++) {
|
---|
54 | BidSequence bsCurrent = bids.get(i);
|
---|
55 | BidSequence bsPrevious = bids.get(i - 1);
|
---|
56 |
|
---|
57 | double currentDistance = getBidDifference(bsCurrent.previous,
|
---|
58 | bsCurrent.current);
|
---|
59 | double previousDistance = getBidDifference(bsPrevious.current,
|
---|
60 | bsCurrent.current);
|
---|
61 |
|
---|
62 | if (currentDistance == previousDistance) {
|
---|
63 | Integer c = counts.get(BidModificationStrategy.UNKNOWN);
|
---|
64 | int currentCount = c == null ? 0 : c;
|
---|
65 | counts.put(BidModificationStrategy.UNKNOWN, currentCount++);
|
---|
66 | } else if (currentDistance < previousDistance) {
|
---|
67 | Integer c = counts
|
---|
68 | .get(BidModificationStrategy.MODIFY_PREVIOUS);
|
---|
69 | int currentCount = c == null ? 0 : c;
|
---|
70 | counts.put(BidModificationStrategy.MODIFY_PREVIOUS,
|
---|
71 | currentCount++);
|
---|
72 | } else {
|
---|
73 | Integer c = counts.get(BidModificationStrategy.MODIFY_SELF);
|
---|
74 | int currentCount = c == null ? 0 : c;
|
---|
75 | counts.put(BidModificationStrategy.MODIFY_SELF,
|
---|
76 | currentCount++);
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | int maxCount = 0;
|
---|
81 | BidModificationStrategy maxStrategy = BidModificationStrategy.UNKNOWN;
|
---|
82 |
|
---|
83 | for (Entry<BidModificationStrategy, Integer> e : counts.entrySet()) {
|
---|
84 | if (e.getValue() > maxCount) {
|
---|
85 | maxStrategy = e.getKey();
|
---|
86 | maxCount = e.getValue();
|
---|
87 | }
|
---|
88 | }
|
---|
89 | return maxStrategy;
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Get the amount of different values between two bids
|
---|
95 | *
|
---|
96 | * @param bid1 Bid
|
---|
97 | * @param bid2 Bid
|
---|
98 | * @return the amount of different values between bid1 and bid2
|
---|
99 | */
|
---|
100 | private double getBidDifference(Bid bid1, Bid bid2) {
|
---|
101 | HashMap<Integer, Boolean> counts = new HashMap<Integer, Boolean>();
|
---|
102 | for (Issue i : bid1.getIssues()) {
|
---|
103 | try {
|
---|
104 | Value value1 = bid1.getValue(i.getNumber());
|
---|
105 | Value value2 = bid2.getValue(i.getNumber());
|
---|
106 | counts.put(i.getNumber(), value1 == value2);
|
---|
107 | } catch (Exception e) {
|
---|
108 | e.printStackTrace();
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | double differenceCount = 0;
|
---|
113 |
|
---|
114 | for (Entry<Integer, Boolean> e : counts.entrySet()) {
|
---|
115 | if (!e.getValue())
|
---|
116 | differenceCount += 1;
|
---|
117 | }
|
---|
118 |
|
---|
119 | return differenceCount;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * There are three strategies defined,
|
---|
124 | * UNKNOWN (everything unclassified),
|
---|
125 | * MODIFY_SELF (using own bids) and
|
---|
126 | * MODIFY_PREVIOUS (modify opponent bids more to your liking)
|
---|
127 | */
|
---|
128 | enum BidModificationStrategy {
|
---|
129 | UNKNOWN, MODIFY_SELF, MODIFY_PREVIOUS
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Wrapper for two following bids
|
---|
134 | */
|
---|
135 | class BidSequence {
|
---|
136 | Bid previous;
|
---|
137 | Bid current;
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Wrapper for two following bids
|
---|
141 | */
|
---|
142 | BidSequence(Bid previous, Bid current) {
|
---|
143 | this.previous = previous;
|
---|
144 | this.current = current;
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|