source: src/main/java/agents/anac/y2016/pars2/ParsAgent2.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: 35.8 KB
Line 
1package agents.anac.y2016.pars2;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6import java.util.Map;
7import java.util.Set;
8
9import genius.core.AgentID;
10import genius.core.Bid;
11import genius.core.DeadlineType;
12import genius.core.actions.Accept;
13import genius.core.actions.Action;
14import genius.core.actions.Offer;
15import genius.core.issue.Issue;
16import genius.core.issue.IssueDiscrete;
17import genius.core.timeline.Timeline;
18import genius.core.utility.AdditiveUtilitySpace;
19import genius.core.utility.UtilitySpace;
20import negotiator.parties.AbstractTimeDependentNegotiationParty;
21
22/**
23 * This is ParsAgent party.
24 */
25public class ParsAgent2 extends AbstractTimeDependentNegotiationParty {
26 Bid lastBid;
27 Bid myLastBid;
28 Bid myBestBid;
29 String lastAction;
30 String oppAName;
31 String oppBName;
32 boolean isLastRound;
33 double myutility;
34 double ConstantUtility = 0.8d;
35 boolean Imfirst = false;
36 long prevTime = 0;
37 Boolean withDiscount = null;
38 boolean fornullAgent = false;
39 long beginTime;
40 int round;
41
42 ArrayList<BidUtility> opponentAB = new ArrayList<BidUtility>();
43 OpponentPreferences oppAPreferences = new OpponentPreferences();
44 OpponentPreferences oppBPreferences = new OpponentPreferences();
45 ArrayList<Bid> oppABids = new ArrayList<Bid>();
46 ArrayList<Bid> oppBBids = new ArrayList<Bid>();
47 ArrayList<Bid> rejectedBid = new ArrayList<Bid>();
48 ArrayList<Cluster> clusterA;
49 ArrayList<Cluster> clusterB;
50 ClusterHistory clusterHistoryA;
51 ClusterHistory clusterHistoryB;
52 int numberOfclusters = 3;
53 // long timePeriod=30;
54 int slotNum = 100;
55 double myConcessionRate = 0.15d;
56 int currentSlotA;
57 int currentSlotB;
58 boolean clusterIsRefreshA;
59 boolean clusterIsRefreshB;
60 float concessionA;
61 float concessionB;
62
63 public ParsAgent2() {
64 myutility = 0.80000000000000004D;
65 Imfirst = false;
66 withDiscount = null;
67 fornullAgent = false;
68 opponentAB = new ArrayList();
69 oppAPreferences = new OpponentPreferences();
70 oppBPreferences = new OpponentPreferences();
71 rejectedBid = new ArrayList<Bid>();
72 beginTime = System.currentTimeMillis();
73 prevTime = beginTime;
74 lastAction = "offer";
75
76 }
77
78 public static void main(String[] arg) {
79 ParsAgent2 p = new ParsAgent2();
80 }
81
82 public void clusterOpponentBid(ArrayList<Bid> bidHistory, String opParty,
83 boolean repeat) {
84
85 int k = numberOfclusters;
86 ArrayList<Cluster> clusters;
87 if (opParty.equals("A"))
88 clusters = clusterA;
89 else
90 clusters = clusterB;
91
92 if (bidHistory.size() > k) {
93 if (!repeat) {
94 if (clusters == null) {
95 clusters = new ArrayList<Cluster>(k);
96 for (int i = 0; i < k; ++i) {
97 clusters.add(new Cluster());
98 }
99 }
100 if (opParty.equals("A"))
101 clusterA = clusters;
102 else
103 clusterB = clusters;
104
105 int next = bidHistory.size() / k;
106 int temp = 0;
107 for (int i = 0; i < k; ++i) {
108 clusters.get(i).setCenter(bidHistory.get(temp));
109 temp += next;
110 }
111 // }
112 for (int i = 0; i < clusters.size(); ++i) {
113 // ArrayList<Bid> removable = clusters.get(i).getMembers();
114 if (clusters.get(i).getMembers() != null)
115 // clusters.get(i).getMembers().removeAll(removable);
116 clusters.get(i).getMembers().clear();
117 }
118 }
119 for (int i = 0; i < bidHistory.size(); ++i) {
120 defineCluster(clusters, bidHistory.get(i));
121 }
122
123 if (refreshClusterCenters(clusters)) {
124
125 for (int i = 0; i < clusters.size(); ++i) {
126 // ArrayList<Bid> removable = clusters.get(i).getMembers();
127 // clusters.get(i).getMembers().removeAll(removable);
128 clusters.get(i).getMembers().clear();
129
130 }
131 clusterOpponentBid(bidHistory, opParty, true);
132 }
133 }
134 }
135
136 public boolean refreshClusterCenters(ArrayList<Cluster> clusters) {
137 // ArrayList<Issue> dissues = utilitySpace.getDomain().getIssues();
138 boolean needRefresh = false;
139 for (int i = 0; i < clusters.size(); ++i) {
140 Cluster clus = clusters.get(i);
141 clus.getMembers().add(0, clus.getCenter());
142 float[] dist = new float[clus.getMembers().size()];
143 float[][] dismatrix = new float[clus.getMembers().size()][clus
144 .getMembers().size()];
145 for (int j = 0; j < clus.getMembers().size(); ++j) {
146 Bid checkBid = clus.getMembers().get(j);
147 float distance = 0;
148 for (int k = 0; k < j; ++k) {
149 distance += dismatrix[k][j];
150 }
151 for (int k = j + 1; k < clus.getMembers().size(); ++k) {
152 // if (k != j) {
153 float temp = calDist(checkBid, clus.getMembers().get(k));
154 distance += temp;
155 dismatrix[j][k] = temp;
156
157 }
158 dist[j] = distance / clus.getMembers().size();
159 distance = 0f;
160 }
161 float min = dist[0];
162 String out = dist[0] + "";
163
164 int index = 0;
165 for (int n = 1; n < dist.length; ++n) {
166 // out += " " + dist[n];
167
168 if (dist[n] < min) {
169 min = dist[n];
170 index = n;
171 }
172 }
173
174 if (index != 0) {
175 needRefresh = true;
176 }
177
178 clus.setCenter(clus.getMembers().get(index));
179 clus.getMembers().remove(index);
180
181 }
182 return needRefresh;
183 }
184
185 public void defineCluster(ArrayList<Cluster> clusters, Bid bid) {
186 float[] dises = new float[clusters.size()];
187 for (int i = 0; i < clusters.size(); ++i) {
188 dises[i] = calDist(bid, clusters.get(i).getCenter());
189 if (clusters.get(i).members == null)
190 clusters.get(i).members = new ArrayList<Bid>();
191 }
192 float min = dises[0];
193 int index = 0;
194 for (int j = 0; j < dises.length; ++j) {
195 if (dises[j] < min) {
196 min = dises[j];
197 index = j;
198 }
199 }
200
201 clusters.get(index).members.add(bid);
202
203 }
204
205 class ClusterHistory {
206 ArrayList<ArrayList<Bid>> prevCenters;
207 ArrayList<Float> maxDistance;
208 ArrayList<Float> distributionFactor;
209 }
210
211 class Cluster {
212 Bid center;
213 ArrayList<Bid> members;
214
215 public void setCenter(Bid center) {
216 this.center = center;
217 }
218
219 public Bid getCenter() {
220 return center;
221 }
222
223 public void setMembers(ArrayList<Bid> members) {
224 this.members = members;
225 }
226
227 public ArrayList<Bid> getMembers() {
228 return members;
229 }
230 }
231
232 /**
233 * Please keep this constructor. This is called by genius.
234 *
235 * @param utilitySpace
236 * Your utility space.
237 * @param deadlines
238 * The deadlines set for this negotiation.
239 * @param timeline
240 * Value counting from 0 (start) to 1 (end).
241 * @param randomSeed
242 * If you use any randomization, use this seed for it.
243 */
244 public ParsAgent2(UtilitySpace utilitySpace,
245 Map<DeadlineType, Object> deadlines, Timeline timeline,
246 long randomSeed) {
247 // Make sure that this constructor calls it's parent.
248 super();
249 /*
250 * Comment for Bug
251 *
252 * Object[] keys = deadlines.keySet().toArray(); for (int i = 0; i <
253 * keys.length; ++i) { if (keys[i] instanceof DeadlineType) {
254 * DeadlineType type=(DeadlineType) keys[i]; round =
255 * Integer.parseInt(deadlines.get(keys[i]).toString()); } }
256 */
257 }
258
259 /**
260 * Each round this method gets called and ask you to accept or offer. The
261 * first party in the first round is a bit different, it can only propose an
262 * offer.
263 *
264 * @param validActions
265 * Either a list containing both accept and offer or only offer.
266 * @return The chosen action.
267 */
268 @Override
269 public Action chooseAction(List validActions) {
270 ++round;
271 lastAction = "offer";
272 try {
273 if (withDiscount == null)
274 withDiscount = utilitySpace.isDiscounted();
275 if (rejectedBid.size() > 10)
276 rejectedBid.remove(0);
277 if (myBestBid == null)
278 myBestBid = utilitySpace.getMaxUtilityBid();
279 long roundLong = System.currentTimeMillis() - prevTime;
280 prevTime = System.currentTimeMillis();
281 roundLong = roundLong == 0 ? 1 : roundLong;
282 long remainTime = (3 * 60 * 1000)
283 - (System.currentTimeMillis() - beginTime);
284
285 isLastRound = Math.round(remainTime / roundLong) <= 10;
286 if (isLastRound)
287 ConstantUtility = ((0.7d + (remainTime / roundLong) / 10));
288 if (currentSlotA > slotNum) {
289
290 currentSlotA = 0;
291 clusterOpponentBid(oppABids, "A", false);
292 clusterIsRefreshA = true;
293 }
294 if (currentSlotB > slotNum) {
295 currentSlotB = 0;
296 clusterOpponentBid(oppBBids, "B", false);
297 clusterIsRefreshB = true;
298 }
299
300 if (lastBid == null) {
301 Imfirst = true;
302 Bid b = getMybestBid(myBestBid, 0);
303 myLastBid = b;
304 lastBid = myLastBid;
305 return new Offer(getPartyId(), b);
306 } else if (utilitySpace.getUtility(lastBid) >= getMyutility()) {
307 lastAction = "Accept";
308 return new Accept(getPartyId(), lastBid);
309 } else {
310 Bid b = offerMyNewBid();
311 if (b == null || utilitySpace.getUtility(b) < getMyutility()) {
312 b = getMybestBid(myBestBid, 0);
313 }
314
315 if (isLastRound && prob(b) < 0.5) {
316 b = bestMutualCenters();
317
318 if (utilitySpace.getUtility(b) >= getMyutility()) {
319 myLastBid = b;
320 lastBid = myLastBid;
321 return new Offer(getPartyId(), b);
322 }
323 } else {
324 myLastBid = b;
325 lastBid = myLastBid;
326 return new Offer(getPartyId(), b);
327 }
328 }
329
330 } catch (Exception e) {
331 e.printStackTrace();
332 }
333 Bid mb = null;
334 try {
335 mb = getMybestBid(myBestBid, 0);
336 myLastBid = mb;
337 lastBid = myLastBid;
338 return new Offer(getPartyId(), mb);
339 } catch (Exception e) {
340 lastAction = "Accept";
341 return new Accept(getPartyId(), lastBid);
342 }
343
344 }
345
346 public Bid bestMutualCenters() {
347 float min = 0f;
348 int centerindex = 0;
349 int centerindex2 = 0;
350 for (int i = 0; i < clusterA.size(); ++i) {
351 for (int j = 0; j < clusterB.size(); ++j) {
352 float temp = calDist(clusterA.get(i).getCenter(),
353 clusterB.get(j).getCenter());
354 if (temp < min) {
355 min = temp;
356 centerindex = i;
357 centerindex2 = j;
358 }
359 }
360 }
361 if (utilitySpace.getUtility(
362 clusterA.get(centerindex).getCenter()) > utilitySpace
363 .getUtility(clusterB.get(centerindex2).getCenter()))
364 return clusterA.get(centerindex).getCenter();
365 return clusterB.get(centerindex2).getCenter();
366
367 }
368
369 public float prob(Bid mybid) {
370 float prob = 0f;
371 float sum = 0f;
372 int issueNum = utilitySpace.getDomain().getIssues().size();
373 if (oppABids.size() != 0) {
374 for (int i = 0; i < oppABids.size(); ++i) {
375 sum += calDist(mybid, oppABids.get(i));
376 }
377 float probA = (sum / oppABids.size()) / issueNum;
378 sum = 0f;
379 for (int i = 0; i < oppBBids.size(); ++i) {
380 sum += calDist(mybid, oppBBids.get(i));
381 }
382 float probB = (sum / oppBBids.size()) / issueNum;
383 prob = (probA + probB) / 2;
384 }
385 return prob;
386 }
387
388 /**
389 * All offers proposed by the other parties will be received as a message.
390 * You can use this information to your advantage, for example to predict
391 * their utility.
392 *
393 * @param sender
394 * The party that did the action.
395 * @param action
396 * The action that party did.
397 */
398 @Override
399 public void receiveMessage(AgentID sender, Action action) {
400 super.receiveMessage(sender, action);
401 String agentName = sender != null ? sender.toString() : "null";
402 fornullAgent = !fornullAgent;
403
404 if (action != null && action instanceof Offer) {
405 if (!lastAction.equals("Accept"))
406 rejectedBid.add(rejectedBid.size(), myLastBid);
407 Bid newBid = ((Offer) action).getBid();
408 withDiscount = utilitySpace.isDiscounted();
409
410 BidUtility opBid;
411 try {
412 opBid = new BidUtility(newBid, utilitySpace.getUtility(newBid),
413 System.currentTimeMillis());
414 if (oppAName != null && oppAName.equals(agentName)) {
415 // addBidToList(oppAPreferences.getOpponentBids(), opBid);
416 addBidToList(oppABids, newBid);
417 currentSlotA++;
418
419 } else if (oppBName != null && oppBName.equals(agentName)) {
420 // addBidToList(oppBPreferences.getOpponentBids(), opBid);
421 addBidToList(oppBBids, newBid);
422 currentSlotB++;
423
424 } else if (oppAName == null) {
425 oppAName = agentName;
426 // oppAPreferences.getOpponentBids().add(opBid);
427 } else if (oppBName == null) {
428 oppBName = agentName;
429 // oppBPreferences.getOpponentBids().add(opBid);
430 }
431 calculateParamForOpponent((oppAName.equals(agentName)
432 ? oppAPreferences : oppBPreferences), newBid);
433
434 lastBid = newBid;
435 } catch (Exception e) {
436 // System.out.println("Exception 33 " + e.getMessage());
437 }
438 } else if (action != null && action instanceof Accept) {
439 BidUtility opBid = null;
440 if (!lastBid.equals(myLastBid)) {
441 try {
442
443 opBid = new BidUtility(lastBid,
444 utilitySpace.getUtility(lastBid),
445 System.currentTimeMillis());
446 addBidToList(opponentAB, opBid);
447
448 } catch (Exception e) {
449 // System.out.println("Exception 44" + e.getMessage());
450 }
451 }
452 }
453
454 // Here you can listen to other parties' messages
455 }
456
457 /*
458 * public int MyBestValue(int issueindex) { ArrayList<Issue> dissues =
459 * utilitySpace.getDomain().getIssues(); Issue isu =
460 * dissues.get(issueindex); HashMap map = new HashMap(); double maxutil =
461 * 0d; int maxvalIndex = 0; try { map = myBestBid.getValues(); } catch
462 * (Exception e) { System.out.println("Exception 3323 " + e.getMessage());
463 * } if (isu instanceof IssueDiscrete) { IssueDiscrete is =
464 * (IssueDiscrete)isu; for (int num = 0; num < is.getNumberOfValues();
465 * ++num) { map.put(new Integer(issueindex + 1), is.getValue(num)); Bid
466 * temp; double u = 0d; try { temp = new Bid(utilitySpace.getDomain(), map);
467 * u = utilitySpace.getUtility(temp); } catch (Exception e) {
468 * System.out.println("Exception 98989 " + e.getMessage()); } if (u >
469 * maxutil) { maxutil = u; maxvalIndex = num; } break;
470 *
471 *
472 * } } return maxvalIndex; }
473 */
474
475 public Bid offerMyNewBid() {
476 Bid bidNN = null;
477 boolean loop = false;
478 try {
479 if (opponentAB != null && opponentAB.size() != 0) {
480 bidNN = getNNBid(opponentAB);
481
482 }
483
484 if (bidNN == null
485 || utilitySpace.getUtility(bidNN) < getMyutility()) {
486
487 ArrayList isues = getMutualIssues();
488 HashMap map = new HashMap();
489 Bid bid;
490 List<Issue> dissues = utilitySpace.getDomain().getIssues();
491 boolean hasnotNull = false;
492 for (int i = 0; i < isues.size(); ++i) {
493 if (isues.get(i) != null)
494 hasnotNull = true;
495 }
496 if (hasnotNull) {
497 for (int i = 0; i < isues.size(); ++i) {
498 ArrayList keyVal = (ArrayList) isues.get(i);
499 if (keyVal != null
500 && dissues.get(i) instanceof IssueDiscrete) {
501 IssueDiscrete is = (IssueDiscrete) dissues.get(i);
502 for (int num = 0; num < is
503 .getNumberOfValues(); ++num) {
504 if (is.getValue(num).toString()
505 .equals(keyVal.get(0).toString())) {
506 map.put(new Integer(i + 1),
507 is.getValue(num));
508 break;
509 }
510
511 }
512
513 } else if (keyVal == null
514 && dissues.get(i) instanceof IssueDiscrete) {
515 IssueDiscrete is = (IssueDiscrete) dissues.get(i);
516 if (!loop)
517 map.put(new Integer(i + 1), myBestBid
518 .getValues().get(new Integer(i + 1)));
519 else
520 map.put(new Integer(i + 1),
521 getRandomValue(dissues.get(i)));
522
523 } else if (keyVal != null) {
524 map.put(new Integer(i + 1), keyVal.get(0));
525 }
526
527 }
528
529 try {
530 bid = new Bid(utilitySpace.getDomain(),
531 (HashMap) map.clone());
532
533 if (utilitySpace.getUtility(bid) > getMyutility()) {
534 if (rejectedBid.contains(bid) && !loop) {
535 loop = true;
536 } else
537 return bid;
538 } else {
539 return getMybestBid(myBestBid, 0);
540 }
541 } catch (Exception e) {
542 // System.out.println("Exception 55 " + e.getMessage());
543 }
544 }
545 } else
546 return bidNN;
547 } catch (Exception e) {
548 // System.out.println("Exception 121212 == " + e.getMessage());
549 e.printStackTrace();
550 }
551 return getMybestBid(myBestBid, 0);
552 }
553
554 public ArrayList getMutualIssues() {
555 ArrayList mutualList = new ArrayList();
556 List<Issue> dissues = utilitySpace.getDomain().getIssues();
557 int twocycle = 2;
558 while (twocycle > 0) {
559 mutualList = new ArrayList();
560 for (int i = 0; i < dissues.size(); ++i) {
561 if (oppAPreferences.getRepeatedissue()
562 .get(dissues.get(i).getName()) != null) {
563 HashMap vals = (HashMap) oppAPreferences.getRepeatedissue()
564 .get(dissues.get(i).getName());
565 HashMap valsB = (HashMap) oppBPreferences.getRepeatedissue()
566 .get(dissues.get(i).getName());
567 Object[] keys = vals.keySet().toArray();
568 int[] max = new int[] { 0, 0 };
569 Object[] maxkey = new Object[] { null, null };
570 for (int j = 0; j < keys.length; ++j) {
571 Integer temp = (Integer) vals.get(keys[j]);
572 if (temp.intValue() > max[0]) {
573 max[0] = temp.intValue();
574 maxkey[0] = keys[j];
575 } else if (temp.intValue() > max[1]) {
576 max[1] = temp.intValue();
577 maxkey[1] = keys[j];
578 }
579 }
580 if (valsB != null) {
581 Object[] keysB = valsB.keySet().toArray();
582 int[] maxB = new int[] { 0, 0 };
583 ;
584 Object[] maxkeyB = new Object[] { null, null };
585 for (int j = 0; j < keysB.length; ++j) {
586 Integer temp = (Integer) valsB.get(keysB[j]);
587 if (temp.intValue() > maxB[0]) {
588 maxB[0] = temp.intValue();
589 maxkeyB[0] = keysB[j];
590 } else if (temp.intValue() > maxB[1]) {
591 maxB[1] = temp.intValue();
592 maxkeyB[1] = keysB[j];
593 }
594 }
595
596 if (twocycle == 2) {
597 if (maxkey[0] != null && maxkeyB[0] != null
598 && maxkey[0].equals(maxkeyB[0])) {
599 ArrayList l = new ArrayList();
600 l.add(maxkey[0]);
601 l.add(maxB[0]);
602 l.add(max[0]);
603 mutualList.add(i, l);
604 } else
605 mutualList.add(i, null);
606 } else {
607 boolean insideloop = true;
608 for (int m = 0; insideloop && m < 2; ++m) {
609 for (int z = 0; insideloop && z < 2; ++z) {
610 if (maxkey[m] != null && maxkeyB[z] != null
611 && maxkey[m].equals(maxkeyB[z])) {
612 ArrayList l = new ArrayList();
613 l.add(maxkey[m]);
614 l.add(maxB[z]);
615 l.add(max[m]);
616 mutualList.add(i, l);
617 insideloop = false;
618 }
619 }
620 }
621 if (insideloop)
622 mutualList.add(i, null);
623
624 }
625 } else {
626 mutualList.add(i, null);
627 oppBPreferences.getRepeatedissue()
628 .put(dissues.get(i).getName(), new HashMap());
629 }
630 } else {
631 oppAPreferences.getRepeatedissue()
632 .put(dissues.get(i).getName(), new HashMap());
633 mutualList.add(i, null);
634 }
635 if (((HashMap) oppAPreferences.getRepeatedissue()
636 .get(dissues.get(i).getName())).size() == 0
637 || ((HashMap) oppAPreferences.getRepeatedissue()
638 .get(dissues.get(i).getName())).size() == 0) {
639 twocycle--;
640 }
641 }
642 if (twocycle != 0) {
643 twocycle--;
644 float nullval = 0.0f;
645 for (int i = 0; i < mutualList.size(); ++i) {
646 if (mutualList.get(i) != null) {
647 ++nullval;
648 }
649 }
650 nullval = nullval / mutualList.size();
651 if (nullval >= 0.5)
652 twocycle--;
653
654 }
655 }
656 return mutualList;
657 }
658
659 public Bid getNNBid(ArrayList<BidUtility> oppAB) {
660 List<Issue> dissues = utilitySpace.getDomain().getIssues();
661 ArrayList<Bid> maxBids = new ArrayList<Bid>();
662 ArrayList<Bid> maximumBids = new ArrayList<Bid>();
663
664 int size = 0;
665
666 Bid newBid;
667
668 int loop = Math.round((float) Math.sqrt(dissues.size()));
669 for (int i = 0; i < 5; ++i) {
670 if (oppAB != null) {
671 if (oppAB.size() > i) {
672 maximumBids.add(i, oppAB.get(i).getBid());
673 }
674 } else
675 return null;
676 }
677 // while (exloop < dissues.size()) {
678
679 Bid maximumBid = null;
680 maximumBid = myBestBid;
681
682 size = 0;
683 // while (oppAB != null && oppAB.size() > size) {
684 for (int i = 0; i < 5; ++i) {
685 int loop2 = loop;
686 while (loop2 >= 0 && maximumBids.size() > i) {
687 int bi = chooseBestIssue();
688 // }
689 Bid b = maximumBids.get(i);
690
691 try {
692 HashMap vals = b.getValues();
693
694 if (utilitySpace.getUtility(b) > getMyutility()) {
695 int index = 0;
696 for (int k = 0; k < maxBids.size(); ++k) {
697 if (utilitySpace
698 .getUtility(maxBids.get(k)) < utilitySpace
699 .getUtility(b)) {
700 index = k;
701 break;
702 }
703 }
704 maxBids.add(index, new Bid(b));
705 // maxutility = utilitySpace.getUtility(maxBid);
706 // loop = -1;
707 }
708 if (maximumBid != null)
709 vals.put(bi, maximumBid.getValue(bi)); // vals.put(bi,
710 // getRandomValue(dissues.get(bi
711 // - 1)));
712 else
713 vals.put(bi, getRandomValue(dissues.get(bi - 1)));
714
715 newBid = new Bid(utilitySpace.getDomain(), vals);
716
717 if (utilitySpace.getUtility(newBid) >= getMyutility()) {
718 int index = 0;
719 for (int k = 0; k < maxBids.size(); ++k) {
720 if (utilitySpace
721 .getUtility(maxBids.get(k)) < utilitySpace
722 .getUtility(newBid)) {
723 index = k;
724 break;
725 }
726 }
727 maxBids.add(index, new Bid(newBid));
728 // maxutility = utilitySpace.getUtility(maxBid);
729 // loop = -1;
730 }
731
732 } catch (Exception e) {
733 // System.out.println("Exception 66 " + e.getMessage());
734 e.printStackTrace();
735 }
736
737 loop2--;
738 }
739
740 }
741 Bid maxBid = null;
742 float maxProb = 0;
743 int index = -1;
744 double rand = Math.random();
745
746 if (maxBids.size() != 0) {
747 loop = Math.round((float) (rand * maxBids.size()));
748 loop = loop == 0 ? 1 : loop;
749 for (int i = 0; i < loop; i++) {
750 if (!rejectedBid.contains(maxBids.get(i))
751 && prob(maxBids.get(i)) > maxProb) {
752 maxProb = prob(maxBids.get(i));
753 index = i;
754 }
755 }
756 if (index != -1)
757 return new Bid(maxBids.get(index));
758 }
759 return maxBid;
760 }
761
762 public int chooseBestIssue() {
763 double random = Math.random();
764 double sumWeight = 0d;
765 AdditiveUtilitySpace utilitySpace1 = (AdditiveUtilitySpace) utilitySpace;
766
767 for (int i = utilitySpace.getDomain().getIssues().size(); i > 0; --i) {
768 sumWeight += utilitySpace1.getWeight(i);
769 if (sumWeight > random)
770 return i;
771 }
772 return 0;
773 }
774
775 public int chooseWorstIssue() {
776 double random = Math.random() * 100;
777 AdditiveUtilitySpace utilitySpace1 = (AdditiveUtilitySpace) utilitySpace;
778 double sumWeight = 0d;
779 int minin = 1;
780 double min = 1.0d;
781 for (int i = utilitySpace.getDomain().getIssues().size(); i > 0; --i) {
782 sumWeight += 1.0d / utilitySpace1.getWeight(i);
783 if (utilitySpace1.getWeight(i) < min) {
784 min = utilitySpace1.getWeight(i);
785 minin = i;
786 }
787 if (sumWeight > random)
788 return i;
789 }
790 return minin;
791 }
792
793 public Bid getMybestBid(Bid sugestBid, int time) {
794 List<Issue> dissues = utilitySpace.getDomain().getIssues();
795 Bid newBid = new Bid(sugestBid);
796 int index = chooseWorstIssue();
797 boolean loop = true;
798 long bidTime = System.currentTimeMillis();
799 while (loop) {
800 if ((System.currentTimeMillis() - bidTime) / 1000 > 3)
801 break;
802 newBid = new Bid(sugestBid);
803 try {
804 HashMap map = newBid.getValues();
805 map.put(index, getRandomValue(dissues.get(index - 1)));
806 newBid = new Bid(utilitySpace.getDomain(), map);
807 if (utilitySpace.getUtility(newBid) > getMyutility()) {
808 return newBid;
809 }
810 } catch (Exception e) {
811 // System.out.println("Exception in my best bid : " +
812 // e.getMessage());
813 e.printStackTrace();
814 loop = false;
815 }
816 }
817 return sugestBid;
818 }
819
820 public void addBidToList(ArrayList<BidUtility> mybids, BidUtility newbid) {
821 int index = mybids.size();
822 for (int i = 0; i < mybids.size(); ++i) {
823 if (mybids.get(i).getUtility() <= newbid.getUtility()) {
824 // if (!mybids.get(i).getBid().equals(newbid.getBid()))
825 index = i;
826 break;
827 // else
828 // return;
829 }
830 }
831 mybids.add(index, newbid);
832
833 }
834
835 public void addBidToList(ArrayList<Bid> mybids, Bid newbid) {
836
837 mybids.add(newbid);
838
839 }
840
841 public void calculateParamForOpponent(OpponentPreferences op, Bid bid) {
842 List<Issue> dissues = utilitySpace.getDomain().getIssues();
843 HashMap bidVal = bid.getValues();
844 Object[] keys = bidVal.keySet().toArray();
845
846 for (int i = 0; i < dissues.size(); ++i) {
847 if (op.getRepeatedissue().get(dissues.get(i).getName()) != null) {
848
849 HashMap vals = (HashMap) op.getRepeatedissue()
850 .get(dissues.get(i).getName());
851
852 try {
853 if (vals.get(bidVal.get(keys[i])) != null) {
854 Integer repet = (Integer) vals.get(bidVal.get(keys[i]));
855 repet = repet + 1;
856 vals.put(bidVal.get(keys[i]), repet);
857 } else {
858 vals.put(bidVal.get(keys[i]), new Integer(1));
859 }
860 } catch (Exception e) {
861 // System.out.println("Exception 88 " + e.getMessage());
862 }
863 } else {
864 HashMap h = new HashMap();
865 // op.getRepeatedissue().get(dissues.get(i).getName());
866 try {
867
868 h.put(bidVal.get(keys[i]), new Integer(1));
869 } catch (Exception e) {
870 // System.out.println("Exception 99 " + e.getMessage());
871 }
872 op.getRepeatedissue().put(dissues.get(i).getName(), h);
873 }
874 }
875
876 }
877
878 public void setMyutility(double myutility) {
879 this.myutility = myutility;
880 }
881
882 public float calDist(Bid b1, Bid b2) {
883 List<Issue> dissues = utilitySpace.getDomain().getIssues();
884 float distance = 0;
885 Set<Integer> keys = b1.getValues().keySet();
886 Object[] keyCenterVals = b2.getValues().keySet().toArray();
887 Object[] keyVals = keys.toArray();
888 for (int j = 0; j < dissues.size(); ++j) {
889
890 if (dissues.get(j) instanceof IssueDiscrete) {
891
892 if (!b1.getValues().get(keyVals[j]).toString().equals(
893 b2.getValues().get(keyCenterVals[j]).toString())) {
894 distance += 1;
895 }
896 } else {
897 float dis = ((Float
898 .parseFloat(b1.getValues().get(keyVals[j]).toString()))
899 - (Float.parseFloat(b2.getValues().get(keyCenterVals[j])
900 .toString())) / dissues.get(j).getChildCount());
901 distance += dis < 0 ? dis * -1f : dis;
902 }
903 }
904
905 return distance;
906 }
907
908 public double getMyutility() {
909 myutility = getTargetUtility();
910 if (clusterIsRefreshA && clusterA != null) {
911 clusterIsRefreshA = false;
912 ArrayList<Bid> centersA = new ArrayList<Bid>();
913
914 float[][] distA = new float[numberOfclusters][numberOfclusters];
915
916 float unormalA = 0;
917
918 float normal = 1.0f / numberOfclusters;
919 for (int i = 0; i < clusterA.size(); ++i) {
920 centersA.add(clusterA.get(i).getCenter());
921 float temp = normal
922 - ((float) clusterA.get(i).getMembers().size())
923 / (float) oppABids.size();
924 unormalA += temp < 0 ? -1 * temp : temp;
925 }
926
927 float maxA = 0;
928
929 for (int i = 0; i < numberOfclusters; ++i) {
930 for (int j = i + 1; j < numberOfclusters; ++j) {
931 // System.out.println("CenterA i "+i
932 // +" "+centersA.get(i));
933 // System.out.println("CenterA j "+j
934 // +" "+centersA.get(j));
935 distA[i][j] = calDist(centersA.get(i), centersA.get(j));
936 if (distA[i][j] > maxA)
937 maxA = distA[i][j];
938
939 }
940 }
941 // System.out.println("max distance A ============================ "
942 // +
943 // maxA + " unormal A " + unormalA);
944 // System.out.println("max distance B ============================ "
945 // +
946 // maxB + " unormal B " + unormalB);
947 if (clusterHistoryA == null) {
948 clusterHistoryA = new ClusterHistory();
949 clusterHistoryA.distributionFactor = new ArrayList<Float>();
950 clusterHistoryA.prevCenters = new ArrayList<ArrayList<Bid>>();
951 clusterHistoryA.maxDistance = new ArrayList<Float>();
952 }
953
954 clusterHistoryA.distributionFactor.add(
955 clusterHistoryA.distributionFactor.size(),
956 new Float(unormalA));
957 clusterHistoryA.maxDistance.add(clusterHistoryA.maxDistance.size(),
958 new Float(maxA));
959 clusterHistoryA.prevCenters.add(clusterHistoryA.prevCenters.size(),
960 new ArrayList(centersA));
961
962 for (int i = 0; i < clusterA.size(); ++i) {
963
964 clusterA.get(i).getMembers().clear();
965 oppABids.clear();
966
967 }
968 float myConcession = 0f;
969 float meanDistributionA = 0f;
970 float meanDistanceA = 0f;
971 float maxCenterDistanceA = 0f;
972 float[][] centerdis = new float[clusterHistoryA.distributionFactor
973 .size()][clusterHistoryA.distributionFactor.size()];
974 for (int i = 0; i < clusterHistoryA.distributionFactor
975 .size(); ++i) {
976 meanDistributionA += clusterHistoryA.distributionFactor.get(i);
977 meanDistanceA += clusterHistoryA.maxDistance.get(i);
978 for (int j = i + 1; j < clusterHistoryA.distributionFactor
979 .size(); ++j) {
980
981 float temp1 = 0f;
982 float max = 0f;
983 float max1 = 0f;
984 for (int k = 0; k < numberOfclusters; ++k) {
985 for (int l = 0; l < numberOfclusters; ++l) {
986 temp1 = calDist(
987 clusterHistoryA.prevCenters.get(i).get(k),
988 clusterHistoryA.prevCenters.get(j).get(l));
989 if (temp1 > max1)
990 max1 = temp1;
991 }
992 }
993 centerdis[i][j] = max;
994
995 if (maxCenterDistanceA < max1)
996 maxCenterDistanceA = max1;
997 }
998
999 // }
1000 }
1001 int issueSize = utilitySpace.getDomain().getIssues().size();
1002 maxCenterDistanceA /= issueSize;
1003 meanDistributionA = (meanDistributionA
1004 / clusterHistoryA.distributionFactor.size()) / 1.33f;
1005 meanDistanceA = (meanDistanceA
1006 / clusterHistoryA.distributionFactor.size()) / issueSize;
1007
1008 float w1 = 0.4f, w2 = 0.2f, w3 = 0.4f;
1009 concessionA = (w1 * maxCenterDistanceA + w2 * meanDistanceA
1010 - w3 * meanDistributionA);
1011 myConcession = concessionA < concessionB ? concessionA
1012 : concessionB;
1013 myConcessionRate = myConcession > 0 && myConcession > 0.15d
1014 ? myConcession : 0.15d;
1015 } else if (clusterIsRefreshB && clusterB != null) {
1016
1017 clusterIsRefreshB = false;
1018
1019 ArrayList<Bid> centersB = new ArrayList<Bid>();
1020
1021 float[][] distB = new float[numberOfclusters][numberOfclusters];
1022
1023 float unormalB = 0;
1024 float normal = 1.0f / numberOfclusters;
1025
1026 for (int i = 0; i < clusterB.size(); ++i) {
1027 centersB.add(clusterB.get(i).getCenter());
1028 // System.out.println("Cluster B members === " + i + " "
1029 // +
1030 // (clusterB.get(i).getMembers().size()));
1031 float temp = normal
1032 - ((float) clusterB.get(i).getMembers().size())
1033 / (float) oppBBids.size();
1034 unormalB += temp < 0 ? -1 * temp : temp;
1035 }
1036
1037 float maxB = 0;
1038 for (int i = 0; i < numberOfclusters; ++i) {
1039 for (int j = i + 1; j < numberOfclusters; ++j) {
1040 // System.out.println("CenterA i "+i
1041 // +" "+centersA.get(i));
1042 // System.out.println("CenterA j "+j
1043 // +" "+centersA.get(j));
1044
1045 distB[i][j] = calDist(centersB.get(i), centersB.get(j));
1046 // System.out.println("CenterB i "+i
1047 // +" "+centersB.get(i));
1048 // System.out.println("CenterB j "+j
1049 // +" "+centersB.get(j));
1050 if (distB[i][j] > maxB)
1051 maxB = distB[i][j];
1052 }
1053 }
1054 // System.out.println("max distance A ============================ "
1055 // +
1056 // maxA + " unormal A " + unormalA);
1057 // System.out.println("max distance B ============================ "
1058 // +
1059 // maxB + " unormal B " + unormalB);
1060
1061 if (clusterHistoryB == null) {
1062 clusterHistoryB = new ClusterHistory();
1063 clusterHistoryB.distributionFactor = new ArrayList<Float>();
1064 clusterHistoryB.prevCenters = new ArrayList<ArrayList<Bid>>();
1065 clusterHistoryB.maxDistance = new ArrayList<Float>();
1066 }
1067
1068 clusterHistoryB.distributionFactor.add(
1069 clusterHistoryB.distributionFactor.size(),
1070 new Float(unormalB));
1071 clusterHistoryB.maxDistance.add(clusterHistoryB.maxDistance.size(),
1072 new Float(maxB));
1073 clusterHistoryB.prevCenters.add(clusterHistoryB.prevCenters.size(),
1074 new ArrayList(centersB));
1075 for (int i = 0; i < clusterB.size(); ++i) {
1076
1077 // ArrayList<Bid> removable = clusterA.get(i).getMembers();
1078 // clusterA.get(i).getMembers().removeAll(removable);
1079 clusterB.get(i).getMembers().clear();
1080 // removable = clusterB.get(i).getMembers();
1081 // clusterB.get(i).getMembers().removeAll(removable);
1082
1083 oppBBids.clear();
1084
1085 }
1086 float myConcession = 0f;
1087 float meanDistributionB = 0f;
1088 float meanDistanceB = 0f;
1089 float maxCenterDistanceB = 0f;
1090
1091 float[][] centerdis = new float[clusterHistoryB.distributionFactor
1092 .size()][clusterHistoryB.distributionFactor.size()];
1093 for (int i = 0; i < clusterHistoryB.distributionFactor
1094 .size(); ++i) {
1095 meanDistributionB += clusterHistoryB.distributionFactor.get(i);
1096
1097 meanDistanceB += clusterHistoryB.maxDistance.get(i);
1098
1099 for (int j = i + 1; j < clusterHistoryB.distributionFactor
1100 .size(); ++j) {
1101
1102 float temp = 0f;
1103 float temp1 = 0f;
1104 float max = 0f;
1105 float max1 = 0f;
1106 for (int k = 0; k < numberOfclusters; ++k) {
1107 for (int l = 0; l < numberOfclusters; ++l) {
1108 temp = calDist(
1109 clusterHistoryB.prevCenters.get(i).get(k),
1110 clusterHistoryB.prevCenters.get(j).get(l));
1111 if (temp > max)
1112 max = temp;
1113
1114 }
1115 }
1116 centerdis[i][j] = max;
1117 if (maxCenterDistanceB < max)
1118 maxCenterDistanceB = max;
1119
1120 }
1121
1122 // }
1123 }
1124 int issueSize = utilitySpace.getDomain().getIssues().size();
1125
1126 maxCenterDistanceB /= issueSize;
1127 meanDistributionB = (meanDistributionB
1128 / clusterHistoryB.distributionFactor.size()) / 1.33f;
1129 meanDistanceB = (meanDistanceB
1130 / clusterHistoryB.distributionFactor.size()) / issueSize;
1131
1132 // System.out.println("for Opponent A All
1133 // ===============================meanDistributionA == "
1134 // +
1135 // meanDistributionA + " meanDistanceA ==" +
1136 // meanDistanceA + " maxCenterDistanceA==" +
1137 // maxCenterDistanceA);
1138 // System.out.println("for Opponent B All
1139 // ===============================meanDistributionB == "
1140 // +
1141 // meanDistributionB + " meanDistanceB ==" +
1142 // meanDistanceB + " maxCenterDistanceB==" +
1143 // maxCenterDistanceB);
1144 float w1 = 0.4f, w2 = 0.2f, w3 = 0.4f;
1145 // System.out.println("concession A =========== " +
1146 // (w1 * maxCenterDistanceA + w2 * meanDistanceA -
1147 // w3 * meanDistributionA));
1148 // System.out.println("concession B =========== " +
1149 // (w1 * maxCenterDistanceB + w2 * meanDistanceB -
1150 // w3 * meanDistributionB));
1151
1152 concessionB = (w1 * maxCenterDistanceB + w2 * meanDistanceB
1153 - w3 * meanDistributionB);
1154 myConcession = concessionA < concessionB ? concessionA
1155 : concessionB;
1156 myConcessionRate = myConcession > 0 && myConcession > 0.15d
1157 ? myConcession : 0.15d;
1158 }
1159
1160 // {
1161 if (myutility < ConstantUtility)
1162 return ConstantUtility;
1163 else
1164 return myutility;
1165 }
1166
1167 @Override
1168 public double getE() {
1169 if (withDiscount)
1170 return myConcessionRate + 0.05d;
1171 return myConcessionRate;
1172 }
1173
1174 private class OpponentPreferences {
1175 private HashMap repeatedissue = new HashMap();
1176 private ArrayList selectedValues;
1177 ArrayList<BidUtility> opponentBids = new ArrayList<BidUtility>();
1178
1179 public void setRepeatedissue(HashMap repeatedissue) {
1180 this.repeatedissue = repeatedissue;
1181 }
1182
1183 public HashMap getRepeatedissue() {
1184 return repeatedissue;
1185 }
1186
1187 public void setSelectedValues(ArrayList selectedValues) {
1188 this.selectedValues = selectedValues;
1189 }
1190
1191 public ArrayList getSelectedValues() {
1192 return selectedValues;
1193 }
1194
1195 public void setOpponentBids(
1196 ArrayList<ParsAgent2.BidUtility> opponentBids) {
1197 this.opponentBids = opponentBids;
1198 }
1199
1200 public ArrayList<ParsAgent2.BidUtility> getOpponentBids() {
1201 return opponentBids;
1202 }
1203 }
1204
1205 private class BidUtility {
1206 private Bid bid;
1207 private double utility;
1208 private long time;
1209
1210 BidUtility(Bid b, double u, long t) {
1211 this.bid = b;
1212 this.utility = u;
1213 this.time = t;
1214 }
1215
1216 BidUtility(BidUtility newbid) {
1217 this.bid = newbid.getBid();
1218 this.utility = newbid.getUtility();
1219 this.time = newbid.getTime();
1220 }
1221
1222 public void setBid(Bid bid) {
1223 this.bid = bid;
1224 }
1225
1226 public Bid getBid() {
1227 return bid;
1228 }
1229
1230 public void setUtility(double utility) {
1231 this.utility = utility;
1232 }
1233
1234 public double getUtility() {
1235 return utility;
1236 }
1237
1238 public void setTime(long time) {
1239 this.time = time;
1240 }
1241
1242 public long getTime() {
1243 return time;
1244 }
1245 }
1246
1247 @Override
1248 public String getDescription() {
1249 return "ANAC2016";
1250 }
1251
1252}
Note: See TracBrowser for help on using the repository browser.