1 | package bilateralexamples;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 |
|
---|
5 | import genius.core.Bid;
|
---|
6 | import genius.core.Domain;
|
---|
7 | import genius.core.actions.Accept;
|
---|
8 | import genius.core.actions.Action;
|
---|
9 | import genius.core.actions.Offer;
|
---|
10 | import genius.core.parties.AbstractNegotiationParty;
|
---|
11 | import genius.core.parties.NegotiationInfo;
|
---|
12 | import genius.core.timeline.DiscreteTimeline;
|
---|
13 | import genius.core.timeline.Timeline.Type;
|
---|
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 |
|
---|
19 | /**
|
---|
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.
|
---|
28 | *
|
---|
29 | */
|
---|
30 | @SuppressWarnings("serial")
|
---|
31 | public class CustomUtilitySpacePartyExample extends AbstractNegotiationParty
|
---|
32 | {
|
---|
33 | /** This agent will make bids above the minimuTarget */
|
---|
34 | private double minimumTarget = 1;
|
---|
35 |
|
---|
36 | @Override
|
---|
37 | public void init(NegotiationInfo info)
|
---|
38 | {
|
---|
39 | super.init(info);
|
---|
40 | log("This is an example of a party that deals with preference uncertainty by defining a Custom UtilitySpace estimate.");
|
---|
41 | log("The user model is: " + userModel);
|
---|
42 | if (!hasPreferenceUncertainty())
|
---|
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());
|
---|
51 |
|
---|
52 | Bid randomBid = getUtilitySpace().getDomain().getRandomBid(rand);
|
---|
53 | log("The estimate of the utility of a random bid (" + randomBid + ") is: " + getUtility(randomBid));
|
---|
54 |
|
---|
55 | if (userModel instanceof ExperimentalUserModel)
|
---|
56 | {
|
---|
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 |
|
---|
67 | /**
|
---|
68 | * A simple concession function over time, accepting in the last rounds
|
---|
69 | */
|
---|
70 | @Override
|
---|
71 | public Action chooseAction(List<Class<? extends Action>> possibleActions)
|
---|
72 | {
|
---|
73 | if (timeline.getType() != Type.Rounds || !hasPreferenceUncertainty())
|
---|
74 | {
|
---|
75 | log("This agent displays more interesting behavior with a round-based timeline and preference uncertainty; now it simply generates random bids.");
|
---|
76 | return new Offer(getPartyId(), generateRandomBid());
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
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)
|
---|
83 | {
|
---|
84 | Bid receivedBid = ((Offer) getLastReceivedAction()).getBid();
|
---|
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 |
|
---|
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 | }
|
---|
102 |
|
---|
103 | // Return a random, conceding offer above minimumTarget
|
---|
104 | Bid randomBid;
|
---|
105 | do
|
---|
106 | {
|
---|
107 | randomBid = generateRandomBid();
|
---|
108 | minimumTarget *= 0.999;
|
---|
109 | }
|
---|
110 | while (getUtility(randomBid) < minimumTarget);
|
---|
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 |
|
---|
129 | /**
|
---|
130 | * Defines a custom UtilitySpace based on the closest known bid to deal with preference uncertainty.
|
---|
131 | */
|
---|
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();
|
---|
149 | Double min = bidRanking.getLowUtility();
|
---|
150 | double max = bidRanking.getHighUtility();
|
---|
151 |
|
---|
152 | int i = bidRanking.indexOf(b);
|
---|
153 |
|
---|
154 | // index:0 has utility min, index n-1 has utility max
|
---|
155 | return min + i * (max - min) / (double) bidRanking.getSize();
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Finds the bid in the bid ranking that is most similar to bid given in the argument bid
|
---|
160 | */
|
---|
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 | }
|
---|
180 |
|
---|
181 | private static void log(String s)
|
---|
182 | {
|
---|
183 | System.out.println(s);
|
---|
184 | }
|
---|
185 |
|
---|
186 | }
|
---|