source: src/test/java/uncertaintyexample/CustomUtilitySpacePartyExample.java@ 182

Last change on this file since 182 was 182, checked in by Tim Baarslag, 6 years ago

Improved CustomUtilitySpacePartyExample

File size: 5.1 KB
RevLine 
[168]1package uncertaintyexample;
[151]2
3import java.util.List;
4
5import genius.core.Bid;
6import genius.core.Domain;
[182]7import genius.core.actions.Accept;
[151]8import genius.core.actions.Action;
9import genius.core.actions.Offer;
10import genius.core.parties.AbstractNegotiationParty;
11import genius.core.parties.NegotiationInfo;
[182]12import genius.core.timeline.DiscreteTimeline;
13import genius.core.timeline.Timeline.Type;
[151]14import genius.core.uncertainty.BidRanking;
15import genius.core.uncertainty.ExperimentalUserModel;
16import genius.core.utility.AbstractUtilitySpace;
17import 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]30public 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 }
83
[166]84 // Return a random, conceding offer
[151]85 Bid randomBid;
[182]86 double target = 1;
[151]87 do
88 {
89 randomBid = generateRandomBid();
[166]90 target *= 0.999;
[151]91 }
[166]92 while (getUtility(randomBid) < target);
[151]93 return new Offer(getPartyId(), randomBid);
94 }
95
96 /**
97 * We override the default estimate of the utility
98 * space by using {@link ClosestKnownBid} defined below.
99 */
100 @Override
101 public AbstractUtilitySpace estimateUtilitySpace()
102 {
103 return new ClosestKnownBid(getDomain());
104 }
105
106 @Override
107 public String getDescription() {
108 return "Example agent with a custom utility space";
109 }
110
[182]111 /**
112 * Defines a custom UtilitySpace based on the closest known bid to deal with preference uncertainty.
113 */
[151]114 private class ClosestKnownBid extends CustomUtilitySpace
115 {
116
117 public ClosestKnownBid(Domain dom) {
118 super(dom);
119 }
120
121 @Override
122 public double getUtility(Bid bid)
123 {
124 Bid closestRankedBid = getClosestBidRanked(bid);
125 return estimateUtilityOfRankedBid(closestRankedBid);
126 }
127
128 public double estimateUtilityOfRankedBid(Bid b)
129 {
130 BidRanking bidRanking = getUserModel().getBidRanking();
[182]131 Double min = bidRanking.getLowUtility();
132 double max = bidRanking.getHighUtility();
133
[151]134 int i = bidRanking.indexOf(b);
135
[182]136 // index:0 has utility min, index n-1 has utility max
137 return min + i * (max - min) / (double) bidRanking.getSize();
[151]138 }
139
[167]140 /**
141 * Finds the bid in the bid ranking that is most similar to bid given in the argument bid
142 */
[151]143 public Bid getClosestBidRanked(Bid bid)
144 {
145 List<Bid> bidOrder = getUserModel().getBidRanking().getBidOrder();
146 Bid closestBid = null;
147 double closestDistance = Double.MAX_VALUE;
148
149 for (Bid b : bidOrder)
150 {
151 double d = 1 / (double) b.countEqualValues(bid);
152 if (d < closestDistance)
153 {
154 closestDistance = d;
155 closestBid = b;
156 }
157 }
158 return closestBid;
159 }
160
161 }
[167]162
163 private static void log(String s)
164 {
165 System.out.println(s);
166 }
[151]167
168}
Note: See TracBrowser for help on using the repository browser.