1 | package agents;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.Random;
|
---|
5 |
|
---|
6 | import agents.bayesianopponentmodel.BayesianOpponentModelScalable;
|
---|
7 | import agents.bayesianopponentmodel.OpponentModel;
|
---|
8 | import agents.bayesianopponentmodel.OpponentModelUtilSpace;
|
---|
9 | import genius.core.Agent;
|
---|
10 | import genius.core.Bid;
|
---|
11 | import genius.core.BidIterator;
|
---|
12 | import genius.core.SupportedNegotiationSetting;
|
---|
13 | import genius.core.actions.Accept;
|
---|
14 | import genius.core.actions.Action;
|
---|
15 | import genius.core.actions.EndNegotiation;
|
---|
16 | import genius.core.actions.Offer;
|
---|
17 | import genius.core.analysis.BidPoint;
|
---|
18 | import genius.core.analysis.BidSpace;
|
---|
19 | import genius.core.issue.Issue;
|
---|
20 | import genius.core.utility.AdditiveUtilitySpace;
|
---|
21 | import genius.core.xml.SimpleElement;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Wrapper for opponentmodelspace, so that it is a neat utilityspace that we can
|
---|
25 | * give to the bidspace.
|
---|
26 | *
|
---|
27 | * @author Tim Baarslag & Dmytro Tykhonov
|
---|
28 | */
|
---|
29 |
|
---|
30 | public class BayesianAgent extends Agent {
|
---|
31 |
|
---|
32 | private Action messageOpponent;
|
---|
33 | private Bid myLastBid = null;
|
---|
34 | protected Action myLastAction = null;
|
---|
35 | protected Bid fOpponentPreviousBid = null;
|
---|
36 |
|
---|
37 | private enum ACTIONTYPE {
|
---|
38 | START, OFFER, ACCEPT, BREAKOFF
|
---|
39 | };
|
---|
40 |
|
---|
41 | /** See paper. Standard = TIT_FOR_TAT */
|
---|
42 | private enum STRATEGY {
|
---|
43 | SMART, SERIAL, RESPONSIVE, RANDOM, TIT_FOR_TAT
|
---|
44 | };
|
---|
45 |
|
---|
46 | private STRATEGY fStrategy = STRATEGY.TIT_FOR_TAT;
|
---|
47 | private int fSmartSteps;
|
---|
48 | protected OpponentModel fOpponentModel;
|
---|
49 | private static final double CONCESSIONFACTOR = 0.04;
|
---|
50 | private static final double ALLOWED_UTILITY_DEVIATION = 0.01;
|
---|
51 | private static final int NUMBER_OF_SMART_STEPS = 0;
|
---|
52 | private ArrayList<Bid> myPreviousBids;
|
---|
53 | private boolean fSkipDistanceCalc = false;
|
---|
54 | private boolean logging = !false;
|
---|
55 |
|
---|
56 | // Class constructor
|
---|
57 | public BayesianAgent() {
|
---|
58 | super();
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public String getVersion() {
|
---|
63 | return "2.1";
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | public String getName() {
|
---|
68 | return "Bayesian Agent";
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public void init() {
|
---|
73 | @SuppressWarnings("unused")
|
---|
74 | // cast to ensure AdditiveUtilitySpace
|
---|
75 | AdditiveUtilitySpace a = (AdditiveUtilitySpace) utilitySpace;
|
---|
76 | messageOpponent = null;
|
---|
77 | myLastAction = null;
|
---|
78 | fSmartSteps = 0;
|
---|
79 | myPreviousBids = new ArrayList<Bid>();
|
---|
80 | prepareOpponentModel();
|
---|
81 | }
|
---|
82 |
|
---|
83 | protected void prepareOpponentModel() {
|
---|
84 | fOpponentModel = new BayesianOpponentModelScalable(
|
---|
85 | (AdditiveUtilitySpace) utilitySpace);
|
---|
86 | }
|
---|
87 |
|
---|
88 | // Class methods
|
---|
89 | @Override
|
---|
90 | public void ReceiveMessage(Action opponentAction) {
|
---|
91 | messageOpponent = opponentAction;
|
---|
92 | }
|
---|
93 |
|
---|
94 | private Action proposeInitialBid() throws Exception {
|
---|
95 | Bid lBid = null;
|
---|
96 | // Return (one of the) possible bid(s) with maximal utility.
|
---|
97 | lBid = ((AdditiveUtilitySpace) utilitySpace).getMaxUtilityBid();
|
---|
98 | fSmartSteps = NUMBER_OF_SMART_STEPS;
|
---|
99 | myLastBid = lBid;
|
---|
100 | return new Offer(getAgentID(), lBid);
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | *
|
---|
105 | * @param pOppntBid
|
---|
106 | * @return a counterbid that has max util for us and an opponent utility
|
---|
107 | * that is equal to 1-estimated utility of opponent's last bid. Or,
|
---|
108 | * if that bid was done already before, another bid that has same
|
---|
109 | * utility in our space as that counterbid.
|
---|
110 | * @throws Exception
|
---|
111 | */
|
---|
112 | private Bid getNextBid(Bid pOppntBid) throws Exception {
|
---|
113 | if (pOppntBid == null)
|
---|
114 | throw new NullPointerException("pOpptBid=null");
|
---|
115 | if (myLastBid == null)
|
---|
116 | throw new Exception("myLastBid==null");
|
---|
117 | log("Get next bid ...");
|
---|
118 |
|
---|
119 | BidSpace bs = new BidSpace(utilitySpace,
|
---|
120 | new OpponentModelUtilSpace(fOpponentModel), false, true);
|
---|
121 | // System.out.println("Bidspace:\n"+bs);
|
---|
122 |
|
---|
123 | // compute opponent's concession
|
---|
124 | double opponentConcession = 0.;
|
---|
125 | if (fOpponentPreviousBid == null)
|
---|
126 | opponentConcession = 0;
|
---|
127 | else {
|
---|
128 | double opponentUtil = fOpponentModel
|
---|
129 | .getNormalizedUtility(pOppntBid);
|
---|
130 | double opponentFirstBidUtil = fOpponentModel.getNormalizedUtility(
|
---|
131 | fOpponentModel.fBiddingHistory.get(0));
|
---|
132 | opponentConcession = opponentUtil - opponentFirstBidUtil;
|
---|
133 | }
|
---|
134 | log("opponent Concession:" + opponentConcession);
|
---|
135 |
|
---|
136 | // determine our bid point
|
---|
137 | double OurFirstBidOppUtil = fOpponentModel
|
---|
138 | .getNormalizedUtility(myPreviousBids.get(0));
|
---|
139 | double OurTargetBidOppUtil = OurFirstBidOppUtil - opponentConcession;
|
---|
140 | if (OurTargetBidOppUtil > 1)
|
---|
141 | OurTargetBidOppUtil = 1.;
|
---|
142 | if (OurTargetBidOppUtil < OurFirstBidOppUtil)
|
---|
143 | OurTargetBidOppUtil = OurFirstBidOppUtil;
|
---|
144 | log("our target opponent utility=" + OurTargetBidOppUtil);
|
---|
145 |
|
---|
146 | // find the target on the pareto curve
|
---|
147 | double targetUtil = bs.ourUtilityOnPareto(OurTargetBidOppUtil);
|
---|
148 | // exclude only the last bid
|
---|
149 | // ArrayList<Bid> excludeBids = new ArrayList<Bid>();
|
---|
150 | // excludeBids.add(myPreviousBids.get(myPreviousBids.size()-1));
|
---|
151 | BidPoint bp = bs.getNearestBidPoint(targetUtil, OurTargetBidOppUtil, .5,
|
---|
152 | .1, myPreviousBids);
|
---|
153 | log("found bid " + bp);
|
---|
154 | return bp.getBid();
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * get a new bid (not done before) that has ourUtility for us.
|
---|
159 | *
|
---|
160 | * @param ourUtility
|
---|
161 | * @return the bid with max opponent utility that is close to ourUtility. or
|
---|
162 | * null if there is no such bid.
|
---|
163 | */
|
---|
164 | Bid getNewBidWithUtil(double ourUtility, BidSpace bs) {
|
---|
165 | BidPoint bestbid = null;
|
---|
166 | double bestbidutil = 0;
|
---|
167 | for (BidPoint p : bs.bidPoints) {
|
---|
168 | if (Math.abs(
|
---|
169 | ourUtility - p.getUtilityA()) < ALLOWED_UTILITY_DEVIATION
|
---|
170 | && p.getUtilityB() > bestbidutil
|
---|
171 | && !myPreviousBids.contains(p.getBid())) {
|
---|
172 | bestbid = p;
|
---|
173 | bestbidutil = p.getUtilityB();
|
---|
174 | }
|
---|
175 | }
|
---|
176 | if (bestbid == null)
|
---|
177 | return null;
|
---|
178 | return bestbid.getBid();
|
---|
179 | }
|
---|
180 |
|
---|
181 | private Bid getNextBidSmart(Bid pOppntBid) throws Exception {
|
---|
182 | double lMyUtility, lOppntUtility, lTargetUtility;
|
---|
183 | // Both parties have made an initial bid. Compute associated utilities
|
---|
184 | // from my point of view.
|
---|
185 | lMyUtility = utilitySpace.getUtility(myLastBid);
|
---|
186 | lOppntUtility = utilitySpace.getUtility(pOppntBid);
|
---|
187 | if (fSmartSteps >= NUMBER_OF_SMART_STEPS) {
|
---|
188 | lTargetUtility = getTargetUtility(lMyUtility, lOppntUtility);
|
---|
189 | fSmartSteps = 0;
|
---|
190 | } else {
|
---|
191 | lTargetUtility = lMyUtility;
|
---|
192 | fSmartSteps++;
|
---|
193 | }
|
---|
194 | return getTradeOff(lTargetUtility, pOppntBid);
|
---|
195 | }
|
---|
196 |
|
---|
197 | private Bid getTradeOff(double pUtility, Bid pOppntBid) throws Exception {
|
---|
198 | Bid lBid = null;
|
---|
199 | double lExpectedUtility = -100;
|
---|
200 | BidIterator lIter = new BidIterator(utilitySpace.getDomain());
|
---|
201 | // int i=1;
|
---|
202 | while (lIter.hasNext()) {
|
---|
203 | Bid tmpBid = lIter.next();
|
---|
204 | // System.out.println(tmpBid);
|
---|
205 | // System.out.println(String.valueOf(i++));
|
---|
206 | if (Math.abs(utilitySpace.getUtility(tmpBid)
|
---|
207 | - pUtility) < ALLOWED_UTILITY_DEVIATION) {
|
---|
208 | // double lTmpSim = fSimilarity.getSimilarity(tmpBid,
|
---|
209 | // pOppntBid);
|
---|
210 | double lTmpExpecteUtility = fOpponentModel
|
---|
211 | .getNormalizedUtility(tmpBid);
|
---|
212 | if (lTmpExpecteUtility > lExpectedUtility) {
|
---|
213 | lExpectedUtility = lTmpExpecteUtility;
|
---|
214 | lBid = tmpBid;
|
---|
215 | }
|
---|
216 | }
|
---|
217 | } // while
|
---|
218 | return lBid;
|
---|
219 | }
|
---|
220 |
|
---|
221 | private Bid proposeNextBid(Bid pOppntBid) throws Exception {
|
---|
222 | Bid lBid = null;
|
---|
223 | switch (fStrategy) {
|
---|
224 | case TIT_FOR_TAT:
|
---|
225 | lBid = getNextBid(pOppntBid);
|
---|
226 | break;
|
---|
227 | case SMART:
|
---|
228 | lBid = getNextBidSmart(pOppntBid);
|
---|
229 | break;
|
---|
230 | default:
|
---|
231 | throw new Exception("unknown strategy " + fStrategy);
|
---|
232 | }
|
---|
233 | myLastBid = lBid;
|
---|
234 | return lBid;
|
---|
235 | }
|
---|
236 |
|
---|
237 | @Override
|
---|
238 | public Action chooseAction() {
|
---|
239 | Action lAction = null;
|
---|
240 | ACTIONTYPE lActionType;
|
---|
241 | Bid lOppntBid = null;
|
---|
242 |
|
---|
243 | try {
|
---|
244 | lActionType = getActionType(messageOpponent);
|
---|
245 | switch (lActionType) {
|
---|
246 | case OFFER: // Offer received from opponent
|
---|
247 | lOppntBid = ((Offer) messageOpponent).getBid();
|
---|
248 | // if (fOpponentModel.haveSeenBefore(lOppntBid)) {
|
---|
249 | // lAction=myLastAction; break; }
|
---|
250 | // double lDistance = calculateEuclideanDistanceUtilitySpace();
|
---|
251 | // if(myLastAction==null) dumpDistancesToLog(0);
|
---|
252 | System.out.print("Updating beliefs ...");
|
---|
253 | if (myPreviousBids.size() < 8)
|
---|
254 | fOpponentModel.updateBeliefs(lOppntBid);
|
---|
255 | // dumpDistancesToLog(fRound++);
|
---|
256 | System.out.println("Done!");
|
---|
257 | if (myLastAction == null)
|
---|
258 | // Other agent started, lets propose my initial bid.
|
---|
259 | lAction = proposeInitialBid();
|
---|
260 | else {
|
---|
261 | double offeredutil = utilitySpace.getUtility(lOppntBid);
|
---|
262 | // double time=((new
|
---|
263 | // Date()).getTime()-startTime.getTime())/(1000.*totalTime);
|
---|
264 | // double P=Paccept(offeredutil,time);
|
---|
265 | // log("time="+time+" offeredutil="+offeredutil+" accept
|
---|
266 | // probability P="+P);
|
---|
267 | /* if (.05*P>Math.random()) was here too */
|
---|
268 | if (isAcceptableBefore(offeredutil)) {
|
---|
269 | // Opponent bids equally, or outbids my previous bid, so
|
---|
270 | // lets accept
|
---|
271 | lAction = new Accept(getAgentID(), lOppntBid);
|
---|
272 | log("opponent's bid higher than util of my last bid! accepted");
|
---|
273 | } else {
|
---|
274 | Bid lnextBid = proposeNextBid(lOppntBid);
|
---|
275 | lAction = new Offer(getAgentID(), lnextBid);
|
---|
276 |
|
---|
277 | // Liviu: it doesn't allow proposing null bid
|
---|
278 | if (lnextBid == null)
|
---|
279 | lnextBid = myPreviousBids
|
---|
280 | .get(myPreviousBids.size() - 1);
|
---|
281 |
|
---|
282 | // Propose counteroffer. Get next bid.
|
---|
283 | // Check if utility of the new bid is lower than utility
|
---|
284 | // of the opponent's last bid
|
---|
285 | // if yes then accept last bid of the opponent.
|
---|
286 | // Before 22-12-2010 it was: if (offeredutil*1.03 >=
|
---|
287 | // utilitySpace.getUtility(lnextBid))
|
---|
288 | if (isAcceptableAfter(offeredutil, lnextBid)) {
|
---|
289 | // Opponent bids equally, or outbids my previous
|
---|
290 | // bid, so lets accept
|
---|
291 | lAction = new Accept(getAgentID(), lOppntBid);
|
---|
292 | log("opponent's bid higher than util of my next bid! accepted");
|
---|
293 | }
|
---|
294 |
|
---|
295 | }
|
---|
296 | // remember current bid of the opponent as its previous bid
|
---|
297 | fOpponentPreviousBid = lOppntBid;
|
---|
298 | }
|
---|
299 | break;
|
---|
300 | case ACCEPT:
|
---|
301 | case BREAKOFF:
|
---|
302 | // nothing left to do. Negotiation ended, which should be
|
---|
303 | // checked by
|
---|
304 | // Negotiator...
|
---|
305 | break;
|
---|
306 | default:
|
---|
307 | // I am starting, but not sure whether Negotiator checks this,
|
---|
308 | // so
|
---|
309 | // lets check also myLastAction...
|
---|
310 | if (myLastAction == null) {
|
---|
311 | // dumpDistancesToLog(fRound++);
|
---|
312 | lAction = proposeInitialBid();
|
---|
313 | } else
|
---|
314 | // simply repeat last action
|
---|
315 | lAction = myLastAction;
|
---|
316 | break;
|
---|
317 | }
|
---|
318 | } catch (Exception e) {
|
---|
319 | log("Exception in chooseAction:" + e.getMessage());
|
---|
320 | e.printStackTrace();
|
---|
321 | lAction = new Offer(getAgentID(), myLastBid);
|
---|
322 | }
|
---|
323 | myLastAction = lAction;
|
---|
324 | if (myLastAction instanceof Offer) {
|
---|
325 | myPreviousBids.add(((Offer) myLastAction).getBid());
|
---|
326 | myLastBid = ((Offer) myLastAction).getBid();
|
---|
327 | }
|
---|
328 | return lAction;
|
---|
329 | }
|
---|
330 |
|
---|
331 | /**
|
---|
332 | * Returns whether the offered utility is acceptable after computing a
|
---|
333 | * counter offer.
|
---|
334 | */
|
---|
335 | protected boolean isAcceptableAfter(double offeredutil, Bid lnextBid)
|
---|
336 | throws Exception {
|
---|
337 | return offeredutil >= utilitySpace.getUtility(lnextBid);
|
---|
338 | }
|
---|
339 |
|
---|
340 | /**
|
---|
341 | * Returns whether the offered utility is acceptable before computing a
|
---|
342 | * counter offer.
|
---|
343 | */
|
---|
344 | protected boolean isAcceptableBefore(double offeredutil) throws Exception {
|
---|
345 | return offeredutil * 1.03 >= utilitySpace.getUtility(myLastBid);
|
---|
346 | }
|
---|
347 |
|
---|
348 | private ACTIONTYPE getActionType(Action lAction) {
|
---|
349 | ACTIONTYPE lActionType = ACTIONTYPE.START;
|
---|
350 | if (lAction instanceof Offer)
|
---|
351 | lActionType = ACTIONTYPE.OFFER;
|
---|
352 | else if (lAction instanceof Accept)
|
---|
353 | lActionType = ACTIONTYPE.ACCEPT;
|
---|
354 | else if (lAction instanceof EndNegotiation)
|
---|
355 | lActionType = ACTIONTYPE.BREAKOFF;
|
---|
356 | return lActionType;
|
---|
357 | }
|
---|
358 |
|
---|
359 | private double getTargetUtility(double myUtility, double oppntUtility) {
|
---|
360 | return myUtility - getConcessionFactor();
|
---|
361 | }
|
---|
362 |
|
---|
363 | private double getConcessionFactor() {
|
---|
364 | // The more the agent is willing to concess on its aspiration value, the
|
---|
365 | // higher this factor.
|
---|
366 | return CONCESSIONFACTOR;
|
---|
367 | }
|
---|
368 |
|
---|
369 | /**
|
---|
370 | * Prints out debug information only if the fDebug = true
|
---|
371 | *
|
---|
372 | * @param pMessage
|
---|
373 | * - debug informaton to print
|
---|
374 | */
|
---|
375 | private void log(String pMessage) {
|
---|
376 | if (logging)
|
---|
377 | System.out.println(pMessage);
|
---|
378 | }
|
---|
379 |
|
---|
380 | private double sq(double x) {
|
---|
381 | return x * x;
|
---|
382 | }
|
---|
383 |
|
---|
384 | private double calculateEuclideanDistanceUtilitySpace(double[] pLearnedUtil,
|
---|
385 | double[] pOpponentUtil) {
|
---|
386 | double lDistance = 0;
|
---|
387 | try {
|
---|
388 | for (int i = 0; i < pLearnedUtil.length; i++)
|
---|
389 | lDistance = lDistance + sq(pOpponentUtil[i] - pLearnedUtil[i]);
|
---|
390 | } catch (Exception e) {
|
---|
391 | e.printStackTrace();
|
---|
392 | }
|
---|
393 | lDistance = lDistance
|
---|
394 | / utilitySpace.getDomain().getNumberOfPossibleBids();
|
---|
395 | return lDistance;
|
---|
396 | }
|
---|
397 |
|
---|
398 | private double calculateEuclideanDistanceWeghts(double[] pExpectedWeight) {
|
---|
399 | double lDistance = 0;
|
---|
400 | int i = 0;
|
---|
401 | try {
|
---|
402 | for (Issue lIssue : utilitySpace.getDomain().getIssues()) {
|
---|
403 | lDistance = lDistance + sq(
|
---|
404 | fNegotiation.getOpponentWeight(this, lIssue.getNumber())
|
---|
405 | - pExpectedWeight[i]);
|
---|
406 | i++;
|
---|
407 | }
|
---|
408 | } catch (Exception e) {
|
---|
409 | e.printStackTrace();
|
---|
410 | }
|
---|
411 | return lDistance / i;
|
---|
412 | }
|
---|
413 |
|
---|
414 | private double calculatePearsonDistanceUtilitySpace(
|
---|
415 | double[] pLearnedUtility, double[] pOpponentUtil) {
|
---|
416 | double lDistance = 0;
|
---|
417 | double lAverageLearnedUtil = 0;
|
---|
418 | double lAverageOriginalUtil = 0;
|
---|
419 | // calculate average values
|
---|
420 | for (int i = 0; i < pLearnedUtility.length; i++) {
|
---|
421 | lAverageLearnedUtil = lAverageLearnedUtil + pLearnedUtility[i];
|
---|
422 | lAverageOriginalUtil = lAverageOriginalUtil + pOpponentUtil[i];
|
---|
423 | }
|
---|
424 | lAverageLearnedUtil = lAverageLearnedUtil
|
---|
425 | / (utilitySpace.getDomain().getNumberOfPossibleBids());
|
---|
426 | lAverageOriginalUtil = lAverageOriginalUtil
|
---|
427 | / (utilitySpace.getDomain().getNumberOfPossibleBids());
|
---|
428 | // calculate the distance itself
|
---|
429 | double lSumX = 0;
|
---|
430 | double lSumY = 0;
|
---|
431 | for (int i = 0; i < pLearnedUtility.length; i++) {
|
---|
432 | lDistance = lDistance + (pLearnedUtility[i] - lAverageLearnedUtil)
|
---|
433 | * (pOpponentUtil[i] - lAverageOriginalUtil);
|
---|
434 | lSumX = lSumX + sq(pLearnedUtility[i] - lAverageLearnedUtil);
|
---|
435 | lSumY = lSumY + sq(pOpponentUtil[i] - lAverageOriginalUtil);
|
---|
436 |
|
---|
437 | }
|
---|
438 |
|
---|
439 | return lDistance / (Math.sqrt(lSumX * lSumY));
|
---|
440 | }
|
---|
441 |
|
---|
442 | private double calculatePearsonDistanceWeghts(double[] pExpectedWeight) {
|
---|
443 | double lDistance = 0;
|
---|
444 | double lAverageLearnedWeight = 0;
|
---|
445 | double lAverageOriginalWeight = 0;
|
---|
446 | int i = 0;
|
---|
447 | try {
|
---|
448 | for (Issue lIssue : utilitySpace.getDomain().getIssues()) {
|
---|
449 | lAverageLearnedWeight = lAverageLearnedWeight
|
---|
450 | + pExpectedWeight[i];
|
---|
451 | lAverageOriginalWeight = lAverageOriginalWeight + fNegotiation
|
---|
452 | .getOpponentWeight(this, lIssue.getNumber());
|
---|
453 | i++;
|
---|
454 | }
|
---|
455 | } catch (Exception e) {
|
---|
456 | e.printStackTrace();
|
---|
457 | }
|
---|
458 | lAverageLearnedWeight = lAverageLearnedWeight / (i);
|
---|
459 | lAverageOriginalWeight = lAverageOriginalWeight / (i);
|
---|
460 |
|
---|
461 | // calculate the distance itself
|
---|
462 | i = 0;
|
---|
463 | double lSumX = 0;
|
---|
464 | double lSumY = 0;
|
---|
465 | try {
|
---|
466 | for (Issue lIssue : utilitySpace.getDomain().getIssues()) {
|
---|
467 | lDistance = lDistance + (fNegotiation.getOpponentWeight(this,
|
---|
468 | lIssue.getNumber()) - lAverageOriginalWeight)
|
---|
469 | * (pExpectedWeight[i] - lAverageLearnedWeight);
|
---|
470 | lSumX = lSumX + sq(
|
---|
471 | fNegotiation.getOpponentWeight(this, lIssue.getNumber())
|
---|
472 | - lAverageOriginalWeight);
|
---|
473 | lSumY = lSumY + sq(pExpectedWeight[i] - lAverageLearnedWeight);
|
---|
474 | i++;
|
---|
475 | }
|
---|
476 | } catch (Exception e) {
|
---|
477 | e.printStackTrace();
|
---|
478 | }
|
---|
479 |
|
---|
480 | return lDistance / (Math.sqrt(lSumX * lSumY));
|
---|
481 | }
|
---|
482 |
|
---|
483 | private double calculateRankingDistanceUtilitySpaceMonteCarlo(
|
---|
484 | double[] pLearnedUtil, double[] pOpponentUtil) {
|
---|
485 | double lDistance = 0;
|
---|
486 | int lNumberOfPossibleBids = (int) (utilitySpace.getDomain()
|
---|
487 | .getNumberOfPossibleBids());
|
---|
488 | int lNumberOfComparisons = 10000;
|
---|
489 | for (int k = 0; k < lNumberOfComparisons; k++) {
|
---|
490 | int i = (new Random()).nextInt(lNumberOfPossibleBids - 1);
|
---|
491 | int j = (new Random()).nextInt(lNumberOfPossibleBids - 1);
|
---|
492 | if (((pLearnedUtil[i] > pLearnedUtil[j])
|
---|
493 | && (pOpponentUtil[i] > pOpponentUtil[j]))
|
---|
494 | || ((pLearnedUtil[i] < pLearnedUtil[j])
|
---|
495 | && (pOpponentUtil[i] < pOpponentUtil[j]))
|
---|
496 | || ((pLearnedUtil[i] == pLearnedUtil[j])
|
---|
497 | && (pOpponentUtil[i] == pOpponentUtil[j]))) {
|
---|
498 |
|
---|
499 | } else
|
---|
500 | lDistance++;
|
---|
501 |
|
---|
502 | }
|
---|
503 | return (lDistance) / (lNumberOfComparisons);
|
---|
504 | }
|
---|
505 |
|
---|
506 | private double calculateRankingDistanceUtilitySpace(double[] pLearnedUtil,
|
---|
507 | double[] pOpponentUtil) {
|
---|
508 |
|
---|
509 | double lDistance = 0;
|
---|
510 | int lNumberOfPossibleBids = (int) (utilitySpace.getDomain()
|
---|
511 | .getNumberOfPossibleBids());
|
---|
512 |
|
---|
513 | try {
|
---|
514 | for (int i = 0; i < lNumberOfPossibleBids - 1; i++) {
|
---|
515 | for (int j = i + 1; j < lNumberOfPossibleBids; j++) {
|
---|
516 | // if(i==j) continue;
|
---|
517 | if (Math.signum(pLearnedUtil[i] - pLearnedUtil[j]) != Math
|
---|
518 | .signum(pOpponentUtil[i] - pOpponentUtil[j]))
|
---|
519 | lDistance++;
|
---|
520 |
|
---|
521 | } // for
|
---|
522 | } // for
|
---|
523 | } catch (Exception e) {
|
---|
524 | e.printStackTrace();
|
---|
525 | }
|
---|
526 |
|
---|
527 | lDistance = 2 * lDistance
|
---|
528 | / (utilitySpace.getDomain().getNumberOfPossibleBids()
|
---|
529 | * (utilitySpace.getDomain().getNumberOfPossibleBids()));
|
---|
530 | return lDistance;
|
---|
531 | }
|
---|
532 |
|
---|
533 | private double calculateRankingDistanceWeghts(double pExpectedWeights[]) {
|
---|
534 | double lDistance = 0;
|
---|
535 | double[] lOriginalWeights = new double[utilitySpace.getDomain()
|
---|
536 | .getIssues().size()];
|
---|
537 | int k = 0;
|
---|
538 | try {
|
---|
539 | for (Issue lIssue : utilitySpace.getDomain().getIssues()) {
|
---|
540 | lOriginalWeights[k] = fNegotiation.getOpponentWeight(this,
|
---|
541 | lIssue.getNumber());
|
---|
542 | k++;
|
---|
543 | }
|
---|
544 | } catch (Exception e) {
|
---|
545 | e.printStackTrace();
|
---|
546 | }
|
---|
547 | k = 0;
|
---|
548 | int nrOfIssues = utilitySpace.getDomain().getIssues().size();
|
---|
549 | for (int i = 0; i < nrOfIssues - 1; i++) {
|
---|
550 | for (int j = i + 1; j < nrOfIssues; j++) {
|
---|
551 | k++;
|
---|
552 | double tmpWeightLearned = pExpectedWeights[i];
|
---|
553 | double tmpWeightOriginal = lOriginalWeights[i];
|
---|
554 | double tmpWeight2Learned = pExpectedWeights[j];
|
---|
555 | double tmpWeight2Original = lOriginalWeights[j];
|
---|
556 | if (((tmpWeightLearned > tmpWeight2Learned)
|
---|
557 | && (tmpWeightOriginal > tmpWeight2Original))
|
---|
558 | || ((tmpWeightLearned < tmpWeight2Learned)
|
---|
559 | && (tmpWeightOriginal < tmpWeight2Original))
|
---|
560 | || ((tmpWeightLearned == tmpWeight2Learned)
|
---|
561 | && (tmpWeightOriginal == tmpWeight2Original))) {
|
---|
562 |
|
---|
563 | } else
|
---|
564 | lDistance++;
|
---|
565 |
|
---|
566 | }
|
---|
567 | }
|
---|
568 | return (lDistance) / (k);
|
---|
569 | }
|
---|
570 |
|
---|
571 | protected void dumpDistancesToLog(int pRound) {
|
---|
572 | if (fSkipDistanceCalc)
|
---|
573 | return;
|
---|
574 | System.out.print(getName()
|
---|
575 | + ": calculating distance between the learned space and the original one ...");
|
---|
576 |
|
---|
577 | double lExpectedWeights[] = new double[utilitySpace.getDomain()
|
---|
578 | .getIssues().size()];
|
---|
579 | int i = 0;
|
---|
580 | for (Issue lIssue : utilitySpace.getDomain().getIssues()) {
|
---|
581 | lExpectedWeights[i] = fOpponentModel.getExpectedWeight(i);
|
---|
582 | i++;
|
---|
583 | }
|
---|
584 |
|
---|
585 | double pLearnedUtil[] = new double[(int) (utilitySpace.getDomain()
|
---|
586 | .getNumberOfPossibleBids())];
|
---|
587 | // HashMap<Bid, Double> pLearnedSpace = new HashMap<Bid, Double>();
|
---|
588 | BidIterator lIter = new BidIterator(utilitySpace.getDomain());
|
---|
589 | i = 0;
|
---|
590 | while (lIter.hasNext()) {
|
---|
591 | Bid lBid = lIter.next();
|
---|
592 | try {
|
---|
593 | pLearnedUtil[i] = fOpponentModel.getNormalizedUtility(lBid);
|
---|
594 | // pLearnedSpace.put(lBid, new Double(pLearnedUtil[i]));
|
---|
595 |
|
---|
596 | } catch (Exception e) {
|
---|
597 | e.printStackTrace();
|
---|
598 | }
|
---|
599 | i++;
|
---|
600 | }
|
---|
601 | double pOpponentUtil[] = new double[(int) (utilitySpace.getDomain()
|
---|
602 | .getNumberOfPossibleBids())];
|
---|
603 | // HashMap<Bid, Double> pOpponentSpace = new HashMap<Bid, Double>();
|
---|
604 | lIter = new BidIterator(utilitySpace.getDomain());
|
---|
605 | i = 0;
|
---|
606 | while (lIter.hasNext()) {
|
---|
607 | Bid lBid = lIter.next();
|
---|
608 | try {
|
---|
609 | pOpponentUtil[i] = fNegotiation.getOpponentUtility(this, lBid);
|
---|
610 | // pOpponentSpace.put(lBid, new Double(pOpponentUtil[i]));
|
---|
611 | } catch (Exception e) {
|
---|
612 | e.printStackTrace();
|
---|
613 | }
|
---|
614 | i++;
|
---|
615 | }
|
---|
616 |
|
---|
617 | double lEuclideanDistUtil = calculateEuclideanDistanceUtilitySpace(
|
---|
618 | pLearnedUtil, pOpponentUtil);
|
---|
619 | double lEuclideanDistWeights = calculateEuclideanDistanceWeghts(
|
---|
620 | lExpectedWeights);
|
---|
621 | double lRankingDistUtil = 0;
|
---|
622 | if ((int) (utilitySpace.getDomain().getNumberOfPossibleBids()) > 100000)
|
---|
623 | lRankingDistUtil = calculateRankingDistanceUtilitySpaceMonteCarlo(
|
---|
624 | pLearnedUtil, pOpponentUtil);
|
---|
625 | else
|
---|
626 | lRankingDistUtil = calculateRankingDistanceUtilitySpace(
|
---|
627 | pLearnedUtil, pOpponentUtil);
|
---|
628 | double lRankingDistWeights = calculateRankingDistanceWeghts(
|
---|
629 | lExpectedWeights);
|
---|
630 | double lPearsonDistUtil = calculatePearsonDistanceUtilitySpace(
|
---|
631 | pLearnedUtil, pOpponentUtil);
|
---|
632 | double lPearsonDistWeights = calculatePearsonDistanceWeghts(
|
---|
633 | lExpectedWeights);
|
---|
634 | SimpleElement lLearningPerformance = new SimpleElement(
|
---|
635 | "learning_performance");
|
---|
636 | lLearningPerformance.setAttribute("round", String.valueOf(pRound));
|
---|
637 | lLearningPerformance.setAttribute("agent", getName());
|
---|
638 | lLearningPerformance.setAttribute("euclidean_distance_utility_space",
|
---|
639 | String.valueOf(lEuclideanDistUtil));
|
---|
640 | lLearningPerformance.setAttribute("euclidean_distance_weights",
|
---|
641 | String.valueOf(lEuclideanDistWeights));
|
---|
642 | lLearningPerformance.setAttribute("ranking_distance_utility_space",
|
---|
643 | String.valueOf(lRankingDistUtil));
|
---|
644 | lLearningPerformance.setAttribute("ranking_distance_weights",
|
---|
645 | String.valueOf(lRankingDistWeights));
|
---|
646 | lLearningPerformance.setAttribute("pearson_distance_utility_space",
|
---|
647 | String.valueOf(lPearsonDistUtil));
|
---|
648 | lLearningPerformance.setAttribute("pearson_distance_weights",
|
---|
649 | String.valueOf(lPearsonDistWeights));
|
---|
650 | System.out.println("Done!");
|
---|
651 | System.out.println(lLearningPerformance.toString());
|
---|
652 | fNegotiation.addAdditionalLog(lLearningPerformance);
|
---|
653 |
|
---|
654 | }
|
---|
655 |
|
---|
656 | @Override
|
---|
657 | public SupportedNegotiationSetting getSupportedNegotiationSetting() {
|
---|
658 | return SupportedNegotiationSetting.getLinearUtilitySpaceInstance();
|
---|
659 | }
|
---|
660 |
|
---|
661 | @Override
|
---|
662 | public String getDescription() {
|
---|
663 | return "bayesian opponent model to guide offers";
|
---|
664 | }
|
---|
665 | }
|
---|