1 | package parties.in4010.q12015.group13;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.List;
|
---|
5 |
|
---|
6 | import genius.core.Bid;
|
---|
7 | import genius.core.Domain;
|
---|
8 | import genius.core.issue.Issue;
|
---|
9 | import genius.core.issue.IssueDiscrete;
|
---|
10 | import genius.core.issue.IssueInteger;
|
---|
11 | import genius.core.issue.Value;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Models an opponent using the frequency model
|
---|
15 | */
|
---|
16 | public class FrequencyOpponentModel {
|
---|
17 |
|
---|
18 | private double n;
|
---|
19 |
|
---|
20 | // issueModels contains (per issue) the model for that specific issue.
|
---|
21 | protected ArrayList<IssueModel> issueModels;
|
---|
22 | // weights contains the estimated weights
|
---|
23 | protected ArrayList<Double> weights;
|
---|
24 | // issueNumbers contains the issue number for each issue
|
---|
25 | protected ArrayList<Integer> issueNumbers;
|
---|
26 |
|
---|
27 | protected Bid previousBid = null;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Constructs a model for a single opponent. Domain can only contain
|
---|
31 | * Discrete or Integer issues, but all the default domains only contain
|
---|
32 | * these types.
|
---|
33 | */
|
---|
34 | public FrequencyOpponentModel(Domain domain, double n) {
|
---|
35 | this.n = n;
|
---|
36 |
|
---|
37 | List<Issue> issues = domain.getIssues();
|
---|
38 | issueModels = new ArrayList(issues.size());
|
---|
39 | weights = new ArrayList(issues.size());
|
---|
40 | issueNumbers = new ArrayList(issues.size());
|
---|
41 |
|
---|
42 | for (Issue issue : issues) {
|
---|
43 | switch (issue.getType()) {
|
---|
44 | case DISCRETE:
|
---|
45 | issueModels.add(new DiscreteIssueModel((IssueDiscrete) issue));
|
---|
46 | break;
|
---|
47 | case INTEGER:
|
---|
48 | issueModels.add(new IntegerIssueModel((IssueInteger) issue));
|
---|
49 | break;
|
---|
50 | default:
|
---|
51 | throw new RuntimeException("Unknown Issue Type: "
|
---|
52 | + issue.getType());
|
---|
53 | }
|
---|
54 |
|
---|
55 | weights.add(1.0 / issues.size());
|
---|
56 | issueNumbers.add(issue.getNumber());
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | public double estimateUtility(Bid b) {
|
---|
61 | double ret = 0;
|
---|
62 |
|
---|
63 | for (int i = 0; i < issueModels.size(); i++) {
|
---|
64 | try {
|
---|
65 | ret += weights.get(i)
|
---|
66 | * issueModels.get(i).estimateUtility(
|
---|
67 | b.getValue(issueNumbers.get(i)));
|
---|
68 | } catch (Exception ex) {
|
---|
69 | System.err.println("Exception while estimating utility: "
|
---|
70 | + ex.getMessage());
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | return ret;
|
---|
75 | }
|
---|
76 |
|
---|
77 | public void addBid(Bid b) throws Exception {
|
---|
78 | for (int i = 0; i < issueModels.size(); i++) {
|
---|
79 | Value v = b.getValue(issueNumbers.get(i));
|
---|
80 | issueModels.get(i).addBid(v);
|
---|
81 | }
|
---|
82 |
|
---|
83 | if (previousBid != null) {
|
---|
84 | for (int i = 0; i < weights.size(); i++) {
|
---|
85 | if (b.getValue(issueNumbers.get(i)).equals(
|
---|
86 | previousBid.getValue(issueNumbers.get(i)))) {
|
---|
87 | weights.set(i, weights.get(i) + n);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | normalizeWeights();
|
---|
91 | }
|
---|
92 |
|
---|
93 | previousBid = b;
|
---|
94 | }
|
---|
95 |
|
---|
96 | private void normalizeWeights() {
|
---|
97 | double sum = Util.getSum(weights);
|
---|
98 |
|
---|
99 | for (int i = 0; i < weights.size(); i++) {
|
---|
100 | weights.set(i, weights.get(i) / sum);
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | public Bid getPreviousBid() {
|
---|
105 | return previousBid;
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | public String toString() {
|
---|
110 | String ret = "";
|
---|
111 |
|
---|
112 | for (int i = 0; i < issueModels.size(); i++) {
|
---|
113 | ret += "Issue " + issueNumbers.get(i) + " (weight = "
|
---|
114 | + weights.get(i) + " )" + ":\n";
|
---|
115 |
|
---|
116 | ret += issueModels.get(i).toString();
|
---|
117 | }
|
---|
118 |
|
---|
119 | return ret;
|
---|
120 | }
|
---|
121 | }
|
---|