source: src/main/java/agents/anac/y2017/mamenchis/PreferModel.java

Last change on this file was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 10.2 KB
RevLine 
[127]1package agents.anac.y2017.mamenchis;
2
3import java.util.HashMap;
4import java.util.LinkedHashMap;
5import java.util.List;
6import java.util.Map;
7import java.util.Map.Entry;
8
9import genius.core.Bid;
10import genius.core.issue.Issue;
11import genius.core.issue.IssueDiscrete;
12import genius.core.issue.IssueInteger;
13import genius.core.issue.Objective;
14import genius.core.issue.Value;
15import genius.core.issue.ValueDiscrete;
16import genius.core.issue.ValueInteger;
17import genius.core.utility.Evaluator;
18import genius.core.utility.EvaluatorDiscrete;
19import genius.core.utility.EvaluatorInteger;
20import genius.core.utility.UtilitySpace;
21import genius.core.xml.SimpleElement;
22
23import java.util.Set;
24
25public class PreferModel {
26
27 private UtilitySpace utilitySpace;
28 private Set<Entry<Objective, Evaluator>> evaluators;
29 private int numOfIssues;
30 private double[] weights;
31 private int[] indexOfMaxFreq;
32 private int numOfValues;
33 private int learnRate;
34
35 public PreferModel(UtilitySpace utilitySpace, int num) {
36 this.utilitySpace = utilitySpace;
37 numOfIssues = num;
38 weights = new double[num];
39 initWeights();
40 initEvaluators();
41 setNumOfValues();
42 learnRate = 1;
43 }
44
45 public Set<Entry<Objective, Evaluator>> getEvaluators() {
46 return evaluators;
47 }
48
49 private void setEvaluators(Set<Entry<Objective, Evaluator>> evaluators) {
50 this.evaluators = evaluators;
51 }
52
53 public double[] getWeights() {
54 return weights;
55 }
56
57 public void setWeights(double weights[]) {
58 this.weights = weights;
59 }
60
61 public int[] getIndexOfMaxFreq() {
62 return indexOfMaxFreq;
63 }
64
65 public void setIndexOfMaxFreq(int[] indexOfMaxFreq) {
66 this.indexOfMaxFreq = indexOfMaxFreq;
67 }
68
69 public int getNumOfValues() {
70 return numOfValues;
71 }
72
73 public void setNumOfValues() {
74 int num = 0;
75 List<Issue> issues = utilitySpace.getDomain().getIssues();
76 for (Issue issue : issues) {
77 switch (issue.getType()) {
78 case DISCRETE:
79 IssueDiscrete discrete = (IssueDiscrete) issue;
80 num += discrete.getNumberOfValues();
81 break;
82 case INTEGER:
83 IssueInteger integer = (IssueInteger) issue;
84 int distance = integer.getUpperBound() - integer.getLowerBound();
85 num += (distance + 1);
86 break;
87 default:
88 break;
89 }
90 }
91 numOfValues = num;
92 }
93
94 private void initEvaluators() {
95 try {
96 Object[] issuesXML = ((SimpleElement) (utilitySpace.toXML().getChildByTagName("objective")[0]))
97 .getChildByTagName("issue").clone();
98 Map<Objective, Evaluator> map = new LinkedHashMap<>();
99 for (int i = 0; i < numOfIssues; i++) {
100 Issue issue = utilitySpace.getDomain().getIssues().get(i);
101 switch (issue.getType()) {
102 case DISCRETE:
103 List<ValueDiscrete> values = ((IssueDiscrete) utilitySpace.getDomain().getIssues().get(i))
104 .getValues();
105 EvaluatorDiscrete ed = new EvaluatorDiscrete();
106 ed.loadFromXML((SimpleElement) issuesXML[i]);
107 ed.setWeight(weights[i]);
108 for (ValueDiscrete item : values) {
109 ed.addEvaluation(item, (int) 1);
110 }
111 map.put(issue, ed);
112 break;
113 case INTEGER:
114 EvaluatorInteger et = new EvaluatorInteger();
115 et.loadFromXML((SimpleElement) issuesXML[i]);
116 et.setWeight(weights[i]);
117 et.setLinearFunction(0.0, 1.0);
118 map.put(issue, et);
119 break;
120 default:
121 System.err.println("Unsuported issue type " + issue.getType());
122 break;
123 }
124 setEvaluators((map.entrySet()));
125 }
126 } catch (Exception e) {
127 // TODO Auto-generated catch block
128 e.printStackTrace();
129 }
130 }
131
132 public void loadEvaluatorFromMyPrefer() {
133 try {
134 Object[] issuesXML = ((SimpleElement) (utilitySpace.toXML().getChildByTagName("objective")[0]))
135 .getChildByTagName("issue").clone();
136 Object[] weightXML = ((SimpleElement) (utilitySpace.toXML().getChildByTagName("objective")[0]))
137 .getChildByTagName("weight");
138 Map<Objective, Evaluator> map = new LinkedHashMap<>();
139 for (int i = 0; i < numOfIssues; i++) {
140 Issue issue = utilitySpace.getDomain().getIssues().get(i);
141 switch (issue.getType()) {
142 case DISCRETE:
143 EvaluatorDiscrete ed = new EvaluatorDiscrete();
144 ed.loadFromXML((SimpleElement) issuesXML[i]);
145 ed.setWeight(Double.parseDouble(((SimpleElement) weightXML[i]).getAttribute("value")));
146 map.put(issue, ed);
147 break;
148 case INTEGER:
149 EvaluatorInteger et = new EvaluatorInteger();
150 et.loadFromXML((SimpleElement) issuesXML[i]);
151 et.setWeight(Double.parseDouble(((SimpleElement) weightXML[i]).getAttribute("value")));
152 map.put(issue, et);
153 break;
154 default:
155 System.err.println("Unsuported issue type " + issue.getType());
156 break;
157 }
158 setEvaluators((map.entrySet()));
159 }
160 } catch (Exception e) {
161 // TODO Auto-generated catch block
162 e.printStackTrace();
163 }
164 }
165
166 public void updateEvaluators(Bid bid, int learnRate) {
167 HashMap<Integer, Value> values = bid.getValues();
168 List<Issue> issues = bid.getIssues();
169 int i = 0;
170 for (Entry<Objective, Evaluator> evaluator : evaluators) {
171 evaluator.getValue().setWeight(weights[i]);
172 try {
173 Value value = values.get(i + 1);
174 Issue issue = issues.get(i);
175 for (int j = 0; j < numOfIssues; j++) {
176 if (evaluator.getKey().toString().equals(issues.get(j).toString())) {
177 value = values.get(j + 1);
178 issue = issues.get(j);
179 break;
180 }
181 }
182 switch (value.getType()) {
183 case DISCRETE:
184 ValueDiscrete valueDiscrete = (ValueDiscrete) values.get(i + 1);
185 int old = ((EvaluatorDiscrete) evaluator.getValue()).getEvaluationNotNormalized(valueDiscrete);
186 ((EvaluatorDiscrete) evaluator.getValue()).setEvaluation(value, old + learnRate);
187 break;
188 case INTEGER:
189 EvaluatorInteger ei = ((EvaluatorInteger) evaluator.getValue());
190 if (ei.weightLocked()) {
191 continue;
192 }
193 IssueInteger issueInteger = (IssueInteger) issue;
194 ValueInteger valueInteger = (ValueInteger) value;
195 int iValue = valueInteger.getValue();
196 int distanceToUpper = Math.abs(issueInteger.getUpperBound() - iValue);
197 int distanceToLower = Math.abs(issueInteger.getLowerBound() - iValue);
198 if (distanceToUpper < distanceToLower) {
199 ei.setLinearFunction(0.0, 1.0);
200 } else {
201 ei.setLinearFunction(1.0, 0.0);
202 }
203 ei.lockWeight();
204 break;
205 default:
206 break;
207 }
208
209 } catch (Exception e) {
210 // TODO Auto-generated catch block
211 e.printStackTrace();
212 }
213 i++;
214 }
215
216 }
217
218 private void initWeights() {
219 for (int i = 0; i < weights.length; i++) {
220 weights[i] = 1D / (double) numOfIssues;
221 }
222 }
223
224 public void updateEvaluators(Bid bid) {
225 boolean hasUpadated = false;
226 HashMap<Integer, Value> values = bid.getValues();
227 List<Issue> issues = bid.getIssues();
228 int i = 0;
229 for (Entry<Objective, Evaluator> evaluator : evaluators) {
230 try {
231 Value value = values.get(i + 1);
232 Issue issue = issues.get(i);
233 for (int j = 0; j < numOfIssues; j++) {
234 if (evaluator.getKey().toString().equals(issues.get(j).toString())) {
235 value = values.get(j + 1);
236 issue = issues.get(j);
237 break;
238 }
239 }
240 switch (value.getType()) {
241 case DISCRETE:
242 ValueDiscrete valueDiscrete = (ValueDiscrete) values.get(i + 1);
243 int old = ((EvaluatorDiscrete) evaluator.getValue()).getEvaluationNotNormalized(valueDiscrete);
244 ((EvaluatorDiscrete) evaluator.getValue()).setEvaluation(value, old + 1);
245 hasUpadated = true;
246 break;
247 case INTEGER:
248 EvaluatorInteger ei = ((EvaluatorInteger) evaluator.getValue());
249 if (ei.weightLocked()) {
250 continue;
251 }
252 IssueInteger issueInteger = (IssueInteger) issue;
253 ValueInteger valueInteger = (ValueInteger) value;
254 int iValue = valueInteger.getValue();
255 int distanceToUpper = Math.abs(issueInteger.getUpperBound() - iValue);
256 int distanceToLower = Math.abs(issueInteger.getLowerBound() - iValue);
257 if (distanceToUpper < distanceToLower) {
258 ei.setLinearFunction(0.0, 1.0);
259 } else {
260 ei.setLinearFunction(1.0, 0.0);
261 }
262 hasUpadated = true;
263 ei.lockWeight();
264 break;
265 default:
266 break;
267 }
268 } catch (Exception e) {
269 // TODO Auto-generated catch block
270 e.printStackTrace();
271 }
272 i++;
273 }
274 if (hasUpadated) {
275 learnRate--;
276 }
277 }
278
279 public double getUtil(Bid bid) {
280 double util = 0D;
281 Map<Integer, Value> values = bid.getValues();
282 int issueNr = 1;
283 for (Entry<Objective, Evaluator> evaluator : evaluators) {
284 try {
285 Value value = values.get(issueNr);
286 double weight = evaluator.getValue().getWeight();
287 double valueEvaluation = 0.0;
288 switch (value.getType()) {
289 case DISCRETE:
290 EvaluatorDiscrete dEvaluator = (EvaluatorDiscrete) evaluator.getValue();
291 valueEvaluation = dEvaluator.getEvaluation((ValueDiscrete) value);
292 util += weight * valueEvaluation;
293 break;
294 case INTEGER:
295 EvaluatorInteger iEvaluator = (EvaluatorInteger) evaluator.getValue();
296 valueEvaluation = iEvaluator.getEvaluation(Integer.parseInt(value.toString()));
297 util += weight * valueEvaluation;
298 break;
299 default:
300 break;
301 }
302 } catch (Exception e) {
303 // TODO Auto-generated catch block
304 e.printStackTrace();
305 }
306 issueNr++;
307 }
308 return util;
309 }
310
311 public double getValueEvaluation(int issueNr, Value value) {
312 double evaluation = 0.0;
313 int i = 1;
314 for (Entry<Objective, Evaluator> evaluator : evaluators) {
315 if (i != issueNr) {
316 i++;
317 continue;
318 } else {
319 switch (value.getType()) {
320 case DISCRETE:
321 ValueDiscrete dValue = (ValueDiscrete) value;
322 EvaluatorDiscrete dEvaluator = (EvaluatorDiscrete) evaluator.getValue();
323 try {
324 evaluation = dEvaluator.getWeight() * dEvaluator.getEvaluation(dValue);
325 } catch (Exception e) {
326 // TODO Auto-generated catch block
327 e.printStackTrace();
328 }
329 break;
330 case INTEGER:
331 ValueInteger vInteger = (ValueInteger) value;
332 EvaluatorInteger iEvaluator = (EvaluatorInteger) evaluator.getValue();
333 evaluation = iEvaluator.getWeight() * iEvaluator.getEvaluation(vInteger.getValue());
334 break;
335 default:
336 System.err.println("Unsupported value type: " + value.getType());
337 break;
338 }
339 i++;
340 }
341 }
342 return evaluation;
343 }
344}
Note: See TracBrowser for help on using the repository browser.