source: opponentmodel/src/main/java/geniusweb/opponentmodel/bayesian/TriangleValueSetUtilities.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: 4.5 KB
Line 
1package geniusweb.opponentmodel.bayesian;
2
3import java.math.BigDecimal;
4import java.math.RoundingMode;
5
6import geniusweb.issuevalue.NumberValue;
7import geniusweb.issuevalue.Value;
8import geniusweb.profile.utilityspace.NumberValueSetUtilities;
9import tudelft.utilities.immutablelist.Range;
10
11/**
12 * Similar to {@link NumberValueSetUtilities} but contains an extra mid point
13 * between low and high value. Utilities are linearly interpolated within the
14 * triangle and 0 outside. This is used by the {@link BayesianOpponentModel} and
15 * not json-serializable.
16 * <p>
17 * DO NOT USE THIS. Private use for {@link BayesianOpponentModel}.
18 */
19class TriangleValueSetUtilities extends NumberValueSetUtilities {
20
21 /**
22 * The value and utility at mid point.
23 */
24 private final BigDecimal midValue;
25 private final BigDecimal midUtility;
26
27 /**
28 *
29 * @param lowValue the low value of the {@link Range}
30 * @param lowUtility the utility of the {@link #lowValue}. must be &ge; 0
31 * @param midValue the mid value of the {@link Range}. must be
32 * &ge;lowValue and &le;highValue.
33 * @param midUtility the utility of the {@link #midValue}.
34 * @param highValue the high value of the {@link Range}. Must be @le;1
35 * @param highUtility the utility of the {@link #highValue}
36 */
37 public TriangleValueSetUtilities(BigDecimal lowValue, BigDecimal lowUtility,
38 BigDecimal midValue, BigDecimal midUtility, BigDecimal highValue,
39 BigDecimal highUtility) {
40 super(lowValue, lowUtility, highValue, highUtility);
41 if (midValue == null || midUtility == null) {
42 throw new NullPointerException(
43 "arguments midValue, midUtility must be non-null");
44 }
45 // ensure 0 <= lowvalue <=midvalue <= highvalue <=1
46 if (midValue.compareTo(lowValue) < 0
47 || midValue.compareTo(highValue) > 0)
48 throw new IllegalArgumentException(
49 "midvalue must be between lowValue and highValue");
50
51 this.midValue = midValue;
52 this.midUtility = midUtility;
53 }
54
55 /**
56 * {@inheritDoc} <
57 * <h2>Details</h2> Any value &ge; {@link #midValue} will be interpolated
58 * from the mid-high interval; any value below the {@link #midValue} with
59 * the low-mid interval. Any value outside low-high interval returns 0.
60 * {@link #midUtility} is returned if value =midValue.
61 */
62 @Override
63 public BigDecimal getUtility(Value value) {
64 if (!(value instanceof NumberValue)) {
65 return BigDecimal.ZERO;
66 }
67 BigDecimal x = ((NumberValue) value).getValue();
68 if (x.compareTo(getLowValue()) < 0 || x.compareTo(getHighValue()) > 0)
69 return BigDecimal.ZERO;
70
71 BigDecimal lowValue, highValue, lowUtility, highUtility;
72
73 if (x.compareTo(midValue) < 0) {
74 lowValue = getLowValue();
75 lowUtility = getLowUtility();
76 highValue = midValue;
77 highUtility = midUtility;
78 } else {
79 lowValue = midValue;
80 lowUtility = midUtility;
81 highValue = getHighValue();
82 highUtility = getHighUtility();
83 }
84 if (x.compareTo(midValue) == 0)
85 return midUtility;
86
87 // we need to be careful to avoid round errors from divides.
88 // so we return lowU + deltaU * (x-lowV) /deltaV
89 BigDecimal deltaU = highUtility.subtract(lowUtility);
90 BigDecimal deltaV = highValue.subtract(lowValue);
91 // deltaV can't be 0 because value!=midValue.
92
93 return lowUtility.add(deltaU.multiply(
94 x.subtract(lowValue).divide(deltaV, 8, RoundingMode.HALF_UP)));
95 }
96
97 /**
98 *
99 * @return the mid value
100 */
101 public BigDecimal getMidValue() {
102 return midValue;
103 }
104
105 /**
106 *
107 * @return the utility of the mid value.
108 */
109 public BigDecimal getMidUtility() {
110 return midUtility;
111 }
112
113 @Override
114 public String toString() {
115 return "TriangleValueSetUtilities(" + getLowValue() + "->"
116 + getLowUtility() + "," + midValue + "->" + midUtility + ","
117 + getHighValue() + "->" + getHighUtility() + ")";
118 }
119
120 @Override
121 public int hashCode() {
122 final int prime = 31;
123 int result = super.hashCode();
124 result = prime * result
125 + ((midUtility == null) ? 0 : midUtility.hashCode());
126 result = prime * result
127 + ((midValue == null) ? 0 : midValue.hashCode());
128 return result;
129 }
130
131 @Override
132 public boolean equals(Object obj) {
133 if (this == obj)
134 return true;
135 if (!super.equals(obj))
136 return false;
137 if (getClass() != obj.getClass())
138 return false;
139 TriangleValueSetUtilities other = (TriangleValueSetUtilities) obj;
140 if (midUtility == null) {
141 if (other.midUtility != null)
142 return false;
143 } else if (!midUtility.equals(other.midUtility))
144 return false;
145 if (midValue == null) {
146 if (other.midValue != null)
147 return false;
148 } else if (!midValue.equals(other.midValue))
149 return false;
150 return true;
151 }
152
153}
Note: See TracBrowser for help on using the repository browser.