1 | package agents.anac.y2019.garavelagent;
|
---|
2 |
|
---|
3 | import agents.org.apache.commons.math.stat.regression.OLSMultipleLinearRegression;
|
---|
4 | import genius.core.Bid;
|
---|
5 | import genius.core.Domain;
|
---|
6 | import genius.core.issue.Issue;
|
---|
7 | import genius.core.issue.IssueDiscrete;
|
---|
8 | import genius.core.issue.Value;
|
---|
9 | import genius.core.issue.ValueDiscrete;
|
---|
10 | import javafx.util.Pair;
|
---|
11 |
|
---|
12 | import java.util.*;
|
---|
13 |
|
---|
14 | public class utils {
|
---|
15 |
|
---|
16 | private static HashMap<String, Integer> issueFrequency;
|
---|
17 | public static List<String> mostWanted;
|
---|
18 |
|
---|
19 | public static HashMap<String, Integer> getFrequency() {
|
---|
20 | return issueFrequency;
|
---|
21 | }
|
---|
22 |
|
---|
23 | private static <K, V> K getKey(Map<K, V> map, V value) {
|
---|
24 | for (Map.Entry<K, V> entry : map.entrySet()) {
|
---|
25 | if (entry.getValue().equals(value)) {
|
---|
26 | return entry.getKey();
|
---|
27 | }
|
---|
28 | }
|
---|
29 | return null;
|
---|
30 | }
|
---|
31 |
|
---|
32 | public static Double sum(Double[] arr) {
|
---|
33 | Double sum = 0.0; // initialize sum
|
---|
34 | int i;
|
---|
35 | // Iterate through all elements and add them to sum
|
---|
36 | for (i = 0; i < arr.length; i++)
|
---|
37 | sum += arr[i];
|
---|
38 |
|
---|
39 | return sum;
|
---|
40 | }
|
---|
41 |
|
---|
42 | public static int getIndexOfValueInIssue(int issueIndex, String value, Bid currentBid) {
|
---|
43 | IssueDiscrete is = (IssueDiscrete) currentBid.getIssues().get(issueIndex);
|
---|
44 | return is.getValueIndex(value);
|
---|
45 | }
|
---|
46 |
|
---|
47 | // i is used for recursion, for the initial call this should be 0
|
---|
48 | public static List<List<String>> generateAllPossibleBids(List<List<String>> input, int i) {
|
---|
49 |
|
---|
50 | // stop condition
|
---|
51 | if (i == input.size()) {
|
---|
52 | // return a list with an empty list
|
---|
53 | List<List<String>> result = new ArrayList<List<String>>();
|
---|
54 | result.add(new ArrayList<String>());
|
---|
55 | return result;
|
---|
56 | }
|
---|
57 |
|
---|
58 | List<List<String>> result = new ArrayList<List<String>>();
|
---|
59 | List<List<String>> recursive = generateAllPossibleBids(input, i + 1); // recursive call
|
---|
60 |
|
---|
61 | // for each element of the first list of input
|
---|
62 | for (int j = 0; j < input.get(i).size(); j++) {
|
---|
63 | // add the element to all combinations obtained for the rest of the lists
|
---|
64 | for (int k = 0; k < recursive.size(); k++) {
|
---|
65 | // copy a combination from recursive
|
---|
66 | List<String> newList = new ArrayList<String>(recursive.get(k));
|
---|
67 | // add element of the first list
|
---|
68 | newList.add(input.get(i).get(j));
|
---|
69 | // add new combination to result
|
---|
70 | result.add(newList);
|
---|
71 | }
|
---|
72 | }
|
---|
73 | return result;
|
---|
74 | }
|
---|
75 |
|
---|
76 | public static double predict(OLSMultipleLinearRegression regression, double[] x) {
|
---|
77 | if (regression == null) {
|
---|
78 | throw new IllegalArgumentException("regression must not be null.");
|
---|
79 | }
|
---|
80 | double[] beta = regression.estimateRegressionParameters();
|
---|
81 |
|
---|
82 | double prediction = beta[0];
|
---|
83 | for (int i = 1; i < beta.length; i++) {
|
---|
84 | prediction += beta[i] * x[i - 1];
|
---|
85 | }
|
---|
86 | return prediction;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public static double scale(double x, double lowerBound, double max) {
|
---|
90 | return ((x - lowerBound) / (max - lowerBound)) * (1 - lowerBound) + lowerBound;
|
---|
91 | }
|
---|
92 |
|
---|
93 | public static int getIssueCount(List<List<ValueDiscrete>> allIssues) {
|
---|
94 | int countAll = 0;
|
---|
95 | ArrayList<String> allIssuesAsArray = new ArrayList<>();
|
---|
96 |
|
---|
97 |
|
---|
98 | for (int i = 0; i < allIssues.size(); i++) {
|
---|
99 | for (int j = 0; j < allIssues.get(i).size(); j++) {
|
---|
100 | allIssuesAsArray.add(allIssues.get(i).get(j).toString());
|
---|
101 | countAll++;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | return countAll;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | public static void getIssueDiscrete(List<Issue> issues, List<List<ValueDiscrete>> allIssues) {
|
---|
109 | for (Issue x : issues) {
|
---|
110 | IssueDiscrete is = (IssueDiscrete) x;
|
---|
111 | allIssues.add(is.getValues());
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | public static double[][] encodeBids(List<Bid> bidOrder, int countAll, List<List<ValueDiscrete>> allIssues) {
|
---|
117 | double[][] oneHotEncoded = new double[bidOrder.size()][countAll];
|
---|
118 | int count = 0;
|
---|
119 | for (int i = 0; i < oneHotEncoded.length; i++) {
|
---|
120 | for (int j = 0; j < oneHotEncoded[0].length; j++) {
|
---|
121 | for (int k = 0; k < bidOrder.get(i).getValues().values().size(); k++) {
|
---|
122 | for (int l = 0; l < allIssues.get(k).size(); l++) {
|
---|
123 | if (bidOrder.get(i).getValues().values().toArray()[k].toString().equals(allIssues.get(k).get(l).toString())) {
|
---|
124 | oneHotEncoded[i][count] = 1.0;
|
---|
125 | } else {
|
---|
126 | oneHotEncoded[i][count] = 0.0;
|
---|
127 | }
|
---|
128 | count++;
|
---|
129 | }
|
---|
130 | }
|
---|
131 | count = 0;
|
---|
132 | }
|
---|
133 | }
|
---|
134 | return oneHotEncoded;
|
---|
135 | }
|
---|
136 |
|
---|
137 | public static double[] encodeBid(Bid bid, int countAll, List<List<ValueDiscrete>> allIssues) {
|
---|
138 | double[] oneHotEncoded = new double[countAll];
|
---|
139 | int count = 0;
|
---|
140 |
|
---|
141 | for (int j = 0; j < oneHotEncoded.length; j++) {
|
---|
142 | for (int k = 0; k < bid.getValues().values().size(); k++) {
|
---|
143 | for (int l = 0; l < allIssues.get(k).size(); l++) {
|
---|
144 | if (bid.getValues().values().toArray()[k].toString().equals(allIssues.get(k).get(l).toString())) {
|
---|
145 | oneHotEncoded[count] = 1.0;
|
---|
146 | } else {
|
---|
147 | oneHotEncoded[count] = 0.0;
|
---|
148 | }
|
---|
149 | count++;
|
---|
150 | }
|
---|
151 | }
|
---|
152 | count = 0;
|
---|
153 | }
|
---|
154 |
|
---|
155 | return oneHotEncoded;
|
---|
156 | }
|
---|
157 |
|
---|
158 | public static Bid asBid(Domain domain, String[] asString) {
|
---|
159 | HashMap<Integer, Value> values = new HashMap();
|
---|
160 | for (int i = 1; i <= asString.length; i++) {
|
---|
161 | Value val = new ValueDiscrete(asString[i - 1]);
|
---|
162 | values.put(i, val);
|
---|
163 | }
|
---|
164 | return new Bid(domain, values);
|
---|
165 | }
|
---|
166 |
|
---|
167 | public static List<String> bidToListOfString(Bid input) {
|
---|
168 | List<String> result = new ArrayList<>();
|
---|
169 |
|
---|
170 | for (int i = 0; i < input.getValues().size(); i++) {
|
---|
171 | result.add(input.getValues().values().toArray()[i].toString());
|
---|
172 | }
|
---|
173 | return result;
|
---|
174 | }
|
---|
175 |
|
---|
176 | public static double[][] encodeListOfStrings(List<List<String>> bidOrder, int countAll, List<List<ValueDiscrete>> allIssues) {
|
---|
177 | double[][] oneHotEncoded = new double[bidOrder.size()][countAll];
|
---|
178 | int count = 0;
|
---|
179 | for (int i = 0; i < oneHotEncoded.length; i++) {
|
---|
180 | for (int j = 0; j < oneHotEncoded[0].length; j++) {
|
---|
181 | for (int k = 0; k < bidOrder.get(i).size(); k++) {
|
---|
182 | for (int l = 0; l < allIssues.get(k).size(); l++) {
|
---|
183 | if (bidOrder.get(i).get(k).equals(allIssues.get(k).get(l).toString())) {
|
---|
184 | oneHotEncoded[i][count] = 1.0;
|
---|
185 | } else {
|
---|
186 | oneHotEncoded[i][count] = 0.0;
|
---|
187 | }
|
---|
188 | count++;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | count = 0;
|
---|
192 | }
|
---|
193 | }
|
---|
194 | return oneHotEncoded;
|
---|
195 | }
|
---|
196 |
|
---|
197 | private static double[] encodeListOfString(List<String> bidOrder, int countAll, List<List<ValueDiscrete>> allIssues) {
|
---|
198 | double[] oneHotEncoded = new double[countAll];
|
---|
199 | int count = 0;
|
---|
200 |
|
---|
201 | for (int j = 0; j < oneHotEncoded.length; j++) {
|
---|
202 | for (int k = 0; k < bidOrder.size(); k++) {
|
---|
203 | for (int l = 0; l < allIssues.get(k).size(); l++) {
|
---|
204 | if (bidOrder.get(k).equals(allIssues.get(k).get(l).toString())) {
|
---|
205 | oneHotEncoded[count] = 1.0;
|
---|
206 | } else {
|
---|
207 | oneHotEncoded[count] = 0.0;
|
---|
208 | }
|
---|
209 | count++;
|
---|
210 | }
|
---|
211 | }
|
---|
212 | count = 0;
|
---|
213 | }
|
---|
214 |
|
---|
215 | return oneHotEncoded;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | public static List<Pair<List<String>, Double>> frequencyModelling(List<Bid> allOpponentBids, List<List<String>> allIssuesAsString, double[][] opponentModelValue) {
|
---|
220 |
|
---|
221 | HashMap<String, Integer> frequency = getIssueFrequency(allOpponentBids);
|
---|
222 | List<String> keys = new ArrayList<>(frequency.keySet());
|
---|
223 | List<Integer> values = new ArrayList<>(frequency.values());
|
---|
224 |
|
---|
225 | int[] indexes = indexesOfTopElements(values.stream().mapToInt(i -> i).toArray(), allIssuesAsString.size() / 2 - 1);
|
---|
226 |
|
---|
227 | mostWanted = new ArrayList<>();
|
---|
228 | for (int i = 0; i < indexes.length; i++) {
|
---|
229 | mostWanted.add(keys.get(indexes[i]));
|
---|
230 | }
|
---|
231 |
|
---|
232 | double[][] normalized = normalize(allOpponentBids.get(0), allOpponentBids.size(), allIssuesAsString, frequency, opponentModelValue);
|
---|
233 | double[][] model = normalized.clone();
|
---|
234 | for (int i = 0; i < model.length; i++) {
|
---|
235 | for (int j = 0; j < model[i].length; j++) {
|
---|
236 | model[i][j] = (model[i][j] + (1.0 / model[i].length)) / 2.0;
|
---|
237 | }
|
---|
238 | }
|
---|
239 | return generateOpponentBidSpace(model, allIssuesAsString);
|
---|
240 | }
|
---|
241 |
|
---|
242 | public static List<List<String>> getOptimalBids(List<List<String>> allPossibleBids, List<String> mostWanted, OLSMultipleLinearRegression regression, Domain domain, int countAll, List<List<ValueDiscrete>> allIssues) {
|
---|
243 | List<List<String>> result = clone(allPossibleBids);
|
---|
244 | List<String> wanted = new ArrayList<>();
|
---|
245 | for (int i = 0; i < mostWanted.size(); i++) {
|
---|
246 | wanted.add(mostWanted.get(i).split("_")[1]);
|
---|
247 | }
|
---|
248 |
|
---|
249 | for (int i = 0; i < wanted.size(); i++) {
|
---|
250 | for (int j = 0; j < result.size(); j++) {
|
---|
251 | if (!result.get(j).contains(wanted.get(i))) {
|
---|
252 | result.remove(j);
|
---|
253 | j--;
|
---|
254 | }
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | for (int i = 0; i < result.size(); i++) {
|
---|
259 | if (predict(regression, encodeListOfString(result.get(i), countAll, allIssues)) < 0.9) {
|
---|
260 | result.remove(i);
|
---|
261 | i--;
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 | return result;
|
---|
266 | }
|
---|
267 |
|
---|
268 | private static HashMap<String, Integer> getIssueFrequency(List<Bid> Bids) {
|
---|
269 | issueFrequency = new HashMap<>();
|
---|
270 |
|
---|
271 | for (int i = 0; i < Bids.size(); i++) {
|
---|
272 | for (int j = 0; j < Bids.get(i).getValues().values().size(); j++) {
|
---|
273 | String currentIssue = Bids.get(i).getIssues().get(j).toString() + "_" + Bids.get(i).getValues().values().toArray()[j].toString();
|
---|
274 | if (issueFrequency.containsKey(currentIssue)) {
|
---|
275 | issueFrequency.put(currentIssue, issueFrequency.get(currentIssue) + 1);
|
---|
276 | } else {
|
---|
277 | issueFrequency.put(currentIssue, 1);
|
---|
278 | }
|
---|
279 | }
|
---|
280 | }
|
---|
281 | return issueFrequency;
|
---|
282 | }
|
---|
283 |
|
---|
284 | private static double[][] normalize(Bid sampleBid, int bidCount, List<List<String>> allIssuesAsString, HashMap<String, Integer> frequency, double[][] opponentModelValue) {
|
---|
285 | double[][] result = Arrays.copyOf(opponentModelValue, opponentModelValue.length);
|
---|
286 |
|
---|
287 | for (int i = 0; i < result.length; i++) {
|
---|
288 | for (int j = 0; j < result[i].length; j++) {
|
---|
289 | result[i][j] = frequency.getOrDefault(sampleBid.getIssues().get(i).toString() + "_" + allIssuesAsString.get(i).get(j), 0) / (double) bidCount;
|
---|
290 | }
|
---|
291 | }
|
---|
292 | return result;
|
---|
293 | }
|
---|
294 |
|
---|
295 | private static List<Pair<List<String>, Double>> generateOpponentBidSpace(double[][] model, List<List<String>> allIssuesAsString) {
|
---|
296 | HashMap<List<String>, Double> opponentBidSpace = new HashMap<>();
|
---|
297 |
|
---|
298 | List<List<String>> allBids = generateAllPossibleBids(allIssuesAsString, 0);
|
---|
299 | reverse(allBids);
|
---|
300 |
|
---|
301 | for (int i = 0; i < allBids.size(); i++) {
|
---|
302 | double util = 0;
|
---|
303 | for (int j = 0; j < allBids.get(i).size(); j++) {
|
---|
304 | util += model[j][allIssuesAsString.get(j).indexOf(allBids.get(i).get(j))];
|
---|
305 | }
|
---|
306 | opponentBidSpace.put(allBids.get(i), util);
|
---|
307 | }
|
---|
308 |
|
---|
309 | return sortByValue(opponentBidSpace);
|
---|
310 |
|
---|
311 | }
|
---|
312 |
|
---|
313 | public static void reverse(List<List<String>> allPossibleBids) {
|
---|
314 | for (List<String> sublist : allPossibleBids)
|
---|
315 | Collections.reverse(sublist);
|
---|
316 | }
|
---|
317 |
|
---|
318 | private static List<Pair<List<String>, Double>> sortByValue(HashMap<List<String>, Double> input) {
|
---|
319 | List<Pair<List<String>, Double>> result = new ArrayList<>();
|
---|
320 | ArrayList<Double> valueList = new ArrayList<>(input.values());
|
---|
321 | Collections.sort(valueList);
|
---|
322 | double min = valueList.get(0);
|
---|
323 | double max = valueList.get(valueList.size() - 1);
|
---|
324 | double scaleFactor = max - min;
|
---|
325 | for (int i = 0; i < valueList.size(); i++) {
|
---|
326 | result.add(new Pair<>(getKey(input, valueList.get(i)), ((valueList.get(i) - min) / scaleFactor)));
|
---|
327 | }
|
---|
328 |
|
---|
329 | return result;
|
---|
330 | }
|
---|
331 |
|
---|
332 | private static int[] indexesOfTopElements(int[] orig, int nummax) {
|
---|
333 | try {
|
---|
334 | int[] copy = Arrays.copyOf(orig, orig.length);
|
---|
335 | Arrays.sort(copy);
|
---|
336 | int[] honey = Arrays.copyOfRange(copy, copy.length - nummax, copy.length);
|
---|
337 | int[] result = new int[nummax];
|
---|
338 | int resultPos = 0;
|
---|
339 | for (int i = 0; i < orig.length; i++) {
|
---|
340 | int onTrial = orig[i];
|
---|
341 | int index = Arrays.binarySearch(honey, onTrial);
|
---|
342 | if (index < 0) continue;
|
---|
343 | result[resultPos++] = i;
|
---|
344 | }
|
---|
345 | return result;
|
---|
346 | } catch (Exception e) {
|
---|
347 | return new int[]{0};
|
---|
348 | }
|
---|
349 |
|
---|
350 | }
|
---|
351 |
|
---|
352 | private static List<List<String>> clone(final List<List<String>> src) {
|
---|
353 | List<List<String>> dest = new ArrayList<>();
|
---|
354 | for (List<String> sublist : src) {
|
---|
355 | List<String> temp = new ArrayList<>();
|
---|
356 | for (String val : sublist) {
|
---|
357 | temp.add(val);
|
---|
358 | }
|
---|
359 | dest.add(temp);
|
---|
360 | }
|
---|
361 | return dest;
|
---|
362 | }
|
---|
363 |
|
---|
364 | }
|
---|