source: src/main/java/agents/anac/y2018/agreeableagent2018/FrequencyBasedOpponentModel.java@ 345

Last change on this file since 345 was 343, checked in by Tim Baarslag, 4 years ago

Fixed all errors in all 2018 agents

File size: 3.5 KB
Line 
1package agents.anac.y2018.agreeableagent2018;
2
3
4import java.util.List;
5
6import java.util.ArrayList;
7import java.util.HashMap;
8import java.util.Map;
9
10import java.io.Serializable;
11
12import genius.core.Bid;
13import genius.core.issue.Issue;
14import genius.core.issue.IssueDiscrete;
15
16/**
17 * Created by Sahar Mirzayi
18 * University of Tehran
19 * Agent Lab.
20 * Sahar.Mirzayi @ gmail.com
21 */
22
23public 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}
Note: See TracBrowser for help on using the repository browser.