source: src/main/java/agents/anac/y2015/Mercury/Mercury.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: 39.8 KB
Line 
1package agents.anac.y2015.Mercury;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6import java.util.Random;
7
8import genius.core.AgentID;
9import genius.core.Bid;
10import genius.core.BidIterator;
11import genius.core.actions.Accept;
12import genius.core.actions.Action;
13import genius.core.actions.EndNegotiation;
14import genius.core.actions.Offer;
15import genius.core.issue.Issue;
16import genius.core.issue.IssueDiscrete;
17import genius.core.issue.IssueInteger;
18import genius.core.issue.IssueReal;
19import genius.core.issue.Value;
20import genius.core.issue.ValueInteger;
21import genius.core.issue.ValueReal;
22import genius.core.parties.AbstractNegotiationParty;
23import genius.core.parties.NegotiationInfo;
24import genius.core.timeline.Timeline;
25import genius.core.utility.AdditiveUtilitySpace;
26
27public class Mercury extends AbstractNegotiationParty {
28
29 private double totalTime;
30 private Action ActionOfOpponent = null;
31 private Object party = null;
32 private Object myparty = null;
33 private double maximumOfBid;
34 private OwnBidHistory ownBidHistory;
35 private OpponentBidHistory opponentBidHistory;
36 private double minimumUtilityThreshold;
37 private double utilitythreshold;
38 private double MaximumUtility;
39 private double timeLeftBefore;
40 private double timeLeftAfter;
41 private double maximumTimeOfOpponent;
42 private double maximumTimeOfOwn;
43 private double discountingFactor;
44 private double concedeToDiscountingFactor;
45 private double concedeToDiscountingFactor_original;
46 private double minConcedeToDiscountingFactor;
47 private ArrayList<ArrayList<Bid>> bidsBetweenUtility;
48 private boolean concedeToOpponent;
49 private boolean toughAgent; // if we propose a bid that was proposed by the
50 // opponnet, then it should be accepted.
51 private double alpha1;// the larger alpha is, the more tough the agent is.
52 private Bid bid_maximum_utility;// the bid with the maximum utility over the
53 // utility space.
54 private double reservationValue;
55 private Random random;
56 private Bid maxBid = null;
57 private int myRounds = 0;
58
59 private int numberOfParty = -1;
60 private double acceConThres = 0.9; // when obtainning an acceptance signal,
61 // try to seal the agreement with
62 // utility discount on it own
63 // private double miniLevel = 0;
64 private ArrayList<Object> myParOrder = new ArrayList<Object>();
65 private boolean halfSucc = false; // if true, our offer has been accepted by
66 // one agent
67
68 // private double succDis = 0.9;
69
70 @Override
71 public void init(NegotiationInfo info) {
72 super.init(info);
73
74 myparty = getPartyId();
75
76 try {
77 random = new Random();
78 maximumOfBid = this.utilitySpace.getDomain()
79 .getNumberOfPossibleBids();
80 ownBidHistory = new OwnBidHistory();
81 opponentBidHistory = new OpponentBidHistory();
82 bidsBetweenUtility = new ArrayList<ArrayList<Bid>>();
83 this.bid_maximum_utility = utilitySpace.getMaxUtilityBid();
84 this.utilitythreshold = utilitySpace
85 .getUtility(bid_maximum_utility); // initial
86 // utility
87 // threshold
88 this.MaximumUtility = this.utilitythreshold;
89 this.timeLeftAfter = 0;
90 this.timeLeftBefore = 0;
91 this.totalTime = timeline.getTotalTime();
92 this.maximumTimeOfOpponent = 0;
93 this.maximumTimeOfOwn = 0;
94 this.minConcedeToDiscountingFactor = 0.08;// 0.1;
95 this.discountingFactor = 1;
96 if (utilitySpace.getDiscountFactor() <= 1D
97 && utilitySpace.getDiscountFactor() > 0D) {
98 this.discountingFactor = utilitySpace.getDiscountFactor();
99 }
100 this.chooseUtilityThreshold();
101 this.calculateBidsBetweenUtility();
102 this.chooseConcedeToDiscountingDegree();
103 this.opponentBidHistory
104 .initializeDataStructures(utilitySpace.getDomain());
105 this.timeLeftAfter = timeline.getCurrentTime();
106 this.concedeToOpponent = false;
107 this.toughAgent = false;
108 this.alpha1 = 2;
109 this.reservationValue = 0;
110 if (utilitySpace.getReservationValue() > 0) {
111 this.reservationValue = utilitySpace.getReservationValue();
112 }
113
114 maxBid = utilitySpace.getMaxUtilityBid();
115 } catch (Exception e) {
116 System.out.println("initialization error" + e.getMessage());
117 }
118 }
119
120 @Override
121 public void receiveMessage(AgentID sender, Action opponentAction) {
122 super.receiveMessage(sender, opponentAction);
123
124 this.ActionOfOpponent = opponentAction;
125 this.party = sender;
126
127 // System.out.println("party id is" +myparty+","+sender);
128
129 if (numberOfParty == -1) {
130 numberOfParty = getNumberOfParties();
131 opponentBidHistory.initiPartyRec(numberOfParty,
132 (AdditiveUtilitySpace) this.utilitySpace);
133 // System.out.println("first informed
134 // action"+opponentAction.toString());
135 } else if (opponentAction instanceof Offer) {
136
137 if (myParOrder.size() < numberOfParty)
138 myParOrder.add(sender);
139
140 this.opponentBidHistory.updateOpponentModel(
141 ((Offer) ActionOfOpponent).getBid(), sender,
142 utilitySpace.getDomain(),
143 (AdditiveUtilitySpace) this.utilitySpace);
144
145 this.opponentBidHistory
146 .updateOppRec(((Offer) ActionOfOpponent).getBid(), sender);
147 // System.out.println("following informed
148 // action"+opponentAction.toString());
149
150 } else if (opponentAction instanceof Accept) {
151 //
152 // System.out.println("current informed action is
153 // accept"+opponentAction.toString());
154 // System.out.println("current informed action is
155 // accept"+myParOrder.indexOf(sender)+","+myParOrder.indexOf(myparty));
156 if (myParOrder.size() < numberOfParty)
157 myParOrder.add(sender);
158
159 if ((myParOrder.indexOf(sender) + 1) % numberOfParty == myParOrder
160 .indexOf(myparty)
161 || (myParOrder.indexOf(sender) - 1)
162 % numberOfParty == myParOrder.indexOf(myparty)) {
163
164 halfSucc = true; // true
165 System.out.println("Half success action caused by " + sender
166 + "," + opponentAction.toString());
167
168 this.opponentBidHistory.updateOpponentModel(
169 ownBidHistory.getLastBid(), sender,
170 utilitySpace.getDomain(),
171 (AdditiveUtilitySpace) this.utilitySpace);
172
173 this.opponentBidHistory.updateAccRec(ownBidHistory.getLastBid(),
174 sender);
175
176 } else {
177 // only works when agents are less than 3!
178 this.opponentBidHistory.updateOpponentModel(
179 opponentBidHistory.getLastOppBid(), sender,
180 utilitySpace.getDomain(),
181 (AdditiveUtilitySpace) this.utilitySpace);
182
183 this.opponentBidHistory.updateAccRec(
184 opponentBidHistory.getLastOppBid(), sender);
185 }
186
187 } else {
188 //
189 if (myParOrder.size() < numberOfParty)
190 myParOrder.add(sender);
191
192 System.out.println("Unexpected action from " + sender + ","
193 + opponentAction.toString());
194 }
195
196 }
197
198 @Override
199 public Action chooseAction(List<Class<? extends Action>> validActions) {
200 Action action = null;
201 Bid bid = null;
202 myRounds++;
203
204 // System.out.println("halfSucces " +
205 // halfSucc+","+myRounds+","+this.estimateRoundLeft(false)+","+estimateRoundLeft(true));
206
207 if (myParOrder.size() < numberOfParty)
208 myParOrder.add(myparty);
209
210 try {
211 // System.out.println("i propose " + debug + " bid at time " +
212 // timeline.getTime());
213 this.timeLeftBefore = timeline.getCurrentTime();
214
215 // we propose first and propose the bid with maximum utility
216 if (!validActions.contains(Accept.class)) {
217 bid = this.bid_maximum_utility;
218 action = new Offer(getPartyId(), bid);
219 } else if (ActionOfOpponent instanceof Offer) {// the opponent
220 // propose first and
221 // we response
222 // secondly
223 // update opponent model first
224 // this.opponentBidHistory.updateOpponentModel(
225 // ((Offer) ActionOfOpponent).getBid(),
226 // utilitySpace.getDomain(), this.utilitySpace);
227
228 this.updateConcedeDegree();
229 // update the estimation
230 if (myRounds <= 30) {
231 // bid = utilitySpace.getMaxUtilityBid();
232 bid = this.bid_maximum_utility;
233
234 action = new Offer(getPartyId(), bid);
235 } else {// other conditions
236 if (timeline.getTime() < 0.97) {// still have some rounds
237 // left to further
238 // negotiate (the major
239 // negotiation period)
240 if (!this.halfSucc) {
241 bid = BidToOffer();
242 } else {
243 bid = NiceBidtoOffer();
244 }
245
246 Boolean IsAccept = AcceptOpponentOffer(
247 ((Offer) ActionOfOpponent).getBid(), bid);
248 Boolean IsTerminate = TerminateCurrentNegotiation(bid);
249 if (IsAccept && !IsTerminate) {
250 action = new Accept(getPartyId(),
251 this.opponentBidHistory.getLastOppBid());
252 } else if (IsTerminate && !IsAccept) {
253 action = new EndNegotiation(getPartyId());
254 // action = new Offer(maxBid);
255 } else if (IsAccept && IsTerminate) {
256 if (this.utilitySpace
257 .getUtility(((Offer) ActionOfOpponent)
258 .getBid()) > this.reservationValue) {
259 action = new Accept(getPartyId(),
260 this.opponentBidHistory
261 .getLastOppBid());
262 } else {
263 action = new EndNegotiation(getPartyId());
264 // action = new Offer(maxBid);
265 }
266 } else {
267 // we expect that the negotiation is over once we
268 // select a bid from the opponent's history.
269 if (this.concedeToOpponent == true) {
270 // bid =
271 // opponentBidHistory.chooseBestFromHistory(this.utilitySpace);
272 bid = opponentBidHistory.getBestBidInHistory();
273
274 action = new Offer(getPartyId(), bid);
275 this.toughAgent = true;
276 this.concedeToOpponent = false;
277 } else {
278 action = new Offer(getPartyId(), bid);
279 this.toughAgent = false;
280 }
281 }
282 } else {// this is the last chance and we concede by
283 // providing the opponent the best offer he ever
284 // proposed to us
285 // in this case, it corresponds to an opponent whose
286 // decision time is short
287 if (timeline.getTime() > 0.995) {
288 // bid =
289 // opponentBidHistory.chooseBestFromHistory(this.utilitySpace);
290
291 if (!this.halfSucc) {
292 bid = opponentBidHistory.getBestBidInHistory();
293 } else {
294 bid = NiceBidtoOffer();
295 }
296
297 // this is specially designed to avoid that we got
298 // very low utility by searching between an
299 // acceptable range (when the domain is small)
300 if (this.utilitySpace.getUtility(bid) < 0.75) {
301 List<Bid> candidateBids = this
302 .getBidsBetweenUtility(
303 this.MaximumUtility - 0.15,
304 this.MaximumUtility - 0.02);
305 // if the candidate bids do not exsit and also
306 // the deadline is approaching in next round, we
307 // concede.
308 // if (candidateBids.size() == 1 &&
309 // timeline.getTime()>0.9998) {
310 // we have no chance to make a new proposal
311 // before the deadline
312 if (timeline.getTime() > 0.9999) {
313 // bid =
314 // opponentBidHistory.getMiniBestOpp(); //
315 // more concessive
316
317 if (this.utilitySpace
318 .getUtility(opponentBidHistory
319 .getMiniBestOpp()) > 0.5) {
320 bid = opponentBidHistory
321 .getMiniBestOpp(); // more
322 // concessive
323 } else if (this.utilitySpace
324 .getUtility(opponentBidHistory
325 .getBestBidInHistory()) > 0.5) {
326 bid = opponentBidHistory
327 .getBestBidInHistory();
328 } else {
329 bid = ownBidHistory.getLastBid();
330 }
331
332 } else {
333 bid = opponentBidHistory.ChooseBid(
334 candidateBids,
335 this.utilitySpace.getDomain());
336 }
337 if (bid == null) {
338 // bid = opponentBidHistory
339 // .getBestBidInHistory();
340 bid = ownBidHistory.getLastBid();
341 }
342 }
343 Boolean IsAccept = AcceptOpponentOffer(
344 ((Offer) ActionOfOpponent).getBid(), bid);
345 Boolean IsTerminate = TerminateCurrentNegotiation(
346 bid);
347 if (IsAccept && !IsTerminate) {
348 action = new Accept(getPartyId(),
349 this.opponentBidHistory
350 .getLastOppBid());
351 } else if (IsTerminate && !IsAccept) {
352 action = new EndNegotiation(getPartyId());
353 // action = new Offer(maxBid);
354 } else if (IsTerminate && IsAccept) {
355 if (this.utilitySpace
356 .getUtility(((Offer) ActionOfOpponent)
357 .getBid()) > this.reservationValue) {
358 action = new Accept(getPartyId(),
359 this.opponentBidHistory
360 .getLastOppBid());
361 } else {
362 action = new EndNegotiation(getPartyId());
363 // action = new Offer(maxBid);
364 }
365 } else {
366 if (this.toughAgent == true) {
367 action = new Accept(getPartyId(),
368 this.opponentBidHistory
369 .getLastOppBid());
370 } else {
371 action = new Offer(getPartyId(), bid);
372 }
373 }
374 // in this case, it corresponds to the situation
375 // that we encounter an opponent who needs more
376 // computation to make decision each round
377 } else {// we still have some time to negotiate,
378 // and be tough by sticking with the lowest one in
379 // previous offer history.
380 // we also have to make the decisin fast to avoid
381 // reaching the deadline before the decision is made
382 // bid = ownBidHistory.GetMinBidInHistory();//reduce
383 // the computational cost
384 if (!this.halfSucc) {
385 bid = opponentBidHistory.getBestBidInHistory();
386 } else {
387 bid = NiceBidtoOffer();
388 }
389
390 // System.out.println("test----------------------------------------------------------"
391 // + timeline.getTime());
392 Boolean IsAccept = AcceptOpponentOffer(
393 ((Offer) ActionOfOpponent).getBid(), bid);
394 Boolean IsTerminate = TerminateCurrentNegotiation(
395 bid);
396
397 if (IsAccept && !IsTerminate) {
398 action = new Accept(getPartyId(),
399 this.opponentBidHistory
400 .getLastOppBid());
401 } else if (IsTerminate && !IsAccept) {
402 action = new EndNegotiation(getPartyId());
403 // action = new Offer(maxBid);
404 } else if (IsAccept && IsTerminate) {
405 if (this.utilitySpace
406 .getUtility(((Offer) ActionOfOpponent)
407 .getBid()) > this.reservationValue) {
408 action = new Accept(getPartyId(),
409 this.opponentBidHistory
410 .getLastOppBid());
411 } else {
412 action = new EndNegotiation(getPartyId());
413 // action = new Offer(maxBid);
414 }
415 } else {
416 action = new Offer(getPartyId(), bid);
417
418 }
419 }
420 }
421 }
422
423 if (myRounds > 2) {
424 if (ownBidHistory.isInsideMyBids(
425 ((Offer) ActionOfOpponent).getBid()))
426 action = new Accept(getPartyId(),
427 this.opponentBidHistory.getLastOppBid());
428 }
429
430 } else if (ActionOfOpponent instanceof Accept) {
431
432 // bid = opponentBidHistory.getLastAcce();
433
434 /*
435 * try{
436 *
437 * if ( this.utilitythreshold*acceConThres <=
438 * this.utilitySpace.getUtility
439 * (opponentBidHistory.getLastOppBid()) ){ System.out.println(
440 * "accept an offer after seeing an acceptance action with a not bad result"
441 * +ActionOfOpponent.getAgent()); action = new Accept(); }else{
442 *
443 * try{
444 *
445 * this.utilitythreshold =
446 * utilitySpace.getUtility(ownBidHistory.getLastBid()); double
447 * max = this.utilitythreshold * 1.2; if(max >
448 * this.MaximumUtility) max = this.MaximumUtility; //double min
449 * = (this.utilitythreshold -
450 * utilitySpace.getUtility(ownBidHistory.getLastBid())) +
451 * utilitySpace.getUtility(ownBidHistory.getLastBid()); double
452 * min = this.utilitythreshold * 0.95 * timeline.getTime();
453 * //System.out.println("test in method NiceBidtoOffer"+this.
454 * utilitythreshold+","+max+","+min); if (min < 0.5){ min = 0.5;
455 * if(min > max) max = min; }
456 *
457 * List<Bid> candidateBids = getBidsBetweenUtility(min, max);
458 *
459 * Random random = new Random();
460 *
461 * int s =0;
462 *
463 * try{ s = random.nextInt(candidateBids.size()); }catch
464 * (Exception e){ s = 0; }
465 *
466 * bid = candidateBids.get(s);
467 *
468 * }catch (Exception e){ System.out.println("error - 1"); bid =
469 * ownBidHistory.getLastBid(); }
470 *
471 *
472 * } //action = new Offer(ownBidHistory.getLastBid()); }catch
473 * (Exception ee) { action = new Accept(); }
474 */
475
476 try {
477
478 if (this.utilitythreshold
479 * acceConThres <= this.utilitySpace.getUtility(
480 opponentBidHistory.getLastOppBid())) {
481 System.out.println(
482 "accept an offer after seeing an acceptance action with a not bad result"
483 + ActionOfOpponent.getAgent());
484 action = new Accept(getPartyId(),
485 this.opponentBidHistory.getLastOppBid());
486 } else {
487 bid = BidToOffer();
488
489 Boolean IsAccept = AcceptOpponentOffer(
490 ((Offer) ActionOfOpponent).getBid(), bid);
491 Boolean IsTerminate = TerminateCurrentNegotiation(bid);
492
493 if (IsAccept && !IsTerminate) {
494 action = new Accept(getPartyId(),
495 this.opponentBidHistory.getLastOppBid());
496 } else if (IsTerminate && !IsAccept) {
497 action = new EndNegotiation(getPartyId());
498 // action = new Offer(maxBid);
499 } else if (IsAccept && IsTerminate) {
500 if (this.utilitySpace
501 .getUtility(((Offer) ActionOfOpponent)
502 .getBid()) > this.reservationValue) {
503 action = new Accept(getPartyId(),
504 this.opponentBidHistory
505 .getLastOppBid());
506 } else {
507 action = new EndNegotiation(getPartyId());
508 // action = new Offer(maxBid);
509 }
510 } else {
511 action = new Offer(getPartyId(), bid);
512 }
513
514 }
515
516 } catch (Exception e) {
517
518 }
519
520 }
521 // System.out.println("i propose " + debug + " bid at time " +
522 // timeline.getTime());
523
524 // System.out.println("exception number - 3, Ares!"+bid);
525
526 // if (bid != null && !ownBidHistory.isInsideMyBids(bid))
527 // this.ownBidHistory.addBid(bid, utilitySpace);
528
529 if (bid != null)
530 this.ownBidHistory.addBid(bid,
531 (AdditiveUtilitySpace) utilitySpace);
532
533 this.timeLeftAfter = timeline.getCurrentTime();
534 this.estimateRoundLeft(false);// update the estimation
535 } catch (Exception e) {
536 System.out.println("Exception in ChooseAction:" + e.getMessage());
537 System.out.println(estimateRoundLeft(false));
538 // action = new Accept(getAgentID()); // accept if anything goes
539 if (ActionOfOpponent instanceof Accept) {
540 action = new Accept(getPartyId(),
541 this.opponentBidHistory.getLastOppBid());
542 } else {
543 System.out.println("handle exception - 1");
544 // action = new EndNegotiation();
545 }
546 // action = new EndNegotiation(); // terminate if anything
547 // action = new Offer(maxBid); // goes wrong.
548 }
549
550 if (action == null)
551 action = new Offer(getPartyId(), maxBid);
552
553 if (this.discountingFactor <= 0.5 && this.reservationValue >= 0.45
554 && timeline.getTime() > 0.15)
555 action = new EndNegotiation(getPartyId());
556
557 if (timeline.getCurrentTime() > timeline.getTotalTime() * 1.1) {
558 System.out.println("exception in negotiation time for Ares!"
559 + timeline.getCurrentTime() + ","
560 + timeline.getTotalTime());
561 action = new EndNegotiation(getPartyId());
562 // return action;
563 }
564
565 /*
566 * if (ownBidHistory.numOfBidsProposed() > 30){
567 *
568 * System.out.println("exception number - 2, Ares!"+opponentBidHistory.
569 * partyOrder .size()+","+opponentBidHistory.partyOrder.get(0)+","+
570 * opponentBidHistory .partyOrder.get(1));
571 *
572 * try{ System.out.println("max opp utili, Ares!"+this.utilitySpace.
573 * getUtility (this.opponentBidHistory.getBestBidInHistory())+","
574 * +this.utilitySpace
575 * .getUtility(this.opponentBidHistory.getBestOpp(this.
576 * opponentBidHistory.partyOrder.get(0)))+","
577 * +this.utilitySpace.getUtility
578 * (this.opponentBidHistory.getBestOpp(this.
579 * opponentBidHistory.partyOrder.get(1)))); } catch (Exception e){
580 * System.out.println("exception number - 1, Ares!"); } }
581 */
582
583 return action;
584 }
585
586 /*
587 * principle: randomization over those candidate bids to let the opponent
588 * have a better model of my utility profile return the bid to be offered in
589 * the next round
590 */
591 private Bid BidToOffer() {
592 Bid bidReturned = null;
593 double decreasingAmount_1 = 0.09; // 0.05
594 double decreasingAmount_2 = 0.25;
595 try {
596
597 double maximumOfBid = this.MaximumUtility;// utilitySpace.getUtility(utilitySpace.getMaxUtilityBid());
598 double minimumOfBid = this.MaximumUtility;
599 // used when the domain is very large.
600 // make concession when the domin is large
601 if (this.discountingFactor == 1 && this.maximumOfBid > 3200) {
602 // minimumOfBid = this.MaximumUtility - decreasingAmount_1;
603
604 /*
605 * if (timeline.getTime() <= 0.68){ minimumOfBid =
606 * this.MaximumUtility * (1 - decreasingAmount_1 *
607 * Math.pow(timeline.getTime(),3)); }else if(timeline.getTime()
608 * > 0.68 && timeline.getTime() <= 0.81 ){ minimumOfBid =
609 * this.MaximumUtility * (1 -
610 * decreasingAmount_1*1.3*Math.pow(timeline.getTime(),2)); }else
611 * if (timeline.getTime() > 0.81 ){ minimumOfBid =
612 * this.MaximumUtility * (1 -
613 * decreasingAmount_1*(0.4+timeline.getTime())); }
614 */
615
616 minimumOfBid = this.MaximumUtility * 0.94
617 + 0.06 * this.MaximumUtility
618 * (1 - Math.pow(timeline.getTime(), 3));
619
620 // make further concession when the deadline is approaching and
621 // the domain is large
622 if (this.discountingFactor > 1 - decreasingAmount_2
623 && this.maximumOfBid > 10000
624 && timeline.getTime() >= 0.98) {
625 minimumOfBid = this.MaximumUtility - decreasingAmount_2;
626 }
627 if (this.utilitythreshold > minimumOfBid) {
628 this.utilitythreshold = minimumOfBid;
629 }
630 } /*
631 * else if (this.discountingFactor > 1 - decreasingAmount_3 &&
632 * this.maximumOfBid >= 100000 && this.maximumOfBid < 300000) {
633 * minimumOfBid = this.MaximumUtility - decreasingAmount_3; }
634 * else if (this.discountingFactor > 1 - decreasingAmount_4 &&
635 * this.maximumOfBid >= 300000) { minimumOfBid =
636 * this.MaximumUtility - decreasingAmount_4; }
637 */else {// the general case
638 if (timeline.getTime() <= this.concedeToDiscountingFactor) {
639 double minThreshold = (maximumOfBid
640 * this.discountingFactor)
641 / Math.pow(this.discountingFactor,
642 this.concedeToDiscountingFactor);
643 this.utilitythreshold = maximumOfBid
644 - (maximumOfBid - minThreshold) * Math.pow(
645 (timeline.getTime()
646 / this.concedeToDiscountingFactor),
647 alpha1);
648 } else {
649 this.utilitythreshold = (maximumOfBid
650 * this.discountingFactor)
651 / Math.pow(this.discountingFactor,
652 timeline.getTime());
653 }
654 minimumOfBid = this.utilitythreshold;
655 }
656
657 /*
658 * if(minimumOfBid < 0.9 && this.guessOpponentType == false){
659 * if(this.opponentBidHistory.getSize() <= 2){ this.opponentType =
660 * 1;//tough opponent alpha1 = 2; } else{ this.opponentType = 0;
661 * alpha1 = 4; } this.guessOpponentType = true;//we only guess the
662 * opponent type once here System.out.println("we guess the opponent
663 * type is "+this.opponentType); }
664 */
665
666 // choose from the opponent bid history first to reduce calculation
667 // time
668
669 /*
670 * if (halfSucc){ minimumOfBid = minimumOfBid * succDis *
671 * timeline.getTime(); this.utilitythreshold = this.utilitythreshold
672 * * succDis * timeline.getTime(); }
673 */
674
675 // Bid bestBidOfferedByOpponent =
676 // opponentBidHistory.getBestBidInHistory();
677 /*
678 * if (halfSucc){
679 * if(utilitySpace.getUtility(opponentBidHistory.getMiniBestOpp()) >
680 * 0.51){ minimumOfBid =
681 * utilitySpace.getUtility(opponentBidHistory.getMiniBestOpp
682 * ())*(2-Math.pow(timeline.getTime(),2.5)); }else{ minimumOfBid =
683 * 0.51*(2-Math.pow(timeline.getTime(),2.5)); }
684 *
685 * maximumOfBid =
686 * utilitySpace.getUtility(opponentBidHistory.getLastAcce());
687 *
688 * if (maximumOfBid < minimumOfBid){ maximumOfBid =
689 * minimumOfBid*1.05; } }
690 */
691
692 Bid bestBidOfferedByOpponent = opponentBidHistory.getMiniBestOpp();
693
694 if (utilitySpace.getUtility(
695 bestBidOfferedByOpponent) >= this.utilitythreshold
696 || utilitySpace.getUtility(
697 bestBidOfferedByOpponent) >= minimumOfBid) {
698 return bestBidOfferedByOpponent;
699 }
700 List<Bid> candidateBids = this.getBidsBetweenUtility(minimumOfBid,
701 maximumOfBid);
702
703 bidReturned = opponentBidHistory.ChooseBid(candidateBids,
704 this.utilitySpace.getDomain());
705
706 if (bidReturned == null) {
707 System.out.println("no bids can be found.");
708 bidReturned = this.utilitySpace.getMaxUtilityBid();
709 }
710 } catch (Exception e) {
711 System.out
712 .println(e.getMessage() + "exception in method BidToOffer");
713 }
714 // System.out.println("the current threshold is " +
715 // this.utilitythreshold + " with the value of alpha1 is " + alpha1);
716 return bidReturned;
717 }
718
719 private Bid NiceBidtoOffer() {
720 Bid bid = null;
721
722 try {
723
724 // this.utilitythreshold =
725 // utilitySpace.getUtility(opponentBidHistory.getLastAcce());
726 double max = this.utilitythreshold * 1.05;
727 // double min =
728 // utilitySpace.getUtility(opponentBidHistory.getMiniBestOpp());
729 double min = this.utilitythreshold * 0.95 * timeline.getTime();
730 // System.out.println("test in method
731 // NiceBidtoOffer"+this.utilitythreshold+","+max+","+min);
732
733 if (min < 0.5) {
734 min = 0.5;
735 }
736 if (max < 0.5)
737 max = 0.5;
738
739 bid = genRanBid(min, max);
740
741 } catch (Exception e) {
742 System.out.println("error in method NiceBidtoOffer");
743 bid = ownBidHistory.getLastBid();
744 }
745
746 return bid;
747 }
748
749 /*
750 * decide whether to accept the current offer or not
751 */
752 private boolean AcceptOpponentOffer(Bid opponentBid, Bid ownBid) {
753 double currentUtility = 0;
754 double nextRoundUtility = 0;
755 double maximumUtility = 0;
756 this.concedeToOpponent = false;
757 try {
758 currentUtility = this.utilitySpace.getUtility(opponentBid);
759 maximumUtility = this.MaximumUtility;// utilitySpace.getUtility(utilitySpace.getMaxUtilityBid());
760 } catch (Exception e) {
761 System.out.println(e.getMessage()
762 + "Exception in method AcceptOpponentOffer part 1");
763 }
764 try {
765 nextRoundUtility = this.utilitySpace.getUtility(ownBid);
766 } catch (Exception e) {
767 System.out.println(e.getMessage()
768 + "Exception in method AcceptOpponentOffer part 2");
769 }
770 // System.out.println(this.utilitythreshold +"at time "+
771 // timeline.getTime());
772 if (currentUtility >= this.utilitythreshold
773 || currentUtility >= nextRoundUtility) {
774 return true;
775 } else {
776 // if the current utility with discount is larger than the predicted
777 // maximum utility with discount
778 // then accept it.
779 double predictMaximumUtility = maximumUtility
780 * this.discountingFactor;
781 // double currentMaximumUtility =
782 // this.utilitySpace.getUtilityWithDiscount(opponentBidHistory.chooseBestFromHistory(utilitySpace),
783 // timeline);
784 double currentMaximumUtility = this.utilitySpace
785 .getUtilityWithDiscount(
786 opponentBidHistory.getBestBidInHistory(), timeline);
787 if (currentMaximumUtility > predictMaximumUtility
788 && timeline.getTime() > this.concedeToDiscountingFactor) {
789 try {
790 // if the current offer is approximately as good as the best
791 // one in the history, then accept it.
792 if (utilitySpace
793 .getUtility(opponentBid) >= utilitySpace.getUtility(
794 opponentBidHistory.getBestBidInHistory())
795 - 0.01) {
796 return true;
797 } else {
798 this.concedeToOpponent = true;
799 return false;
800 }
801 } catch (Exception e) {
802 System.out
803 .println("exception in Method AcceptOpponentOffer");
804 return true;
805 }
806 // retrieve the opponent's biding history and utilize it
807 } else if (currentMaximumUtility > this.utilitythreshold
808 * Math.pow(this.discountingFactor, timeline.getTime())) {
809 try {
810 // if the current offer is approximately as good as the best
811 // one in the history, then accept it.
812 if (utilitySpace
813 .getUtility(opponentBid) >= utilitySpace.getUtility(
814 opponentBidHistory.getBestBidInHistory())
815 - 0.01) {
816 return true;
817 } else {
818 System.out.println(
819 "test" + utilitySpace.getUtility(opponentBid)
820 + this.utilitythreshold);
821 this.concedeToOpponent = true;
822 return false;
823 }
824 } catch (Exception e) {
825 System.out
826 .println("exception in Method AcceptOpponentOffer");
827 return true;
828 }
829 } else {
830 return false;
831 }
832 }
833 }
834
835 /*
836 * decide whether or not to terminate now
837 */
838 private boolean TerminateCurrentNegotiation(Bid ownBid) {
839 double currentUtility = 0;
840 double nextRoundUtility = 0;
841 double maximumUtility = 0;
842 this.concedeToOpponent = false;
843 try {
844 currentUtility = this.reservationValue;
845 nextRoundUtility = this.utilitySpace.getUtility(ownBid);
846 maximumUtility = this.MaximumUtility;
847 } catch (Exception e) {
848 System.out.println(e.getMessage()
849 + "Exception in method TerminateCurrentNegotiation part 1");
850 }
851
852 if (currentUtility >= this.utilitythreshold
853 || currentUtility >= nextRoundUtility) {
854 return true;
855 } else {
856 // if the current reseravation utility with discount is larger than
857 // the predicted maximum utility with discount
858 // then terminate the negotiation.
859 double predictMaximumUtility = maximumUtility
860 * this.discountingFactor;
861 double currentMaximumUtility = this.utilitySpace
862 .getReservationValueWithDiscount(timeline);
863 // System.out.println("the current reserved value is "+
864 // this.reservationValue+" after discounting is
865 // "+currentMaximumUtility);
866 if (currentMaximumUtility > predictMaximumUtility
867 && timeline.getTime() > this.concedeToDiscountingFactor) {
868 return true;
869 } else {
870 return false;
871 }
872 }
873 }
874
875 /*
876 * estimate the number of rounds left before reaching the deadline @param
877 * opponent @return
878 */
879
880 private int estimateRoundLeft(boolean opponent) {
881 double round;
882
883 if (timeline.getType() == Timeline.Type.Rounds) {
884
885 // System.out.println((int)(timeline.getTotalTime() -
886 // timeline.getCurrentTime()));
887
888 return (int) (timeline.getTotalTime() - timeline.getCurrentTime());
889 } else {
890
891 if (opponent == true) {
892 if (this.timeLeftBefore
893 - this.timeLeftAfter > this.maximumTimeOfOpponent) {
894 this.maximumTimeOfOpponent = this.timeLeftBefore
895 - this.timeLeftAfter;
896 }
897 } else {
898 if (this.timeLeftAfter
899 - this.timeLeftBefore > this.maximumTimeOfOwn) {
900 this.maximumTimeOfOwn = this.timeLeftAfter
901 - this.timeLeftBefore;
902 }
903 }
904 if (this.maximumTimeOfOpponent + this.maximumTimeOfOwn == 0) {
905 System.out.println("divided by zero exception");
906 }
907 round = (this.totalTime - timeline.getCurrentTime())
908 / (this.maximumTimeOfOpponent + this.maximumTimeOfOwn);
909 // System.out.println("current time is " +
910 // timeline.getElapsedSeconds()
911 // + "---" + round + "----" + this.maximumTimeOfOpponent);
912 return ((int) (round));
913 }
914 }
915
916 /*
917 * pre-processing to save the computational time each round
918 */
919 private void calculateBidsBetweenUtility() {
920 BidIterator myBidIterator = new BidIterator(
921 this.utilitySpace.getDomain());
922
923 try {
924 // double maximumUtility =
925 // utilitySpace.getUtility(utilitySpace.getMaxUtilityBid());
926 double maximumUtility = this.MaximumUtility;
927 double minUtility = this.minimumUtilityThreshold;
928 int maximumRounds = (int) ((maximumUtility - minUtility) / 0.01);
929 // initalization for each arraylist storing the bids between each
930 // range
931 for (int i = 0; i < maximumRounds; i++) {
932 ArrayList<Bid> BidList = new ArrayList<Bid>();
933 // BidList.add(this.bid_maximum_utility);
934 this.bidsBetweenUtility.add(BidList);
935 }
936 this.bidsBetweenUtility.get(maximumRounds - 1)
937 .add(this.bid_maximum_utility);
938 // note that here we may need to use some trick to reduce the
939 // computation cost (to be checked later);
940 // add those bids in each range into the corresponding arraylist
941 int limits = 0;
942 if (this.maximumOfBid < 20000) {
943 while (myBidIterator.hasNext()) {
944 Bid b = myBidIterator.next();
945 for (int i = 0; i < maximumRounds; i++) {
946 if (utilitySpace.getUtility(b) <= (i + 1) * 0.01
947 + minUtility
948 && utilitySpace.getUtility(b) >= i * 0.01
949 + minUtility) {
950 this.bidsBetweenUtility.get(i).add(b);
951 break;
952 }
953 }
954 // limits++;
955 }
956 } else {
957 while (limits <= 20000) {
958 Bid b = this.RandomSearchBid();
959 for (int i = 0; i < maximumRounds; i++) {
960 if (utilitySpace.getUtility(b) <= (i + 1) * 0.01
961 + minUtility
962 && utilitySpace.getUtility(b) >= i * 0.01
963 + minUtility) {
964 this.bidsBetweenUtility.get(i).add(b);
965 break;
966 }
967 }
968 limits++;
969 }
970 }
971 } catch (Exception e) {
972 System.out.println("Exception in calculateBidsBetweenUtility()");
973 e.printStackTrace();
974 }
975 }
976
977 private Bid RandomSearchBid() throws Exception {
978 HashMap<Integer, Value> values = new HashMap<Integer, Value>();
979 List<Issue> issues = utilitySpace.getDomain().getIssues();
980 Bid bid = null;
981
982 for (Issue lIssue : issues) {
983 switch (lIssue.getType()) {
984 case DISCRETE:
985 IssueDiscrete lIssueDiscrete = (IssueDiscrete) lIssue;
986 int optionIndex = random
987 .nextInt(lIssueDiscrete.getNumberOfValues());
988
989 values.put(lIssue.getNumber(),
990 lIssueDiscrete.getValue(optionIndex));
991 break;
992 case REAL:
993 IssueReal lIssueReal = (IssueReal) lIssue;
994 int optionInd = random.nextInt(
995 lIssueReal.getNumberOfDiscretizationSteps() - 1);
996 values.put(lIssueReal.getNumber(),
997 new ValueReal(lIssueReal.getLowerBound() + (lIssueReal
998 .getUpperBound() - lIssueReal.getLowerBound())
999 * (optionInd) / (lIssueReal
1000 .getNumberOfDiscretizationSteps())));
1001 break;
1002 case INTEGER:
1003 IssueInteger lIssueInteger = (IssueInteger) lIssue;
1004 int optionIndex2 = lIssueInteger.getLowerBound()
1005 + random.nextInt(lIssueInteger.getUpperBound()
1006 - lIssueInteger.getLowerBound());
1007 values.put(lIssueInteger.getNumber(),
1008 new ValueInteger(optionIndex2));
1009 break;
1010 default:
1011 throw new Exception(
1012 "issue type " + lIssue.getType() + " not supported");
1013 }
1014 }
1015 bid = new Bid(utilitySpace.getDomain(), values);
1016 return bid;
1017 }
1018
1019 /*
1020 * Get all the bids within a given utility range.
1021 */
1022 private List<Bid> getBidsBetweenUtility(double lowerBound,
1023 double upperBound) {
1024 List<Bid> bidsInRange = new ArrayList<Bid>();
1025 try {
1026 int range = (int) ((upperBound - this.minimumUtilityThreshold)
1027 / 0.01);
1028 int initial = (int) ((lowerBound - this.minimumUtilityThreshold)
1029 / 0.01);
1030 // System.out.println(range+"---"+initial);
1031 for (int i = initial; i < range; i++) {
1032 bidsInRange.addAll(this.bidsBetweenUtility.get(i));
1033 }
1034 if (bidsInRange.isEmpty()) {
1035 bidsInRange.add(this.bid_maximum_utility);
1036 }
1037 } catch (Exception e) {
1038 System.out.println("Exception in getBidsBetweenUtility");
1039 e.printStackTrace();
1040 }
1041 return bidsInRange;
1042 }
1043
1044 /*
1045 * determine the lowest bound of our utility threshold based on the
1046 * discounting factor we think that the minimum utility threshold should not
1047 * be related with the discounting degree.
1048 */
1049 private void chooseUtilityThreshold() {
1050 double discountingFactor = this.discountingFactor;
1051 if (discountingFactor >= 0.9) {
1052 this.minimumUtilityThreshold = 0;// this.MaximumUtility - 0.09;
1053 } else {
1054 // this.minimumUtilityThreshold = 0.85;
1055 this.minimumUtilityThreshold = 0;// this.MaximumUtility - 0.09;
1056 }
1057 }
1058
1059 /*
1060 * determine concede-to-time degree based on the discounting factor.
1061 */
1062
1063 private void chooseConcedeToDiscountingDegree() {
1064 double alpha = 0;
1065 double beta = 1.5;// 1.3;//this value controls the rate at which the
1066 // agent concedes to the discouting factor.
1067 // the larger beta is, the more the agent makes concesions.
1068 // if (utilitySpace.getDomain().getNumberOfPossibleBids() > 100) {
1069 /*
1070 * if (this.maximumOfBid > 100) { beta = 2;//1.3; } else { beta = 1.5; }
1071 */
1072 // the vaule of beta depends on the discounting factor (trade-off
1073 // between concede-to-time degree and discouting factor)
1074 if (this.discountingFactor > 0.75) {
1075 beta = 1.85;
1076 } else if (this.discountingFactor > 0.5) {
1077 beta = 1.63;
1078 } else {
1079 beta = 1.2;
1080 }
1081 alpha = Math.pow(this.discountingFactor, beta);
1082 this.concedeToDiscountingFactor = this.minConcedeToDiscountingFactor
1083 + (1 - this.minConcedeToDiscountingFactor) * alpha;
1084 this.concedeToDiscountingFactor_original = this.concedeToDiscountingFactor;
1085 // System.out.println("concedeToDiscountingFactor is "
1086 // + this.concedeToDiscountingFactor + "current time is "
1087 // + timeline.getTime());
1088 }
1089
1090 /*
1091 * update the concede-to-time degree based on the predicted toughness degree
1092 * of the opponent
1093 */
1094
1095 private void updateConcedeDegree() {
1096 double gama = 10;
1097 double weight = 0.1;
1098 double opponnetToughnessDegree = this.opponentBidHistory
1099 .getConcessionDegree();
1100 // this.concedeToDiscountingFactor =
1101 // this.concedeToDiscountingFactor_original * (1 +
1102 // opponnetToughnessDegree);
1103 this.concedeToDiscountingFactor = this.concedeToDiscountingFactor_original
1104 + weight * (1 - this.concedeToDiscountingFactor_original)
1105 * Math.pow(opponnetToughnessDegree, gama);
1106 if (this.concedeToDiscountingFactor >= 1) {
1107 this.concedeToDiscountingFactor = 1;
1108 }
1109 // System.out.println("concedeToDiscountingFactor is " +
1110 // this.concedeToDiscountingFactor + "current time is " +
1111 // timeline.getTime() + "original concedetodiscoutingfactor is " +
1112 // this.concedeToDiscountingFactor_original);
1113 }
1114
1115 private Bid genRanBid(double min, double max) {
1116 HashMap<Integer, Value> values = new HashMap<Integer, Value>(); // pairs
1117 // <issuenumber,chosen
1118 // value
1119 // string>
1120 List<Issue> issues = utilitySpace.getDomain().getIssues();
1121 int counter = 0;
1122 int limit = 1000;
1123 double fmax = max;
1124
1125 Bid bid = null;
1126 do {
1127 for (Issue lIssue : issues) {
1128 switch (lIssue.getType()) {
1129 case DISCRETE:
1130 IssueDiscrete lIssueDiscrete = (IssueDiscrete) lIssue;
1131 int optionIndex = random
1132 .nextInt(lIssueDiscrete.getNumberOfValues());
1133 values.put(lIssue.getNumber(),
1134 lIssueDiscrete.getValue(optionIndex));
1135 break;
1136 case REAL:
1137 IssueReal lIssueReal = (IssueReal) lIssue;
1138 int optionInd = random.nextInt(
1139 lIssueReal.getNumberOfDiscretizationSteps() - 1);
1140 values.put(lIssueReal.getNumber(), new ValueReal(lIssueReal
1141 .getLowerBound()
1142 + (lIssueReal.getUpperBound()
1143 - lIssueReal.getLowerBound()) * (optionInd)
1144 / (lIssueReal
1145 .getNumberOfDiscretizationSteps())));
1146 break;
1147 case INTEGER:
1148 IssueInteger lIssueInteger = (IssueInteger) lIssue;
1149 int optionIndex2 = lIssueInteger.getLowerBound()
1150 + random.nextInt(lIssueInteger.getUpperBound()
1151 - lIssueInteger.getLowerBound());
1152 values.put(lIssueInteger.getNumber(),
1153 new ValueInteger(optionIndex2));
1154 break;
1155 default:
1156 //
1157 }
1158 }
1159
1160 try {
1161 bid = new Bid(utilitySpace.getDomain(), values);
1162 } catch (Exception e) {
1163 // System.out.println("error in generating random bids");
1164 }
1165
1166 counter++;
1167 if (counter > limit) {
1168 limit = limit + 500;
1169 fmax += 0.005;
1170 // return mBidHistory.getMyLastBid();
1171 }
1172
1173 if (counter > 4000)
1174 return ownBidHistory.getLastBid();
1175
1176 } while (getUtility(bid) < min || getUtility(bid) > fmax);
1177
1178 return bid;
1179 }
1180
1181 @Override
1182 public String getDescription() {
1183 return "ANAC2015";
1184 }
1185
1186}
Note: See TracBrowser for help on using the repository browser.