source: exampleparties/timedependentparty/src/main/java/geniusweb/exampleparties/timedependentparty/ExtendedUtilSpace.java@ 52

Last change on this file since 52 was 52, checked in by ruud, 14 months ago

Fixed small issues in domaineditor.

File size: 3.0 KB
Line 
1package geniusweb.exampleparties.timedependentparty;
2
3import java.math.BigDecimal;
4import java.math.BigInteger;
5import java.util.Collections;
6import java.util.LinkedList;
7
8import geniusweb.bidspace.BidsWithUtility;
9import geniusweb.bidspace.Interval;
10import geniusweb.bidspace.IssueInfo;
11import geniusweb.issuevalue.Bid;
12import geniusweb.issuevalue.Value;
13import geniusweb.profile.utilityspace.LinearAdditive;
14import 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 */
21public 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}
Note: See TracBrowser for help on using the repository browser.