1 | package agents.anac.y2010.AgentSmith;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.HashMap;
|
---|
5 |
|
---|
6 | import genius.core.Bid;
|
---|
7 | import genius.core.issue.Issue;
|
---|
8 | import genius.core.issue.Value;
|
---|
9 | import genius.core.issue.ValueDiscrete;
|
---|
10 | import genius.core.issue.ValueInteger;
|
---|
11 | import genius.core.issue.ValueReal;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Model of one issue, takes all values of the opponent on this issue. Then the
|
---|
15 | * utility and it's weight can be calculated on this issue. In the OpponentModel
|
---|
16 | * this information is used to determine the utility of the other party.
|
---|
17 | */
|
---|
18 | public class IssueModel {
|
---|
19 | private ArrayList<Value> fValues;
|
---|
20 | private Issue fIssue;
|
---|
21 | private Bounds fBounds;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Constructor
|
---|
25 | *
|
---|
26 | * @param lIssue
|
---|
27 | */
|
---|
28 | public IssueModel(Issue lIssue) {
|
---|
29 | fValues = new ArrayList<Value>();
|
---|
30 | fIssue = lIssue;
|
---|
31 | fBounds = new Bounds(fIssue);
|
---|
32 | }
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * The mean of the values in the values list.
|
---|
36 | */
|
---|
37 | public double getAverage() {
|
---|
38 | double lTotal = 0;
|
---|
39 | for (Value lWeight : fValues) {
|
---|
40 | lTotal += getNumberValue(lWeight);
|
---|
41 | }
|
---|
42 | return lTotal / fValues.size();
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * The standard deviation utility of the issues.
|
---|
47 | */
|
---|
48 | public double getDeviation() {
|
---|
49 | double lIssueAverage = getAverage();
|
---|
50 | double lTotal = 0;
|
---|
51 | for (Value lWeight : fValues) {
|
---|
52 | lTotal += Math.pow(getNumberValue(lWeight) - lIssueAverage, 2);
|
---|
53 | }
|
---|
54 |
|
---|
55 | return Math.sqrt(lTotal / (double) fValues.size());
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Add a value to the list
|
---|
60 | */
|
---|
61 | public void addValue(Value pValue) {
|
---|
62 | fValues.add(pValue);
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * The value of an issue in a double type
|
---|
67 | */
|
---|
68 | public double getNumberValue(Value pValue) {
|
---|
69 | switch (fIssue.getType()) {
|
---|
70 | case DISCRETE:
|
---|
71 | throw new RuntimeException("No get value for discret");
|
---|
72 | case REAL:
|
---|
73 | return ((ValueReal) pValue).getValue();
|
---|
74 | case INTEGER:
|
---|
75 | return ((ValueInteger) pValue).getValue();
|
---|
76 | default:
|
---|
77 | return 0;
|
---|
78 | }
|
---|
79 |
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * The utility of a bid, which can be real, integer or discrete
|
---|
84 | */
|
---|
85 | public double getUtility(Bid pBid) {
|
---|
86 | double lUtility = 0;
|
---|
87 | switch (fIssue.getType()) {
|
---|
88 | case INTEGER:
|
---|
89 | lUtility = getRealUtility(pBid);
|
---|
90 | break;
|
---|
91 | case REAL:
|
---|
92 | lUtility = getRealUtility(pBid);
|
---|
93 | break;
|
---|
94 | case DISCRETE:
|
---|
95 | lUtility = getDiscreteUtility(pBid);
|
---|
96 | break;
|
---|
97 | }
|
---|
98 |
|
---|
99 | return lUtility;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * The utility of if this issue is discrete. Takes the amount of values that
|
---|
104 | * are the same and divides it by the total amount of values. If there are
|
---|
105 | * lots of same values as the bid, the utility will be high, else it will be
|
---|
106 | * low.
|
---|
107 | */
|
---|
108 | public double getDiscreteUtility(Bid pBid) {
|
---|
109 | double lSame = 0;
|
---|
110 | for (Value lValue : fValues) {
|
---|
111 | ValueDiscrete lDiscreteValue = (ValueDiscrete) lValue;
|
---|
112 | if (lDiscreteValue.getValue().equals(
|
---|
113 | ((ValueDiscrete) getBidValue(pBid)).getValue())) {
|
---|
114 | lSame++;
|
---|
115 | }
|
---|
116 | }
|
---|
117 | return this.fValues.size() == 0 ? 0 : lSame
|
---|
118 | / (double) this.fValues.size();
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * The utility of a bid in the real or integer case
|
---|
123 | */
|
---|
124 | public double getRealUtility(Bid pBid) {
|
---|
125 | return 1 - Math.abs(fBounds
|
---|
126 | .normalize(getNumberValue(getBidValue(pBid)))
|
---|
127 | - fBounds.normalize(getAverage()));
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Get's the importance of this issues utility
|
---|
132 | */
|
---|
133 | public double getWeight() {
|
---|
134 | double lWeight = 0;
|
---|
135 | switch (fIssue.getType()) {
|
---|
136 | case INTEGER:
|
---|
137 | lWeight = getRealWeight();
|
---|
138 | break;
|
---|
139 | case REAL:
|
---|
140 | lWeight = getRealWeight();
|
---|
141 | break;
|
---|
142 | case DISCRETE:
|
---|
143 | lWeight = getDiscreteWeight();
|
---|
144 | break;
|
---|
145 | }
|
---|
146 |
|
---|
147 | return lWeight;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Gets the weight of this value. If there are lots of changes, it's not
|
---|
152 | * important, else it is important.
|
---|
153 | */
|
---|
154 | public double getDiscreteWeight() {
|
---|
155 | HashMap<String, Integer> lCounter = new HashMap<String, Integer>();
|
---|
156 | for (Value lValue : fValues) {
|
---|
157 | ValueDiscrete lDiscreteValue = (ValueDiscrete) lValue;
|
---|
158 | int lCount = lCounter.get(lDiscreteValue.getValue()) != null ? lCounter
|
---|
159 | .get(lDiscreteValue.getValue()) : 0;
|
---|
160 | lCount++;
|
---|
161 | lCounter.put(lDiscreteValue.getValue(), lCount);
|
---|
162 | }
|
---|
163 | double lMax = 0;
|
---|
164 | double lTotal = 0;
|
---|
165 |
|
---|
166 | for (int lC : lCounter.values()) {
|
---|
167 | if (lC > lMax)
|
---|
168 | lMax = lC;
|
---|
169 | lTotal += lC;
|
---|
170 | }
|
---|
171 |
|
---|
172 | return lMax / lTotal;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * returns the real (or integer) weight
|
---|
177 | */
|
---|
178 | public double getRealWeight() {
|
---|
179 | return 1 / (1 + getDeviation());
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * returns the value of a bid
|
---|
184 | */
|
---|
185 | public Value getBidValue(Bid pBid) {
|
---|
186 | return getBidValueByIssue(pBid, fIssue);
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * returns the value of an issue in a bid
|
---|
191 | */
|
---|
192 | public static Value getBidValueByIssue(Bid pBid, Issue pIssue) {
|
---|
193 | Value lValue = null;
|
---|
194 | try {
|
---|
195 | lValue = pBid.getValue(pIssue.getNumber());
|
---|
196 | } catch (Exception e) {
|
---|
197 | }
|
---|
198 |
|
---|
199 | return lValue;
|
---|
200 |
|
---|
201 | }
|
---|
202 | }
|
---|