1 | package agents;
|
---|
2 |
|
---|
3 | import genius.core.Bid;
|
---|
4 | import genius.core.BidIterator;
|
---|
5 | import genius.core.analysis.BidPoint;
|
---|
6 | import genius.core.analysis.ParetoFrontier;
|
---|
7 | import genius.core.utility.AdditiveUtilitySpace;
|
---|
8 | import genius.core.utility.UtilitySpace;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Like {@link ParetoFrontier}, but with extra functionality. This version loads
|
---|
12 | * the frontier by itself using the given utility spaces.
|
---|
13 | *
|
---|
14 | * <h1>notes</h1>We may decide to move these functions into
|
---|
15 | * {@link ParetoFrontier}.
|
---|
16 | *
|
---|
17 | * When a utility space changes, you currently just need to make a new
|
---|
18 | * {@link ParetoFrontierPlus} from scratch using the constructor. This is
|
---|
19 | * because we can not listen to changes in {@link AdditiveUtilitySpace}s
|
---|
20 | *
|
---|
21 | * @author W.Pasman 3sep14
|
---|
22 | *
|
---|
23 | */
|
---|
24 | public class ParetoFrontierPlus {
|
---|
25 | UtilitySpace mySpace, otherSpace;
|
---|
26 | private ParetoFrontier pareto = null;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Get a {@link ParetoFrontier} for this outcome space.
|
---|
30 | *
|
---|
31 | * @throws Exception
|
---|
32 | */
|
---|
33 | public ParetoFrontierPlus(UtilitySpace spaceMe, UtilitySpace spaceOther) {
|
---|
34 | mySpace = spaceMe;
|
---|
35 | otherSpace = spaceOther;
|
---|
36 | try {
|
---|
37 | computeParetoFrontier();
|
---|
38 | } catch (Exception e) {
|
---|
39 | throw new IllegalStateException(
|
---|
40 | "Failed to compute Pareto Frontier", e);
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * compute the pareto frontier. Warning, this may take some time and may
|
---|
46 | * even run out of memory if the space is really large.
|
---|
47 | *
|
---|
48 | * @throws Exception
|
---|
49 | * if getUtility fails.
|
---|
50 | * */
|
---|
51 | private void computeParetoFrontier() throws Exception {
|
---|
52 | Bid bid;
|
---|
53 |
|
---|
54 | BidIterator bids = new BidIterator(mySpace.getDomain());
|
---|
55 | pareto = new ParetoFrontier();
|
---|
56 | while (bids.hasNext()) {
|
---|
57 | bid = bids.next();
|
---|
58 | BidPoint bp = new BidPoint(bid, mySpace.getUtility(bid),
|
---|
59 | otherSpace.getUtility(bid));
|
---|
60 | pareto.mergeIntoFrontier(bp);
|
---|
61 | }
|
---|
62 |
|
---|
63 | pareto.sort();
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Project a utility onto a pareto frontier point
|
---|
68 | *
|
---|
69 | * @param otherUtil
|
---|
70 | * the utility that the projected point should be close to.
|
---|
71 | * @return a {@link BidPoint} on the pareto that has minimal distance to the
|
---|
72 | * given otherUtil.
|
---|
73 | */
|
---|
74 | public BidPoint getBidNearOpponentUtility(double otherUtil) {
|
---|
75 | BidPoint nearest = null;
|
---|
76 | double dist = 10; // larger than any real distance in utilspace.
|
---|
77 | for (BidPoint bidpoint : pareto.getFrontier()) {
|
---|
78 | double newdist = Math.abs(bidpoint.getUtilityB() - otherUtil);
|
---|
79 | if (newdist < dist) {
|
---|
80 | nearest = bidpoint;
|
---|
81 | dist = newdist;
|
---|
82 | }
|
---|
83 | }
|
---|
84 | return nearest;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * get the bid on the pareto that has myUtility nearest to given target
|
---|
89 | * utility
|
---|
90 | *
|
---|
91 | * @param bid
|
---|
92 | * @return
|
---|
93 | */
|
---|
94 | public BidPoint getBidNearMyUtility(double utility) {
|
---|
95 | BidPoint nearest = null;
|
---|
96 | double dist = 10; // larger than any real distance in utilspace.
|
---|
97 | for (BidPoint bidpoint : pareto.getFrontier()) {
|
---|
98 | double newdist = Math.abs(bidpoint.getUtilityA() - utility);
|
---|
99 | if (newdist < dist) {
|
---|
100 | nearest = bidpoint;
|
---|
101 | dist = newdist;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | return nearest;
|
---|
105 |
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * get the bid on the pareto that has at least the given utility for me (as
|
---|
110 | * close as possible to the target)
|
---|
111 | *
|
---|
112 | * @param utility
|
---|
113 | * target utility for me.
|
---|
114 | * @return bid that is nearest bid above or at target utility for me. May
|
---|
115 | * return null if no such bid.
|
---|
116 | */
|
---|
117 |
|
---|
118 | public BidPoint getBidWithMinimumUtility(double utility) {
|
---|
119 | BidPoint nearest = null;
|
---|
120 | double dist = 10; // larger than any real distance in utilspace.
|
---|
121 | double newdist;
|
---|
122 | for (BidPoint bidpoint : pareto.getFrontier()) {
|
---|
123 | newdist = bidpoint.getUtilityA() - utility;
|
---|
124 | if (newdist > 0 && newdist < dist) {
|
---|
125 | nearest = bidpoint;
|
---|
126 | dist = newdist;
|
---|
127 | }
|
---|
128 | }
|
---|
129 | return nearest;
|
---|
130 | }
|
---|
131 | }
|
---|