1 | package agents.anac.y2019.authenticagent.serviceclasses;
|
---|
2 |
|
---|
3 | import genius.core.Bid;
|
---|
4 | import genius.core.issue.Issue;
|
---|
5 | import genius.core.issue.IssueDiscrete;
|
---|
6 | import genius.core.issue.Value;
|
---|
7 | import genius.core.issue.ValueDiscrete;
|
---|
8 |
|
---|
9 | import java.io.Serializable;
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Map;
|
---|
14 |
|
---|
15 | public class UtilityEstimationService implements Serializable {
|
---|
16 |
|
---|
17 | private static List<Issue> domainIssues;
|
---|
18 | private static List<Map<String, Integer>> numberOfRepeatsForEachValueOfAnIssue = new ArrayList<>();
|
---|
19 | private static double numberOfPossibleBids;
|
---|
20 | private static int numberOfIssues;
|
---|
21 | private static double[] weightsForIssues;
|
---|
22 |
|
---|
23 | public static void init(List<Issue> listOfExistingIssues, double npb) {
|
---|
24 | numberOfPossibleBids = npb;
|
---|
25 | domainIssues = listOfExistingIssues;
|
---|
26 | numberOfIssues = listOfExistingIssues.size();
|
---|
27 | weightsForIssues = new double[numberOfIssues];
|
---|
28 | for (int i = 0; i < numberOfIssues; i++) {
|
---|
29 | weightsForIssues[i] = 0;
|
---|
30 | }
|
---|
31 | for (int i = 0; i < numberOfIssues; i++) {
|
---|
32 | IssueDiscrete discreteIssue = (IssueDiscrete) domainIssues.get(i);
|
---|
33 | int numberOfValues = discreteIssue.getNumberOfValues();
|
---|
34 | Map<String, Integer> repeatsForEachValue = new HashMap<>();
|
---|
35 | String valueInString;
|
---|
36 | for (int j = 0; j < numberOfValues; j++) {
|
---|
37 | valueInString = discreteIssue.getValue(j).toString();
|
---|
38 | repeatsForEachValue.put(valueInString, 0);
|
---|
39 | }
|
---|
40 | numberOfRepeatsForEachValueOfAnIssue.add(repeatsForEachValue);
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | static void updateModel(Bid bid) {
|
---|
45 | if (bid == null) return;
|
---|
46 | for (int i = 0; i < domainIssues.size(); i++) {
|
---|
47 | String key = bid.getValue(i + 1).toString();
|
---|
48 | Integer currentValue = numberOfRepeatsForEachValueOfAnIssue.get(i).get(key);
|
---|
49 | if(currentValue == null) {
|
---|
50 | System.out.println("was null");
|
---|
51 | currentValue = 0;
|
---|
52 | } else
|
---|
53 | currentValue++;
|
---|
54 | numberOfRepeatsForEachValueOfAnIssue.get(i).put(key, currentValue);
|
---|
55 | updateIssueWeight();
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public static double getEstimatedUtilityFor(Bid bid) {
|
---|
60 | // if (numberOfPossibleBids == 0) {
|
---|
61 | // return 0;
|
---|
62 | // }
|
---|
63 | double sumOfEachIssueUtility = 0;
|
---|
64 | double sumOfIssueWeight = 0;
|
---|
65 | for (double anIssueWeight : weightsForIssues) {
|
---|
66 | sumOfIssueWeight += anIssueWeight;
|
---|
67 | }
|
---|
68 | for (int i = 1; i <= domainIssues.size(); i++) {
|
---|
69 | String value = bid.getValue(i).toString();
|
---|
70 | Integer numberOfPreOffers = numberOfRepeatsForEachValueOfAnIssue.get(i - 1).get(value);
|
---|
71 | if(weightsForIssues[i - 1] != 0 && sumOfIssueWeight != 0)
|
---|
72 | sumOfEachIssueUtility += ((double) numberOfPreOffers
|
---|
73 | / numberOfPossibleBids) * (weightsForIssues[i - 1] / sumOfIssueWeight);
|
---|
74 | else
|
---|
75 | sumOfEachIssueUtility += ((double) numberOfPreOffers
|
---|
76 | / numberOfPossibleBids) * (1 / (float)numberOfIssues);
|
---|
77 | }
|
---|
78 | return sumOfEachIssueUtility / numberOfIssues;
|
---|
79 | }
|
---|
80 |
|
---|
81 | private static void updateIssueWeight() {
|
---|
82 | for (int i = 0; i < numberOfIssues; i++) {
|
---|
83 | Map<String, Integer> issue = numberOfRepeatsForEachValueOfAnIssue.get(i);
|
---|
84 | weightsForIssues[i] = calculateStandardDeviation(issue);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | private static double calculateStandardDeviation(Map<String, Integer> issue) {
|
---|
89 | double sum = 0, mean;
|
---|
90 | double standardDeviation;
|
---|
91 | int size = issue.size();
|
---|
92 | for (Object o : issue.entrySet()) {
|
---|
93 | Map.Entry pair = (Map.Entry) o;
|
---|
94 | sum += (Integer) pair.getValue();
|
---|
95 | }
|
---|
96 | mean = sum / size;
|
---|
97 | double sum2 = 0;
|
---|
98 |
|
---|
99 | for (Object o : issue.entrySet()) {
|
---|
100 | Map.Entry pair2 = (Map.Entry) o;
|
---|
101 | sum2 += Math.pow((mean - (Integer) pair2.getValue()), 2);
|
---|
102 | }
|
---|
103 | if (sum2 != 0)
|
---|
104 | standardDeviation = Math.sqrt(sum2 / size);
|
---|
105 | else
|
---|
106 | standardDeviation = 0;
|
---|
107 | return standardDeviation;
|
---|
108 | }
|
---|
109 | }
|
---|