source: src/main/java/agents/anac/y2015/Phoenix/PhoenixParty.java

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

Initial import : Genius 9.0.0

File size: 36.2 KB
Line 
1package agents.anac.y2015.Phoenix;
2
3/**
4 * @author Max Lam @ CUHK
5 *
6 */
7
8import java.io.FileNotFoundException;
9import java.io.FileOutputStream;
10import java.io.PrintStream;
11import java.util.ArrayList;
12import java.util.HashMap;
13import java.util.List;
14import java.util.Random;
15
16import agents.Jama.Matrix;
17import agents.anac.y2015.Phoenix.GP.CovLINard;
18import agents.anac.y2015.Phoenix.GP.CovNoise;
19import agents.anac.y2015.Phoenix.GP.CovSum;
20import agents.anac.y2015.Phoenix.GP.CovarianceFunction;
21import agents.anac.y2015.Phoenix.GP.GaussianProcess;
22import genius.core.AgentID;
23import genius.core.Bid;
24import genius.core.BidIterator;
25import genius.core.Domain;
26import genius.core.actions.Accept;
27import genius.core.actions.Action;
28import genius.core.actions.EndNegotiation;
29import genius.core.actions.NoAction;
30import genius.core.actions.Offer;
31import genius.core.issue.Issue;
32import genius.core.issue.IssueDiscrete;
33import genius.core.issue.Value;
34import genius.core.parties.AbstractNegotiationParty;
35import genius.core.parties.NegotiationInfo;
36
37public class PhoenixParty extends AbstractNegotiationParty {
38 // ///////////////////////////////////////////////////////////////////
39 // //////////////////// Define Needed Variables //////////////////////
40 // ///////////////////////////////////////////////////////////////////
41
42 PrintStream logTxt, OriOut;
43 private boolean debug = false;
44 private boolean cctime = true;
45 private double startTime = 0;
46
47 private Action opponentAction = null;
48 private Random rand = null;
49 private Bid myLastBid = null;
50 private double myLastBidUtility;
51 private double myLastBidTime;
52 private Bid opponentPreviousBid1 = null;
53 private Bid opponentPreviousBid = null;
54 private Bid opponentBestOfferBid1 = null;
55 private Bid opponentFirstBid1 = null;
56 private double opponentPreviousBidUtility1;
57 private double opponentPreviousBidUtility;
58 private double opponentPreviousBidTime1;
59 private Bid bestReceivedBid1 = null;
60 private double bestReceivedBidUtility1;
61 private Bid opponentPreviousBid2 = null;
62 private Bid opponentBestOfferBid2 = null;
63 private Bid opponentFirstBid2 = null;
64 private double opponentPreviousBidUtility2;
65 private double opponentPreviousBidTime2;
66 private Bid bestReceivedBid2 = null;
67 private double bestReceivedBidUtility2;
68 private Bid maxBid = null;
69 private double maxBidUtility;
70 private Bid minBid = null;
71 private double minBidUtility;
72 private ArrayList<Double> allBids = null;
73 private ArrayList<Bid> opponentBids = null;
74 private ArrayList<Bid> myPreviousBids = null;
75 private double opponentNoOfferTime = 10;
76 private double compaction, compaction2;
77 private double trainingTimeSeperator = 0.05;
78 private int timeDivisionSize = 400;
79 private int utilityDivisionSize = 100;
80 private double a = 3, b = 12;
81 private double meanOfOpponentUtilities;
82 private double varianceOfOpponentUtilities;
83 private double estimatedMean;
84 private double selfVar;
85 private double sumOfOpponentBidUtilities = 0;
86 private int randomSeeCreate;
87 private double firstAcceptTime = 1.0;
88 Domain domain;
89 double[][] freqFunc;
90 int countOpp = 0, lastCountOpp = 0;
91 Offer[] roundOffers = null;
92 int TimeToPrint;
93 String fileName;
94 int markNo = 0;
95
96 // ///////////////////////////////////////////////////////////////////
97 // /////////////// Public Functions Overriding Agent /////////////////
98 // ///////////////////////////////////////////////////////////////////
99
100 @Override
101 public void init(NegotiationInfo info) {
102 super.init(info);
103 init();
104 }
105
106 public void init() {
107 try {
108
109 if (getNumberOfParties() > 0) {
110 roundOffers = new Offer[getNumberOfParties() - 1];
111 }
112 startTime = timeline.getCurrentTime();
113 domain = utilitySpace.getDomain();
114 issues = domain.getIssues();
115 markNo = 0;
116 countOpp = 0;
117 firstAcceptTime = 1.0;
118 fileName = getName();
119 lastCountOpp = 0;
120 currentTimeIndex = 0;
121 alpha = 3.0; // impact of delta over time
122 beta = 1.5; // concession factor
123 omega = 1.25; // weight reflects
124 epsilon = 0.7; // controls influence of lambda
125 xi = 0.95; // maximum concession
126 gamma = 1.0;
127 opponentNoOfferTime = 30;
128 trainingTimeSeperator = 0.01;
129 timeDivisionSize = 200;
130 utilityDivisionSize = 45;
131 ppp = 0;
132 int maxSIze = 0;
133 for (Integer i = 0; i < issues.size(); i++) {
134 IssueDiscrete issue = (IssueDiscrete) issues.get(i);
135 if (issue.getNumberOfValues() > maxSIze) {
136 maxSIze = issue.getNumberOfValues();
137 }
138 }
139 freqFunc = new double[issues.size()][maxSIze];
140 for (Integer i = 0; i < issues.size(); i++) {
141 for (int j = 0; j < maxSIze; j++) {
142 freqFunc[i][j] = 1.0;
143 }
144 }
145 a = 3;
146 b = 12;
147 rand = new Random(randomSeeCreate);
148 lastTrainingIndex = 0;
149 sumOfOpponentBidUtilities = 0;
150 randomSeeCreate = 123;
151 compareX = 0;
152 fixedInputSize = 40;
153 rangeofPrediction = 10;
154 timeDivision = 5;
155 tryNewBidTime = 0;
156 lastTrainingTime = 0;
157 phiAcceptanceBound = 0.5;
158 allBids = new ArrayList<Double>();
159 maxUtilitiesAsInputData = new ArrayList<Double>();
160 timePointsAsOutputData = new ArrayList<Double>();
161 delta = utilitySpace.getDiscountFactor();
162 theta = utilitySpace.getReservationValue();
163 System.out.println("theta=" + theta);
164 System.out.println("delta=" + (delta));
165 currentTimeIndex = 0;
166 opponentAction = null;
167 fileName = getName() + hashCode() + ".txt";
168 logTxt = new PrintStream(new FileOutputStream(fileName, false));
169 logTxt.println(" ");
170 myLastBid = null;
171 opponentPreviousBid1 = null;
172 bestReceivedBid1 = null;
173 opponentFirstBid1 = null;
174 opponentPreviousBid2 = null;
175 bestReceivedBid2 = null;
176 opponentFirstBid2 = null;
177 maxBid = utilitySpace.getMaxUtilityBid();
178 minBid = utilitySpace.getMinUtilityBid();
179 maxBidUtility = utilitySpace.getUtility(maxBid);
180 minBidUtility = utilitySpace.getUtility(minBid);
181 myLastBidUtility = maxBidUtility;
182 uTargetForAcceptance = maxBidUtility;
183 uTargetForBidding = maxBidUtility;
184 opponentBids = new ArrayList<Bid>();
185 myPreviousBids = new ArrayList<Bid>();
186 compaction = 1 / (maxBidUtility - minBidUtility);
187 a += compaction - varianceOfOpponentUtilities;
188 generateTimeSamples(timeDivisionSize);
189 saveInTermsOfTime = new timeList[timeDivisionSize];
190 for (int i = 0; i < timeDivisionSize; i++) {
191 saveInTermsOfTime[i] = new timeList();
192 }
193 fixedInputSize = (int) Math.min(
194 utilitySpace.getDomain().getNumberOfPossibleBids(),
195 fixedInputSize);
196 if (utilitySpace.getDomain().getNumberOfPossibleBids() < 50) {
197 if (utilitySpace.getDomain().getNumberOfPossibleBids() <= 5) {
198 utilityDivisionSize = (int) utilitySpace.getDomain()
199 .getNumberOfPossibleBids() / 2;
200 } else if (utilitySpace.getDomain()
201 .getNumberOfPossibleBids() <= 11) {
202 utilityDivisionSize = (int) utilitySpace.getDomain()
203 .getNumberOfPossibleBids() / 2;
204 } else {
205 utilityDivisionSize = (int) utilitySpace.getDomain()
206 .getNumberOfPossibleBids() / 3;
207 }
208 } else {
209 utilityDivisionSize = Math.min(
210 (int) (utilitySpace.getDomain()
211 .getNumberOfPossibleBids() / 15),
212 utilityDivisionSize);
213 }
214 generateUtilitySamples(utilityDivisionSize);
215 saveInTermsOfUtility = new BidList[utilityDivisionSize];
216 for (int i = 0; i < utilityDivisionSize; i++) {
217 saveInTermsOfUtility[i] = new BidList();
218 }
219 covFunc = new CovSum(1, new CovLINard(1), new CovNoise());
220 // covFunc = new CovSum(1,new CovNNone(), new CovNoise());
221 // CovSEiso cf = new CovSEiso();
222 gp = new GaussianProcess(covFunc);
223 double[][] logtheta0 = new double[][] { { 0.1 }, { 0.2 }
224 // {Math.log(0.1)}
225 };
226 params0 = new Matrix(logtheta0);
227 // setPartyId(getPartyId());
228 estimatedMean = minBidUtility
229 + (minBidUtility + maxBidUtility) / 3.0;
230 if (domain.getNumberOfPossibleBids() <= 6400) {
231 allBids.clear();
232 BidIterator bidItr = new BidIterator(domain);
233 while (bidItr.hasNext()) {
234 recordBidInUtilities(bidItr.next());
235 }
236 estimatedMean = estimateMean();
237 }
238 Edelta = estimatedMean;
239 } catch (Exception ex) {
240 System.out.println("init:" + ex);
241 ex.printStackTrace();
242 }
243 }
244
245 public double estimateMean() {
246 double sum = 0;
247 for (int i = 0; i < allBids.size(); i++) {
248 sum += allBids.get(i);
249 }
250 sum /= (allBids.size() * 1.0);
251 return sum;
252 }
253
254 public String getName() {
255 return "PhoenixParty";
256 }
257
258 public double getTime() {
259 if (cctime) {
260 return (timeline.getCurrentTime() - startTime)
261 * (Math.pow(2 - delta, 0.5)) / (180.0);
262 }
263 return timeline.getTime() * (Math.pow(2 - delta, 0.5));
264
265 }
266
267 public double getTrueTime() {
268 if (cctime) {
269 return (timeline.getCurrentTime() - startTime) / 180.0;
270 }
271 return timeline.getTime();
272 }
273
274 public double getCurrentTime() {
275 if (cctime) {
276 return (timeline.getCurrentTime() - startTime);
277 }
278 return timeline.getCurrentTime();
279 }
280
281 @Override
282 public void receiveMessage(AgentID sender, Action arguments) {
283 super.receiveMessage(sender, arguments);
284 countOpp++;
285 if (arguments instanceof Offer) {
286 try {
287 // next opponent
288 if (countOpp == lastCountOpp + 1) {
289 opponentPreviousBid1 = ((Offer) arguments).getBid();
290 opponentPreviousBidUtility1 = getUtility(
291 opponentPreviousBid1);
292 }
293 // next and next opponent (last opponent)
294 else if (countOpp == lastCountOpp + 2) {
295 opponentPreviousBid2 = ((Offer) arguments).getBid();
296 opponentPreviousBidUtility2 = getUtility(
297 opponentPreviousBid2);
298 }
299 opponentPreviousBid = ((Offer) arguments).getBid();
300 opponentPreviousBidUtility = getUtility(opponentPreviousBid);
301 recordOffer((Offer) arguments);
302 } catch (Exception e) {
303 System.out.println(e.getMessage());
304 }
305 } else if (arguments instanceof Accept) {
306
307 if (countOpp == lastCountOpp + 1) {
308 controlFreqFunc(myLastBid, -100 * Math.pow(getTime(), 0.5));
309 if (getTrueTime() < firstAcceptTime) {
310 firstAcceptTime = getTrueTime();
311 }
312 } else {
313 controlFreqFunc(opponentPreviousBid,
314 -100 * Math.pow(getTime(), 0.5));
315 }
316
317 }
318 }
319
320 @Override
321 public Action chooseAction(List<Class<? extends Action>> possibleActions) {
322
323 Action currentAction = new NoAction(getPartyId());
324 double currentTime = getTime();
325 double compareU = 1;
326 try {
327 if (possibleActions.contains(Accept.class)
328 || possibleActions.contains(Offer.class)) {
329 double bound = (uTargetForBidding + uTargetForAcceptance) / 2;
330 if (currentTimeIndex > TimeToPrint) {
331 TimeToPrint = currentTimeIndex;
332
333 }
334 lastCountOpp = countOpp;
335 if (getNumberOfParties() > 0) {
336 roundOffers = new Offer[getNumberOfParties() - 1];
337 }
338 theta = utilitySpace.getReservationValueUndiscounted();
339 Domain domain = utilitySpace.getDomain();
340 if ((myLastBid == null) || (opponentPreviousBid1 == null)
341 || (opponentPreviousBid2 == null)) {
342 myLastBid = maxBid;
343 uTargetForAcceptance = maxBidUtility;
344 uTargetForBidding = maxBidUtility;
345 currentAction = new Offer(getPartyId(), myLastBid);
346 } else {
347 if (timeToTrain()) {
348 updateParas();
349 getTargetUtility();
350 }
351 if (debug && getTime() > 0.02) {
352 try {
353 OriOut = System.out;
354 logTxt = new PrintStream(
355 new FileOutputStream(fileName, true));
356
357 System.setOut(logTxt);
358 } catch (FileNotFoundException ex) {
359 }
360 System.out.println(Double.toString(getTrueTime()) + ","
361 + Double.toString(uTargetForAcceptance) + ","
362 + Double.toString(bound) + "," + Edelta + ","
363 + Predict(getTime()));
364 System.setOut(OriOut);
365 }
366 if (opponentPreviousBidUtility >= uTargetForBidding
367 || (getTime() > (delta * 0.6 + theta * 0.3)
368 && opponentPreviousBidUtility >= uTargetForAcceptance)) {
369 currentAction = new Accept(getPartyId(),
370 opponentPreviousBid);
371 System.out.println(
372 "Accccccccccccccccccccccccccccccccccc");
373 } else {
374 double reservationValue = theta
375 * Math.pow(delta, currentTime);
376 if (theta > 0 && ((currentTime - myLastBidTime)
377 * timeline.getTotalTime() > opponentNoOfferTime
378 || ((domain.getNumberOfPossibleBids() < 100
379 && currentTime > delta * (1.0 - theta
380 + bestReceivedBidUtility2))
381 && (theta >= bestReceivedBidUtility2))
382 && theta >= 0.4
383 * (maxBidUtility - utilitySpace
384 .getUtility(
385 opponentFirstBid2))
386 + utilitySpace.getUtility(
387 opponentFirstBid2))) {
388
389 currentAction = new EndNegotiation(getPartyId());
390 } else {
391 Bid bidToOffer = BiddingMethod();
392 currentAction = new Offer(getPartyId(), bidToOffer);
393 if (reservationValue > utilitySpace
394 .getUtility(bidToOffer)) {
395 currentAction = new EndNegotiation(
396 getPartyId());
397 }
398 compareU = utilitySpace.getUtility(bidToOffer);
399 }
400 }
401 }
402
403 if ((currentAction instanceof Offer)) {
404 myLastBid = ((Offer) currentAction).getBid();
405 myPreviousBids.add(myLastBid);
406 myLastBidTime = currentTime;
407 myLastBidUtility = utilitySpace.getUtility(myLastBid);
408 }
409 double endTime = 0.998;
410 if (currentTime >= endTime
411 && domain.getNumberOfPossibleBids() > 5) {
412 if (opponentPreviousBidUtility > theta) {
413 if (theta == 0 || opponentPreviousBidUtility > theta * 2
414 || myLastBidUtility
415 - opponentPreviousBidUtility < (maxBidUtility
416 - minBidUtility) / 4) {
417 currentAction = new Accept(getPartyId(),
418 opponentPreviousBid);
419 System.out.println(
420 "Accccccccccccccccccccccccccccccccccc");
421 }
422 }
423 }
424 if (theta >= uTargetForBidding) {
425 currentAction = new EndNegotiation(getPartyId());
426 // System.out.println("compareU="+compareU+"
427 // uTargetForBidding="+uTargetForBidding);
428 }
429 } else {
430 currentAction = new NoAction(getPartyId());
431 }
432
433 } catch (Exception ex) {
434 // System.out.println(ex.getMessage());
435 }
436
437 return currentAction;
438 }
439
440 // ///////////////////////////////////////////////////////////////////
441 // //////////////// Implement Our Designed Function //////////////////
442 // ///////////////////////////////////////////////////////////////////
443
444 class timeList extends ArrayList<Double> {
445 }
446
447 timeList[] saveInTermsOfTime;
448
449 class BidList extends ArrayList<Bid> {
450 }
451
452 BidList[] saveInTermsOfUtility;
453 private double[] timeSamples;
454 private double[] utilitySamples;
455
456 private ArrayList<Double> maxUtilitiesAsInputData = new ArrayList<Double>();
457 private ArrayList<Double> timePointsAsOutputData = new ArrayList<Double>();
458
459 private Matrix inputDataMatrix;
460 private Matrix outputDataMatrix;
461 private Matrix params0;
462 private HashMap lastBidValues;
463 private CovarianceFunction covFunc;
464 private GaussianProcess gp;
465
466 //
467
468 // Parameters
469 //
470 double delta;
471 double theta;
472 double alpha = 2.0; // impact of delta over time
473 double beta = 1.5; // concession factor
474 double omega = 1.3; // weight reflects
475 double epsilon = 0.7; // controls influence of lambda
476 double xi = 0.95; // acceptance tolerance for pessimistic forecast
477 double lambda; // maximum concession
478 double gamma = 1.0; // ratio of new to all counter-offers over past ten
479 // intervals
480 double rho; // compromise point
481 double phi; // probability of accepting the most possible bid
482 double Elow; // lowest expectation to negotiation
483 double Edelta;
484 double[] EdeltaPast;
485 double R;
486 double ppp;
487 double tV;
488 double uHat;
489 double uTarget;
490 double uTargetForAcceptance;
491 double uTargetForBidding;
492 double compareX = 0;
493 double opponentBestOfferUtility1;
494 double opponentBestOfferUtility2;
495 List<Issue> issues;
496 int fixedInputSize = 50;
497 int rangeofPrediction = 20;
498 int timeDivision = 10;
499 double phiAcceptanceBound = 0.5;
500
501 private void generateUtilitySamples(int m) {
502 utilitySamples = new double[m];
503 for (int i = 0; i < m; i++) {
504 utilitySamples[i] = minBidUtility
505 + i * (maxBidUtility - minBidUtility) / (m * 1.0);
506 }
507 }
508
509 private void generateTimeSamples(int n) {
510 timeSamples = new double[n];
511 EdeltaPast = new double[n];
512 for (int i = 0; i < n; i++) {
513 timeSamples[i] = (i * 1.0 / n);
514 }
515 }
516
517 double tryNewBidTime = 0;
518
519 public Bid BiddingMethod() throws Exception {
520 try {
521 Bid bid = utilitySpace.getMaxUtilityBid();
522 Domain domain = utilitySpace.getDomain();
523 int limits = 0;
524 if (domain.getNumberOfPossibleBids() > 6400) {
525 allBids.clear();
526 while (limits <= Math.max(domain.getNumberOfPossibleBids() / 10,
527 888)) {
528 recordBidInUtilities(domain.getRandomBid(rand));
529 limits++;
530 }
531 double maybeEstimatedMean = estimateMean();
532 if (maybeEstimatedMean > estimatedMean) {
533 estimatedMean = (maybeEstimatedMean + estimatedMean) / 2.0;
534 }
535 }
536 int indexToFindBids = findBidUtilityIndex(uTargetForBidding);
537 if (utilityDivisionSize == 3) {
538 bid = (saveInTermsOfUtility[1 + rand.nextInt(2)]).get(0);
539 return bid;
540 }
541
542 double mean = 0;
543 int countb = 0;
544 for (Bid bb : myPreviousBids) {
545 countb++;
546 mean += utilitySpace.getUtility(bb);
547 if (countb > 100) {
548 break;
549 }
550 }
551 mean /= countb;
552 countb = 0;
553 for (Bid bb : myPreviousBids) {
554 countb++;
555 selfVar += Math.pow(mean - utilitySpace.getUtility(bb), 2);
556 if (countb > 100) {
557 break;
558 }
559 }
560 selfVar /= countb;
561
562 if (utilityDivisionSize > 30) {
563 if (rand.nextInt(12) > 10) {
564 if (indexToFindBids > 0)
565 indexToFindBids--;
566 }
567 int indexToRandBids = findBidUtilityIndex(
568 (maxBidUtility + uTargetForAcceptance) / 2);
569 if (domain.getNumberOfPossibleBids() < 1000) {
570 if (rand.nextInt(10) >= 3
571 + delta * Math.pow(6, 1 - getTime())) {
572 indexToFindBids += rand.nextInt(Math.min(
573 utilityDivisionSize - 1 - indexToFindBids,
574 Math.abs(indexToRandBids - indexToFindBids)));
575 }
576 } else {
577 if (rand.nextInt(11) >= 3
578 + delta * Math.pow(6, 1 - getTime())) {
579 indexToFindBids += rand.nextInt(Math.min(
580 utilityDivisionSize - 1 - indexToFindBids,
581 Math.abs(indexToRandBids - indexToFindBids)));
582 }
583 }
584 } else if (utilityDivisionSize == 3) {
585 if (rand.nextInt(20) > 18) {
586 if (indexToFindBids > 0)
587 indexToFindBids--;
588 }
589 }
590
591 BidList tempBL = saveInTermsOfUtility[indexToFindBids];
592 while (tempBL.size() == 0) {
593 indexToFindBids++;
594 tempBL = saveInTermsOfUtility[indexToFindBids];
595 if (indexToFindBids == utilityDivisionSize - 1)
596 break;
597 }
598
599 double rank = heuristicRank(bid);
600 limits = 0;
601 /* time : bad bids -> good bids */
602 double changingTime = 0.4 * delta / opponentConcession();
603 for (Bid tempbid : tempBL) {
604 if (changingTime < getTime()) {
605 if (heuristicRank(tempbid)
606 * (0.95 + 0.05 * rand.nextDouble()) >= rank) {
607 bid = tempbid;
608 rank = heuristicRank(bid);
609 }
610 } else {
611 if (heuristicRank(tempbid)
612 * (0.95 + 0.05 * rand.nextDouble()) <= rank) {
613 bid = tempbid;
614 rank = heuristicRank(bid);
615 }
616 }
617 if (limits++ > 100) {
618 break;
619 }
620 }
621 if (rand.nextDouble() > delta * Math.min(
622 (Math.pow(Math.min(getTime() + 0.1, 0.8), 0.3)), 0.5)) {
623 bid = tempBL.get(rand.nextInt(tempBL.size()));
624 }
625 if (bid.equals(opponentFirstBid1)
626 || bid.equals(opponentFirstBid2)) {
627 bid = myLastBid;
628 }
629 controlFreqFunc(bid, 0.5);
630 double timeToPlay = (delta * 1.0 - theta) * rand.nextDouble()
631 + 0.1 * delta;
632 double durationToPlay = trainingTimeSeperator * delta;
633 if (getTime() >= timeToPlay
634 && getTime() <= timeToPlay + durationToPlay) {
635 bid = myLastBid;
636 }
637 return bid;
638 } catch (Exception e) {
639 System.out.println(e.getMessage());
640 return utilitySpace.getMaxUtilityBid();
641 }
642 }
643
644 private int findBidUtilityIndex(double utility) {
645 // System.out.println("utilityDivisionSize="+utilityDivisionSize);
646 for (int i = 1; i < utilityDivisionSize; i++) {
647 if (utilitySamples[i] > utility) {
648 return i - 1;
649 }
650 }
651 return utilityDivisionSize - 1;
652 }
653
654 private void controlFreqFunc(Bid bid, double weight) {
655 if (bid != null) {
656 for (Integer in = 0; in < issues.size(); in++) {
657 Value bv = (bid.getValues()).get(issues.get(in).getNumber());
658 IssueDiscrete issue = (IssueDiscrete) issues.get(in);
659 for (Integer j = 0; j < issue.getNumberOfValues(); j++) {
660 Value v = issue.getValue(j);
661 if (debug) {
662 // System.out.println("f["+in+","+j+"]="+freqFunc[in][j]);
663 }
664 if (v.equals(bv)) {
665 freqFunc[in][j] *= 1.0 + 0.01 * weight;
666 }
667 }
668 }
669 }
670 }
671
672 private double heuristicRank(Bid bid) {
673 double distance = 0;
674 HashMap<Integer, Value> bidValues = bid.getValues();
675 HashMap<Integer, Value> compareBidValues = opponentPreviousBid1
676 .getValues();
677 HashMap<Integer, Value> compareBidValues2 = opponentBestOfferBid1
678 .getValues();
679 HashMap<Integer, Value> compareBidValues3 = opponentFirstBid1
680 .getValues();
681
682 double w1 = 2, w2 = 4, w3 = 10;
683 if (selfVar < 0.05 || varianceOfOpponentUtilities < 0.05) {
684 w3 *= 1 + 0.5 * rand.nextDouble();
685 }
686 if (bidValues.equals(opponentFirstBid1.getValues())) {
687 return 1e3;
688 }
689 if (bidValues.equals(compareBidValues)) {
690 return 0;
691 }
692 int numberOfIssues = issues.size();
693 for (Integer in = 0; in < numberOfIssues; in++) {
694 double a = 0, b = 0;
695 Value av = bidValues.get(issues.get(in).getNumber());
696 Value bv = compareBidValues.get(issues.get(in).getNumber());
697 Issue issue = issues.get(in);
698 switch (issue.getType()) {
699 case DISCRETE:
700 IssueDiscrete lIssueDiscrete = (IssueDiscrete) issue;
701 for (int j = 0; j < lIssueDiscrete.getNumberOfValues(); j++) {
702 Value v = lIssueDiscrete.getValue(j);
703 if (v.equals(av)) {
704 a = j;
705 }
706 if (v.equals(bv)) {
707 b = j;
708 w1 *= Math.min(freqFunc[in][j], w1);
709 }
710 }
711 break;
712 }
713 distance += w1 * Math.abs(b - a);
714 }
715 for (Integer in = 0; in < numberOfIssues; in++) {
716 double a = 0, b = 0;
717 Value av = bidValues.get(issues.get(in).getNumber());
718 Value bv = compareBidValues2.get(issues.get(in).getNumber());
719 Issue issue = issues.get(in);
720
721 switch (issue.getType()) {
722 case DISCRETE:
723 IssueDiscrete lIssueDiscrete = (IssueDiscrete) issue;
724 for (int j = 0; j < lIssueDiscrete.getNumberOfValues(); j++) {
725 Value v = lIssueDiscrete.getValue(j);
726 if (v.equals(av)) {
727 a = j;
728 }
729 if (v.equals(bv)) {
730 b = j;
731 w2 *= Math.min(freqFunc[in][j], w2);
732 }
733 }
734 break;
735 }
736 distance += w2 * Math.abs(b - a);
737 }
738 for (Integer in = 0; in < numberOfIssues; in++) {
739 double a = 0, b = 0;
740 Value av = bidValues.get(issues.get(in).getNumber());
741 Value bv = compareBidValues3.get(issues.get(in).getNumber());
742 Issue issue = issues.get(in);
743 switch (issue.getType()) {
744 case DISCRETE:
745 IssueDiscrete lIssueDiscrete = (IssueDiscrete) issue;
746 for (int j = 0; j < lIssueDiscrete.getNumberOfValues(); j++) {
747 Value v = lIssueDiscrete.getValue(j);
748 if (v.equals(av)) {
749 a = j;
750 }
751 if (v.equals(bv)) {
752 b = j;
753 w3 *= Math.min(freqFunc[in][j], w3);
754 }
755 }
756 break;
757 }
758 distance += w3 * Math.abs(b - a);
759 }
760 double w12 = 1, w22 = 3, w32 = 10;
761 compareBidValues = opponentPreviousBid2.getValues();
762 compareBidValues2 = opponentBestOfferBid2.getValues();
763 compareBidValues3 = opponentFirstBid2.getValues();
764 if (selfVar < 0.05 || varianceOfOpponentUtilities < 0.05) {
765 w32 *= 1 + 0.2 * rand.nextDouble();
766 }
767 if (bidValues.equals(opponentFirstBid2.getValues())) {
768 return 1e3;
769 }
770 if (bidValues.equals(compareBidValues)) {
771 return 0;
772 }
773 numberOfIssues = issues.size();
774 for (Integer in = 0; in < numberOfIssues; in++) {
775 double a = 0, b = 0;
776 Value av = bidValues.get(issues.get(in).getNumber());
777 Value bv = compareBidValues.get(issues.get(in).getNumber());
778 Issue issue = issues.get(in);
779 switch (issue.getType()) {
780 case DISCRETE:
781 IssueDiscrete lIssueDiscrete = (IssueDiscrete) issue;
782 for (int j = 0; j < lIssueDiscrete.getNumberOfValues(); j++) {
783 Value v = lIssueDiscrete.getValue(j);
784 if (v.equals(av)) {
785 a = j;
786 }
787 if (v.equals(bv)) {
788 b = j;
789 w12 *= Math.min(freqFunc[in][j], w12);
790 }
791 }
792 break;
793 }
794 distance += w12 * Math.abs(b - a);
795 }
796 for (Integer in = 0; in < numberOfIssues; in++) {
797 double a = 0, b = 0;
798 Value av = bidValues.get(issues.get(in).getNumber());
799 Value bv = compareBidValues2.get(issues.get(in).getNumber());
800 Issue issue = issues.get(in);
801
802 switch (issue.getType()) {
803 case DISCRETE:
804 IssueDiscrete lIssueDiscrete = (IssueDiscrete) issue;
805 for (int j = 0; j < lIssueDiscrete.getNumberOfValues(); j++) {
806 Value v = lIssueDiscrete.getValue(j);
807 if (v.equals(av)) {
808 a = j;
809 }
810 if (v.equals(bv)) {
811 b = j;
812 w22 *= Math.min(freqFunc[in][j], w22);
813 }
814 }
815 break;
816 }
817 distance += w22 * Math.abs(b - a);
818 }
819 for (Integer in = 0; in < numberOfIssues; in++) {
820 double a = 0, b = 0;
821 Value av = bidValues.get(issues.get(in).getNumber());
822 Value bv = compareBidValues3.get(issues.get(in).getNumber());
823 Issue issue = issues.get(in);
824 switch (issue.getType()) {
825 case DISCRETE:
826 IssueDiscrete lIssueDiscrete = (IssueDiscrete) issue;
827 for (int j = 0; j < lIssueDiscrete.getNumberOfValues(); j++) {
828 Value v = lIssueDiscrete.getValue(j);
829 if (v.equals(av)) {
830 a = j;
831 }
832 if (v.equals(bv)) {
833 b = j;
834 w32 *= Math.min(freqFunc[in][j], w32);
835 }
836 }
837 break;
838 }
839 distance += w32 * Math.abs(b - a);
840 }
841 return Math.sqrt(distance / ((w1 + w2 + w3 + w12 + w22 + w32)));
842 }
843
844 private void recordBidInUtilities(Bid bid) throws Exception {
845 double utilityToRecord = utilitySpace.getUtility(bid);
846 allBids.add(utilityToRecord);
847 int index = findBidUtilityIndex(utilityToRecord);
848 if (!(saveInTermsOfUtility[index]).contains(bid)) {
849 (saveInTermsOfUtility[index]).add(bid);
850 }
851 }
852
853 double opponentConcession() {
854 double modelPast = 0;
855 double modelConcess = Edelta;
856 int count = 0;
857 int startTimeIndex = Math.max(0, currentTimeIndex - 5);
858 for (int i = startTimeIndex; i < currentTimeIndex; i++) {
859 modelPast += EdeltaPast[i];
860 count++;
861 }
862 modelPast /= (1.0 * count);
863 modelConcess /= modelPast;
864 /*
865 *
866 * modelConcess > 1 : opponent is conceding modelConcess < 1 : opponent
867 * is exploiting
868 */
869 return modelConcess;
870 }
871
872 double uncertainty;
873
874 private void getTargetUtility() throws Exception {
875 double currentTime = getTime();
876 R = RFunction(currentTime);
877
878 Edelta = discountedExpectation(currentTime + 0.02);
879 uncertainty = PredictUncertainty(currentTime + 0.02);
880
881 double rate = 1.5 + 0.3 * rand.nextDouble();
882 rate *= (0.3 + uTargetForAcceptance - Edelta + (1 - Math.pow(1.0, 1.3))
883 - Math.pow(theta, 1.5));
884 rate /= opponentConcession();
885 double opponentBidDistance = Math.abs(Edelta - uTargetForAcceptance)
886 * compaction2;
887 uTargetForBidding = (R * Math.pow(3, -getTime() * rate)
888 + Edelta * Math.abs(1 - opponentBidDistance) / (2.0))
889 / (Math.abs(1 - opponentBidDistance) / (2.0)
890 + Math.pow(3, -getTime() * rate));
891
892 double baseValue = (Math.max(Elow, Edelta) + Edelta / uncertainty
893 + uncertainty * Elow) / (1.0 + 1 / uncertainty + uncertainty);
894 double diffToElow = 1.2 * Math.abs(uTargetForBidding - baseValue);
895 if (delta <= 1) {
896 double x = 2 * (1 - getTime()) * delta, y = delta,
897 z = (2 * getTime()) / delta;
898 double uTargetForBiddingF1 = diffToElow * Math.pow(5,
899 -(Math.pow(getTime(), 0.6) * (2.3 - firstAcceptTime))
900 * Math.pow(2 - delta, getTime()));
901 double uTargetForBiddingF2 = diffToElow
902 * (1 / ((Math.pow((2 * getTime()) * (2.3 - firstAcceptTime),
903 delta + theta / 3.0) + 0.05) * 20) + 0.4 * delta);
904 double uTargetForBiddingF3 = diffToElow * (1
905 / ((Math.log((20.0 / (delta + theta / 3.0)) * getTime()
906 * (2.3 - firstAcceptTime) + 1.1)) * 10)
907 + 0.3 * delta);
908 uTargetForBidding = baseValue
909 + (z * uTargetForBiddingF3 + y * uTargetForBiddingF2
910 + x * uTargetForBiddingF1) / (x + y + z);
911 }
912 if ((selfVar < 0.05 || varianceOfOpponentUtilities < 0.05)
913 && timeline.getCurrentTime() % 2 == 1) {
914 uTargetForBidding *= 0.95 + 0.1 * rand.nextDouble();
915 }
916 if (uTargetForBidding <= uTargetForAcceptance) {
917 uTargetForAcceptance = (uTargetForBidding + uTargetForAcceptance)
918 / 2.0;
919 }
920 }
921
922 private void updateParas() {
923 try {
924 selfVar = 0;
925 lastTrainingTime = getTime();
926 getTrainingData();
927 trainGP();
928 meanOfOpponentUtilities = 0;
929 varianceOfOpponentUtilities = 0;
930 for (double u : maxUtilitiesAsInputData) {
931 meanOfOpponentUtilities += u;
932 }
933 meanOfOpponentUtilities /= maxUtilitiesAsInputData.size();
934 for (double u : maxUtilitiesAsInputData) {
935 varianceOfOpponentUtilities += Math
936 .pow(u - meanOfOpponentUtilities, 2);
937 }
938 varianceOfOpponentUtilities /= maxUtilitiesAsInputData.size();
939 double currentTime = getTime();
940
941 } catch (Exception ex) {
942 // System.out.println("updateParas: "+ex.getMessage());
943 }
944 }
945
946 private double Predict(double queryTimePt) {
947 try {
948 double[][] q = { { queryTimePt } };
949 Matrix queryInput = new Matrix(q);
950 Matrix[] res = gp.predict(queryInput);
951 return Math.max(Math.min(res[0].get(0, 0), maxBidUtility),
952 minBidUtility);
953 } catch (Exception ex) {
954 // System.out.println("Predict error: "+ex.getMessage());
955 return minBidUtility;
956 }
957 }
958
959 private double PredictUncertainty(double queryTimePt) {
960 try {
961 double[][] q = { { queryTimePt } };
962 Matrix queryInput = new Matrix(q);
963 Matrix[] res = gp.predict(queryInput);
964 return 1 / (1 + Math.exp(-1 * Math.exp(res[1].get(0, 0))));
965 } catch (Exception ex) {
966 // System.out.println("Predict error: "+ex.getMessage());
967 return minBidUtility;
968 }
969 }
970
971 private double lastTrainingTime = 0;
972
973 private double discountedExpectation(double time) {
974 double w = 0.5;
975 if (getTime() > 10 * trainingTimeSeperator) {
976 w = opponentConcession() / 2.0;
977 }
978 double predictedUtility = (uTargetForAcceptance
979 * Math.pow(0.5, getTime()) * (2 + w)
980 + Math.pow(0.5, 1 - getTime()) * 3 * w
981 * (1.0 / PredictUncertainty(time)) * Predict(time)
982 + 2 * w * meanOfOpponentUtilities)
983 / (2 * w + Math.pow(0.5, getTime()) * (2 + w)
984 + Math.pow(0.5, 1 - getTime()) * 3 * w
985 * (1.0 / PredictUncertainty(time)));
986 return predictedUtility * Math
987 .pow(Math.pow(1.0 + (0.1 * theta + 0.05), 0.7), getTime());
988 }
989
990 private double lowestExpectation() throws Exception {
991 double uFIrst = Math.max(utilitySpace.getUtility(opponentFirstBid1),
992 utilitySpace.getUtility(opponentFirstBid2));
993 if (uFIrst < estimatedMean) {
994 uFIrst = estimatedMean;
995 }
996 Elow = uFIrst + (maxBidUtility - uFIrst)
997 * Math.pow(delta, 0.5 + (0.1) * rand.nextDouble()) / 2.5;
998 return Elow * Math.pow(0.9, Math.pow(getTrueTime(), 2.0));
999 }
1000
1001 private double RFunction(double time) {
1002 try {
1003 double Elow = lowestExpectation() * Math.pow(0.8 + (0.2 * ppp),
1004 Math.pow(time, Math.pow(1.0, 0.65)));
1005 if (time >= 1) {
1006 return maxBidUtility;
1007 }
1008 omega = 1.26;
1009 if (getTime() > 10 * trainingTimeSeperator && getTime() < 0.3) {
1010 double comparePoint = opponentConcession()
1011 * discountedExpectation(time);
1012 double disToPoint = Math.abs(R - comparePoint)
1013 / Math.abs(maxBidUtility - Math.min(
1014 utilitySpace.getUtility(opponentFirstBid1),
1015 utilitySpace.getUtility(opponentFirstBid2)));
1016 ppp = disToPoint;
1017 omega = 1.16 + 0.5 * (disToPoint);
1018 }
1019 omega = Math.min(omega, 1.35);
1020 double component1 = Math
1021 .abs(1 - Math.pow(time, 1 / Math.pow(Math.abs(0.5), beta)));
1022 double component2 = Math
1023 .cos((1 - Math.pow(1.0, 1.3) * Math.pow(compaction2, 0.5))
1024 / (omega));
1025 double addR = component1 * component2 * (maxBidUtility - Elow);
1026 addR *= Math.pow(1.06, compaction2);
1027 R = Math.min(maxBidUtility, Elow + addR);
1028 return R;
1029 } catch (Exception e) {
1030 return maxBidUtility;
1031 }
1032
1033 }
1034
1035 double sigmoid(double x) {
1036 return 1 / (1 + Math.exp(-1 * x));
1037 }
1038
1039 int currentTimeIndex = 0;
1040
1041 private void recordOffer(Offer offerToRecord) throws Exception {
1042
1043 Bid bidToRecord = offerToRecord.getBid();
1044 if (countOpp == lastCountOpp + 1) {
1045 if (opponentFirstBid1 == null) {
1046 opponentFirstBid1 = bidToRecord;
1047 Edelta = utilitySpace.getUtility(opponentFirstBid1);
1048 minBidUtility = utilitySpace.getUtility(opponentFirstBid1);
1049 compaction2 = 1 / (maxBidUtility - minBidUtility);
1050 opponentBestOfferBid1 = opponentFirstBid1;
1051 controlFreqFunc(opponentFirstBid1, 10);
1052 } else {
1053 if (bidToRecord.equals(opponentFirstBid1)) {
1054 controlFreqFunc(myLastBid, 10);
1055 }
1056 }
1057 opponentBids.add(bidToRecord);
1058 opponentPreviousBid1 = bidToRecord;
1059 double utilityToRecord = utilitySpace.getUtility(bidToRecord);
1060 opponentBestOfferUtility1 = utilitySpace
1061 .getUtility(opponentBestOfferBid1);
1062 if (utilityToRecord > opponentBestOfferUtility1) {
1063 opponentBestOfferBid1 = bidToRecord;
1064 if (countOpp == lastCountOpp + 1) {
1065 controlFreqFunc(opponentBestOfferBid1, -1);
1066 }
1067 }
1068 opponentPreviousBidUtility1 = utilityToRecord;
1069 sumOfOpponentBidUtilities += utilityToRecord;
1070 double timeToRecord = getTime();
1071 opponentPreviousBidTime1 = timeToRecord;
1072
1073 bestReceivedBid1 = opponentBestOfferBid1;
1074 bestReceivedBidUtility1 = utilitySpace.getUtility(bestReceivedBid1);
1075 } else if (countOpp == lastCountOpp + 2) {
1076 if (opponentFirstBid2 == null) {
1077 opponentFirstBid2 = bidToRecord;
1078 opponentBestOfferBid2 = opponentFirstBid2;
1079 controlFreqFunc(opponentFirstBid2, 5);
1080 } else {
1081 if (bidToRecord.equals(opponentFirstBid2)) {
1082 controlFreqFunc(myLastBid, 1);
1083
1084 }
1085 }
1086
1087 opponentPreviousBid2 = bidToRecord;
1088 double utilityToRecord = utilitySpace.getUtility(bidToRecord);
1089 opponentBestOfferUtility2 = utilitySpace
1090 .getUtility(opponentBestOfferBid2);
1091 if (utilityToRecord > opponentBestOfferUtility2) {
1092 opponentBestOfferBid2 = bidToRecord;
1093 controlFreqFunc(opponentBestOfferBid2, -1);
1094
1095 }
1096 opponentPreviousBidUtility2 = utilityToRecord;
1097 bestReceivedBid2 = opponentBestOfferBid2;
1098 bestReceivedBidUtility2 = utilitySpace.getUtility(bestReceivedBid2);
1099 }
1100 if (opponentFirstBid1 != null && opponentBestOfferBid2 != null) {
1101 compaction2 = 1 / Math.abs(maxBidUtility
1102 - Math.min(utilitySpace.getUtility(opponentFirstBid1),
1103 utilitySpace.getUtility(opponentFirstBid2)));
1104 }
1105 if (currentTimeIndex != timeSamples.length - 1) {
1106 if (getTime() > timeSamples[currentTimeIndex + 1]) {
1107 if (getTime() > 5 * trainingTimeSeperator) {
1108 EdeltaPast[currentTimeIndex] = discountedExpectation(
1109 getTime() + trainingTimeSeperator);
1110 }
1111 currentTimeIndex++;
1112 }
1113 }
1114 if (countOpp == lastCountOpp + 2) {
1115 (saveInTermsOfTime[currentTimeIndex]).add(Math.min(
1116 opponentPreviousBidUtility1, opponentPreviousBidUtility2));
1117 recordBidInUtilities(bidToRecord);
1118 }
1119 }
1120
1121 private boolean timeToTrain() {
1122 return (getTime() - lastTrainingTime) > trainingTimeSeperator;
1123 }
1124
1125 int lastTrainingIndex = 0;
1126
1127 private void getTrainingData() {
1128 try {
1129 double currentTime = getTime();
1130 int numberOfRows = 0;
1131
1132 for (int i = lastTrainingIndex; i < timeSamples.length; i++) {
1133 double timeIntervalLowerBound = timeSamples[i];
1134 double timeIntervalUpperBound = 1.0;
1135 if (i != timeSamples.length - 1) {
1136 timeIntervalUpperBound = timeSamples[i + 1];
1137 }
1138
1139 double sumU = 0;
1140 double countU = 0;
1141 double maxU = 0;
1142 double timePt = (timeIntervalLowerBound
1143 + timeIntervalUpperBound) / 2;
1144 for (double pastUtility : saveInTermsOfTime[i]) {
1145 if (timePointsAsOutputData.contains(pastUtility))
1146 continue;
1147 sumU += pastUtility;
1148 countU += 1.0;
1149 maxU = Math.max(maxU, pastUtility);
1150 }
1151 if (saveInTermsOfTime[i].size() > 0) {
1152 double meanU = sumU / countU;
1153 maxUtilitiesAsInputData.add(maxU);
1154 timePointsAsOutputData.add(timePt);
1155 numberOfRows++;
1156 }
1157 if (opponentPreviousBidTime1 < timeIntervalUpperBound) {
1158 lastTrainingIndex = i - 1;
1159 break;
1160 }
1161 }
1162
1163 while (maxUtilitiesAsInputData.size() > fixedInputSize) {
1164 maxUtilitiesAsInputData.remove(0);
1165 timePointsAsOutputData.remove(0);
1166 }
1167 numberOfRows = timePointsAsOutputData.size();
1168 double[][] inputData = new double[numberOfRows][1];
1169 double[][] outputData = new double[numberOfRows][1];
1170 for (int i = 0; i < numberOfRows; i++) {
1171 outputData[i][0] = maxUtilitiesAsInputData.get(i);
1172 inputData[i][0] = timePointsAsOutputData.get(i);
1173 }
1174 inputDataMatrix = new Matrix(inputData);
1175 outputDataMatrix = new Matrix(outputData);
1176
1177 } catch (Exception ex) {
1178 // System.out.println("getTrainingData: "+ex.getMessage());
1179 }
1180
1181 }
1182
1183 private void trainGP() {
1184 try {
1185 gp.train(inputDataMatrix, outputDataMatrix, params0);
1186 } catch (Exception e) {
1187 // System.out.println("trainGP: "+e.getMessage());
1188 }
1189
1190 }
1191
1192 @Override
1193 public String getDescription() {
1194 return "ANAC2015";
1195 }
1196
1197}
Note: See TracBrowser for help on using the repository browser.