1 | package geniusweb.exampleparties.timedependentparty;
|
---|
2 |
|
---|
3 | import java.math.BigDecimal;
|
---|
4 | import java.math.BigInteger;
|
---|
5 | import java.util.Collections;
|
---|
6 | import java.util.LinkedList;
|
---|
7 |
|
---|
8 | import geniusweb.bidspace.BidsWithUtility;
|
---|
9 | import geniusweb.bidspace.Interval;
|
---|
10 | import geniusweb.bidspace.IssueInfo;
|
---|
11 | import geniusweb.issuevalue.Bid;
|
---|
12 | import geniusweb.issuevalue.Value;
|
---|
13 | import geniusweb.profile.utilityspace.LinearAdditive;
|
---|
14 | import tudelft.utilities.immutablelist.ImmutableList;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Inner class for TimeDependentParty, made public for testing purposes. This
|
---|
18 | * class may change in the future, use at your own risk.
|
---|
19 | *
|
---|
20 | */
|
---|
21 | public class ExtendedUtilSpace {
|
---|
22 | private LinearAdditive utilspace;
|
---|
23 | /**
|
---|
24 | * The tolerance for a utility. Generally utilities can be this amount
|
---|
25 | * smaller than requested. See also {@link #computeTolerance()}.
|
---|
26 | */
|
---|
27 | private BigDecimal tolerance;
|
---|
28 | private BidsWithUtility bidutils;
|
---|
29 | // min and max achievable utility
|
---|
30 | private BigDecimal minUtil;
|
---|
31 | private BigDecimal maxUtil;
|
---|
32 |
|
---|
33 | public ExtendedUtilSpace(LinearAdditive space) {
|
---|
34 | this.utilspace = space;
|
---|
35 | bidutils = new BidsWithUtility(utilspace);
|
---|
36 | computeMinMax();
|
---|
37 | this.tolerance = computeTolerance();
|
---|
38 |
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Computes the fields minutil and maxUtil.
|
---|
43 | * <p>
|
---|
44 | * TODO this is simplistic, very expensive method and may cause us to run
|
---|
45 | * out of time on large domains.
|
---|
46 | * <p>
|
---|
47 | * Assumes that utilspace and bidutils have been set properly.
|
---|
48 | */
|
---|
49 | private void computeMinMax() {
|
---|
50 | Interval range = bidutils.getRange();
|
---|
51 | this.minUtil = range.getMin();
|
---|
52 | this.maxUtil = range.getMax();
|
---|
53 |
|
---|
54 | Bid rvbid = utilspace.getReservationBid();
|
---|
55 | if (rvbid != null) {
|
---|
56 | BigDecimal rv = utilspace.getUtility(rvbid);
|
---|
57 | if (rv.compareTo(minUtil) > 0)
|
---|
58 | minUtil = rv;
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Tolerance is the Interval we need when searching bids. When we are close
|
---|
64 | * to the maximum utility, this value has to be the distance between the
|
---|
65 | * best and one-but-best utility.
|
---|
66 | *
|
---|
67 | * @return the minimum tolerance required, which is the minimum difference
|
---|
68 | * between the weighted utility of the best and one-but-best issue
|
---|
69 | * value.
|
---|
70 | */
|
---|
71 | protected BigDecimal computeTolerance() {
|
---|
72 | BigDecimal tolerance = BigDecimal.ONE;
|
---|
73 | for (IssueInfo iss : bidutils.getInfo()) {
|
---|
74 | if (iss.getValues().size().compareTo(BigInteger.ONE) > 0) {
|
---|
75 | // we have at least 2 values.
|
---|
76 | LinkedList<BigDecimal> values = new LinkedList<BigDecimal>();
|
---|
77 | for (Value val : iss.getValues()) {
|
---|
78 | values.add(iss.getWeightedUtil(val));
|
---|
79 | }
|
---|
80 | Collections.sort(values);
|
---|
81 | Collections.reverse(values);
|
---|
82 | tolerance = tolerance
|
---|
83 | .min(values.get(0).subtract(values.get(1)));
|
---|
84 | }
|
---|
85 | }
|
---|
86 | return tolerance;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public BigDecimal getMin() {
|
---|
90 | return minUtil;
|
---|
91 | }
|
---|
92 |
|
---|
93 | public BigDecimal getMax() {
|
---|
94 | return maxUtil;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * @param utilityGoal the requested utility
|
---|
99 | * @return bids with utility inside [utilitygoal-{@link #tolerance},
|
---|
100 | * utilitygoal]
|
---|
101 | */
|
---|
102 | public ImmutableList<Bid> getBids(BigDecimal utilityGoal) {
|
---|
103 | return bidutils.getBids(
|
---|
104 | new Interval(utilityGoal.subtract(tolerance), utilityGoal));
|
---|
105 | }
|
---|
106 |
|
---|
107 | }
|
---|