source: src/main/java/parties/in4010/q12015/group10/Opponent.java@ 126

Last change on this file since 126 was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 16.2 KB
Line 
1package parties.in4010.q12015.group10;
2
3import java.io.FileWriter;
4import java.io.IOException;
5import java.io.PrintWriter;
6import java.text.DateFormat;
7import java.text.SimpleDateFormat;
8import java.util.ArrayList;
9import java.util.Date;
10
11import genius.core.AgentID;
12import genius.core.Bid;
13import genius.core.BidHistory;
14import genius.core.Deadline;
15import genius.core.bidding.BidDetails;
16import genius.core.issue.IssueDiscrete;
17import genius.core.issue.Value;
18import genius.core.issue.ValueDiscrete;
19import genius.core.utility.AdditiveUtilitySpace;
20import genius.core.utility.EvaluatorDiscrete;
21import genius.core.xml.SimpleElement;
22
23//import negotiator.Bid;
24
25public class Opponent {
26
27 // estimated utilities
28
29 private AgentID agentID; // Actual name (could be anything, like
30 // "DoubleOSeven")
31 private int myPartyNumber; // Our numeric representation of the party
32 // (instead of the string that could be
33 // anything, like "DoubleOSeven"))
34
35 private BidHistory myMadeBidHistory = new BidHistory(); // Offers that I
36 // made, along with
37 // the time at which
38 // I made them, and
39 // the utility for
40 // Group 10.
41 private BidHistory myAcceptedBidHistory = new BidHistory();// Offers that I
42 // accepted,
43 // along with
44 // the time at
45 // which I
46 // accepted
47 // them, and the
48 // utility for
49 // Group 10.
50
51 private AdditiveUtilitySpace myEstUtilitySpace;
52
53 private boolean hasMadeFirstBid = false;
54
55 private int numberOfIssues;
56 private int[] numberOfValuesPerIssue;
57
58 private int listSize;
59
60 private Deadline myDeadline;
61
62 // create a bidhistory in which you can select the nth bid
63 private ArrayList<BidDetails> customBidHistory = new ArrayList<BidDetails>();
64
65 // All these arrays are based on the same list index:
66
67 // Ind. Iss. Val freq valueUnchanged
68 // 0 Issue 0 value0 1 false
69 // 1 value1 10 true
70 // 2 value2 1 false
71 // 3 Issue 1 value0 6 etc
72 // 4 value1 6
73 // 5 Issue 2 value0 3
74 // 6 value1 3
75 // 7 value2 0
76 // 8 value3 3
77 // 9 value4 3
78 // 10 Issue 3 value0 etc
79 // 11 value1
80
81 private Value[] valueList;
82 private double[] valueValueList;
83 private int[] valueIndexList;
84 private int[] issueIndexList;
85 private double[] freqList;
86 private boolean[] valueUnchangedList;
87
88 private EvaluatorDiscrete[] myEvaluatorDiscrete;
89
90 private int bidGetValueIndexOffset = 1;
91
92 // constructor
93 Opponent(int givenPartyNumber, AdditiveUtilitySpace myUtilitySpace,
94 Deadline givenDeadline) {
95 myDeadline = givenDeadline;
96 myPartyNumber = givenPartyNumber;
97 myEstUtilitySpace = new AdditiveUtilitySpace(myUtilitySpace);
98
99 numberOfIssues = myEstUtilitySpace.getNrOfEvaluators();
100 numberOfValuesPerIssue = new int[numberOfIssues];
101
102 // Extract the total value space
103 for (int issueIndex = 0; issueIndex < numberOfIssues; issueIndex++) {
104 numberOfValuesPerIssue[issueIndex] = ((IssueDiscrete) myEstUtilitySpace
105 .getDomain().getIssues().get(issueIndex))
106 .getNumberOfValues();
107 listSize = listSize + numberOfValuesPerIssue[issueIndex];
108 }
109
110 // Define the table
111 valueList = new Value[listSize]; // a list of all values, for all issues
112 valueValueList = new double[listSize]; // a list of all valueValues
113 valueIndexList = new int[listSize]; // a list of all value indexes
114 // within the issue
115 issueIndexList = new int[listSize]; // a list of all issue indexes
116 // corresponding to the value
117 freqList = new double[listSize]; // a list of the number of occurance
118 // (number of times) a value has
119 // been included in a bid
120 valueUnchangedList = new boolean[listSize]; // a list containing whether
121 // a value has remained the
122 // same
123 // initialize the table
124 int listIndex = 0;
125 for (int issueIndex = 0; issueIndex < numberOfIssues; issueIndex++) {
126 for (int valueIndex = 0; valueIndex < numberOfValuesPerIssue[issueIndex]; valueIndex++) {
127 valueList[listIndex] = ((IssueDiscrete) myEstUtilitySpace
128 .getDomain().getIssues().get(issueIndex))
129 .getValue(valueIndex);
130 valueValueList[listIndex] = 1;
131 valueIndexList[listIndex] = valueIndex;
132 issueIndexList[listIndex] = issueIndex;
133 freqList[listIndex] = 0;
134 valueUnchangedList[listIndex] = false;
135
136 listIndex++;
137 }
138 }
139 // construct the evaluator functions
140
141 myEvaluatorDiscrete = new EvaluatorDiscrete[myEstUtilitySpace
142 .getNrOfEvaluators()];
143
144 for (int issueIndex = 0; issueIndex < myEstUtilitySpace
145 .getNrOfEvaluators(); issueIndex++) {
146 myEvaluatorDiscrete[issueIndex] = new EvaluatorDiscrete();
147 for (int valueIndex = 0; valueIndex < ((IssueDiscrete) myEstUtilitySpace
148 .getDomain().getIssues().get(issueIndex))
149 .getNumberOfValues(); valueIndex++) {
150 myEvaluatorDiscrete[issueIndex].addEvaluation(
151 ((IssueDiscrete) myEstUtilitySpace.getDomain()
152 .getIssues().get(issueIndex))
153 .getValue(valueIndex), 1);
154 }
155 myEvaluatorDiscrete[issueIndex]
156 .setWeight(1 / ((double) myEstUtilitySpace
157 .getNrOfEvaluators()));
158 myEstUtilitySpace.addEvaluator((IssueDiscrete) myEstUtilitySpace
159 .getDomain().getIssues().get(issueIndex),
160 myEvaluatorDiscrete[issueIndex]);
161 }
162 }
163
164 // Define methods for Opponents class
165
166 void setAgentID(AgentID givenAgentID) {
167 agentID = givenAgentID;
168 }
169
170 void StoreOfferedBid(BidDetails madeBidDetails) {
171 // check whether this is the first Bid
172 Bid previousBid;
173 if (hasMadeFirstBid) {
174 previousBid = myMadeBidHistory.getLastBid();
175 // reset the unchanged list
176 for (int listIndex = 0; listIndex < listSize; listIndex++) {
177 valueUnchangedList[listIndex] = false;
178 }
179 } else {
180 previousBid = null;
181 }
182
183 myMadeBidHistory.add(madeBidDetails);
184 customBidHistory.add(madeBidDetails);
185
186 // update the frequency and unchanged list
187 for (int issueIndex = 0; issueIndex < numberOfIssues; issueIndex++) {
188 try {
189 for (int listIndex = 0; listIndex < listSize; listIndex++) {
190 // When reading the values in a bid, the index starts at
191 // ONE!!!
192 if (myMadeBidHistory.getLastBid()
193 .getValue(issueIndex + bidGetValueIndexOffset)
194 .equals(valueList[listIndex])) {
195 // updating consecutive list, unless this is the first
196 // bid.
197 if (hasMadeFirstBid) {
198 if (previousBid.getValue(
199 issueIndex + bidGetValueIndexOffset)
200 .equals(valueList[listIndex])) {
201 valueUnchangedList[listIndex] = true;
202 }
203 }
204 // updating the frequency list
205 double freqScale = 1;
206 double freqStep = 1;
207 if (valueUnchangedList[listIndex]) {
208 for (int listIndex2 = 0; listIndex2 < listSize; listIndex2++) {
209 if (issueIndexList[listIndex2] == issueIndexList[listIndex]) {
210 freqList[listIndex2] = freqList[listIndex2]
211 + freqStep;
212 }
213 }
214
215 } else {
216 freqScale = 2;
217 }
218 freqList[listIndex] = freqList[listIndex] + freqStep
219 * freqScale;
220 }
221 }
222 } catch (Exception e) {
223 e.printStackTrace();
224 System.out
225 .println("throw/catch triggered, in class(opponent) when reading the value of a bid");
226 System.out.println("thrown issueIndex" + issueIndex);
227
228 }
229 }
230 if (!hasMadeFirstBid) {
231 hasMadeFirstBid = true;
232 }
233
234 }
235
236 void setValueValueAtIndex(int issueIndex, int listIndex, double evaluationIn) {
237 try {
238 myEvaluatorDiscrete[issueIndex].setEvaluationDouble(
239 (ValueDiscrete) valueList[listIndex], evaluationIn);
240 myEstUtilitySpace.addEvaluator((IssueDiscrete) myEstUtilitySpace
241 .getDomain().getIssues().get(issueIndex),
242 myEvaluatorDiscrete[issueIndex]);
243 } catch (Exception e) {
244 e.printStackTrace();
245 System.out
246 .println("catch throw triggered: in Class Opponent.setValueValue");
247 }
248 }
249
250 void setValueValueListAtIndex(int listIndex, double valueValueIn) {
251 valueValueList[listIndex] = valueValueIn;
252 }
253
254 void setWeightAtIndex(int issueIndex, double weightIn) {
255 myEvaluatorDiscrete[issueIndex].setWeight(weightIn);
256 myEstUtilitySpace.addEvaluator((IssueDiscrete) myEstUtilitySpace
257 .getDomain().getIssues().get(issueIndex),
258 myEvaluatorDiscrete[issueIndex]);
259 }
260
261 void StoreAcceptedBid(BidDetails acceptedBidDetails) {
262 myAcceptedBidHistory.add(acceptedBidDetails);
263 }
264
265 AdditiveUtilitySpace getEstimatedUtilitySpace() {
266 return myEstUtilitySpace;
267 }
268
269 IssueDiscrete getIssue(int issueIndex) {
270 return ((IssueDiscrete) myEstUtilitySpace.getDomain().getIssues()
271 .get(issueIndex));
272 }
273
274 boolean IssueUnChanged(int issueIndex) {
275 boolean issueConsecutive = false;
276 for (int listIndex = 0; listIndex < listSize; listIndex++) {
277 if ((issueIndex == issueIndexList[listIndex])
278 & (valueUnchangedList[listIndex])) {
279 issueConsecutive = true;
280 }
281 }
282 return issueConsecutive;
283 }
284
285 double getValueFreq(int listIndexIn) {
286 return freqList[listIndexIn];
287 }
288
289 double getIssueMaxFreq(int issueIndex) {
290 double maxIssueFreq = 0;
291 for (int listIndex = 0; listIndex < listSize; listIndex++) {
292 if (issueIndex == issueIndexList[listIndex]) {
293 if (maxIssueFreq <= freqList[listIndex]) {
294 maxIssueFreq = freqList[listIndex];
295 }
296 }
297 }
298 return maxIssueFreq;
299 }
300
301 int getIssueIndexFromListIndex(int listIndexIn) {
302 // returns the issue index given the list index
303 return issueIndexList[listIndexIn];
304 }
305
306 int getValueIndexFromListIndex(int listIndexIn) {
307 // returns the value index given the list index
308 return valueIndexList[listIndexIn];
309 }
310
311 ValueDiscrete getValueInList(int ListIndexIn) {
312 // returns the value given the list
313 return (ValueDiscrete) valueList[ListIndexIn];
314 }
315
316 double getValueValueInList(int ListIndexIn) {
317 // returns the value given the list
318 return valueValueList[ListIndexIn];
319 }
320
321 double getMaxValueValuePerIssue(int issueIndex) {
322 double maxIssueValueValue = 0;
323 for (int listIndex = 0; listIndex < listSize; listIndex++) {
324 if (issueIndex == issueIndexList[listIndex]) {
325 if (maxIssueValueValue <= valueValueList[listIndex]) {
326 maxIssueValueValue = valueValueList[listIndex];
327 }
328 }
329 }
330 return maxIssueValueValue;
331 }
332
333 int getListSize() {
334 return listSize;
335 }
336
337 EvaluatorDiscrete getEvaluator(int issueIndex) {
338 return myEvaluatorDiscrete[issueIndex];
339 }
340
341 int getHistorySize() {
342 return myMadeBidHistory.size();
343 }
344
345 void printTable() {
346 System.out.println(" ");
347 System.out
348 .println("-------------------------------------------------------");
349 System.out.println("Printing Opponent Data Table: opp nr : "
350 + myPartyNumber);
351 System.out
352 .println("-------------------------------------------------------");
353 System.out.println("LastBid: " + myMadeBidHistory.getLastBid());
354
355 System.out.println("----- begin table ---------");
356 for (int listIndex = 0; listIndex < listSize; listIndex++) {
357 System.out.println(valueList[listIndex] + "-- issue nr: "
358 + issueIndexList[listIndex] + " -- value nr: "
359 + valueIndexList[listIndex] + " -- freq: "
360 + freqList[listIndex] + " -- unchanged: "
361 + valueUnchangedList[listIndex]);
362 }
363 System.out.println("----- end table ---------");
364 System.out.println("----- Evaluator functions --------------");
365 for (int issueIndex = 0; issueIndex < numberOfIssues; issueIndex++) {
366 System.out.println("Evaluator function Nr " + issueIndex
367 + " -- weight: "
368 + myEvaluatorDiscrete[issueIndex].getWeight()
369 + " -- descr: " + myEvaluatorDiscrete[issueIndex]);
370 }
371 System.out.println("----- End evaluator functions ---------");
372 System.out
373 .println("-------------------------------------------------------");
374 System.out.println("End openent data listing");
375 System.out
376 .println("-------------------------------------------------------");
377 System.out.println(" ");
378 }
379
380 void printValueValueDifference() {
381 double[] realValueValueList = { 1, 0.66, 0.66, 0.33, 0.33, 1, 0.66,
382 0.33, 0.5, 0.25, 1, 0.25, 0.25, 0.25, 1, 0.5, 0.66, 1, 0.33,
383 0.66, 1, 0.33, 0.33 };
384 double[] realweight = { 0.19, 0.28, 0.19, 0.05, 0.19, 0.1 };
385
386 // error calculation
387 double errorValuesSquare = 0;
388 for (int listIndex = 0; listIndex < listSize; listIndex++) {
389 if (valueValueList[listIndex] > 0.01) {
390 errorValuesSquare = errorValuesSquare
391 + (valueValueList[listIndex] - realValueValueList[listIndex])
392 * (valueValueList[listIndex] - realValueValueList[listIndex]);
393 }
394 }
395 double errorIssuesSquare = 0;
396 for (int issueIndex = 0; issueIndex < numberOfIssues; issueIndex++) {
397 errorIssuesSquare = errorIssuesSquare
398 + (myEvaluatorDiscrete[issueIndex].getWeight() - realweight[issueIndex])
399 * (myEvaluatorDiscrete[issueIndex].getWeight() - realweight[issueIndex]);
400 }
401
402 System.out.println(" ");
403 System.out
404 .println("-------------------------------------------------------");
405 System.out.println("Printing Opponent ValueValues: opp nr : "
406 + myPartyNumber);
407 System.out
408 .println("-------------------------------------------------------");
409
410 System.out
411 .println("----- begin modeling results --------------------------");
412 for (int listIndex = 0; listIndex < listSize; listIndex++) {
413 System.out.println(valueList[listIndex] + " -- issue nr: "
414 + issueIndexList[listIndex] + " -- valueValue: "
415 + valueValueList[listIndex] + " -- realValue: "
416 + realValueValueList[listIndex]);
417 }
418 System.out
419 .println("-------------------------------------------------------");
420 System.out.println("values errorSquare : " + errorValuesSquare);
421 System.out
422 .println("-------------------------------------------------------");
423 System.out
424 .println("----- weights -----------------------------------------");
425 for (int issueIndex = 0; issueIndex < numberOfIssues; issueIndex++) {
426 System.out.println("Issue Nr " + issueIndex + " -- weight: "
427 + myEvaluatorDiscrete[issueIndex].getWeight()
428 + " -- realweight: " + realweight[issueIndex]);
429 }
430 System.out
431 .println("-------------------------------------------------------");
432 System.out.println("weights errorSquare : " + errorIssuesSquare);
433 System.out
434 .println("-------------------------------------------------------");
435 System.out.println("End openent modeling results");
436 System.out
437 .println("-------------------------------------------------------");
438 System.out.println(" ");
439 }
440
441 public Bid getBidfromHistory(int bidNumber) {
442 Bid bid = customBidHistory.get(bidNumber).getBid();
443 return bid;
444 }
445
446 public void ExportEstimateUtilSpace(String utilSpaceFileNameWithoutExtension) {
447 String fileName = utilSpaceFileNameWithoutExtension + ".txt";
448 String fileLocation = "exportedUtilsEstimates";
449 PrintWriter writer;
450 try {
451
452 writer = new PrintWriter(new FileWriter(fileLocation + "/"
453 + fileName));
454
455 writer.println(Integer.toString(numberOfIssues));
456 // weights
457 for (int issueIndex = 0; issueIndex < numberOfIssues; issueIndex++) {
458 writer.println(String.format("%1.4f",
459 myEvaluatorDiscrete[issueIndex].getWeight()));
460 }
461 // numberOfValuesPerIssue[]
462 for (int issueIndex = 0; issueIndex < numberOfIssues; issueIndex++) {
463 writer.println(Integer
464 .toString(numberOfValuesPerIssue[issueIndex]));
465 }
466 // valueValues
467 for (int listIndex = 0; listIndex < listSize; listIndex++) {
468 writer.println(String
469 .format("%1.4f", valueValueList[listIndex]));
470 }
471
472 writer.close();
473
474 } catch (IOException e) {
475 // TODO Auto-generated catch block
476 e.printStackTrace();
477 }
478
479 }
480
481 public void ExportProfile(AdditiveUtilitySpace utilitySpaceToExport,
482 String profileFileNameWithoutExtension) {
483
484 // This function saves the specified utilityspace under the given name.
485 // ACTUAL time and date and file
486 // extension (.xml) are added automatically. For example, this:
487 // ExportProfile(utilSpace, "someName");
488 // results in for example: "someName-20151020-163758.xml"
489
490 DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
491 Date date = new Date();
492 String dateStr = dateFormat.format(date);
493
494 String profileFolder = "exportedProfiles";
495
496 // String profileFileName = profileFolder + "/" +
497 // profileFileNameWithoutExtension + "-" + dateStr + ".xml";
498 String profileFileName = profileFolder + "/"
499 + profileFileNameWithoutExtension + ".xml";
500
501 try {
502 SimpleElement profileElement = utilitySpaceToExport.toXML();
503 profileElement.saveToFile(profileFileName);
504 } catch (IOException e) {
505 e.printStackTrace();
506 System.out.println("Erorr exporting profile");
507 }
508
509 }
510
511}
Note: See TracBrowser for help on using the repository browser.