source: src/main/java/negotiator/parties/CustomUtilitySpaceExampleParty.java@ 167

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

Improved examples on how to use BOA components and how to combine them into a single party using BoaParty

File size: 4.2 KB
Line 
1package negotiator.parties;
2
3import java.util.List;
4
5import genius.core.Bid;
6import genius.core.Domain;
7import genius.core.actions.Action;
8import genius.core.actions.Offer;
9import genius.core.parties.AbstractNegotiationParty;
10import genius.core.parties.NegotiationInfo;
11import genius.core.uncertainty.BidRanking;
12import genius.core.uncertainty.ExperimentalUserModel;
13import genius.core.utility.AbstractUtilitySpace;
14import genius.core.utility.CustomUtilitySpace;
15
16/**
17 * Example of a party that deals with preference uncertainty by defining a custom UtilitySpace
18 * based on the closest known bid.
19 *
20 * Given a bid b and a preference ranking o1 <= o2 <= ... < on from the user model, it does the following:
21 * It finds the outcome oi that is 'most similar' to b (in terms of overlapping values)
22 * It then estimates u(b) to be: (i / n) * (highestUtil - lowestUtil)
23 *
24 * Note that this agent's estimate of the utility function is not linear additive.
25 */
26public class CustomUtilitySpaceExampleParty extends AbstractNegotiationParty
27{
28
29 @Override
30 public void init(NegotiationInfo info)
31 {
32 super.init(info);
33 log("This is an example of a party that deals with preference uncertainty by defining with a Custom UtilitySpace estimate.");
34 log("The user model is: " + userModel);
35 if (userModel == null)
36 {
37 log("There is no preference uncertainty. Try this agent with a negotiation scenario that has preference uncertainty enabled.");
38 return;
39 }
40
41 log("Lowest util: " + userModel.getBidRanking().getLowUtility()
42 + ". Highest util: " + userModel.getBidRanking().getHighUtility());
43 log("The estimated utility space is: " + getUtilitySpace());
44
45 Bid randomBid = getUtilitySpace().getDomain().getRandomBid(rand);
46 log("The estimate of the utility of a random bid (" + randomBid + ") is: " + getUtility(randomBid));
47
48 if (userModel instanceof ExperimentalUserModel)
49 {
50 log("You have given the agent access to the real utility space for debugging purposes.");
51 ExperimentalUserModel e = (ExperimentalUserModel) userModel;
52 AbstractUtilitySpace realUSpace = e.getRealUtilitySpace();
53
54 log("The real utility space is: " + realUSpace);
55 log("The real utility of the random bid is: "
56 + realUSpace.getUtility(randomBid));
57 }
58 }
59
60 /**
61 * A simple concession function over time
62 */
63 @Override
64 public Action chooseAction(List<Class<? extends Action>> possibleActions)
65 {
66 double target = 1;
67 // Return a random, conceding offer
68 Bid randomBid;
69 do
70 {
71 randomBid = generateRandomBid();
72 target *= 0.999;
73 }
74 while (getUtility(randomBid) < target);
75 return new Offer(getPartyId(), randomBid);
76 }
77
78 /**
79 * We override the default estimate of the utility
80 * space by using {@link ClosestKnownBid} defined below.
81 */
82 @Override
83 public AbstractUtilitySpace estimateUtilitySpace()
84 {
85 return new ClosestKnownBid(getDomain());
86 }
87
88 @Override
89 public String getDescription() {
90 return "Example agent with a custom utility space";
91 }
92
93 private class ClosestKnownBid extends CustomUtilitySpace
94 {
95
96 public ClosestKnownBid(Domain dom) {
97 super(dom);
98 }
99
100 @Override
101 public double getUtility(Bid bid)
102 {
103 Bid closestRankedBid = getClosestBidRanked(bid);
104 System.out.println("Closest bid: " + closestRankedBid);
105 return estimateUtilityOfRankedBid(closestRankedBid);
106 }
107
108 public double estimateUtilityOfRankedBid(Bid b)
109 {
110 BidRanking bidRanking = getUserModel().getBidRanking();
111 int i = bidRanking.indexOf(b);
112
113 System.out.println("Index: " + i);
114 // index:0 has utility 0, index n-1 has utility 1
115 return i / (double) bidRanking.getSize();
116 }
117
118 /**
119 * Finds the bid in the bid ranking that is most similar to bid given in the argument bid
120 */
121 public Bid getClosestBidRanked(Bid bid)
122 {
123 List<Bid> bidOrder = getUserModel().getBidRanking().getBidOrder();
124 Bid closestBid = null;
125 double closestDistance = Double.MAX_VALUE;
126
127 for (Bid b : bidOrder)
128 {
129 double d = 1 / (double) b.countEqualValues(bid);
130 if (d < closestDistance)
131 {
132 closestDistance = d;
133 closestBid = b;
134 }
135 }
136 return closestBid;
137 }
138
139 }
140
141 private static void log(String s)
142 {
143 System.out.println(s);
144 }
145
146}
Note: See TracBrowser for help on using the repository browser.