[184] | 1 | package bilateralexamples;
|
---|
[151] | 2 |
|
---|
| 3 | import java.util.List;
|
---|
| 4 |
|
---|
| 5 | import genius.core.Bid;
|
---|
| 6 | import genius.core.Domain;
|
---|
[182] | 7 | import genius.core.actions.Accept;
|
---|
[151] | 8 | import genius.core.actions.Action;
|
---|
| 9 | import genius.core.actions.Offer;
|
---|
| 10 | import genius.core.parties.AbstractNegotiationParty;
|
---|
| 11 | import genius.core.parties.NegotiationInfo;
|
---|
[182] | 12 | import genius.core.timeline.DiscreteTimeline;
|
---|
| 13 | import genius.core.timeline.Timeline.Type;
|
---|
[151] | 14 | import genius.core.uncertainty.BidRanking;
|
---|
| 15 | import genius.core.uncertainty.ExperimentalUserModel;
|
---|
| 16 | import genius.core.utility.AbstractUtilitySpace;
|
---|
| 17 | import genius.core.utility.CustomUtilitySpace;
|
---|
| 18 |
|
---|
[166] | 19 | /**
|
---|
[167] | 20 | * Example of a party that deals with preference uncertainty by defining a custom UtilitySpace
|
---|
| 21 | * based on the closest known bid.
|
---|
| 22 | *
|
---|
| 23 | * Given a bid b and a preference ranking o1 <= o2 <= ... < on from the user model, it does the following:
|
---|
| 24 | * It finds the outcome oi that is 'most similar' to b (in terms of overlapping values)
|
---|
| 25 | * It then estimates u(b) to be: (i / n) * (highestUtil - lowestUtil)
|
---|
| 26 | *
|
---|
| 27 | * Note that this agent's estimate of the utility function is not linear additive.
|
---|
[166] | 28 | */
|
---|
[182] | 29 | @SuppressWarnings("serial")
|
---|
[168] | 30 | public class CustomUtilitySpacePartyExample extends AbstractNegotiationParty
|
---|
[151] | 31 | {
|
---|
| 32 |
|
---|
| 33 | @Override
|
---|
| 34 | public void init(NegotiationInfo info)
|
---|
| 35 | {
|
---|
| 36 | super.init(info);
|
---|
[182] | 37 | log("This is an example of a party that deals with preference uncertainty by defining a Custom UtilitySpace estimate.");
|
---|
[151] | 38 | log("The user model is: " + userModel);
|
---|
[182] | 39 | if (!hasPreferenceUncertainty())
|
---|
[167] | 40 | {
|
---|
| 41 | log("There is no preference uncertainty. Try this agent with a negotiation scenario that has preference uncertainty enabled.");
|
---|
| 42 | return;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | log("Lowest util: " + userModel.getBidRanking().getLowUtility()
|
---|
| 46 | + ". Highest util: " + userModel.getBidRanking().getHighUtility());
|
---|
| 47 | log("The estimated utility space is: " + getUtilitySpace());
|
---|
[151] | 48 |
|
---|
| 49 | Bid randomBid = getUtilitySpace().getDomain().getRandomBid(rand);
|
---|
[167] | 50 | log("The estimate of the utility of a random bid (" + randomBid + ") is: " + getUtility(randomBid));
|
---|
[151] | 51 |
|
---|
[167] | 52 | if (userModel instanceof ExperimentalUserModel)
|
---|
| 53 | {
|
---|
[151] | 54 | log("You have given the agent access to the real utility space for debugging purposes.");
|
---|
| 55 | ExperimentalUserModel e = (ExperimentalUserModel) userModel;
|
---|
| 56 | AbstractUtilitySpace realUSpace = e.getRealUtilitySpace();
|
---|
| 57 |
|
---|
| 58 | log("The real utility space is: " + realUSpace);
|
---|
| 59 | log("The real utility of the random bid is: "
|
---|
| 60 | + realUSpace.getUtility(randomBid));
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[167] | 64 | /**
|
---|
[182] | 65 | * A simple concession function over time, accepting in the last rounds
|
---|
[167] | 66 | */
|
---|
[151] | 67 | @Override
|
---|
| 68 | public Action chooseAction(List<Class<? extends Action>> possibleActions)
|
---|
| 69 | {
|
---|
[182] | 70 | if (timeline.getType() != Type.Rounds)
|
---|
| 71 | {
|
---|
| 72 | log("This agent displays more interesting behavior with a round-based timeline.");
|
---|
| 73 | return new Offer(getPartyId(), generateRandomBid());
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | // Accept in the last 3 rounds
|
---|
| 77 | DiscreteTimeline t = (DiscreteTimeline) timeline;
|
---|
| 78 | if (possibleActions.contains(Accept.class) && t.getOwnRoundsLeft() < 3)
|
---|
| 79 | {
|
---|
| 80 | if (getLastReceivedAction() instanceof Offer)
|
---|
| 81 | return new Accept(getPartyId(), Offer.getBidFromAction(getLastReceivedAction()));
|
---|
| 82 | }
|
---|
[184] | 83 |
|
---|
| 84 | // Sample code that accepts offers that appear in the top 10% of offers
|
---|
| 85 | // in the user model
|
---|
| 86 | if (getLastReceivedAction() instanceof Offer) {
|
---|
| 87 | Bid receivedBid = ((Offer) getLastReceivedAction()).getBid();
|
---|
| 88 | List<Bid> bidOrder = userModel.getBidRanking().getBidOrder();
|
---|
| 89 |
|
---|
| 90 | // If the rank of the received bid is known
|
---|
| 91 | if (bidOrder.contains(receivedBid)) {
|
---|
| 92 | double percentile = (bidOrder.size()
|
---|
| 93 | - bidOrder.indexOf(receivedBid))
|
---|
| 94 | / (double) bidOrder.size();
|
---|
| 95 | if (percentile < 0.1)
|
---|
| 96 | return new Accept(getPartyId(), receivedBid);
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
[182] | 99 |
|
---|
[166] | 100 | // Return a random, conceding offer
|
---|
[151] | 101 | Bid randomBid;
|
---|
[182] | 102 | double target = 1;
|
---|
[151] | 103 | do
|
---|
| 104 | {
|
---|
| 105 | randomBid = generateRandomBid();
|
---|
[166] | 106 | target *= 0.999;
|
---|
[151] | 107 | }
|
---|
[166] | 108 | while (getUtility(randomBid) < target);
|
---|
[151] | 109 | return new Offer(getPartyId(), randomBid);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | /**
|
---|
| 113 | * We override the default estimate of the utility
|
---|
| 114 | * space by using {@link ClosestKnownBid} defined below.
|
---|
| 115 | */
|
---|
| 116 | @Override
|
---|
| 117 | public AbstractUtilitySpace estimateUtilitySpace()
|
---|
| 118 | {
|
---|
| 119 | return new ClosestKnownBid(getDomain());
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | @Override
|
---|
| 123 | public String getDescription() {
|
---|
| 124 | return "Example agent with a custom utility space";
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[182] | 127 | /**
|
---|
| 128 | * Defines a custom UtilitySpace based on the closest known bid to deal with preference uncertainty.
|
---|
| 129 | */
|
---|
[151] | 130 | private class ClosestKnownBid extends CustomUtilitySpace
|
---|
| 131 | {
|
---|
| 132 |
|
---|
| 133 | public ClosestKnownBid(Domain dom) {
|
---|
| 134 | super(dom);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | @Override
|
---|
| 138 | public double getUtility(Bid bid)
|
---|
| 139 | {
|
---|
| 140 | Bid closestRankedBid = getClosestBidRanked(bid);
|
---|
| 141 | return estimateUtilityOfRankedBid(closestRankedBid);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | public double estimateUtilityOfRankedBid(Bid b)
|
---|
| 145 | {
|
---|
| 146 | BidRanking bidRanking = getUserModel().getBidRanking();
|
---|
[182] | 147 | Double min = bidRanking.getLowUtility();
|
---|
| 148 | double max = bidRanking.getHighUtility();
|
---|
| 149 |
|
---|
[151] | 150 | int i = bidRanking.indexOf(b);
|
---|
| 151 |
|
---|
[182] | 152 | // index:0 has utility min, index n-1 has utility max
|
---|
| 153 | return min + i * (max - min) / (double) bidRanking.getSize();
|
---|
[151] | 154 | }
|
---|
| 155 |
|
---|
[167] | 156 | /**
|
---|
| 157 | * Finds the bid in the bid ranking that is most similar to bid given in the argument bid
|
---|
| 158 | */
|
---|
[151] | 159 | public Bid getClosestBidRanked(Bid bid)
|
---|
| 160 | {
|
---|
| 161 | List<Bid> bidOrder = getUserModel().getBidRanking().getBidOrder();
|
---|
| 162 | Bid closestBid = null;
|
---|
| 163 | double closestDistance = Double.MAX_VALUE;
|
---|
| 164 |
|
---|
| 165 | for (Bid b : bidOrder)
|
---|
| 166 | {
|
---|
| 167 | double d = 1 / (double) b.countEqualValues(bid);
|
---|
| 168 | if (d < closestDistance)
|
---|
| 169 | {
|
---|
| 170 | closestDistance = d;
|
---|
| 171 | closestBid = b;
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 | return closestBid;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | }
|
---|
[167] | 178 |
|
---|
| 179 | private static void log(String s)
|
---|
| 180 | {
|
---|
| 181 | System.out.println(s);
|
---|
| 182 | }
|
---|
[151] | 183 |
|
---|
| 184 | }
|
---|