1 | package negotiator.parties;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 |
|
---|
5 | import genius.core.Bid;
|
---|
6 | import genius.core.Domain;
|
---|
7 | import genius.core.actions.Action;
|
---|
8 | import genius.core.actions.Offer;
|
---|
9 | import genius.core.parties.AbstractNegotiationParty;
|
---|
10 | import genius.core.parties.NegotiationInfo;
|
---|
11 | import genius.core.uncertainty.BidRanking;
|
---|
12 | import genius.core.uncertainty.ExperimentalUserModel;
|
---|
13 | import genius.core.utility.AbstractUtilitySpace;
|
---|
14 | import genius.core.utility.CustomUtilitySpace;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Example of a party that deals with preference uncertainty
|
---|
18 | * by defining a custom UtilitySpace based on the closest known bid.
|
---|
19 | */
|
---|
20 | public class CustomUtilitySpaceExampleParty extends AbstractNegotiationParty
|
---|
21 | {
|
---|
22 |
|
---|
23 | @Override
|
---|
24 | public void init(NegotiationInfo info)
|
---|
25 | {
|
---|
26 | super.init(info);
|
---|
27 | log("This is an example of a party that deals with preference uncertainty with a Custom UtilitySpace.");
|
---|
28 | log("The user model is: " + userModel);
|
---|
29 | log("The default estimated utility space is: " + getUtilitySpace());
|
---|
30 |
|
---|
31 | Bid randomBid = getUtilitySpace().getDomain().getRandomBid(rand);
|
---|
32 | log("The default estimate of the utility of a random bid (" + randomBid
|
---|
33 | + ") is: " + getUtility(randomBid));
|
---|
34 |
|
---|
35 | if (userModel instanceof ExperimentalUserModel) {
|
---|
36 | log("You have given the agent access to the real utility space for debugging purposes.");
|
---|
37 | ExperimentalUserModel e = (ExperimentalUserModel) userModel;
|
---|
38 | AbstractUtilitySpace realUSpace = e.getRealUtilitySpace();
|
---|
39 |
|
---|
40 | log("The real utility space is: " + realUSpace);
|
---|
41 | log("The real utility of the random bid is: "
|
---|
42 | + realUSpace.getUtility(randomBid));
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Override
|
---|
47 | public Action chooseAction(List<Class<? extends Action>> possibleActions)
|
---|
48 | {
|
---|
49 | double target = 1;
|
---|
50 | // Return a random, conceding offer
|
---|
51 | Bid randomBid;
|
---|
52 | do
|
---|
53 | {
|
---|
54 | randomBid = generateRandomBid();
|
---|
55 | target *= 0.999;
|
---|
56 | }
|
---|
57 | while (getUtility(randomBid) < target);
|
---|
58 | return new Offer(getPartyId(), randomBid);
|
---|
59 | }
|
---|
60 |
|
---|
61 | private void log(String s) {
|
---|
62 | System.out.println(s);
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * We override the default estimate of the utility
|
---|
67 | * space by using {@link ClosestKnownBid} defined below.
|
---|
68 | */
|
---|
69 | @Override
|
---|
70 | public AbstractUtilitySpace estimateUtilitySpace()
|
---|
71 | {
|
---|
72 | return new ClosestKnownBid(getDomain());
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | public String getDescription() {
|
---|
77 | return "Example agent with a custom utility space";
|
---|
78 | }
|
---|
79 |
|
---|
80 | private class ClosestKnownBid extends CustomUtilitySpace
|
---|
81 | {
|
---|
82 |
|
---|
83 | public ClosestKnownBid(Domain dom) {
|
---|
84 | super(dom);
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public double getUtility(Bid bid)
|
---|
89 | {
|
---|
90 | Bid closestRankedBid = getClosestBidRanked(bid);
|
---|
91 | System.out.println("Closest bid: " + closestRankedBid);
|
---|
92 | return estimateUtilityOfRankedBid(closestRankedBid);
|
---|
93 | }
|
---|
94 |
|
---|
95 | public double estimateUtilityOfRankedBid(Bid b)
|
---|
96 | {
|
---|
97 | BidRanking bidRanking = getUserModel().getBidRanking();
|
---|
98 | int i = bidRanking.indexOf(b);
|
---|
99 |
|
---|
100 | System.out.println("Index: " + i);
|
---|
101 | // index:0 has utility 0, index n-1 has utility 1
|
---|
102 | return i / (double) bidRanking.getSize();
|
---|
103 | }
|
---|
104 |
|
---|
105 | public Bid getClosestBidRanked(Bid bid)
|
---|
106 | {
|
---|
107 | List<Bid> bidOrder = getUserModel().getBidRanking().getBidOrder();
|
---|
108 | Bid closestBid = null;
|
---|
109 | double closestDistance = Double.MAX_VALUE;
|
---|
110 | int rank = 0;
|
---|
111 | int closestRank = 0;
|
---|
112 |
|
---|
113 | for (Bid b : bidOrder)
|
---|
114 | {
|
---|
115 | double d = 1 / (double) b.countEqualValues(bid);
|
---|
116 | if (d < closestDistance)
|
---|
117 | {
|
---|
118 | closestDistance = d;
|
---|
119 | closestBid = b;
|
---|
120 | closestRank = rank;
|
---|
121 | }
|
---|
122 | rank++;
|
---|
123 | }
|
---|
124 | return closestBid;
|
---|
125 | }
|
---|
126 |
|
---|
127 | }
|
---|
128 |
|
---|
129 | }
|
---|