source: src/main/java/agents/anac/y2019/solveragent/SolverAgent.java@ 346

Last change on this file since 346 was 200, checked in by Katsuhide Fujita, 5 years ago

Add ANAC 2019 agents

  • Property svn:executable set to *
File size: 35.7 KB
Line 
1package agents.anac.y2019.solveragent;
2
3import agents.org.apache.commons.math.stat.regression.OLSMultipleLinearRegression;
4import genius.core.Bid;
5import genius.core.actions.Accept;
6import genius.core.actions.Action;
7import genius.core.actions.Offer;
8import genius.core.boaframework.OutcomeSpace;
9import genius.core.issue.Issue;
10import genius.core.issue.IssueDiscrete;
11import genius.core.issue.Value;
12import genius.core.issue.ValueDiscrete;
13import genius.core.parties.AbstractNegotiationParty;
14import genius.core.parties.NegotiationInfo;
15import genius.core.uncertainty.AdditiveUtilitySpaceFactory;
16import genius.core.utility.AbstractUtilitySpace;
17
18import java.util.*;
19import java.util.Map.Entry;
20import java.util.stream.Collectors;
21
22
23public class SolverAgent extends AbstractNegotiationParty {
24 private List<List<IssueValuePair>> comparisonPerIssue;
25 private List<Map<Value, IssueValuePair>> pairMapList;
26 private Bid lastReceivedBid = null;
27 private double firstPhaseLow = 0;
28 private OutcomeSpace outcomespace;
29 private List<Bid> phase2Bids;
30 private boolean isConcession = false;
31 private boolean isFirst = false;
32 private boolean nashFound = false;
33 private Bid previousBid;
34 private List<Bid> previousBids;
35 private List<List<ValueDiscrete>> values;
36 private Map<Bid, Double> reversedBidMap;
37 private List<List<Double>> expectedWeightsPerValueOrdering;
38 private Bid maxBid;
39 private Map<Bid, Double> previousOpponentBidsMap;
40 private int issuesSize;
41 private Map<Double, Bid> nashBids;
42 private Bid nashBid;
43 private List<Bid> bidsAfterNash;
44 private Random rand;
45 private Map<Double, List<Bid>> utilBidMap;
46 private ArrayList<Double> utilBidMapKeys;
47 private int valuesCount;
48 private Set<Value> opponentValues;
49 private Bid ourMinBid;
50 private List<Bid> sortedBids;
51 private List<Bid> phase1Bids;
52 private int phase2Index = 0;
53 private int bidGivenCount = 0;
54 double phase1Bound;
55
56 private double[][] encodeListOfStrings(List<Bid> bidOrder, int countAll) {
57 double[][] oneHotEncoded = new double[bidOrder.size()][countAll];
58 int count = 0;
59 for (int i = 0; i < oneHotEncoded.length; i++) {
60 for (int j = 0; j < oneHotEncoded[0].length; j++) {
61 for (int k = 0; k < issuesSize; k++) {
62 for (int l = 0; l < values.get(k).size(); l++) {
63 if (bidOrder.get(i).getValue(k + 1).equals(values.get(k).get(l))) {
64 oneHotEncoded[i][count] = 1.0;
65 } else {
66 oneHotEncoded[i][count] = 0.0;
67 }
68 count++;
69 }
70 }
71 count = 0;
72 }
73 }
74 return oneHotEncoded;
75 }
76
77 private static <T> List<List<T>> powerSet(List<T> originalSet) {
78 List<List<T>> sets = new ArrayList<>();
79 if (originalSet.isEmpty()) {
80 sets.add(new ArrayList<>());
81 return sets;
82 }
83 List<T> list = new ArrayList<>(originalSet);
84 T head = list.get(0);
85 List<T> rest = new ArrayList<>(list.subList(1, list.size()));
86 for (List<T> set : powerSet(rest)) {
87 List<T> newSet = new ArrayList<>();
88 newSet.add(head);
89 newSet.addAll(set);
90 sets.add(newSet);
91 sets.add(set);
92 }
93 return sets;
94 }
95
96 private static <T> List<T> reverseList(List<T> list) {
97 return list.stream()
98 .collect(Collectors.collectingAndThen(
99 Collectors.toCollection(ArrayList::new), lst -> {
100 Collections.reverse(lst);
101 return lst.stream();
102 }
103 )).collect(Collectors.toCollection(ArrayList::new));
104 }
105
106 class ComparisonObject {
107 private List<IssueValuePair> items;
108 private List<Integer> issues;
109 int weight;
110
111
112 ComparisonObject(List<IssueValuePair> items, List<Integer> issues, int weight) {
113 this.issues = new ArrayList<>();
114 this.issues.addAll(issues);
115 this.items = new ArrayList<>(Collections.nCopies(pairMapList.size(), new IssueValuePair(-1, -1)));
116 int i = 0;
117 for (Integer iss : issues) {
118 this.items.set(iss, items.get(i++));
119 }
120 this.weight = weight;
121 }
122
123 IssueValuePair getValueAtIssueNo(int i) {
124 return items.get(i - 1);
125 }
126
127 void setWeight(int weight) {
128 this.weight = weight;
129 }
130
131 boolean isComparable(ComparisonObject other) {
132 for (IssueValuePair pair : other.getItems()) {
133 if (this.items.contains(pair))
134 return false;
135 }
136 return true;
137 }
138
139 int getValueIndex(int iss) {
140 return comparisonPerIssue.get(issues.get(iss)).indexOf(items.get(issues.get(iss)));
141 }
142
143 double getValueWeightOfIssue(int iss) {
144 return expectedWeightsPerValueOrdering.get(iss).get(comparisonPerIssue.get(iss).indexOf(items.get(iss)));
145 }
146
147 List<IssueValuePair> getItems() {
148 return items;
149 }
150
151 public List<Integer> getIssues() {
152 return issues;
153 }
154
155 int getIssueSize() {
156 return issues.size();
157 }
158
159 public String toString() {
160 StringBuilder string = new StringBuilder();
161 string.append("[").append(issues.get(0)).append(":").append(items.get(issues.get(0)));
162 for (int i = 1; i < issues.size(); i++) {
163 string.append(", ").append(issues.get(i)).append(":").append(items.get(issues.get(i)));
164 }
165 string.append("]");
166 return string.toString();
167 }
168
169 }
170
171 class IssueValuePair {
172 int first;
173 int second;
174
175 IssueValuePair(int first, int second) {
176 this.first = first;
177 this.second = second;
178 }
179
180 public String toString() {
181 if (first == -1) return "None";
182 return getKeysByValue(pairMapList.get(first), this).toString();
183 }
184
185 private Value getKeysByValue(Map<Value, IssueValuePair> map, IssueValuePair value) {
186 for (Value val : map.keySet()) {
187 if (map.get(val) == value) return val;
188 }
189 return new ValueDiscrete("0");
190 }
191
192 public boolean isNone() {
193 return first == -1 && second == -1;
194 }
195 }
196
197 @Override
198 public void init(NegotiationInfo info) {
199 super.init(info);
200 List<Bid> bidOrder = info.getUserModel().getBidRanking().getBidOrder();
201 previousBids = new ArrayList<>();
202 rand = new Random();
203 valuesCount = 0;
204
205 outcomespace = new OutcomeSpace(utilitySpace);
206
207 List<Issue> issues = info.getUserModel().getDomain().getIssues();
208 issuesSize = issues.size();
209 previousOpponentBidsMap = new HashMap<>();
210 nashBids = new HashMap<>();
211 bidsAfterNash = new ArrayList<>();
212 opponentValues = new HashSet<>();
213 isFirst = false;
214
215
216
217 values = new ArrayList<>();
218
219 for (Issue x : issues) {
220 values.add(((IssueDiscrete) x).getValues());
221 valuesCount += ((IssueDiscrete) x).getValues().size();
222 }
223
224 pairMapList = new ArrayList<>();
225
226 for (int i = 0; i < values.size(); i++) {
227 Map<Value, IssueValuePair> valuesMap = new HashMap<>();
228 for (int j = 0; j < values.get(i).size(); j++) {
229 valuesMap.put(values.get(i).get(j), new IssueValuePair(i, j));
230 }
231 pairMapList.add(valuesMap);
232 }
233
234
235 List<List<IssueValuePair>> bidsList = new ArrayList<>();
236 for (Bid bid : bidOrder) {
237 List<Value> ignored = new ArrayList<>(bid.getValues().values());
238 List<IssueValuePair> finalList = new ArrayList<>();
239 for (int i = 0; i < ignored.size(); i++) {
240 IssueValuePair finalListVal = pairMapList.get(i).get(ignored.get(i));
241 finalList.add(finalListVal);
242 }
243 bidsList.add(finalList);
244 }
245
246 int bidListSize = bidsList.size();
247
248 HashMap<List<IssueValuePair>, List<ComparisonObject>> valuesPerFixedValues = new LinkedHashMap<>();
249 comparisonPerIssue = new ArrayList<>(Collections.nCopies(issuesSize, new ArrayList<>()));
250
251 for (int i = 0; i < bidListSize; i++) {
252 List<IssueValuePair> lops = new ArrayList<>(bidsList.get(i));
253 List<List<IssueValuePair>> allSets = powerSet(new ArrayList<>(lops));
254 for (List<IssueValuePair> set : allSets) {
255 List<IssueValuePair> valueList = new ArrayList<>(lops);
256 List<IssueValuePair> key = new ArrayList<>(set);
257 valueList.removeAll(key);
258
259 List<Integer> valueIssueList = new ArrayList<>();
260
261 for (IssueValuePair index : valueList)
262 valueIssueList.add(index.first);
263
264 if (lops.size() == 0 || valueList.size() <= 0) continue;
265 ComparisonObject valObject = new ComparisonObject(valueList, valueIssueList, i);
266
267 if (valuesPerFixedValues.containsKey(key)) {
268 boolean allComparable = true;
269 for (ComparisonObject comp : valuesPerFixedValues.get(key))
270 if (!comp.isComparable(valObject))
271 allComparable = false;
272 if (allComparable)
273 valuesPerFixedValues.get(key).add(valObject);
274 } else {
275 List<ComparisonObject> objectList = new ArrayList<>();
276 objectList.add(valObject);
277 valuesPerFixedValues.put(key, objectList);
278 }
279 }
280 }
281
282 valuesPerFixedValues.entrySet().removeIf(e -> e.getValue().size() < 2);
283
284 for (Entry<List<IssueValuePair>, List<ComparisonObject>> entry : valuesPerFixedValues.entrySet()) {
285 int issueNo = entry.getValue().get(0).getIssues().get(0);
286 if (entry.getKey().size() == issuesSize - 1 && values.get(issueNo).size() == entry.getValue().size()) {
287 List<IssueValuePair> tempList = new ArrayList<>();
288 for (ComparisonObject comp : entry.getValue()) {
289 tempList.add(comp.getValueAtIssueNo(issueNo + 1));
290 comparisonPerIssue.set(issueNo, tempList);
291 }
292 }
293 }
294
295 int p = 0;
296 List<Integer> issuesBroken = new ArrayList<>();
297 for (List<IssueValuePair> entry : comparisonPerIssue) {
298 if (entry.isEmpty()) {
299 issuesBroken.add(p);
300 }
301 p++;
302 }
303
304
305 Map<Integer, Map<List<IssueValuePair>, List<ComparisonObject>>> orderings = new LinkedHashMap<>();
306 for (int i = issuesSize - 1; i > 0; i--) {
307 HashMap<List<IssueValuePair>, List<ComparisonObject>> valuesPerFixedValuesTmp = new LinkedHashMap<>();
308 for (Entry<List<IssueValuePair>, List<ComparisonObject>> x : valuesPerFixedValues.entrySet()) {
309 if (x.getKey().size() == i) {
310 if (valuesPerFixedValuesTmp.containsKey(x.getKey())) {
311 valuesPerFixedValuesTmp.get(x.getKey()).addAll(x.getValue());
312 } else {
313 valuesPerFixedValuesTmp.put(x.getKey(), x.getValue());
314 }
315 }
316 }
317
318 orderings.put(i, valuesPerFixedValuesTmp);
319 }
320
321 List<List<IssueValuePair>> allConflictComparisons = new ArrayList<>();
322
323 for (Entry<List<IssueValuePair>, List<ComparisonObject>> entry : orderings.get(issuesSize - 1).entrySet()) {
324 for (Integer x : entry.getValue().get(0).getIssues()) {
325 List<IssueValuePair> tempSet = new ArrayList<>();
326 for (ComparisonObject comp : entry.getValue()) {
327 tempSet.add(comp.getValueAtIssueNo(x + 1));
328 }
329 allConflictComparisons.addAll(new ArrayList<>(powerSet(tempSet)));
330 allConflictComparisons.removeIf(e -> e.size() != 2);
331 }
332 }
333
334 while (true) {
335 int prevSize = allConflictComparisons.size();
336 for (int i1 = issuesSize - 2; i1 > 0; i1--) {
337 for (Entry<List<IssueValuePair>, List<ComparisonObject>> entry : orderings.get(i1).entrySet()) {
338 int issueCount = entry.getValue().get(0).issues.size();
339
340 for (int i11 = 0; i11 < entry.getValue().size(); i11++) {
341 for (int i12 = i11 + 1; i12 < entry.getValue().size(); i12++) {
342 List<Integer> conflicts = new ArrayList<>();
343 ComparisonObject comp1 = entry.getValue().get(i11);
344 ComparisonObject comp2 = entry.getValue().get(i12);
345 for (int iss = 0; iss < issueCount; iss++) {
346 List<IssueValuePair> tmp = new ArrayList<>();
347 tmp.add(comp1.getValueAtIssueNo(comp1.issues.get(iss) + 1));
348 tmp.add(comp2.getValueAtIssueNo(comp1.issues.get(iss) + 1));
349 Collections.reverse(tmp);
350 if (allConflictComparisons.contains(tmp)) {
351 conflicts.add(comp1.issues.get(iss));
352 }
353 }
354 if (conflicts.size() == issuesSize - i1 - 1) {
355 List<Integer> finalIssues = new ArrayList<>(comp1.issues);
356 finalIssues.removeAll(conflicts);
357 List<IssueValuePair> finalList = new ArrayList<>();
358 finalList.add(comp1.getValueAtIssueNo(finalIssues.get(0) + 1));
359 finalList.add(comp2.getValueAtIssueNo(finalIssues.get(0) + 1));
360 if (allConflictComparisons.contains(finalList)) continue;
361 allConflictComparisons.add(finalList);
362 }
363 }
364 }
365 }
366 }
367
368 if (allConflictComparisons.size() == prevSize) {
369 break;
370 }
371 }
372
373 for (int i = issuesSize - 1; i > 0; i--) {
374 for (Integer iss : issuesBroken) {
375 List<IssueValuePair> orderingsComparison = new ArrayList<>();
376 for (List<ComparisonObject> entry : orderings.get(i).values()) {
377 for (ComparisonObject comp : entry) {
378 IssueValuePair pair = comp.getValueAtIssueNo(iss + 1);
379 if (pair.isNone()) continue;
380 if (orderingsComparison.isEmpty()) orderingsComparison.add(pair);
381 else if (orderingsComparison.contains(pair)) {
382 orderingsComparison.remove(pair);
383 orderingsComparison.add(pair);
384 } else orderingsComparison.add(pair);
385 }
386 }
387 if (comparisonPerIssue.isEmpty() || orderingsComparison.size() > comparisonPerIssue.get(iss).size())
388 comparisonPerIssue.set(iss, orderingsComparison);
389 }
390 }
391
392
393 for (List<IssueValuePair> comparison : allConflictComparisons) {
394 int iss = comparison.get(0).first;
395 List<IssueValuePair> issueList = comparisonPerIssue.get(iss);
396
397 int index1 = issueList.indexOf(comparison.get(0));
398 int index2 = issueList.indexOf(comparison.get(1));
399 if (index1 > index2) {
400 Collections.swap(issueList, index1, index2);
401 comparisonPerIssue.set(iss, issueList);
402 }
403 }
404
405 for (int i = 0; i < issuesSize; i++) {
406 List<IssueValuePair> tmp = new ArrayList<>();
407 for (ValueDiscrete x : values.get(i)) {
408 tmp.add(pairMapList.get(i).get(x));
409 }
410
411 if (!comparisonPerIssue.containsAll(tmp)) {
412 List<IssueValuePair> excludedVals = new ArrayList<>(tmp);
413 excludedVals.removeAll(comparisonPerIssue.get(i));
414
415 comparisonPerIssue.get(i).addAll(comparisonPerIssue.get(i).size() / 2, excludedVals);
416
417 for (int l = 0; l < excludedVals.size(); l++) {
418 for (int l1 = l + 1; l1 < excludedVals.size(); l1++) {
419 for (int j = 0; j < 2; j++) {
420 for (int k = bidsList.size() - 1; k > bidsList.size() - 3; k--) {
421 if ((bidsList.get(j).contains(excludedVals.get(l)) && bidsList.get(k).contains(excludedVals.get(l1)) &&
422 comparisonPerIssue.get(i).indexOf(excludedVals.get(l)) < comparisonPerIssue.get(i).indexOf(excludedVals.get(l1)))) {
423 Collections.swap(comparisonPerIssue.get(i), comparisonPerIssue.get(i).indexOf(excludedVals.get(l)),
424 comparisonPerIssue.get(i).indexOf(excludedVals.get(l1)));
425 }
426 }
427 }
428 }
429 }
430
431 }
432 }
433
434 expectedWeightsPerValueOrdering = new ArrayList<>();
435 for (int i = 0; i < issuesSize; i++) {
436 int valueAmount = values.get(i).size();
437 List<Double> expected = new ArrayList<>();
438 for (int j = 0; j < valueAmount; j++) {
439 expected.add(((double) j + 0.75) / valueAmount);
440 }
441 expectedWeightsPerValueOrdering.add(expected);
442 }
443
444
445 List<Issue> issueRanking = new ArrayList<>(issues);
446 List<List<Issue>> issueRankingList = new ArrayList<>();
447 System.out.println(orderings);
448
449 if (orderings.keySet().contains(issuesSize - 2)) {
450 for (Entry<List<IssueValuePair>, List<ComparisonObject>> entry : orderings.get(issuesSize - 2).entrySet()) {
451 for (int i = 0; i < entry.getValue().size(); i++) {
452 for (int j = i + 1; j < entry.getValue().size(); j++) {
453 ComparisonObject comp1 = entry.getValue().get(i);
454 ComparisonObject comp2 = entry.getValue().get(j);
455 if (comp1.isComparable(comp2)) {
456 int lowerIssue = 0;
457 int greaterIssue = 0;
458 List<Issue> rank = new ArrayList<>();
459 if (comp1.getValueIndex(1) < comp2.getValueIndex(1) &&
460 comp1.getValueIndex(0) < comp2.getValueIndex(0)
461 ) continue;
462 else if (comp1.getValueIndex(1) > comp2.getValueIndex(1)) {
463 rank.add(issues.get(comp1.issues.get(1)));
464 rank.add(issues.get(comp2.issues.get(0)));
465 greaterIssue = comp1.issues.get(0);
466 lowerIssue = comp1.issues.get(1);
467 } else if (comp1.getValueIndex(0) > comp2.getValueIndex(0)) {
468 rank.add(issues.get(comp1.issues.get(0)));
469 rank.add(issues.get(comp2.issues.get(1)));
470 greaterIssue = comp1.issues.get(1);
471 lowerIssue = comp1.issues.get(0);
472 }
473 if (rank.size() == 2) {
474 List<Issue> reversedRank = reverseList(rank);
475
476 if (issueRankingList.contains(reversedRank)) {
477 if ((Math.abs(comp1.getValueWeightOfIssue(greaterIssue) - comp2.getValueWeightOfIssue(greaterIssue)
478 / Math.abs(comp1.getValueWeightOfIssue(lowerIssue) - comp2.getValueWeightOfIssue(lowerIssue)))) < 1) {
479 issueRankingList.remove(reversedRank);
480 issueRankingList.add(rank);
481 }
482 } else if (!issueRankingList.contains(rank)) issueRankingList.add(rank);
483 }
484 }
485 }
486 }
487 }
488
489 for (List<Issue> comparison : issueRankingList) {
490 int index1 = issueRanking.indexOf(comparison.get(0));
491 int index2 = issueRanking.indexOf(comparison.get(1));
492
493 if (index1 > index2) {
494 Collections.swap(issueRanking, index1, index2);
495 }
496 }
497 }
498
499 List<Double> expectedIssueWeight = new ArrayList<>(Collections.nCopies(issuesSize, 0d));
500
501 int issueMid = issuesSize / 2;
502 if (issuesSize % 2 == 0) issueMid--;
503 double difference = 1 / (double) issuesSize;
504 expectedIssueWeight.set(issueMid, difference);
505 for (int i = 1; i <= issuesSize / 2; i++) {
506 if (issueMid + i < issuesSize) {
507 difference = (expectedIssueWeight.get(issueMid + i - 1) + expectedIssueWeight.get(issueMid + i - 1) / (double) issuesSize);
508 expectedIssueWeight.set(issueMid + i, difference);
509 }
510 if (issueMid - i >= 0) {
511 difference = (expectedIssueWeight.get(issueMid - i + 1) - expectedIssueWeight.get(issueMid - i + 1) / (double) issuesSize);
512 expectedIssueWeight.set(issueMid - i, difference);
513 }
514 }
515
516
517 double[] utilitiesPerBid = new double[bidOrder.size()];
518 double[][] encodedBids = encodeBids(bidOrder, valuesCount, values);
519
520 HashSet<Bid> allBidsSet = new HashSet<>(outcomespace.getAllBidsWithoutUtilities());
521 List<Bid> allBids = new ArrayList<>(allBidsSet);
522
523 double[][] allBidsEncoded = encodeListOfStrings(allBids, valuesCount);
524
525 double highestUtil = info.getUserModel().getBidRanking().getHighUtility();
526 double lowestUtil = info.getUserModel().getBidRanking().getLowUtility();
527
528 utilitiesPerBid[0] = lowestUtil;
529 utilitiesPerBid[utilitiesPerBid.length - 1] = highestUtil;
530
531 double delta = highestUtil - lowestUtil;
532 double decrementAmount = delta / (utilitiesPerBid.length - 1);
533
534 for (int i = 1; i < utilitiesPerBid.length - 1; i++) {
535 utilitiesPerBid[i] = utilitiesPerBid[i - 1] + decrementAmount;
536 }
537
538
539 Map<Bid, Double> allBidsRegressionReversed = new HashMap<>();
540
541 if (bidOrder.size() > 23) {
542 OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
543 regression.newSampleData(utilitiesPerBid, encodedBids);
544
545 double[] allBidPredictions = new double[allBids.size()];
546
547 for (int z = 0; z < allBids.size(); z++) {
548 allBidPredictions[z] = predict(regression, allBidsEncoded[z]);
549 }
550
551 double boostAmount = 0;
552 Arrays.sort(allBidPredictions);
553 if (allBidPredictions[0] < 0) {
554 boostAmount = Math.abs(allBidPredictions[0]);
555 }
556 double maxWeight = allBidPredictions[allBidPredictions.length-1] + boostAmount;
557
558 Map<Double, List<Bid>> allBidsRegression = new HashMap<>();
559 allBidsRegressionReversed = new HashMap<>();
560 int z = 0;
561 for (Bid x : allBids) {
562 List<Bid> tmp = new ArrayList<>();
563 tmp.add(x);
564 double val = (predict(regression, allBidsEncoded[z++]) + boostAmount) / maxWeight;
565 if (allBidsRegression.containsKey(val)) allBidsRegression.get(val).add(x);
566 else allBidsRegression.put(val, tmp);
567 allBidsRegressionReversed.put(x, val);
568 }
569 }
570
571
572 Set<Bid> bidSet = new HashSet<>(outcomespace.getAllBidsWithoutUtilities());
573 List<Bid> bidList5 = new ArrayList<>(bidSet);
574 utilBidMap = new HashMap<>();
575 reversedBidMap = new HashMap<>();
576
577 System.out.println("issues:" + issueRanking);
578 System.out.println("vals: " + comparisonPerIssue);
579
580 for (Bid x : bidList5) {
581 List<Value> ignored = new ArrayList<>(x.getValues().values());
582 List<IssueValuePair> bidList = new ArrayList<>();
583 for (int i = 0; i < ignored.size(); i++) {
584 IssueValuePair finalListVal = pairMapList.get(i).get(ignored.get(i));
585 bidList.add(finalListVal);
586 }
587 double result = 0;
588 for (IssueValuePair item : bidList) {
589 result += (expectedIssueWeight.get(issueRanking.indexOf(issues.get(item.first))))
590 * ((expectedWeightsPerValueOrdering.get(item.first).get(comparisonPerIssue.get(item.first).indexOf(item))));
591 }
592
593 if (bidOrder.size() > 23) {
594 result += allBidsRegressionReversed.get(x);
595 result = result / 2;
596 }
597
598 if (utilBidMap.containsKey(result))
599 utilBidMap.get(result).add(x);
600 else {
601 List<Bid> tmp = new ArrayList<>();
602 tmp.add(x);
603 utilBidMap.put(result, tmp);
604 }
605
606 reversedBidMap.put(x, result);
607 }
608
609 utilBidMapKeys = new ArrayList<>(utilBidMap.keySet());
610 Collections.sort(utilBidMapKeys);
611
612 List<Double> keyPrime = new ArrayList<>();
613
614 for (int i = utilBidMapKeys.size() - 1; i > 0; i--) {
615 if (utilBidMapKeys.get(i) < 0.9d) break;
616 keyPrime.add(utilBidMapKeys.get(i));
617 }
618
619 phase2Bids = new ArrayList<>();
620
621 int interval = keyPrime.size() / 20;
622
623 if (keyPrime.size() < 100) {
624 interval = 1;
625 isConcession = true;
626 }
627
628 for (int i = 0; i < keyPrime.size(); i += interval) {
629 phase2Bids.add(utilBidMap.get(keyPrime.get(i)).get(0));
630 }
631
632 Collections.sort(utilBidMapKeys);
633
634 sortedBids = new ArrayList<>();
635 for (Double x: utilBidMapKeys) {
636 sortedBids.addAll(utilBidMap.get(x));
637 }
638
639 double x1 = (double) bidOrder.size();
640 double x2 = (double) allBids.size();
641 double ratio = x1 / x2;
642 System.out.println("rat: " + ratio + " bidordersize: " + bidOrder.size() + " allbid size: " + allBids.size());
643 phase1Bound = 0.55 + (0.1d - ratio) * 3.64d;
644
645 if (phase1Bound < 0.75d)
646 phase1Bound = 0.75d;
647
648 System.out.println(phase1Bound);
649
650 phase1Bids = new ArrayList<>();
651 for (Double x: utilBidMapKeys) {
652 if (x > phase1Bound) {
653 phase1Bids.addAll(utilBidMap.get(x));
654 }
655 }
656
657 phase2Bids = new ArrayList<>();
658 for (Double x: utilBidMapKeys) {
659 if (x > phase1Bound) {
660 phase2Bids.addAll(utilBidMap.get(x));
661 }
662 }
663
664 phase2Index = phase1Bids.size() - 1;
665
666 System.out.println(sortedBids);
667
668 }
669
670 @Override
671 public Action chooseAction(List<Class<? extends Action>> possibleActions) {
672 if (getLastReceivedAction() instanceof Offer) {
673 lastReceivedBid = ((Offer) getLastReceivedAction()).getBid();
674 }
675
676 if (previousBids == null || !possibleActions.contains(Accept.class)) {
677 isFirst = true;
678 return new Offer(getPartyId(), utilBidMap.get(utilBidMapKeys.get(utilBidMapKeys.size() - 1)).get(0));
679 }
680
681 if (lastReceivedBid != null && timeline.getCurrentTime() <= (timeline.getTotalTime() - 1) * 0.95) {
682 if (previousOpponentBidsMap.containsKey(lastReceivedBid))
683 previousOpponentBidsMap.put(lastReceivedBid, previousOpponentBidsMap.get(lastReceivedBid) + 1);
684 else previousOpponentBidsMap.put(lastReceivedBid, 1d);
685
686 opponentValues.addAll(lastReceivedBid.getValues().values());
687 }
688 if (timeline.getCurrentTime() == (timeline.getTotalTime() - 1) * 0.98) {
689 double maxOpponentWeight = Collections.max(previousOpponentBidsMap.entrySet(),
690 Comparator.comparing(Entry::getValue)).getValue();
691 for (Bid x : previousOpponentBidsMap.keySet()) {
692 previousOpponentBidsMap.replace(x, previousOpponentBidsMap.get(x) / maxOpponentWeight);
693 }
694
695 if (previousOpponentBidsMap.size() >= valuesCount && opponentValues.size() >= valuesCount) {
696 double[] utilitiesPerBid = new double[previousOpponentBidsMap.values().size()];
697 int i = 0;
698 for (double v : previousOpponentBidsMap.values()) {
699 utilitiesPerBid[i++] = v;
700 }
701
702 Set<Bid> bidSet = new HashSet<>(outcomespace.getAllBidsWithoutUtilities());
703 List<Bid> allBidList = new ArrayList<>(bidSet);
704
705 double[][] encodedBids = encodeBids(new ArrayList<>(previousOpponentBidsMap.keySet()), valuesCount, values);
706 double[][] oneHotEncodedAll = encodeListOfStrings(allBidList, valuesCount);
707
708 OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
709 regression.newSampleData(utilitiesPerBid, encodedBids);
710
711 Map<Bid, Double> allOpponentBids = new HashMap<>();
712
713 int j = 0;
714 for (Bid x : allBidList) {
715 allOpponentBids.put(x, predict(regression, oneHotEncodedAll[j++]));
716 }
717
718
719 for (Entry<Bid, Double> entry : allOpponentBids.entrySet()) {
720 double nashVal = entry.getValue() * reversedBidMap.get(entry.getKey());
721 nashBids.put(nashVal, entry.getKey());
722 }
723
724 nashBid = nashBids.get(Collections.max(nashBids.keySet()));
725 if (nashBid != null) nashFound = true;
726 for (Entry<Double, Bid> entry : nashBids.entrySet()) {
727 if (reversedBidMap.get(entry.getValue()) > 0.90d) {
728 bidsAfterNash.add(entry.getValue());
729 }
730 }
731 }
732
733 else {
734 for (Entry<Bid, Double> entry : previousOpponentBidsMap.entrySet()) {
735 double nashVal = entry.getValue() * reversedBidMap.get(entry.getKey());
736 nashBids.put(nashVal, entry.getKey());
737 }
738
739 nashBid = nashBids.get(Collections.max(nashBids.keySet()));
740 for (Entry<Double, Bid> entry : nashBids.entrySet()) {
741 if (reversedBidMap.get(entry.getValue()) > 0.70d) {
742 bidsAfterNash.add(entry.getValue());
743 }
744 }
745 }
746 }
747
748 previousBid = getBid();
749
750 if (ourMinBid == null || reversedBidMap.get(previousBid) < reversedBidMap.get(ourMinBid))
751 ourMinBid = previousBid;
752
753 if (reversedBidMap.get(previousBid) <= reversedBidMap.get(lastReceivedBid))
754 return new Accept(getPartyId(), lastReceivedBid);
755
756 if (reversedBidMap.get(lastReceivedBid) >= phase1Bound)
757 return new Accept(getPartyId(), lastReceivedBid);
758
759 if (isFirst && timeline.getCurrentTime() >= timeline.getTotalTime() - 2) {
760 if (reversedBidMap.get(maxBid) >= phase1Bound && !nashFound && reversedBidMap.get(maxBid) > reversedBidMap.get(nashBid))
761 return new Offer(getPartyId(), maxBid);
762 else if (nashFound && reversedBidMap.get(nashBid) >= 0.70d)
763 return new Offer(getPartyId(), nashBid);
764 else return new Offer(getPartyId(), getBid());
765 }
766
767
768 double acceptableUtil = 0.80;
769
770
771 if (maxBid == null || reversedBidMap.get(lastReceivedBid) > reversedBidMap.get(maxBid))
772 maxBid = lastReceivedBid;
773
774
775 if (reversedBidMap.get(maxBid) >= 0.80 && !isFirst && timeline.getCurrentTime() == timeline.getTotalTime() - 2) {
776 if (!nashFound && reversedBidMap.get(nashBid) < 0.70d)
777 return new Offer(getPartyId(), maxBid);
778 else return new Offer(getPartyId(), nashBid);
779 }
780 if (!isFirst && timeline.getCurrentTime() == timeline.getTotalTime() - 1 && acceptableUtil <= reversedBidMap.get(lastReceivedBid)) {
781 return new Accept(getPartyId(), lastReceivedBid);
782 } else {
783 return new Offer(getPartyId(), previousBid);
784 }
785 }
786
787 private Bid getBid() {
788 double time = timeline.getCurrentTime();
789 double firstPhase = timeline.getTotalTime() * 0.3;
790 double secondPhase = timeline.getTotalTime() - 3;
791
792 if (time <= firstPhase) {
793 int bidIndex = rand.nextInt(phase1Bids.size());
794 return phase1Bids.get(bidIndex);
795
796 } else if (time > firstPhase && secondPhase >= time) {
797 bidGivenCount++;
798 phase2Index++;
799
800 if (phase2Bids.size() == phase2Index) {
801 phase2Index = 0;
802 }
803
804 return phase2Bids.get(phase2Index);
805
806 } else if (time >= secondPhase) {
807 return phase1Bids.get(0);
808 }
809
810 return sortedBids.get(sortedBids.size()-1);
811 }
812
813 /**
814 * With this method, you can override the default estimate of the utility
815 * space given uncertain preferences specified by the user model. This
816 * example sets every value to zero.
817 */
818 @Override
819 public AbstractUtilitySpace estimateUtilitySpace() {
820 return new AdditiveUtilitySpaceFactory(getDomain()).getUtilitySpace();
821 }
822
823 @Override
824 public String getDescription() {
825 return "An agent that solves uncertainty with a special solver";
826 }
827
828 private double[][] encodeBids(List<Bid> bidOrder, int valueCount, List<List<ValueDiscrete>> allIssues) {
829 double[][] oneHotEncoded = new double[bidOrder.size()][valueCount];
830 int count = 0;
831 for (int i = 0; i < oneHotEncoded.length; i++) {
832 for (int j = 0; j < oneHotEncoded[0].length; j++) {
833 for (int k = 0; k < bidOrder.get(i).getValues().values().size(); k++) {
834 for (int l = 0; l < allIssues.get(k).size(); l++) {
835 if (bidOrder.get(i).getValues().values().toArray()[k].toString().equals(allIssues.get(k).get(l).toString())) {
836 oneHotEncoded[i][count] = 1.0;
837 } else {
838 oneHotEncoded[i][count] = 0.0;
839 }
840 count++;
841 }
842 }
843 count = 0;
844 }
845 }
846 return oneHotEncoded;
847 }
848
849 private double predict(OLSMultipleLinearRegression regression, double[] x) {
850 if (regression == null) {
851 throw new IllegalArgumentException("regression must not be null.");
852 }
853 double[] beta = regression.estimateRegressionParameters();
854
855 double prediction = beta[0];
856 for (int i = 1; i < beta.length; i++) {
857 prediction += beta[i] * x[i - 1];
858 }
859
860 return prediction;
861 }
862}
863
Note: See TracBrowser for help on using the repository browser.