source: src/main/java/agents/BayesianAgentForAuction.java@ 1

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

Initial import : Genius 9.0.0

File size: 27.8 KB
Line 
1package agents;
2
3import java.util.ArrayList;
4
5import agents.bayesianopponentmodel.BayesianOpponentModel;
6import agents.bayesianopponentmodel.OpponentModel;
7import agents.bayesianopponentmodel.OpponentModelUtilSpace;
8import genius.core.Agent;
9import genius.core.AgentParam;
10import genius.core.Bid;
11import genius.core.BidIterator;
12import genius.core.actions.Accept;
13import genius.core.actions.Action;
14import genius.core.actions.EndNegotiation;
15import genius.core.actions.Offer;
16import genius.core.analysis.BidPoint;
17import genius.core.analysis.BidSpace;
18import genius.core.tournament.VariablesAndValues.AgentParameterVariable;
19import genius.core.utility.AdditiveUtilitySpace;
20
21/**
22 * Wrapper for opponentmodelspace, so that it is a neat utilityspace that we can
23 * give to the bidspace.
24 *
25 * @author wouter
26 *
27 */
28
29public class BayesianAgentForAuction extends Agent {
30
31 protected Action messageOpponent;
32 protected Bid myLastBid = null;
33 protected Action myLastAction = null;
34 protected Bid fOpponentPreviousBid = null;
35
36 protected enum ROLE {
37 CENTER, PROVIDER, IRRELEVANT
38 };
39
40 public enum PHASE {
41 FIRST_PHASE, SECOND_PHASE
42 };
43
44 protected enum ACTIONTYPE {
45 START, OFFER, ACCEPT, BREAKOFF
46 };
47
48 private enum STRATEGY {
49 SMART, SERIAL, RESPONSIVE, RANDOM, TIT_FOR_TAT, AUCTION
50 };
51
52 private STRATEGY fStrategy = STRATEGY.AUCTION;
53 private boolean fMarketPreassure = false;
54 protected int fSmartSteps;
55 protected OpponentModel fOpponentModel;
56 protected OpponentModel[] fOpponentModels = null;
57 protected double CONCESSIONFACTOR = 0.04;
58 private static final double ALLOWED_UTILITY_DEVIATION = 0.01;
59 protected static final int NUMBER_OF_SMART_STEPS = 0;
60 protected ArrayList<Bid> myPreviousBids;
61 private boolean fDebug = false;
62 protected PHASE fPhase = null;
63 protected ROLE fRole;
64
65 // Class constructor
66 public BayesianAgentForAuction() {
67 super();
68
69 }
70
71 @Override
72 public String getVersion() {
73 return "2.0";
74 }
75
76 /** Dummy variables, for testing only. W.Pasman 19aug08 */
77 public static ArrayList<AgentParam> getParameters() {
78 ArrayList<AgentParam> parameters = new ArrayList<AgentParam>();
79 parameters.add(new AgentParam(BayesianAgentForAuction.class.getName(),
80 "reservation", 0., 1.));
81 parameters.add(new AgentParam(BayesianAgentForAuction.class.getName(),
82 "starting_utility", 0., 1.));
83 parameters.add(new AgentParam(BayesianAgentForAuction.class.getName(),
84 "role", -1., 3.));
85 parameters.add(new AgentParam(BayesianAgentForAuction.class.getName(),
86 "phase", -1., 1.));
87 parameters.add(new AgentParam(BayesianAgentForAuction.class.getName(),
88 "opponent", -1., 1.));
89 return parameters;
90 }
91
92 public void init() {
93 messageOpponent = null;
94 myLastAction = null;
95 fSmartSteps = 0;
96 myPreviousBids = new ArrayList<Bid>();
97 fOpponentPreviousBid = null;
98 if ((getParameterValues().isEmpty())
99 || (getParameterValues().get(
100 new AgentParameterVariable(new AgentParam(
101 BayesianAgentForAuction.class.getName(),
102 "phase", new Double(-1.), new Double(1.))))
103 .getValue() < 0))
104 fPhase = PHASE.FIRST_PHASE;
105 else
106 fPhase = PHASE.SECOND_PHASE;
107 if (getParameterValues().isEmpty())
108 fRole = ROLE.IRRELEVANT;
109 else if ((getParameterValues().get(
110 new AgentParameterVariable(new AgentParam(
111 BayesianAgentForAuction.class.getName(), "role", -1.,
112 3.))).getValue() < 0))
113 fRole = ROLE.PROVIDER;
114 else if (getParameterValues().get(
115 new AgentParameterVariable(new AgentParam(
116 BayesianAgentForAuction.class.getName(), "role", -1.,
117 3.))).getValue() < 2) {
118 fRole = ROLE.CENTER;
119 CONCESSIONFACTOR = 0.06;
120 } else
121 fRole = ROLE.IRRELEVANT;
122 if ((fPhase == PHASE.FIRST_PHASE) && (fOpponentModels == null))
123 prepareOpponentModel();
124 if (fRole == ROLE.CENTER) {
125 int index = Double.valueOf(
126 getParameterValues().get(
127 new AgentParameterVariable(new AgentParam(
128 BayesianAgentForAuction.class.getName(),
129 "opponent", -1., 1.))).getValue())
130 .intValue();
131 fOpponentModel = fOpponentModels[index];
132 }
133
134 }
135
136 protected void prepareOpponentModel() {
137 fOpponentModels = new BayesianOpponentModel[2];
138 fOpponentModels[0] = new BayesianOpponentModel(
139 (AdditiveUtilitySpace) utilitySpace);
140 fOpponentModels[1] = new BayesianOpponentModel(
141 (AdditiveUtilitySpace) utilitySpace);
142 fOpponentModel = fOpponentModels[0];
143 }
144
145 // Class methods
146 public void ReceiveMessage(Action opponentAction) {
147 messageOpponent = opponentAction;
148 }
149
150 protected Action proposeInitialBid() throws Exception {
151 Bid lBid = null;
152 switch (fRole) {
153 case CENTER:
154 switch (fPhase) {
155 case FIRST_PHASE:
156 lBid = getMaxUtilityBid();
157 break;
158 case SECOND_PHASE:
159 double lSecondBest = getParameterValues().get(
160 new AgentParameterVariable(new AgentParam(
161 BayesianAgentForAuction.class.getName(),
162 "reservation", 0., 1.))).getValue();
163 lBid = getTradeOff(lSecondBest);
164 break;
165 }
166 break;
167 case PROVIDER:
168 switch (fPhase) {
169 case FIRST_PHASE:
170 double lReservationValue = utilitySpace.getReservationValue();// parametervalues.get("reservation");
171 lBid = getTradeOff(lReservationValue);
172 break;
173 case SECOND_PHASE:
174 lBid = getMaxUtilityBid();
175 break;
176 }
177 break;
178 case IRRELEVANT:
179 switch (fPhase) {
180 case FIRST_PHASE:
181 // if(fRole=ROLE.)
182 lBid = getMaxUtilityBid();
183 break;
184 case SECOND_PHASE:
185 double lSecondBest = getParameterValues().get(
186 new AgentParameterVariable(new AgentParam(
187 BayesianAgentForAuction.class.getName(),
188 "starting_utility", 0., 1.))).getValue();
189 lBid = getTradeOff(lSecondBest);
190 break;
191 }
192 break;
193 }
194 // Return (one of the) possible bid(s) with maximal utility.
195 fSmartSteps = NUMBER_OF_SMART_STEPS;
196 myLastBid = lBid;
197 return new Offer(getAgentID(), lBid);
198 }
199
200 /**
201 *
202 * @param pOppntBid
203 * @return a counterbid that has max util for us and an opponent utility
204 * that is equal to 1-estimated utility of opponent's last bid. Or,
205 * if that bid was done already before, another bid that has same
206 * utility in our space as that counterbid.
207 * @throws Exception
208 */
209 protected Bid getNextBidAuction(Bid pOppntBid) throws Exception {
210 if (pOppntBid == null)
211 throw new NullPointerException("pOpptBid=null");
212 if (myLastBid == null)
213 throw new Exception("myLastBid==null");
214 log("Get next bid ...");
215 Bid lBid = null;
216
217 switch (fRole) {
218 case CENTER:
219 switch (fPhase) {
220 case FIRST_PHASE:
221 lBid = getNextBidSmart(pOppntBid);
222 break;
223 case SECOND_PHASE:
224 double lSecondBest = getParameterValues().get(
225 new AgentParameterVariable(new AgentParam(
226 BayesianAgentForAuction.class.getName(),
227 "reservation", 0., 1.))).getValue();
228 lBid = getTradeOff(lSecondBest);
229 break;
230 }
231 break;
232 case PROVIDER:
233 switch (fPhase) {
234 case FIRST_PHASE:
235 double lReservationValue = utilitySpace.getReservationValue();// parametervalues.get("reservation");
236 lBid = getTradeOff(lReservationValue);
237 break;
238 case SECOND_PHASE:
239 lBid = getNextBidSmart(pOppntBid);
240 break;
241 }
242 break;
243 case IRRELEVANT:
244 switch (fPhase) {
245 case FIRST_PHASE:
246 lBid = getNextBidSmart(pOppntBid);
247 break;
248 case SECOND_PHASE:
249 lBid = getNextBidSmart(pOppntBid);
250 break;
251 }
252 break;
253
254 }
255 return lBid;
256 }
257
258 /**
259 *
260 * @param pOppntBid
261 * @return a counterbid that has max util for us and an opponent utility
262 * that is equal to 1-estimated utility of opponent's last bid. Or,
263 * if that bid was done already before, another bid that has same
264 * utility in our space as that counterbid.
265 * @throws Exception
266 */
267 private Bid getNextBid(Bid pOppntBid) throws Exception {
268 if (pOppntBid == null)
269 throw new NullPointerException("pOpptBid=null");
270 if (myLastBid == null)
271 throw new Exception("myLastBid==null");
272 log("Get next bid ...");
273
274 BidSpace bs = new BidSpace((AdditiveUtilitySpace) utilitySpace,
275 new OpponentModelUtilSpace(fOpponentModel), true, true);
276 // System.out.println("Bidspace:\n"+bs);
277
278 // compute opponent's concession
279 double opponentConcession = 0.;
280 if (fOpponentPreviousBid == null)
281 opponentConcession = 0;
282 else {
283 double opponentUtil = fOpponentModel
284 .getNormalizedUtility(pOppntBid);
285 double opponentFirstBidUtil = fOpponentModel
286 .getNormalizedUtility(fOpponentModel.fBiddingHistory.get(0));
287 opponentConcession = opponentUtil - opponentFirstBidUtil;
288 }
289 log("opponent Concession:" + opponentConcession);
290
291 // determine our bid point
292 double OurFirstBidOppUtil = fOpponentModel
293 .getNormalizedUtility(myPreviousBids.get(0));
294 double OurTargetBidOppUtil = OurFirstBidOppUtil - opponentConcession;
295 if (OurTargetBidOppUtil > 1)
296 OurTargetBidOppUtil = 1.;
297 if (OurTargetBidOppUtil < OurFirstBidOppUtil)
298 OurTargetBidOppUtil = OurFirstBidOppUtil;
299 log("our target opponent utility=" + OurTargetBidOppUtil);
300
301 // find the target on the pareto curve
302 double targetUtil = bs.ourUtilityOnPareto(OurTargetBidOppUtil);
303
304 BidPoint bp = bs.getNearestBidPoint(targetUtil, OurTargetBidOppUtil,
305 .5, 1, myPreviousBids);
306 log("found bid " + bp);
307 return bp.getBid();
308 }
309
310 /**
311 * get a new bid (not done before) that has ourUtility for us.
312 *
313 * @param ourUtility
314 * @return the bid with max opponent utility that is close to ourUtility. or
315 * null if there is no such bid.
316 */
317 Bid getNewBidWithUtil(double ourUtility, BidSpace bs) {
318 BidPoint bestbid = null;
319 double bestbidutil = 0;
320 for (BidPoint p : bs.bidPoints) {
321 if (Math.abs(ourUtility - p.getUtilityA()) < ALLOWED_UTILITY_DEVIATION
322 && p.getUtilityB() > bestbidutil
323 && !myPreviousBids.contains(p.getBid())) {
324 bestbid = p;
325 bestbidutil = p.getUtilityB();
326 }
327 }
328 if (bestbid == null)
329 return null;
330 return bestbid.getBid();
331 }
332
333 /**
334 * Wouter: Try to find a bid that has same utility for ourself but max
335 * utility for opponent.
336 *
337 * @author Dmytro
338 * @param pBid
339 * @return
340 * @throws Exception
341 */
342 protected Bid getSmartBid(Bid pBid) throws Exception {
343 Bid lBid = null;
344 double lExpectedUtility = -1;
345 double lUtility = utilitySpace.getUtility(pBid);
346 BidIterator lIter = new BidIterator(utilitySpace.getDomain());
347 // int i=1;
348 while (lIter.hasNext()) {
349 Bid tmpBid = lIter.next();
350 // System.out.println(tmpBid);
351 // System.out.println(String.valueOf(i++));
352 if (Math.abs(utilitySpace.getUtility(tmpBid) - lUtility) < ALLOWED_UTILITY_DEVIATION) {
353 // double lTmpSim = fSimilarity.getSimilarity(tmpBid,
354 // pOppntBid);
355 double lTmpExpecteUtility = fOpponentModel
356 .getNormalizedUtility(tmpBid);
357 if (lTmpExpecteUtility > lExpectedUtility) {
358 lExpectedUtility = lTmpExpecteUtility;
359 lBid = tmpBid;
360 }
361 }
362 }
363 return lBid;
364 // check if really found a better bid. if not return null
365 /*
366 * if(fOpponentModel.getNormalizedUtility(lBid)>(fOpponentModel.
367 * getNormalizedUtility(pBid)+0.04)) return lBid; else return null;
368 */
369 }
370
371 protected Bid getNextBidSmart(Bid pOppntBid) throws Exception {
372 double lMyUtility, lOppntUtility, lTargetUtility;
373 // Both parties have made an initial bid. Compute associated utilities
374 // from my point of view.
375 lMyUtility = utilitySpace.getUtility(myLastBid);
376 lOppntUtility = utilitySpace.getUtility(pOppntBid);
377 if (fSmartSteps >= NUMBER_OF_SMART_STEPS) {
378 lTargetUtility = getTargetUtility(lMyUtility, lOppntUtility);
379 fSmartSteps = 0;
380 } else {
381 lTargetUtility = lMyUtility;
382 fSmartSteps++;
383 }
384 if (lTargetUtility < utilitySpace.getReservationValue())
385 return null;
386 else
387 return getTradeOff(lTargetUtility);
388 }
389
390 protected Bid getTradeOff(double pUtility) throws Exception {
391 if (pUtility < utilitySpace.getReservationValue())
392 return null;
393 Bid lBid = null;
394 double lExpectedUtility = -100;
395 BidIterator lIter = new BidIterator(utilitySpace.getDomain());
396 // int i=1;
397 while (lIter.hasNext()) {
398 Bid tmpBid = lIter.next();
399 // System.out.println(tmpBid);
400 // System.out.println(String.valueOf(i++));
401 if (fMarketPreassure)
402 if (fNegotiation.getOpponentUtility(this, tmpBid) < 0.3)
403 continue;
404 if (Math.abs(utilitySpace.getUtility(tmpBid) - pUtility) < ALLOWED_UTILITY_DEVIATION) {
405 // double lTmpSim = fSimilarity.getSimilarity(tmpBid,
406 // pOppntBid);
407 double lTmpExpecteUtility = fNegotiation.getOpponentUtility(
408 this, tmpBid); // fOpponentModel.getExpectedUtility(tmpBid);
409 if (lTmpExpecteUtility > lExpectedUtility) {
410 lExpectedUtility = lTmpExpecteUtility;
411 lBid = tmpBid;
412 }
413 }
414 } // while
415 return lBid;
416 }
417
418 protected Bid proposeNextBid(Bid pOppntBid) throws Exception {
419 Bid lBid = null;
420 switch (fStrategy) {
421 case TIT_FOR_TAT:
422 lBid = getNextBid(pOppntBid);
423 break;
424 case SMART:
425 lBid = getNextBidSmart(pOppntBid);
426 break;
427 case AUCTION:
428 lBid = getNextBidAuction(pOppntBid);
429 break;
430 default:
431 throw new Exception("unknown strategy " + fStrategy);
432 }
433 myLastBid = lBid;
434 return lBid;
435 }
436
437 public double getOpponentUtility(Bid bid) throws Exception {
438 return fOpponentModel.getExpectedUtility(bid);
439 }
440
441 public Action chooseAction() {
442 Action lAction = null;
443 ACTIONTYPE lActionType;
444 Bid lOppntBid = null;
445
446 try {
447 lActionType = getActionType(messageOpponent);
448 switch (lActionType) {
449 case OFFER: // Offer received from opponent
450 lOppntBid = ((Offer) messageOpponent).getBid();
451 // if (fOpponentModel.haveSeenBefore(lOppntBid)) {
452 // lAction=myLastAction; break; }
453 // double lDistance = calculateEuclideanDistanceUtilitySpace();
454 // if(myLastAction==null) dumpDistancesToLog(0);
455 System.out.print("Updating beliefs ...");
456 if (myPreviousBids.size() < 8)
457 fOpponentModel.updateBeliefs(lOppntBid);
458 // dumpDistancesToLog(fRound++);
459 System.out.println("Done!");
460 if (myLastAction == null)
461 // Other agent started, lets propose my initial bid.
462 lAction = proposeInitialBid();
463 else {
464 double offeredutil = utilitySpace.getUtility(lOppntBid);
465 double time = timeline.getTime();
466 double P = Paccept(offeredutil, time);
467 log("time=" + time + " offeredutil=" + offeredutil
468 + " accept probability P=" + P);
469 if (utilitySpace.getUtility(lOppntBid) * 1.05 >= utilitySpace
470 .getUtility(myLastBid)
471 /* || .05*P>Math.random() */) {
472 // Opponent bids equally, or outbids my previous bid, so
473 // lets accept
474 lAction = new Accept(getAgentID(), lOppntBid);
475 log("randomly accepted");
476 } else {
477 Bid lnextBid = proposeNextBid(lOppntBid);
478 if (lnextBid == null) {
479 lAction = new EndNegotiation(getAgentID());
480 } else {
481
482 lAction = new Offer(getAgentID(), lnextBid);
483
484 // Propose counteroffer. Get next bid.
485 // Check if utility of the new bid is lower than
486 // utility of the opponent's last bid
487 // if yes then accept last bid of the opponent.
488 if (utilitySpace.getUtility(lOppntBid) * 1.05 >= utilitySpace
489 .getUtility(lnextBid)) {
490 // Opponent bids equally, or outbids my previous
491 // bid, so lets accept
492 lAction = new Accept(getAgentID(), lOppntBid);
493 log("opponent's bid higher than util of my last bid! accepted");
494 }
495 }
496
497 }
498 // remember current bid of the opponent as its previous bid
499 fOpponentPreviousBid = lOppntBid;
500 }
501 break;
502 case ACCEPT:
503 case BREAKOFF:
504 // nothing left to do. Negotiation ended, which should be
505 // checked by
506 // Negotiator...
507 break;
508 default:
509 // I am starting, but not sure whether Negotiator checks this,
510 // so
511 // lets check also myLastAction...
512 if (myLastAction == null) {
513 // dumpDistancesToLog(fRound++);
514 lAction = proposeInitialBid();
515 } else
516 // simply repeat last action
517 lAction = myLastAction;
518 break;
519 }
520 } catch (Exception e) {
521 log("Exception in chooseAction:" + e.getMessage());
522 e.printStackTrace();
523 lAction = new Offer(getAgentID(), myLastBid);
524 }
525 myLastAction = lAction;
526 {
527 myPreviousBids.add(((Offer) myLastAction).getBid());
528 myLastBid = ((Offer) myLastAction).getBid();
529 }
530 return lAction;
531 }
532
533 protected ACTIONTYPE getActionType(Action lAction) {
534 ACTIONTYPE lActionType = ACTIONTYPE.START;
535 if (lAction instanceof Offer)
536 lActionType = ACTIONTYPE.OFFER;
537 else if (lAction instanceof Accept)
538 lActionType = ACTIONTYPE.ACCEPT;
539 else if (lAction instanceof EndNegotiation)
540 lActionType = ACTIONTYPE.BREAKOFF;
541 return lActionType;
542 }
543
544 private double getTargetUtility(double myUtility, double oppntUtility) {
545 return myUtility - getConcessionFactor();
546 }
547
548 private double getConcessionFactor() {
549 // The more the agent is willing to concess on its aspiration value, the
550 // higher this factor.
551 return CONCESSIONFACTOR;
552 }
553
554 /**
555 * Prints out debug information only if the fDebug = true
556 *
557 * @param pMessage
558 * - debug informaton to print
559 */
560 private void log(String pMessage) {
561 if (fDebug)
562 System.out.println(pMessage);
563 }
564
565 /**
566 * This function determines the accept probability for an offer. At t=0 it
567 * will prefer high-utility offers. As t gets closer to 1, it will accept
568 * lower utility offers with increasing probability. it will never accept
569 * offers with utility 0.
570 *
571 * @param u
572 * is the utility
573 * @param t
574 * is the time as fraction of the total available time (t=0 at
575 * start, and t=1 at end time)
576 * @return the probability of an accept at time t
577 * @throws Exception
578 * if you use wrong values for u or t.
579 *
580 */
581 protected double Paccept(double u, double t1) throws Exception {
582 double t = t1 * t1 * t1; // get more relaxed more to the end.
583 if (u < 0 || u > 1.05)
584 throw new Exception("utility " + u + " outside [0,1]");
585 if (t < 0 || t > 1)
586 throw new Exception("time " + t + " outside [0,1]");
587 if (u > 1.)
588 u = 1.;
589
590 if (t == 0.5)
591 return u;
592 return (u - 2. * u * t + 2. * (-1. + t + Math.sqrt(sq(-1. + t) + u
593 * (-1. + 2 * t))))
594 / (-1. + 2 * t);
595 }
596
597 private double sq(double x) {
598 return x * x;
599 }
600
601 /*
602 * private double calculateEuclideanDistanceUtilitySpace(double[]
603 * pLearnedUtil, double[] pOpponentUtil) { double lDistance = 0; try {
604 * for(int i=0;i<pLearnedUtil.length;i++) lDistance = lDistance + sq(
605 * pOpponentUtil[i]-pLearnedUtil[i]); } catch (Exception e) {
606 * e.printStackTrace(); } lDistance = lDistance /
607 * utilitySpace.getDomain().getNumberOfPossibleBids(); return lDistance; }
608 * private double calculateEuclideanDistanceWeghts(double[] pExpectedWeight)
609 * { double lDistance = 0; int i=0; try { for(Issue lIssue :
610 * utilitySpace.getDomain().getIssues()) { lDistance = lDistance
611 * +sq(fNegotiation.getOpponentWeight(this, lIssue.getNumber())
612 * -pExpectedWeight[i]); i++; } } catch (Exception e) { e.printStackTrace();
613 * } return lDistance/(double)i; }
614 *
615 * private double calculatePearsonDistanceUtilitySpace(double[]
616 * pLearnedUtility, double[] pOpponentUtil) { double lDistance = 0; double
617 * lAverageLearnedUtil=0; double lAverageOriginalUtil=0; //calculate average
618 * values for(int i=0;i<pLearnedUtility.length;i++) { lAverageLearnedUtil =
619 * lAverageLearnedUtil + pLearnedUtility[i]; lAverageOriginalUtil =
620 * lAverageOriginalUtil + pOpponentUtil[i]; } lAverageLearnedUtil =
621 * lAverageLearnedUtil
622 * /(double)(utilitySpace.getDomain().getNumberOfPossibleBids());
623 * lAverageOriginalUtil = lAverageOriginalUtil/
624 * (double)(utilitySpace.getDomain().getNumberOfPossibleBids()); //calculate
625 * the distance itself double lSumX=0; double lSumY=0; for(int
626 * i=0;i<pLearnedUtility.length;i++) { lDistance = lDistance +
627 * (pLearnedUtility[i]-lAverageLearnedUtil)*
628 * (pOpponentUtil[i]-lAverageOriginalUtil); lSumX = lSumX +
629 * sq(pLearnedUtility[i]-lAverageLearnedUtil); lSumY = lSumY +
630 * sq(pOpponentUtil[i]-lAverageOriginalUtil);
631 *
632 * }
633 *
634 *
635 * return lDistance/(Math.sqrt(lSumX*lSumY)); }
636 *
637 * private double calculatePearsonDistanceWeghts(double[] pExpectedWeight) {
638 * double lDistance = 0; double lAverageLearnedWeight=0; double
639 * lAverageOriginalWeight=0; int i=0; try { for(Issue lIssue :
640 * utilitySpace.getDomain().getIssues()) { lAverageLearnedWeight =
641 * lAverageLearnedWeight +pExpectedWeight[i]; lAverageOriginalWeight =
642 * lAverageOriginalWeight + fNegotiation.getOpponentWeight(this,
643 * lIssue.getNumber()); i++; } } catch (Exception e) { e.printStackTrace();
644 * } lAverageLearnedWeight = lAverageLearnedWeight/(double)(i);
645 * lAverageOriginalWeight= lAverageOriginalWeight/ (double)(i);
646 *
647 * //calculate the distance itself i=0; double lSumX=0; double lSumY=0; try
648 * { for(Issue lIssue : utilitySpace.getDomain().getIssues()) { lDistance =
649 * lDistance +(fNegotiation.getOpponentWeight(this, lIssue.getNumber())-
650 * lAverageOriginalWeight)*(pExpectedWeight[i]-lAverageLearnedWeight); lSumX
651 * = lSumX + sq(fNegotiation.getOpponentWeight(this, lIssue.getNumber())-
652 * lAverageOriginalWeight); lSumY = lSumY +
653 * sq(pExpectedWeight[i]-lAverageLearnedWeight); i++; } } catch (Exception
654 * e) { e.printStackTrace(); }
655 *
656 * return lDistance/(Math.sqrt(lSumX*lSumY)); } private double
657 * calculateRankingDistanceUtilitySpaceMonteCarlo(double[] pLearnedUtil,
658 * double[] pOpponentUtil) { double lDistance = 0; int lNumberOfPossibleBids
659 * = (int)(utilitySpace.getDomain().getNumberOfPossibleBids()); int
660 * lNumberOfComparisons = 10000000; for(int k=0;k<lNumberOfComparisons ;k++)
661 * { int i = (new Random()).nextInt(lNumberOfPossibleBids-1); int j = (new
662 * Random()).nextInt(lNumberOfPossibleBids-1);
663 * if(((pLearnedUtil[i]>pLearnedUtil
664 * [j])&&(pOpponentUtil[i]>pOpponentUtil[j]))||
665 * ((pLearnedUtil[i]<pLearnedUtil
666 * [j])&&(pOpponentUtil[i]<pOpponentUtil[j]))||
667 * ((pLearnedUtil[i]==pLearnedUtil
668 * [j])&&(pOpponentUtil[i]==pOpponentUtil[j]))) {
669 *
670 * } else lDistance++;
671 *
672 * } return ((double)lDistance)/((double)lNumberOfComparisons); } private
673 * double calculateRankingDistanceUtilitySpace(double[] pLearnedUtil,
674 * double[] pOpponentUtil) {
675 *
676 * double lDistance = 0; int lNumberOfPossibleBids =
677 * (int)(utilitySpace.getDomain().getNumberOfPossibleBids());
678 *
679 * try { for(int i=0;i<lNumberOfPossibleBids-1;i++) { for(int
680 * j=i+1;j<lNumberOfPossibleBids;j++) { //if(i==j) continue; if
681 * (Math.signum(
682 * pLearnedUtil[i]-pLearnedUtil[j])!=Math.signum(pOpponentUtil[i
683 * ]-pOpponentUtil[j])) lDistance++;
684 *
685 * } //for } //for } catch (Exception e) { e.printStackTrace(); }
686 *
687 * lDistance = 2 * lDistance /
688 * (utilitySpace.getDomain().getNumberOfPossibleBids
689 * ()*(utilitySpace.getDomain().getNumberOfPossibleBids())); return
690 * lDistance; } private double calculateRankingDistanceWeghts(double
691 * pExpectedWeights[]) { double lDistance = 0; double[] lOriginalWeights =
692 * new double[utilitySpace.getDomain().getIssues().size()]; int k=0; try {
693 * for(Issue lIssue : utilitySpace.getDomain().getIssues()) {
694 * lOriginalWeights[k] = fNegotiation.getOpponentWeight(this,
695 * lIssue.getNumber()); k++; } } catch (Exception e) { e.printStackTrace();
696 * } k=0; int nrOfIssues = utilitySpace.getDomain().getIssues().size();
697 * for(int i=0; i<nrOfIssues-1;i++) { for(int j=i+1;j<nrOfIssues;j++) { k++;
698 * double tmpWeightLearned = pExpectedWeights[i]; double tmpWeightOriginal =
699 * lOriginalWeights[i]; double tmpWeight2Learned = pExpectedWeights[j];
700 * double tmpWeight2Original = lOriginalWeights[j];
701 * if(((tmpWeightLearned>tmpWeight2Learned
702 * )&&(tmpWeightOriginal>tmpWeight2Original))||
703 * ((tmpWeightLearned<tmpWeight2Learned
704 * )&&(tmpWeightOriginal<tmpWeight2Original))||
705 * ((tmpWeightLearned==tmpWeight2Learned
706 * )&&(tmpWeightOriginal==tmpWeight2Original))) {
707 *
708 * } else lDistance++;
709 *
710 * } } return ((double)lDistance)/((double)k); }
711 *
712 * protected void dumpDistancesToLog(int pRound) { if (fSkipDistanceCalc)
713 * return; System.out.print(getName() +
714 * ": calculating distance between the learned space and the original one ..."
715 * );
716 *
717 * double lExpectedWeights[] = new
718 * double[utilitySpace.getDomain().getIssues().size()]; int i=0; for(Issue
719 * lIssue : utilitySpace.getDomain().getIssues()) {
720 * lExpectedWeights[i]=fOpponentModel.getExpectedWeight(i); i++; }
721 *
722 *
723 * double pLearnedUtil[] = new
724 * double[(int)(utilitySpace.getDomain().getNumberOfPossibleBids())]; //
725 * HashMap<Bid, Double> pLearnedSpace = new HashMap<Bid, Double>();
726 * BidIterator lIter = new BidIterator( utilitySpace.getDomain()); i=0;
727 * while(lIter.hasNext()) { Bid lBid = lIter.next(); try { pLearnedUtil[i]
728 * =fOpponentModel.getNormalizedUtility(lBid); // pLearnedSpace.put(lBid,
729 * new Double(pLearnedUtil[i]));
730 *
731 * } catch (Exception e) { e.printStackTrace(); } i++; } double
732 * pOpponentUtil[] = new
733 * double[(int)(utilitySpace.getDomain().getNumberOfPossibleBids())]; //
734 * HashMap<Bid, Double> pOpponentSpace = new HashMap<Bid, Double>(); lIter =
735 * new BidIterator( utilitySpace.getDomain()); i=0; while(lIter.hasNext()) {
736 * Bid lBid = lIter.next(); try { pOpponentUtil[i] =
737 * fNegotiation.getOpponentUtility(this, lBid); // pOpponentSpace.put(lBid,
738 * new Double(pOpponentUtil[i])); } catch (Exception e) {
739 * e.printStackTrace(); } i++; }
740 *
741 * double lEuclideanDistUtil =
742 * calculateEuclideanDistanceUtilitySpace(pLearnedUtil,pOpponentUtil);
743 * double lEuclideanDistWeights =
744 * calculateEuclideanDistanceWeghts(lExpectedWeights); double
745 * lRankingDistUtil = 0;
746 * if((int)(utilitySpace.getDomain().getNumberOfPossibleBids())>100000)
747 * lRankingDistUtil =
748 * calculateRankingDistanceUtilitySpaceMonteCarlo(pLearnedUtil,
749 * pOpponentUtil); else lRankingDistUtil =
750 * calculateRankingDistanceUtilitySpace(pLearnedUtil, pOpponentUtil); double
751 * lRankingDistWeights = calculateRankingDistanceWeghts(lExpectedWeights);
752 * double lPearsonDistUtil =
753 * calculatePearsonDistanceUtilitySpace(pLearnedUtil,pOpponentUtil); double
754 * lPearsonDistWeights = calculatePearsonDistanceWeghts(lExpectedWeights);
755 * SimpleElement lLearningPerformance = new
756 * SimpleElement("learning_performance");
757 * lLearningPerformance.setAttribute("round", String.valueOf(pRound));
758 * lLearningPerformance.setAttribute("euclidean_distance_utility_space",
759 * String.valueOf(lEuclideanDistUtil));
760 * lLearningPerformance.setAttribute("euclidean_distance_weights",
761 * String.valueOf(lEuclideanDistWeights));
762 * lLearningPerformance.setAttribute("ranking_distance_utility_space",
763 * String.valueOf(lRankingDistUtil));
764 * lLearningPerformance.setAttribute("ranking_distance_weights",
765 * String.valueOf(lRankingDistWeights));
766 * lLearningPerformance.setAttribute("pearson_distance_utility_space",
767 * String.valueOf(lPearsonDistUtil));
768 * lLearningPerformance.setAttribute("pearson_distance_weights",
769 * String.valueOf(lPearsonDistWeights)); System.out.println("Done!");
770 * System.out.println(lLearningPerformance.toString());
771 * fNegotiation.addAdditionalLog(lLearningPerformance);
772 *
773 * }
774 */
775 public final Bid getMaxUtilityBid() throws Exception {
776 Bid maxBid = null;
777 double maxutil = 0.;
778 BidIterator bidit = new BidIterator(utilitySpace.getDomain());
779
780 if (bidit.hasNext())
781 maxBid = bidit.next();
782 else
783 throw new Exception("The domain does not contain any bids!");
784 while (bidit.hasNext()) {
785 Bid thisBid = bidit.next();
786 if (fMarketPreassure)
787 if (fNegotiation.getOpponentUtility(this, thisBid) < 0.3)
788 continue;
789 double thisutil = utilitySpace.getUtility(thisBid);
790 if (thisutil > maxutil) {
791 maxutil = thisutil;
792 maxBid = thisBid;
793 }
794 }
795 return maxBid;
796 }
797
798}
Note: See TracBrowser for help on using the repository browser.