1 | package parties.in4010.q12015.group7;
|
---|
2 |
|
---|
3 | import java.util.Collections;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.List;
|
---|
6 | import java.util.Map;
|
---|
7 |
|
---|
8 | import genius.core.Bid;
|
---|
9 | import genius.core.BidHistory;
|
---|
10 | import genius.core.issue.Issue;
|
---|
11 | import genius.core.issue.Value;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Opponent Modeler which estimates issue weights and values according to the
|
---|
15 | * bids that are added
|
---|
16 | *
|
---|
17 | * @author svanbekhoven
|
---|
18 | */
|
---|
19 | public class IssueEstimator {
|
---|
20 | /**
|
---|
21 | * Weights of all issues
|
---|
22 | */
|
---|
23 | private HashMap<Integer, Double> issueWeights;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Weights of all issue values
|
---|
27 | */
|
---|
28 | private HashMap<Integer, HashMap<Value, Integer>> issueValueWeights;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Sensitivity parameter
|
---|
32 | */
|
---|
33 | private double n;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Constructor
|
---|
37 | *
|
---|
38 | * @param n
|
---|
39 | * sensitivity parameter
|
---|
40 | */
|
---|
41 | public IssueEstimator(double n) {
|
---|
42 | this.n = n;
|
---|
43 | this.issueWeights = new HashMap<Integer, Double>();
|
---|
44 | this.issueValueWeights = new HashMap<Integer, HashMap<Value, Integer>>();
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Add a bid to the issue estimator, automatically recalculating the issue
|
---|
49 | * weights and issue value weights
|
---|
50 | *
|
---|
51 | * @param bid
|
---|
52 | * bid you want to add to the estimator
|
---|
53 | * @param bidHistory
|
---|
54 | * bidHistory belonging to the opponent you received the bid from
|
---|
55 | * (excluding(!) the bid in the first parameter)
|
---|
56 | */
|
---|
57 | public void addBid(Bid bid, BidHistory bidHistory) {
|
---|
58 | List<Issue> issues = bid.getIssues();
|
---|
59 |
|
---|
60 | for (Issue issue : issues) {
|
---|
61 | Integer issueNumber = issue.getNumber();
|
---|
62 | Value issueValue = null;
|
---|
63 |
|
---|
64 | try {
|
---|
65 | issueValue = bid.getValue(issueNumber);
|
---|
66 | } catch (Exception e) {
|
---|
67 | e.printStackTrace();
|
---|
68 | }
|
---|
69 |
|
---|
70 | this.updateIssueValueWeights(bid, issues, issueNumber, issueValue);
|
---|
71 | this.updateIssueWeights(bid, bidHistory, issues, issueNumber,
|
---|
72 | issueValue);
|
---|
73 | }
|
---|
74 |
|
---|
75 | this.normalizeWeights();
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Update the issueValueWeights according to a new bid
|
---|
80 | *
|
---|
81 | * @param bid
|
---|
82 | * @param issues
|
---|
83 | * @param issueNumber
|
---|
84 | * @param issueValue
|
---|
85 | */
|
---|
86 | private void updateIssueValueWeights(Bid bid, List<Issue> issues,
|
---|
87 | Integer issueNumber, Value issueValue) {
|
---|
88 | if (!issueValueWeights.containsKey(issueNumber)) {
|
---|
89 | issueValueWeights.put(issueNumber, new HashMap<Value, Integer>());
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (!issueValueWeights.get(issueNumber).containsKey(issueValue)) {
|
---|
93 | issueValueWeights.get(issueNumber).put(issueValue, 0);
|
---|
94 | }
|
---|
95 |
|
---|
96 | issueValueWeights.get(issueNumber).put(issueValue,
|
---|
97 | issueValueWeights.get(issueNumber).get(issueValue) + 1);
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Update the issueWeights according to a new bid
|
---|
102 | *
|
---|
103 | * @param bid
|
---|
104 | * @param bidHistory
|
---|
105 | * @param issues
|
---|
106 | * @param issueNumber
|
---|
107 | * @param issueValue
|
---|
108 | */
|
---|
109 | private void updateIssueWeights(Bid bid, BidHistory bidHistory,
|
---|
110 | List<Issue> issues, Integer issueNumber, Value issueValue) {
|
---|
111 | if (!issueWeights.containsKey(issueNumber)) {
|
---|
112 | issueWeights.put(issueNumber, (double) (1.0 / issues.size()));
|
---|
113 | }
|
---|
114 |
|
---|
115 | Bid lastBid = null;
|
---|
116 | if (bidHistory.size() != 0) {
|
---|
117 | try {
|
---|
118 | lastBid = bidHistory.getLastBid();
|
---|
119 | if (lastBid.getValue(issueNumber).equals(issueValue)) {
|
---|
120 | issueWeights.put(issueNumber, issueWeights.get(issueNumber)
|
---|
121 | + n);
|
---|
122 | }
|
---|
123 | } catch (Exception e) {
|
---|
124 | e.printStackTrace();
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Normalizes the issueWeights
|
---|
131 | */
|
---|
132 | private void normalizeWeights() {
|
---|
133 | Double sum = 0.0;
|
---|
134 | for (Double weight : this.issueWeights.values()) {
|
---|
135 | sum += weight;
|
---|
136 | }
|
---|
137 | for (Map.Entry<Integer, Double> entry : issueWeights.entrySet()) {
|
---|
138 | issueWeights.put(entry.getKey(), entry.getValue() / sum);
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Find the maximum weight of all values
|
---|
144 | *
|
---|
145 | * @param valueWeights
|
---|
146 | * @return maximum value
|
---|
147 | */
|
---|
148 | private double maxHashMap(HashMap<Value, Integer> valueWeights) {
|
---|
149 | return Collections.max(valueWeights.values());
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Returns the issueWeights
|
---|
154 | *
|
---|
155 | * @return
|
---|
156 | */
|
---|
157 | private HashMap<Integer, Double> getIssueWeights() {
|
---|
158 | return this.issueWeights;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Returns the issueValueWeights
|
---|
163 | *
|
---|
164 | * @return
|
---|
165 | */
|
---|
166 | private HashMap<Integer, HashMap<Value, Double>> getValueWeights() {
|
---|
167 | HashMap<Integer, HashMap<Value, Double>> returnValueWeights = new HashMap<Integer, HashMap<Value, Double>>();
|
---|
168 | for (Map.Entry<Integer, HashMap<Value, Integer>> entry : issueValueWeights
|
---|
169 | .entrySet()) {
|
---|
170 | double max = maxHashMap(entry.getValue());
|
---|
171 | returnValueWeights
|
---|
172 | .put(entry.getKey(), new HashMap<Value, Double>());
|
---|
173 | for (Map.Entry<Value, Integer> entryValue : entry.getValue()
|
---|
174 | .entrySet()) {
|
---|
175 | returnValueWeights.get(entry.getKey()).put(entryValue.getKey(),
|
---|
176 | entryValue.getValue() / max);
|
---|
177 | }
|
---|
178 | }
|
---|
179 | return returnValueWeights;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Returns the utility of the bid for this issueEstimator
|
---|
184 | *
|
---|
185 | * @param bid
|
---|
186 | * @return
|
---|
187 | */
|
---|
188 | public double getUtility(Bid bid) {
|
---|
189 | double sum = 0;
|
---|
190 | double issueWeight = 0;
|
---|
191 | List<Issue> issues = bid.getIssues();
|
---|
192 | for (Issue i : issues) {
|
---|
193 | issueWeight = 0;
|
---|
194 | try {
|
---|
195 | if (this.getValueWeights().get(i.getNumber())
|
---|
196 | .containsKey(bid.getValue(i.getNumber()))) {
|
---|
197 | issueWeight = (double) this.getValueWeights()
|
---|
198 | .get(i.getNumber())
|
---|
199 | .get(bid.getValue(i.getNumber()));
|
---|
200 | }
|
---|
201 | } catch (Exception e) {
|
---|
202 | e.printStackTrace();
|
---|
203 | }
|
---|
204 | sum += ((double) this.getIssueWeights().get(i.getNumber()))
|
---|
205 | * issueWeight;
|
---|
206 | }
|
---|
207 |
|
---|
208 | return sum;
|
---|
209 | }
|
---|
210 | }
|
---|