1 | package parties.in4010.q12015.group9;
|
---|
2 |
|
---|
3 | import java.util.HashMap;
|
---|
4 | import java.util.Map.Entry;
|
---|
5 |
|
---|
6 | import genius.core.BidHistory;
|
---|
7 | import genius.core.Domain;
|
---|
8 | import genius.core.actions.Action;
|
---|
9 | import genius.core.bidding.BidDetails;
|
---|
10 | import genius.core.issue.Issue;
|
---|
11 | import genius.core.issue.IssueDiscrete;
|
---|
12 | import genius.core.issue.Objective;
|
---|
13 | import genius.core.issue.ValueDiscrete;
|
---|
14 | import genius.core.utility.AdditiveUtilitySpace;
|
---|
15 | import genius.core.utility.Evaluator;
|
---|
16 | import genius.core.utility.EvaluatorDiscrete;
|
---|
17 |
|
---|
18 | public class OpponentModeling {
|
---|
19 | private HashMap<Object, AdditiveUtilitySpace> opponentUtilities;// Object is
|
---|
20 | // other
|
---|
21 | // agent objects
|
---|
22 | // used to identify
|
---|
23 | // them,
|
---|
24 | // utilityspaces are
|
---|
25 | // current estimates
|
---|
26 | private Domain currentDomain;
|
---|
27 | private AdditiveUtilitySpace ourUtility;
|
---|
28 | private double learnCoef = 0.2;
|
---|
29 | private int learnValueAddition = 1;
|
---|
30 | private int amountOfIssues;
|
---|
31 |
|
---|
32 | public OpponentModeling(AdditiveUtilitySpace ownUtility) {
|
---|
33 | ourUtility = ownUtility;
|
---|
34 | currentDomain = ourUtility.getDomain();
|
---|
35 | opponentUtilities = new HashMap<Object, AdditiveUtilitySpace>();
|
---|
36 | }
|
---|
37 |
|
---|
38 | // Simple getter
|
---|
39 | public HashMap<Object, AdditiveUtilitySpace> getOpponentUtilities() {
|
---|
40 | return opponentUtilities;
|
---|
41 | }
|
---|
42 |
|
---|
43 | // Determines the difference between bids given the opponent's prevoius
|
---|
44 | // Utility Space
|
---|
45 | private HashMap<Integer, Integer> determineDifference(
|
---|
46 | AdditiveUtilitySpace thisSpace, BidDetails first, BidDetails second) {
|
---|
47 |
|
---|
48 | HashMap<Integer, Integer> diff = new HashMap<Integer, Integer>();
|
---|
49 | try {
|
---|
50 | for (Issue i : thisSpace.getDomain().getIssues()) {
|
---|
51 | diff.put(i.getNumber(), (((ValueDiscrete) first.getBid()
|
---|
52 | .getValue(i.getNumber())).equals((ValueDiscrete) second
|
---|
53 | .getBid().getValue(i.getNumber()))) ? 0 : 1);
|
---|
54 | }
|
---|
55 | } catch (Exception ex) {
|
---|
56 | ex.printStackTrace();
|
---|
57 | }
|
---|
58 |
|
---|
59 | return diff;
|
---|
60 | }
|
---|
61 |
|
---|
62 | // This is called whenever a message is received
|
---|
63 | public void updateModel(Object agent, Action action,
|
---|
64 | HashMap<Object, BidHistory> previousBidsMap) {
|
---|
65 | if (!opponentUtilities.containsKey(agent)) {
|
---|
66 | createNewModel(agent);
|
---|
67 | }
|
---|
68 | AdditiveUtilitySpace updatedSpace = opponentUtilities.get(agent);
|
---|
69 | // updating Utility space, both for accepting actions and new offer
|
---|
70 | // actions, will probably want to split those completely
|
---|
71 | if (previousBidsMap.get(agent).size() < 2) {
|
---|
72 | return;
|
---|
73 | }
|
---|
74 | int numberOfUnchanged = 0;
|
---|
75 | BidDetails oppBid = previousBidsMap.get(agent).getHistory()
|
---|
76 | .get(previousBidsMap.get(agent).size() - 1);
|
---|
77 | BidDetails prevOppBid = previousBidsMap.get(agent).getHistory()
|
---|
78 | .get(previousBidsMap.get(agent).size() - 2);
|
---|
79 | HashMap<Integer, Integer> lastDiffSet = determineDifference(
|
---|
80 | updatedSpace, prevOppBid, oppBid);
|
---|
81 |
|
---|
82 | // count the number of changes in value
|
---|
83 | for (Integer i : lastDiffSet.keySet()) {
|
---|
84 | if (lastDiffSet.get(i) == 0)
|
---|
85 | numberOfUnchanged++;
|
---|
86 | }
|
---|
87 | // This is the value to be added to weights of unchanged issues before
|
---|
88 | // normalization.
|
---|
89 | // Also the value that is taken as the minimum possible weight,
|
---|
90 | // (therefore defining the maximum possible also).
|
---|
91 | double goldenValue = learnCoef / (double) amountOfIssues;
|
---|
92 | // The total sum of weights before normalization.
|
---|
93 | double totalSum = 1D + goldenValue * (double) numberOfUnchanged;
|
---|
94 | // The maximum possible weight
|
---|
95 | double maximumWeight = 1D - ((double) amountOfIssues) * goldenValue
|
---|
96 | / totalSum;
|
---|
97 |
|
---|
98 | // re-weighing issues while making sure that the sum remains 1
|
---|
99 | for (Integer i : lastDiffSet.keySet()) {
|
---|
100 | if (lastDiffSet.get(i) == 0
|
---|
101 | && updatedSpace.getWeight(i) < maximumWeight)
|
---|
102 | updatedSpace.setWeight(updatedSpace.getDomain()
|
---|
103 | .getObjectivesRoot().getObjective(i),
|
---|
104 | (updatedSpace.getWeight(i) + goldenValue) / totalSum);
|
---|
105 | else
|
---|
106 | updatedSpace.setWeight(updatedSpace.getDomain()
|
---|
107 | .getObjectivesRoot().getObjective(i),
|
---|
108 | updatedSpace.getWeight(i) / totalSum);
|
---|
109 | }
|
---|
110 | // Then for each issue value that has been offered last time, a constant
|
---|
111 | // value is added to its corresponding ValueDiscrete.
|
---|
112 | try {
|
---|
113 | for (Entry<Objective, Evaluator> e : updatedSpace.getEvaluators()) {
|
---|
114 | // cast issue to discrete and retrieve value. Next, add constant
|
---|
115 | // learnValueAddition to the current preference of the value to
|
---|
116 | // make
|
---|
117 | // it more important
|
---|
118 | ((EvaluatorDiscrete) e.getValue())
|
---|
119 | .setEvaluation(
|
---|
120 | oppBid.getBid().getValue(
|
---|
121 | ((IssueDiscrete) e.getKey())
|
---|
122 | .getNumber()),
|
---|
123 | (learnValueAddition + ((EvaluatorDiscrete) e
|
---|
124 | .getValue())
|
---|
125 | .getEvaluationNotNormalized(((ValueDiscrete) oppBid
|
---|
126 | .getBid().getValue(
|
---|
127 | ((IssueDiscrete) e
|
---|
128 | .getKey())
|
---|
129 | .getNumber())))));
|
---|
130 | }
|
---|
131 | } catch (Exception ex) {
|
---|
132 | ex.printStackTrace();
|
---|
133 | }
|
---|
134 | opponentUtilities.put(agent, updatedSpace);
|
---|
135 | }
|
---|
136 |
|
---|
137 | // creates standard UtilitySpace model given no current information
|
---|
138 | private void createNewModel(Object agent) {
|
---|
139 | // UtilitySpace newUtilitySpace = new UtilitySpace(currentDomain);
|
---|
140 | AdditiveUtilitySpace newUtilitySpace = new AdditiveUtilitySpace(
|
---|
141 | ourUtility);
|
---|
142 | // set all issue weights to be equal and evaluations 1)
|
---|
143 | amountOfIssues = newUtilitySpace.getDomain().getIssues().size();
|
---|
144 | double commonWeight = 1D / (double) amountOfIssues;
|
---|
145 |
|
---|
146 | // initialize the weights
|
---|
147 | for (Entry<Objective, Evaluator> e : newUtilitySpace.getEvaluators()) {
|
---|
148 | // set the issue weights
|
---|
149 | newUtilitySpace.unlock(e.getKey());
|
---|
150 | e.getValue().setWeight(commonWeight);
|
---|
151 | try {
|
---|
152 | // set all value weights to one (they are normalized when
|
---|
153 | // calculating the utility)
|
---|
154 | for (ValueDiscrete vd : ((IssueDiscrete) e.getKey())
|
---|
155 | .getValues())
|
---|
156 | ((EvaluatorDiscrete) e.getValue()).setEvaluation(vd, 1);
|
---|
157 | } catch (Exception ex) {
|
---|
158 | ex.printStackTrace();
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | opponentUtilities.put(agent, newUtilitySpace);
|
---|
163 | }
|
---|
164 |
|
---|
165 | } |
---|