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