[341] | 1 | package agents.anac.y2018.agreeableagent2018;
|
---|
| 2 |
|
---|
| 3 |
|
---|
[343] | 4 | import java.util.List;
|
---|
[341] | 5 |
|
---|
[343] | 6 | import java.util.ArrayList;
|
---|
| 7 | import java.util.HashMap;
|
---|
| 8 | import java.util.Map;
|
---|
| 9 |
|
---|
[341] | 10 | import java.io.Serializable;
|
---|
| 11 |
|
---|
[343] | 12 | import genius.core.Bid;
|
---|
| 13 | import genius.core.issue.Issue;
|
---|
| 14 | import genius.core.issue.IssueDiscrete;
|
---|
| 15 |
|
---|
[341] | 16 | /**
|
---|
| 17 | * Created by Sahar Mirzayi
|
---|
| 18 | * University of Tehran
|
---|
| 19 | * Agent Lab.
|
---|
| 20 | * Sahar.Mirzayi @ gmail.com
|
---|
| 21 | */
|
---|
| 22 |
|
---|
| 23 | public class FrequencyBasedOpponentModel implements Serializable {
|
---|
| 24 |
|
---|
| 25 | private List<Issue> domainIssues;
|
---|
| 26 | private List<Map<String, Integer>> issueValueFrequency = new ArrayList<>();
|
---|
| 27 | private int totalNumberOfBids;
|
---|
| 28 | private int issueCount;
|
---|
| 29 | private double[] issueWeight;
|
---|
| 30 |
|
---|
| 31 | public void init(List<Issue> issues) {
|
---|
| 32 | domainIssues = issues;
|
---|
| 33 | issueCount = issues.size();
|
---|
| 34 | issueWeight = new double[issueCount];
|
---|
| 35 | for (int i = 0; i < issueCount; i++) {
|
---|
| 36 | issueWeight[i] = 0;
|
---|
| 37 | }
|
---|
| 38 | for (int i = 0; i < issueCount; i++) {
|
---|
| 39 | int numberOfValues = ((IssueDiscrete) domainIssues.get(i)).getNumberOfValues();
|
---|
| 40 | Map<String, Integer> x = new HashMap<>();
|
---|
| 41 | for (int j = 0; j < numberOfValues; j++) {
|
---|
| 42 | String s = ((IssueDiscrete) domainIssues.get(i)).getValue(j).toString();
|
---|
| 43 | x.put(s, 0);
|
---|
| 44 | }
|
---|
| 45 | issueValueFrequency.add(x);
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public void updateModel(Bid bid, int numberOfBids) {
|
---|
| 50 | if (bid == null) return;
|
---|
| 51 | totalNumberOfBids = numberOfBids;
|
---|
| 52 | for (int i = 0; i < domainIssues.size(); i++) {
|
---|
| 53 | String key = bid.getValue(i + 1).toString();
|
---|
| 54 | Integer currentValue = issueValueFrequency.get(i).get(key);
|
---|
| 55 | currentValue++;
|
---|
| 56 | issueValueFrequency.get(i).put(key, currentValue);
|
---|
| 57 | updateIssueWeight();
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | private void updateIssueWeight() {
|
---|
| 62 | for (int i = 0; i < issueCount; i++) {
|
---|
| 63 | Map<String, Integer> issue = issueValueFrequency.get(i);
|
---|
| 64 | issueWeight[i] = calculateStandardDeviation(issue);
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | private double calculateStandardDeviation(Map<String, Integer> issue) {
|
---|
| 69 | double sum = 0, mean;
|
---|
| 70 | double standardDeviation;
|
---|
| 71 | int size = issue.size();
|
---|
| 72 | for (Object o : issue.entrySet()) {
|
---|
| 73 | Map.Entry pair = (Map.Entry) o;
|
---|
| 74 | sum += (Integer) pair.getValue();
|
---|
| 75 | }
|
---|
| 76 | mean = sum / size;
|
---|
| 77 | double sum2 = 0;
|
---|
| 78 |
|
---|
| 79 | for (Object o : issue.entrySet()) {
|
---|
| 80 | Map.Entry pair2 = (Map.Entry) o;
|
---|
| 81 | sum2 += Math.pow((mean - (Integer) pair2.getValue()), 2);
|
---|
| 82 | }
|
---|
| 83 | if (sum2 != 0)
|
---|
| 84 | standardDeviation = Math.sqrt(sum2 / size);
|
---|
| 85 | else
|
---|
| 86 | standardDeviation = 0;
|
---|
| 87 | return standardDeviation;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 |
|
---|
| 91 | public double getUtility(Bid bids) {
|
---|
| 92 | if (totalNumberOfBids == 0) return 0;
|
---|
| 93 | double sumOfEachIssueUtility = 0;
|
---|
| 94 | double sumOfIssueWeight=0;
|
---|
| 95 | for (double anIssueWeight : issueWeight) {
|
---|
| 96 | sumOfIssueWeight += anIssueWeight;
|
---|
| 97 | }
|
---|
| 98 | for (int i = 1; i <= domainIssues.size(); i++) {
|
---|
| 99 | String value = bids.getValue(i).toString();
|
---|
| 100 | Integer numberOfPreOffers = issueValueFrequency.get(i - 1).get(value);
|
---|
| 101 | sumOfEachIssueUtility += ((double) numberOfPreOffers
|
---|
| 102 | / (double) totalNumberOfBids) * (issueWeight[i - 1]/sumOfIssueWeight);
|
---|
| 103 | }
|
---|
| 104 | return sumOfEachIssueUtility / issueCount;
|
---|
| 105 | }
|
---|
| 106 | }
|
---|