[151] | 1 | package negotiator.parties;
|
---|
| 2 |
|
---|
| 3 | import java.util.List;
|
---|
| 4 |
|
---|
| 5 | import genius.core.Bid;
|
---|
| 6 | import genius.core.Domain;
|
---|
| 7 | import genius.core.actions.Action;
|
---|
| 8 | import genius.core.actions.Offer;
|
---|
| 9 | import genius.core.parties.AbstractNegotiationParty;
|
---|
| 10 | import genius.core.parties.NegotiationInfo;
|
---|
| 11 | import genius.core.uncertainty.BidRanking;
|
---|
| 12 | import genius.core.uncertainty.ExperimentalUserModel;
|
---|
| 13 | import genius.core.utility.AbstractUtilitySpace;
|
---|
| 14 | import genius.core.utility.CustomUtilitySpace;
|
---|
| 15 |
|
---|
| 16 | public class UncertaintyAgentCustomUtilitySpace extends AbstractNegotiationParty
|
---|
| 17 | {
|
---|
| 18 |
|
---|
| 19 | @Override
|
---|
| 20 | public void init(NegotiationInfo info)
|
---|
| 21 | {
|
---|
| 22 | super.init(info);
|
---|
| 23 | log("This is the Uncertainty Agent example with Custom UtilitySpace.");
|
---|
| 24 | log("The user model is: " + userModel);
|
---|
| 25 | log("The default estimated utility space is: " + getUtilitySpace());
|
---|
| 26 |
|
---|
| 27 | Bid randomBid = getUtilitySpace().getDomain().getRandomBid(rand);
|
---|
| 28 | log("The default estimate of the utility of a random bid (" + randomBid
|
---|
| 29 | + ") is: " + getUtility(randomBid));
|
---|
| 30 |
|
---|
| 31 | if (userModel instanceof ExperimentalUserModel) {
|
---|
| 32 | log("You have given the agent access to the real utility space for debugging purposes.");
|
---|
| 33 | ExperimentalUserModel e = (ExperimentalUserModel) userModel;
|
---|
| 34 | AbstractUtilitySpace realUSpace = e.getRealUtilitySpace();
|
---|
| 35 |
|
---|
| 36 | log("The real utility space is: " + realUSpace);
|
---|
| 37 | log("The real utility of the random bid is: "
|
---|
| 38 | + realUSpace.getUtility(randomBid));
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | @Override
|
---|
| 43 | public Action chooseAction(List<Class<? extends Action>> possibleActions)
|
---|
| 44 | {
|
---|
| 45 | // Otherwise, return a random offer
|
---|
| 46 | Bid randomBid;
|
---|
| 47 | do
|
---|
| 48 | {
|
---|
| 49 | randomBid = generateRandomBid();
|
---|
| 50 | }
|
---|
| 51 | while (getUtility(randomBid) < 0.9);
|
---|
| 52 | return new Offer(getPartyId(), randomBid);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | private void log(String s) {
|
---|
| 56 | System.out.println(s);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
| 60 | * We override the default estimate of the utility
|
---|
| 61 | * space by using {@link ClosestKnownBid} defined below.
|
---|
| 62 | */
|
---|
| 63 | @Override
|
---|
| 64 | public AbstractUtilitySpace estimateUtilitySpace()
|
---|
| 65 | {
|
---|
| 66 | return new ClosestKnownBid(getDomain());
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | @Override
|
---|
| 70 | public String getDescription() {
|
---|
| 71 | return "Example agent with a custom utility space";
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | private class ClosestKnownBid extends CustomUtilitySpace
|
---|
| 75 | {
|
---|
| 76 |
|
---|
| 77 | public ClosestKnownBid(Domain dom) {
|
---|
| 78 | super(dom);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | @Override
|
---|
| 82 | public double getUtility(Bid bid)
|
---|
| 83 | {
|
---|
| 84 | Bid closestRankedBid = getClosestBidRanked(bid);
|
---|
| 85 | System.out.println("Closest bid: " + closestRankedBid);
|
---|
| 86 | return estimateUtilityOfRankedBid(closestRankedBid);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | public double estimateUtilityOfRankedBid(Bid b)
|
---|
| 90 | {
|
---|
| 91 | BidRanking bidRanking = getUserModel().getBidRanking();
|
---|
| 92 | int i = bidRanking.indexOf(b);
|
---|
| 93 |
|
---|
| 94 | System.out.println("Index: " + i);
|
---|
| 95 | // index:0 has utility 0, index n-1 has utility 1
|
---|
| 96 | return i / (double) bidRanking.getSize();
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | public Bid getClosestBidRanked(Bid bid)
|
---|
| 100 | {
|
---|
| 101 | List<Bid> bidOrder = getUserModel().getBidRanking().getBidOrder();
|
---|
| 102 | Bid closestBid = null;
|
---|
| 103 | double closestDistance = Double.MAX_VALUE;
|
---|
| 104 | int rank = 0;
|
---|
| 105 | int closestRank = 0;
|
---|
| 106 |
|
---|
| 107 | for (Bid b : bidOrder)
|
---|
| 108 | {
|
---|
| 109 | double d = 1 / (double) b.countEqualValues(bid);
|
---|
| 110 | if (d < closestDistance)
|
---|
| 111 | {
|
---|
| 112 | closestDistance = d;
|
---|
| 113 | closestBid = b;
|
---|
| 114 | closestRank = rank;
|
---|
| 115 | }
|
---|
| 116 | rank++;
|
---|
| 117 | }
|
---|
| 118 | return closestBid;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | }
|
---|