source: src/main/java/agents/anac/y2015/ParsAgent/ParsAgent.java@ 127

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

Initial import : Genius 9.0.0

File size: 33.5 KB
Line 
1package agents.anac.y2015.ParsAgent;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6
7import genius.core.AgentID;
8import genius.core.Bid;
9import genius.core.actions.Accept;
10import genius.core.actions.Action;
11import genius.core.actions.Offer;
12import genius.core.issue.Issue;
13import genius.core.issue.IssueDiscrete;
14import genius.core.issue.IssueInteger;
15import genius.core.issue.Value;
16import genius.core.issue.ValueInteger;
17import genius.core.utility.AdditiveUtilitySpace;
18import negotiator.parties.AbstractTimeDependentNegotiationParty;
19
20/**
21 * This is ParsAgent party.
22 */
23public class ParsAgent extends AbstractTimeDependentNegotiationParty {
24 Bid lastBid;
25 Action lastAction;
26 String oppAName;
27 String oppBName;
28 int round;
29 double myutility = 0.8d;
30 boolean Imfirst = false;
31 Boolean withDiscount = null;
32 boolean fornullAgent = false;
33 ArrayList<BidUtility> opponentAB = new ArrayList<BidUtility>();
34 OpponentPreferences oppAPreferences = new OpponentPreferences();
35 OpponentPreferences oppBPreferences = new OpponentPreferences();
36
37 /**
38 * Each round this method gets called and ask you to accept or offer. The
39 * first party in the first round is a bit different, it can only propose an
40 * offer.
41 *
42 * @param validActions
43 * Either a list containing both accept and offer or only offer.
44 * @return The chosen action.
45 */
46 @Override
47 public Action chooseAction(List<Class<? extends Action>> validActions) {
48
49 try {
50 if (lastBid == null) {
51 Imfirst = true;
52 Bid b = getMybestBid(utilitySpace.getMaxUtilityBid(), 0);
53 return new Offer(getPartyId(), b);
54 } /*
55 * Comment for Bug else if (round == 0) { if (Imfirst) { return
56 * new Offer(getPartyId(),offerMyNewBid()); } else if
57 * (utilitySpace.getUtility(lastBid) > 0.6d) { return new
58 * Accept(getPartyId()); } }
59 */
60 else {
61 if (lastAction instanceof Accept) {
62 if (utilitySpace.getUtility(lastBid) > getMyutility()) {
63 return new Accept(getPartyId(), lastBid);
64 } else {
65 Bid b = offerMyNewBid();
66 return new Offer(getPartyId(), b);
67 }
68 } else {
69 if (utilitySpace.getUtility(lastBid) > getMyutility()) {
70 return new Accept(getPartyId(), lastBid);
71 } else {
72 Bid b = offerMyNewBid();
73 if (utilitySpace.getUtility(b) < getMyutility())
74 return new Offer(getPartyId(), getMybestBid(
75 utilitySpace.getMaxUtilityBid(), 0));
76 else
77 return new Offer(getPartyId(), b);
78
79 }
80 }
81 }
82 } catch (Exception e) {
83 System.out.println("Error Occured " + e.getMessage());
84 }
85 Bid mb = null;
86 try {
87 mb = utilitySpace.getMaxUtilityBid();
88 return new Offer(getPartyId(), getMybestBid(mb, 0));
89 } catch (Exception e) {
90 try {
91 return new Offer(getPartyId(), mb);
92 } catch (Exception e2) {
93
94 }
95 }
96 return new Accept(getPartyId(), lastBid);
97 }
98
99 /**
100 * All offers proposed by the other parties will be received as a message.
101 * You can use this information to your advantage, for example to predict
102 * their utility.
103 *
104 * @param sender
105 * The party that did the action.
106 * @param action
107 * The action that party did.
108 */
109 @Override
110 public void receiveMessage(AgentID sender, Action action) {
111 super.receiveMessage(sender, action);
112 String agentName = sender == null ? "null" : sender.toString();
113 // action.getAgent() != null ? action.getAgent().toString() :
114 // fornullAgent ? "null1" : "null2";
115 fornullAgent = !fornullAgent;
116 if (action != null && action instanceof Offer) {
117 Bid newBid = ((Offer) action).getBid();
118 if (withDiscount == null) {
119 try {
120 if (utilitySpace.getUtilityWithDiscount(newBid,
121 timeline) != utilitySpace.getUtility(newBid)) {
122 withDiscount = Boolean.TRUE;
123 } else
124 withDiscount = Boolean.FALSE;
125 } catch (Exception e) {
126 System.out.println("Exception " + e.getMessage());
127 }
128 }
129 BidUtility opBid;
130 try {
131 opBid = new BidUtility(newBid, utilitySpace.getUtility(newBid),
132 System.currentTimeMillis());
133
134 if (oppAName != null && oppAName.equals(agentName)) {
135 addBidToList(oppAPreferences.getOpponentBids(), opBid);
136
137 } else if (oppBName != null && oppBName.equals(agentName)) {
138 addBidToList(oppBPreferences.getOpponentBids(), opBid);
139 } else if (oppAName == null) {
140 oppAName = agentName;
141 oppAPreferences.getOpponentBids().add(opBid);
142 } else if (oppBName == null) {
143 oppBName = agentName;
144 oppBPreferences.getOpponentBids().add(opBid);
145 }
146 calculateParamForOpponent((oppAName.equals(agentName)
147 ? oppAPreferences : oppBPreferences), newBid);
148 System.out.println("opp placed bid:" + newBid);
149 lastBid = newBid;
150 } catch (Exception e) {
151 e.printStackTrace();
152 }
153 } else if (action != null && action instanceof Accept) {
154 BidUtility opBid = null;
155 try {
156 opBid = new BidUtility(lastBid,
157 utilitySpace.getUtility(lastBid),
158 System.currentTimeMillis());
159 } catch (Exception e) {
160 System.out.println("Exception 44" + e.getMessage());
161 }
162 addBidToList(opponentAB, opBid);
163 }
164 lastAction = action;
165
166 // Here you can listen to other parties' messages
167 }
168
169 /**
170 * @param issueindex
171 * the issue for which we need the best value (index).
172 * @return the best value-index for issue <issueindex>. For integer values,
173 * returns the best value itself, no 'index'.
174 */
175 public int MyBestValue(int issueindex) {
176 List<Issue> dissues = utilitySpace.getDomain().getIssues();
177 Issue isu = dissues.get(issueindex);
178 HashMap<Integer, Value> map = new HashMap<Integer, Value>();
179 double maxutil = 0d;
180 int maxvalIndex = 0;
181 try {
182 map = utilitySpace.getMaxUtilityBid().getValues();
183 } catch (Exception e) {
184 e.printStackTrace();
185 }
186 if (isu instanceof IssueDiscrete) {
187 IssueDiscrete is = (IssueDiscrete) isu;
188
189 // Bizarre way to get the value-index of this issue in the max-util
190 // bid?
191 for (int num = 0; num < is.getNumberOfValues(); ++num) {
192 map.put(new Integer(issueindex + 1), is.getValue(num));
193 Bid temp;
194 double u = 0d;
195 try {
196 temp = new Bid(utilitySpace.getDomain(), map);
197 u = utilitySpace.getUtility(temp);
198 } catch (Exception e) {
199 e.printStackTrace();
200 }
201 if (u > maxutil) {
202 maxutil = u;
203 maxvalIndex = num;
204 }
205 break;
206
207 }
208 } else if (isu instanceof IssueInteger) {
209 if (map != null) {
210 // +1 because the map has first issue = 1.
211 return ((ValueInteger) map.get(issueindex + 1)).getValue();
212 }
213 }
214 return maxvalIndex;
215 }
216
217 /**
218 * @return a next bid.
219 */
220 public Bid offerMyNewBid() {
221 Bid bidNN = null;
222 if (opponentAB != null && opponentAB.size() != 0)
223 bidNN = getNNBid(opponentAB);
224 try {
225 if (bidNN == null
226 || utilitySpace.getUtility(bidNN) < getMyutility()) {
227 List<List<Object>> isues = getMutualIssues();
228 HashMap<Integer, Value> map = new HashMap<Integer, Value>();
229 Bid bid;
230 List<Issue> dissues = utilitySpace.getDomain().getIssues();
231 for (int i = 0; i < isues.size(); ++i) {
232 List<Object> keyVal = isues.get(i);
233
234 Issue dissue = dissues.get(i);
235 if (dissue instanceof IssueDiscrete) {
236 if (keyVal != null) {
237 IssueDiscrete is = (IssueDiscrete) dissues.get(i);
238 // search the value that matches keyVal(0) and put
239 // in map.
240 // fails silently!
241 for (int num = 0; num < is
242 .getNumberOfValues(); ++num) {
243 if (is.getValue(num).toString()
244 .equals(keyVal.get(0).toString())) {
245 map.put(new Integer(i + 1),
246 is.getValue(num));
247 break;
248 }
249
250 }
251 } else { // keyVal == null
252 IssueDiscrete is = (IssueDiscrete) dissues.get(i);
253 map.put(new Integer(i + 1),
254 is.getValue(MyBestValue(i)));
255 }
256 } else if (dissue instanceof IssueInteger) {
257 if (keyVal != null) {
258 map.put(new Integer(i + 1),
259 (ValueInteger) keyVal.get(0));
260 } else { // keyVal==null
261 // IssueInteger is = (IssueInteger) dissues.get(i);
262 map.put(i + 1, new ValueInteger(MyBestValue(i)));
263 }
264 } else {
265 throw new IllegalStateException(
266 "not supported issue " + dissue);
267 }
268
269 }
270 try {
271 bid = new Bid(utilitySpace.getDomain(),
272 (HashMap) map.clone());
273 if (utilitySpace.getUtility(bid) > getMyutility())
274 return bid;
275 else {
276 return getMybestBid(utilitySpace.getMaxUtilityBid(), 0);
277 }
278 } catch (Exception e) {
279 System.out.println("Exception 55 " + e.getMessage());
280 }
281 } else
282 return bidNN;
283 } catch (Exception e) {
284 e.printStackTrace();
285 }
286 return null;
287 }
288
289 /**
290 * Seems that this determines for each issue the top-2 of occuring values.
291 *
292 * @return list of &lt;issue, maxval1, maxval2 &gt; triplets (triplets
293 * encoded as List<Object>...). maxval1 is mostly used value;
294 * maxval2 is one-but-mostly used.
295 */
296 public List<List<Object>> getMutualIssues() {
297 List<List<Object>> mutualList = new ArrayList<List<Object>>();
298 List<Issue> dissues = utilitySpace.getDomain().getIssues();
299 int twocycle = 2;
300 while (twocycle > 0) {
301 mutualList = new ArrayList<List<Object>>();
302 for (int i = 0; i < dissues.size(); ++i) {
303 updateMutualList(mutualList, dissues, twocycle, i);
304 if (((HashMap) oppAPreferences.getRepeatedissue()
305 .get(dissues.get(i).getName())).size() == 0
306 || ((HashMap) oppAPreferences.getRepeatedissue()
307 .get(dissues.get(i).getName())).size() == 0) {
308 twocycle--;
309 }
310 }
311 if (twocycle != 0) {
312 twocycle--;
313 if (opponentAB.size() == 0) {
314 float nullval = 0.0f;
315 for (int i = 0; i < mutualList.size(); ++i) {
316 if (mutualList.get(i) != null) {
317 ++nullval;
318 }
319 }
320
321 nullval = nullval / mutualList.size();
322 if (nullval >= 0.5)
323 twocycle--;
324 } else
325 twocycle--;
326 }
327 }
328 return mutualList;
329 }
330
331 /**
332 * Search two highest repeated issues for each issue
333 *
334 * @param mutualList
335 * @param dissues
336 * @param twocycle
337 * @param i
338 */
339 private void updateMutualList(List<List<Object>> mutualList,
340 List<Issue> dissues, int twocycle, int i) {
341 if (oppAPreferences.getRepeatedissue()
342 .get(dissues.get(i).getName()) != null) {
343 HashMap<Value, Integer> vals = oppAPreferences.getRepeatedissue()
344 .get(dissues.get(i).getName());
345 HashMap<Value, Integer> valsB = oppBPreferences.getRepeatedissue()
346 .get(dissues.get(i).getName());
347
348 Object[] keys = vals.keySet().toArray();
349 int[] max = new int[] { 0, 0 };
350 Object[] maxkey = new Object[] { null, null };
351 for (int j = 0; j < keys.length; ++j) {
352 Integer temp = vals.get(keys[j]);
353 if (temp.intValue() > max[0]) {
354 max[0] = temp.intValue();
355 maxkey[0] = keys[j];
356 } else if (temp.intValue() > max[1]) {
357 max[1] = temp.intValue();
358 maxkey[1] = keys[j];
359 }
360 }
361 if (valsB != null) {
362 Object[] keysB = valsB.keySet().toArray();
363 int[] maxB = new int[] { 0, 0 };
364 ;
365 Object[] maxkeyB = new Object[] { null, null };
366 for (int j = 0; j < keysB.length; ++j) {
367 Integer temp = valsB.get(keysB[j]);
368 if (temp.intValue() > maxB[0]) {
369 maxB[0] = temp.intValue();
370 maxkeyB[0] = keysB[j];
371 } else if (temp.intValue() > maxB[1]) {
372 maxB[1] = temp.intValue();
373 maxkeyB[1] = keysB[j];
374 }
375 }
376 if (twocycle == 2) {
377 if (maxkey[0] != null && maxkeyB[0] != null
378 && maxkey[0].equals(maxkeyB[0])) {
379 ArrayList<Object> l = new ArrayList();
380 l.add(maxkey[0]);
381 l.add(maxB[0]);
382 l.add(max[0]);
383 mutualList.add(i, l);
384 } else
385 mutualList.add(i, null);
386 } else {
387 boolean insideloop = true;
388 for (int m = 0; insideloop && m < 2; ++m) {
389 for (int z = 0; insideloop && z < 2; ++z) {
390 if (maxkey[m] != null && maxkeyB[z] != null
391 && maxkey[m].equals(maxkeyB[z])) {
392 ArrayList<Object> l = new ArrayList();
393 l.add(maxkey[m]);
394 l.add(maxB[z]);
395 l.add(max[m]);
396 mutualList.add(i, l);
397 insideloop = false;
398 }
399 }
400 }
401 if (insideloop)
402 mutualList.add(i, null);
403
404 }
405 } else {
406 mutualList.add(i, null);
407 oppBPreferences.getRepeatedissue().put(dissues.get(i).getName(),
408 new HashMap());
409 }
410 } else {
411 oppAPreferences.getRepeatedissue().put(dissues.get(i).getName(),
412 new HashMap());
413 mutualList.add(i, null);
414 }
415 }
416
417 public Bid getNNBid(ArrayList<BidUtility> oppAB) {
418 List<Issue> dissues = utilitySpace.getDomain().getIssues();
419 Bid maxBid = null;
420 double maxutility = 0d;
421 int size = 0;
422 int exloop = 0;
423 Bid newBid;
424 while (exloop < dissues.size()) {
425 int bi = chooseBestIssue();
426 size = 0;
427 while (oppAB != null && oppAB.size() > size) {
428 Bid b = oppAB.get(size).getBid();
429 newBid = new Bid(b);
430 try {
431 HashMap vals = b.getValues();
432 vals.put(bi, getRandomValue(dissues.get(bi - 1)));
433 newBid = new Bid(utilitySpace.getDomain(), vals);
434 if (utilitySpace.getUtility(newBid) > getMyutility()
435 && utilitySpace.getUtility(newBid) > maxutility) {
436 maxBid = new Bid(newBid);
437 maxutility = utilitySpace.getUtility(maxBid);
438 }
439
440 } catch (Exception e) {
441 System.out.println("Exception 66 " + e.getMessage());
442 }
443 size++;
444 }
445 exloop++;
446 }
447 return maxBid;
448 }
449
450 public int chooseBestIssue() {
451 double random = Math.random();
452 double sumWeight = 0d;
453 AdditiveUtilitySpace utilitySpace1 = (AdditiveUtilitySpace) utilitySpace;
454
455 for (int i = utilitySpace1.getDomain().getIssues().size(); i > 0; --i) {
456 sumWeight += utilitySpace1.getWeight(i);
457 if (sumWeight > random)
458 return i;
459 }
460 return 0;
461 }
462
463 public int chooseWorstIssue() {
464 double random = Math.random() * 100;
465 double sumWeight = 0d;
466 int minin = 1;
467 double min = 1.0d;
468 AdditiveUtilitySpace utilitySpace1 = (AdditiveUtilitySpace) utilitySpace;
469 for (int i = utilitySpace1.getDomain().getIssues().size(); i > 0; --i) {
470 sumWeight += 1.0d / utilitySpace1.getWeight(i);
471 if (utilitySpace1.getWeight(i) < min) {
472 min = utilitySpace1.getWeight(i);
473 minin = i;
474 }
475 if (sumWeight > random)
476 return i;
477 }
478 return minin;
479 }
480
481 public Bid getMybestBid(Bid sugestBid, int time) {
482 List<Issue> dissues = utilitySpace.getDomain().getIssues();
483 Bid newBid = new Bid(sugestBid);
484 int index = chooseWorstIssue();
485 boolean loop = true;
486 long bidTime = System.currentTimeMillis();
487 while (loop) {
488 if ((System.currentTimeMillis() - bidTime) * 1000 > 3)
489 break;
490 newBid = new Bid(sugestBid);
491 try {
492 HashMap map = newBid.getValues();
493 map.put(index, getRandomValue(dissues.get(index - 1)));
494 newBid = new Bid(utilitySpace.getDomain(), map);
495 if (utilitySpace.getUtility(newBid) > getMyutility()) {
496 return newBid;
497 }
498 } catch (Exception e) {
499 // System.out.println("Exception in my best bid : " +
500 // e.getMessage());
501 loop = false;
502 }
503 }
504 return newBid;
505 }
506
507 public void addBidToList(ArrayList<BidUtility> mybids, BidUtility newbid) {
508 int index = mybids.size();
509 for (int i = 0; i < mybids.size(); ++i) {
510 if (mybids.get(i).getUtility() <= newbid.getUtility()) {
511 if (!mybids.get(i).getBid().equals(newbid.getBid()))
512 index = i;
513 else
514 return;
515 }
516 }
517 mybids.add(index, newbid);
518
519 }
520
521 /**
522 * Updates the opponent preferences model. Basically updates the number of
523 * times the issue values occurred.
524 *
525 * @param op
526 * @param bid
527 */
528 public void calculateParamForOpponent(OpponentPreferences op, Bid bid) {
529 List<Issue> dissues = utilitySpace.getDomain().getIssues();
530 HashMap<Integer, Value> bidVal = bid.getValues();
531 Integer[] keys = new Integer[0];
532 keys = bidVal.keySet().toArray(keys);
533
534 for (int i = 0; i < dissues.size(); ++i) {
535 if (op.getRepeatedissue().get(dissues.get(i).getName()) != null) {
536
537 HashMap<Value, Integer> vals = op.getRepeatedissue()
538 .get(dissues.get(i).getName());
539
540 try {
541 if (vals.get(bidVal.get(keys[i])) != null) {
542 Integer repet = vals.get(bidVal.get(keys[i]));
543 repet = repet + 1;
544 vals.put(bidVal.get(keys[i]), repet);
545 } else {
546 vals.put(bidVal.get(keys[i]), new Integer(1));
547 }
548 } catch (Exception e) {
549 // System.out.println("Exception 88 " + e.getMessage());
550 }
551 } else {
552 HashMap<Value, Integer> h = new HashMap<Value, Integer>();
553 // op.getRepeatedissue().get(dissues.get(i).getName());
554 try {
555
556 h.put(bidVal.get(keys[i]), new Integer(1));
557 } catch (Exception e) {
558 // System.out.println("Exception 99 " + e.getMessage());
559 }
560 op.getRepeatedissue().put(dissues.get(i).getName(), h);
561 }
562 }
563
564 }
565
566 public void setMyutility(double myutility) {
567 this.myutility = myutility;
568 }
569
570 public double getMyutility() {
571 myutility = getTargetUtility();
572 if (myutility < 0.7)
573 return 0.7d;
574 return myutility;
575 }
576
577 @Override
578 public double getE() {
579 if (withDiscount)
580 return 0.20d;
581 return 0.15d;
582 }
583
584 private class OpponentPreferences {
585 /**
586 * map with number of times an issue values occurred in an opponent bid.
587 */
588 private HashMap<String, HashMap<Value, Integer>> repeatedissue = new HashMap();
589 private ArrayList selectedValues;
590 ArrayList<BidUtility> opponentBids = new ArrayList<BidUtility>();
591
592 /**
593 * actually, not used. Code modifies map returned from
594 * {@link #getRepeatedissue()} directly.
595 *
596 * @param repeatedissue
597 */
598 public void setRepeatedissue(
599 HashMap<String, HashMap<Value, Integer>> repeatedissue) {
600 this.repeatedissue = repeatedissue;
601 }
602
603 /**
604 * @return map with number of times an issue values occurred in an
605 * opponent bid
606 */
607 public HashMap<String, HashMap<Value, Integer>> getRepeatedissue() {
608 return repeatedissue;
609 }
610
611 public void setSelectedValues(ArrayList selectedValues) {
612 this.selectedValues = selectedValues;
613 }
614
615 public ArrayList getSelectedValues() {
616 return selectedValues;
617 }
618
619 public void setOpponentBids(
620 ArrayList<ParsAgent.BidUtility> opponentBids) {
621 this.opponentBids = opponentBids;
622 }
623
624 public ArrayList<ParsAgent.BidUtility> getOpponentBids() {
625 return opponentBids;
626 }
627 }
628
629 private class BidUtility {
630 private Bid bid;
631 private double utility;
632 private long time;
633
634 BidUtility(Bid b, double u, long t) {
635 this.bid = b;
636 this.utility = u;
637 this.time = t;
638 }
639
640 BidUtility(BidUtility newbid) {
641 this.bid = newbid.getBid();
642 this.utility = newbid.getUtility();
643 this.time = newbid.getTime();
644 }
645
646 public void setBid(Bid bid) {
647 this.bid = bid;
648 }
649
650 public Bid getBid() {
651 return bid;
652 }
653
654 public void setUtility(double utility) {
655 this.utility = utility;
656 }
657
658 public double getUtility() {
659 return utility;
660 }
661
662 public void setTime(long time) {
663 this.time = time;
664 }
665
666 public long getTime() {
667 return time;
668 }
669 }
670
671 @Override
672 public String getDescription() {
673 return "ANAC2015";
674 }
675
676}
677
678/*
679 * package negotiator.ui;
680 *
681 * import java.util.ArrayList; import java.util.HashMap; import java.util.List;
682 * import java.util.Map; import java.util.Vector; import negotiator.Bid; import
683 * negotiator.DeadlineType; import negotiator.Timeline; import
684 * negotiator.actions.Accept; import negotiator.actions.Action; import
685 * negotiator.actions.Offer; import negotiator.issue.Issue; import
686 * negotiator.issue.IssueDiscrete; import negotiator.issue.IssueInteger; import
687 * negotiator.issue.IssueReal; import negotiator.issue.Objective; import
688 * negotiator.issue.Value; import negotiator.issue.ValueInteger; import
689 * negotiator.issue.ValueReal; import
690 * negotiator.parties.AbstractNegotiationParty; import
691 * negotiator.parties.AbstractTimeDependentNegotiationParty; import
692 * negotiator.utility.UtilitySpace;
693 *
694 *
695 * public class ParsAgent extends AbstractTimeDependentNegotiationParty { Bid
696 * lastBid; Action lastAction; String oppAName; String oppBName; int round;
697 * double myutility = 0.8d; boolean Imfirst = false; Boolean withDiscount =
698 * null; boolean fornullAgent = false; ArrayList<BidUtility> opponentAB = new
699 * ArrayList<BidUtility>(); OpponentPreferences oppAPreferences = new
700 * OpponentPreferences(); OpponentPreferences oppBPreferences = new
701 * OpponentPreferences(); // boolean myturn;
702 *
703 *
704 * public ParsAgent(UtilitySpace utilitySpace, Map<DeadlineType, Object>
705 * deadlines, Timeline timeline, long randomSeed) { // Make sure that this
706 * constructor calls it's parent. super(utilitySpace, deadlines, timeline,
707 * randomSeed); // Object[] keys = deadlines.keySet().toArray(); // for (int i =
708 * 0; i < keys.length; ++i) { // System.out.println("keys[i] ==== "+keys[i]);
709 * // if (keys[i] instanceof DeadlineType) { // // round =
710 * Integer.parseInt(deadlines.get(keys[i]).toString()); //
711 * System.out.println("round ====== "+round); // } // }
712 *
713 *
714 * }
715 *
716 *
717 * @Override public Action chooseAction(List<Class> validActions) { //
718 * myturn=true; round--; try { if (lastBid == null) { Imfirst = true; Bid b =
719 * getMybestBid(utilitySpace.getMaxUtilityBid(), 0); return new
720 * Offer(getPartyId(), b); } else if (round == 0) { if (Imfirst) { return new
721 * Offer(getPartyId(),offerMyNewBid()); } else if
722 * (utilitySpace.getUtility(lastBid) > 0.6d) { return new Accept(getPartyId());
723 * } } else { if (lastAction instanceof Accept) { if
724 * (utilitySpace.getUtility(lastBid) > getMyutility()) { return new
725 * Accept(getPartyId()); } else { Bid b = offerMyNewBid(); return new
726 * Offer(getPartyId(),b); } } else { if (utilitySpace.getUtility(lastBid) >
727 * getMyutility()) { return new Accept(getPartyId()); } else { Bid b =
728 * offerMyNewBid(); if (utilitySpace.getUtility(b) < getMyutility()) return new
729 * Offer(getPartyId(),getMybestBid(utilitySpace.getMaxUtilityBid(), 0)); else
730 * return new Offer(getPartyId(),b);
731 *
732 * } } } } catch (Exception e) { System.out.println("Error Occured " +
733 * e.getMessage()); } Bid mb = null; try { mb = utilitySpace.getMaxUtilityBid();
734 * return new Offer(getPartyId(),getMybestBid(mb, 0)); } catch (Exception e) {
735 * try { return new Offer(getPartyId(),mb); } catch (Exception e2) {
736 *
737 * } } return new Accept(getPartyId()); }
738 *
739 *
740 * @Override public void receiveMessage(Object sender, Action action ) {
741 * super.receiveMessage(sender, action); String agentName =sender.toString();
742 * fornullAgent = !fornullAgent; if (action != null && action instanceof Offer)
743 * { // myturn=false; Bid newBid = ((Offer)action).getBid(); if (withDiscount ==
744 * null) { try { if (utilitySpace.getUtilityWithDiscount(newBid, timeline) !=
745 * utilitySpace.getUtility(newBid)) { withDiscount = Boolean.TRUE; } else
746 * withDiscount = Boolean.FALSE; } catch (Exception e) {
747 * System.out.println("Exception " + e.getMessage()); } } BidUtility opBid; try
748 * { opBid = new BidUtility(newBid, utilitySpace.getUtility(newBid),
749 * System.currentTimeMillis());
750 *
751 * if (oppAName != null && oppAName.equals(agentName)) {
752 * addBidToList(oppAPreferences.getOpponentBids(), opBid);
753 *
754 * } else if (oppBName != null && oppBName.equals(agentName)) {
755 * addBidToList(oppBPreferences.getOpponentBids(), opBid); } else if (oppAName
756 * == null) { oppAName = agentName;
757 * oppAPreferences.getOpponentBids().add(opBid); } else if (oppBName == null) {
758 * oppBName = agentName; oppBPreferences.getOpponentBids().add(opBid); }
759 * calculateParamForOpponent((oppAName.equals(agentName) ? oppAPreferences :
760 * oppBPreferences), newBid); lastBid = newBid; } catch (Exception e) { //
761 * System.out.println("Exception 33 " + e.getMessage()); } } else if (action !=
762 * null && action instanceof Accept ) { //&& !myturn BidUtility opBid = null;
763 * try { opBid = new BidUtility(lastBid, utilitySpace.getUtility(lastBid),
764 * System.currentTimeMillis()); } catch (Exception e) { //
765 * System.out.println("Exception 44" + e.getMessage()); }
766 * addBidToList(opponentAB, opBid); } lastAction = action;
767 *
768 * // Here you can listen to other parties' messages }
769 *
770 * public int MyBestValue(int issueindex) { ArrayList<Issue> dissues =
771 * utilitySpace.getDomain().getIssues(); Issue isu = dissues.get(issueindex);
772 * HashMap map = new HashMap(); double maxutil = 0d; int maxvalIndex = 0; try {
773 * map = utilitySpace.getMaxUtilityBid().getValues(); } catch (Exception e) {
774 * System.out.println("Exception 3323 " + e.getMessage()); } if (isu instanceof
775 * IssueDiscrete) { IssueDiscrete is = (IssueDiscrete)isu; for (int num = 0; num
776 * < is.getNumberOfValues(); ++num) { map.put(new Integer(issueindex + 1),
777 * is.getValue(num)); Bid temp; double u = 0d; try { temp = new
778 * Bid(utilitySpace.getDomain(), map); u = utilitySpace.getUtility(temp); }
779 * catch (Exception e) { System.out.println("Exception 98989 " +
780 * e.getMessage()); } if (u > maxutil) { maxutil = u; maxvalIndex = num; }
781 * break;
782 *
783 *
784 * } } return maxvalIndex; }
785 *
786 * public Bid offerMyNewBid() { Bid bidNN = null; if (opponentAB != null &&
787 * opponentAB.size() != 0) bidNN = getNNBid(opponentAB); try { if (bidNN == null
788 * || utilitySpace.getUtility(bidNN) < getMyutility()) { ArrayList isues =
789 * getMutualIssues(); HashMap map = new HashMap(); Bid bid; ArrayList<Issue>
790 * dissues = utilitySpace.getDomain().getIssues(); for (int i = 0; i <
791 * isues.size(); ++i) { ArrayList keyVal = (ArrayList)isues.get(i); if (keyVal
792 * != null && dissues.get(i) instanceof IssueDiscrete) { IssueDiscrete is =
793 * (IssueDiscrete)dissues.get(i); for (int num = 0; num <
794 * is.getNumberOfValues(); ++num) { if
795 * (is.getValue(num).toString().equals(keyVal.get(0).toString())) { map.put(new
796 * Integer(i + 1), is.getValue(num)); break; }
797 *
798 * }
799 *
800 * } else if (keyVal == null && dissues.get(i) instanceof IssueDiscrete) {
801 * IssueDiscrete is = (IssueDiscrete)dissues.get(i); map.put(new Integer(i + 1),
802 * is.getValue(MyBestValue(i)));
803 *
804 * } else if (keyVal != null) { map.put(new Integer(i + 1), keyVal.get(0)); }
805 *
806 * } try { bid = new Bid(utilitySpace.getDomain(), (HashMap)map.clone()); if
807 * (utilitySpace.getUtility(bid) > getMyutility()) return bid; else return
808 * getMybestBid(utilitySpace.getMaxUtilityBid(), 0); } catch (Exception e) {
809 * System.out.println("Exception 55 " + e.getMessage()); } } else return bidNN;
810 * } catch (Exception e) { // System.out.println("Exception 121212 == " +
811 * e.getMessage()); } return null; }
812 *
813 * public ArrayList getMutualIssues() { ArrayList mutualList = new ArrayList();
814 * ArrayList<Issue> dissues = utilitySpace.getDomain().getIssues(); int twocycle
815 * = 2; while (twocycle > 0) { mutualList = new ArrayList(); for (int i = 0; i <
816 * dissues.size(); ++i) { if
817 * (oppAPreferences.getRepeatedissue().get(dissues.get(i).getName()) != null) {
818 * HashMap vals =
819 * (HashMap)oppAPreferences.getRepeatedissue().get(dissues.get(i).getName());
820 * HashMap valsB =
821 * (HashMap)oppBPreferences.getRepeatedissue().get(dissues.get(i).getName());
822 *
823 * Object[] keys = vals.keySet().toArray(); int[] max = new int[] { 0,0 };
824 * Object[] maxkey = new Object[] {null,null }; for (int j = 0; j < keys.length;
825 * ++j) { Integer temp = (Integer)vals.get(keys[j]); if (temp.intValue() >
826 * max[0]) { max[0] = temp.intValue(); maxkey[0] = keys[j]; } else if
827 * (temp.intValue() > max[1]) { max[1] = temp.intValue(); maxkey[1] = keys[j]; }
828 * } if (valsB != null) { Object[] keysB = valsB.keySet().toArray(); int[] maxB
829 * = new int[] { 0,0 }; ; Object[] maxkeyB = new Object[] { null,null}; for (int
830 * j = 0; j < keysB.length; ++j) { Integer temp = (Integer)valsB.get(keysB[j]);
831 * if (temp.intValue() > maxB[0]) { maxB[0] = temp.intValue(); maxkeyB[0] =
832 * keysB[j]; } else if (temp.intValue() > maxB[1]) { maxB[1] = temp.intValue();
833 * maxkeyB[1] = keysB[j]; } } if (twocycle == 2) { if (maxkey[0] != null &&
834 * maxkeyB[0] != null && maxkey[0].equals(maxkeyB[0])) { ArrayList l = new
835 * ArrayList(); l.add(maxkey[0]); l.add(maxB[0]); l.add(max[0]);
836 * mutualList.add(i, l); } else mutualList.add(i, null); } else { boolean
837 * insideloop=true; for(int m=0;insideloop &&m<2;++m){ for(int
838 * z=0;insideloop&&z<2;++z){ if (maxkey[m] != null && maxkeyB[z] != null &&
839 * maxkey[m].equals(maxkeyB[z])) { ArrayList l = new ArrayList();
840 * l.add(maxkey[m]); l.add(maxB[z]); l.add(max[m]); mutualList.add(i, l);
841 * insideloop=false; } } } if(insideloop) mutualList.add(i, null);
842 *
843 * } } else { mutualList.add(i, null);
844 * oppBPreferences.getRepeatedissue().put(dissues.get(i).getName(), new
845 * HashMap()); } } else {
846 * oppAPreferences.getRepeatedissue().put(dissues.get(i).getName(), new
847 * HashMap()); mutualList.add(i, null); }
848 * if(((HashMap)oppAPreferences.getRepeatedissue
849 * ().get(dissues.get(i).getName())).size()==0 ||
850 * ((HashMap)oppAPreferences.getRepeatedissue
851 * ().get(dissues.get(i).getName())).size()==0){ twocycle--; } } if (twocycle !=
852 * 0) { twocycle--; if (opponentAB.size() == 0) { float nullval = 0.0f; for (int
853 * i = 0; i < mutualList.size(); ++i) { if (mutualList.get(i) != null) {
854 * ++nullval; } }
855 *
856 * nullval = nullval / mutualList.size(); if (nullval >= 0.5) twocycle--; }else
857 * twocycle--; } } return mutualList; }
858 *
859 * public Bid getNNBid(ArrayList<BidUtility> oppAB) { ArrayList<Issue> dissues =
860 * utilitySpace.getDomain().getIssues(); Bid maxBid = null; double maxutility =
861 * 0d; int size = 0; int exloop = 0; Bid newBid; while (exloop < dissues.size())
862 * { int bi = chooseBestIssue(); size = 0; while (oppAB != null && oppAB.size()
863 * > size) { Bid b = oppAB.get(size).getBid(); newBid = new Bid(b); try {
864 * HashMap vals = b.getValues(); vals.put(bi, getRandomValue(dissues.get(bi -
865 * 1))); newBid = new Bid(utilitySpace.getDomain(), vals); if
866 * (utilitySpace.getUtility(newBid) > getMyutility() &&
867 * utilitySpace.getUtility(newBid) > maxutility) { maxBid = new Bid(newBid);
868 * maxutility = utilitySpace.getUtility(maxBid); }
869 *
870 * } catch (Exception e) { System.out.println("Exception 66 " + e.getMessage());
871 * } size++; } exloop++; } return maxBid; }
872 *
873 *
874 * public int chooseBestIssue() { double random = Math.random(); double
875 * sumWeight = 0d;
876 *
877 * for (int i = utilitySpace.getDomain().getIssues().size(); i > 0; --i) {
878 * sumWeight += utilitySpace.getWeight(i); if (sumWeight > random) return i; }
879 * return 0; }
880 *
881 * public int chooseWorstIssue() { double random = Math.random() * 100; double
882 * sumWeight = 0d; int minin = 1; double min = 1.0d; for (int i =
883 * utilitySpace.getDomain().getIssues().size(); i > 0; --i) { sumWeight += 1.0d
884 * / utilitySpace.getWeight(i); if (utilitySpace.getWeight(i) < min) { min =
885 * utilitySpace.getWeight(i); minin = i; } if (sumWeight > random) return i; }
886 * return minin; }
887 *
888 * public Bid getMybestBid(Bid sugestBid, int time) { ArrayList<Issue> dissues =
889 * utilitySpace.getDomain().getIssues(); Bid newBid = new Bid(sugestBid); int
890 * index = chooseWorstIssue(); boolean loop = true; while (loop) { newBid = new
891 * Bid(sugestBid); try { HashMap map = newBid.getValues(); map.put(index,
892 * getRandomValue(dissues.get(index - 1))); newBid = new
893 * Bid(utilitySpace.getDomain(), map); if (utilitySpace.getUtility(newBid) >
894 * getMyutility()) { return newBid; } } catch (Exception e) { //
895 * System.out.println("Exception in my best bid : " + // e.getMessage()); loop =
896 * false; } } return newBid; }
897 *
898 *
899 * public void addBidToList(ArrayList<BidUtility> mybids, BidUtility newbid) {
900 * int index = mybids.size(); for (int i = 0; i < mybids.size(); ++i) { if
901 * (mybids.get(i).getUtility() <= newbid.getUtility()) { if
902 * (!mybids.get(i).getBid().equals(newbid.getBid())) index = i; else return; } }
903 * mybids.add(index, newbid);
904 *
905 * }
906 *
907 *
908 * public void calculateParamForOpponent(OpponentPreferences op, Bid bid) {
909 * ArrayList<Issue> dissues = utilitySpace.getDomain().getIssues(); HashMap
910 * bidVal = bid.getValues(); Object[] keys = bidVal.keySet().toArray();
911 *
912 * for (int i = 0; i < dissues.size(); ++i) { if
913 * (op.getRepeatedissue().get(dissues.get(i).getName()) != null) {
914 *
915 * HashMap vals = (HashMap)op.getRepeatedissue().get(dissues.get(i).getName());
916 *
917 * try { if (vals.get(bidVal.get(keys[i])) != null) { Integer repet =
918 * (Integer)vals.get(bidVal.get(keys[i])); repet = repet + 1;
919 * vals.put(bidVal.get(keys[i]), repet); } else { vals.put(bidVal.get(keys[i]),
920 * new Integer(1)); } } catch (Exception e) { //
921 * System.out.println("Exception 88 " + e.getMessage()); } } else { HashMap h =
922 * new HashMap(); // op.getRepeatedissue().get(dissues.get(i).getName()); try {
923 *
924 * h.put(bidVal.get(keys[i]), new Integer(1)); } catch (Exception e) { //
925 * System.out.println("Exception 99 " + e.getMessage()); }
926 * op.getRepeatedissue().put(dissues.get(i).getName(), h); } }
927 *
928 * }
929 *
930 * public void setMyutility(double myutility) { this.myutility = myutility; }
931 *
932 * public double getMyutility() { myutility = getTargetUtility(); if (myutility
933 * < 0.7) return 0.7d; return myutility; }
934 *
935 * public double getE() { if (withDiscount) return 0.25d; return 0.19d; }
936 *
937 * private class OpponentPreferences { private HashMap repeatedissue = new
938 * HashMap(); private ArrayList selectedValues; ArrayList<BidUtility>
939 * opponentBids = new ArrayList<BidUtility>();
940 *
941 * public void setRepeatedissue(HashMap repeatedissue) { this.repeatedissue =
942 * repeatedissue; }
943 *
944 * public HashMap getRepeatedissue() { return repeatedissue; }
945 *
946 * public void setSelectedValues(ArrayList selectedValues) { this.selectedValues
947 * = selectedValues; }
948 *
949 * public ArrayList getSelectedValues() { return selectedValues; }
950 *
951 * public void setOpponentBids(ArrayList<ParsAgent.BidUtility> opponentBids) {
952 * this.opponentBids = opponentBids; }
953 *
954 * public ArrayList<ParsAgent.BidUtility> getOpponentBids() { return
955 * opponentBids; } }
956 *
957 * private class BidUtility { private Bid bid; private double utility; private
958 * long time;
959 *
960 * BidUtility(Bid b, double u, long t) { this.bid = b; this.utility = u;
961 * this.time = t; }
962 *
963 * BidUtility(BidUtility newbid) { this.bid = newbid.getBid(); this.utility =
964 * newbid.getUtility(); this.time = newbid.getTime(); }
965 *
966 * public void setBid(Bid bid) { this.bid = bid; }
967 *
968 * public Bid getBid() { return bid; }
969 *
970 * public void setUtility(double utility) { this.utility = utility; }
971 *
972 * public double getUtility() { return utility; }
973 *
974 * public void setTime(long time) { this.time = time; }
975 *
976 * public long getTime() { return time; } }
977 *
978 * }
979 */
Note: See TracBrowser for help on using the repository browser.