source: src/test/java/bilateralexamples/CustomUtilitySpacePartyExample.java@ 187

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

CustomUtilitySpacePartyExample now works for all settings

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