1 | package geniusweb.profile.utilityspace;
|
---|
2 |
|
---|
3 | import java.math.BigDecimal;
|
---|
4 | import java.math.RoundingMode;
|
---|
5 |
|
---|
6 | import org.eclipse.jdt.annotation.NonNull;
|
---|
7 |
|
---|
8 | import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
---|
9 | import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
---|
10 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
11 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
12 |
|
---|
13 | import geniusweb.issuevalue.NumberValue;
|
---|
14 | import geniusweb.issuevalue.NumberValueSet;
|
---|
15 | import geniusweb.issuevalue.Value;
|
---|
16 | import geniusweb.issuevalue.ValueSet;
|
---|
17 | import tudelft.utilities.immutablelist.Range;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * The low and high values (from a {@link NumberValueSet} are given each a
|
---|
21 | * different utility. This linearly interpolates in-between utility values.
|
---|
22 | *
|
---|
23 | */
|
---|
24 | @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
|
---|
25 | public class NumberValueSetUtilities implements ValueSetUtilities {
|
---|
26 |
|
---|
27 | /** lowest possible value and utility at that point */
|
---|
28 | private final @NonNull BigDecimal lowValue;
|
---|
29 | private final @NonNull BigDecimal lowUtility;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * highest possible value and utility at that point
|
---|
33 | */
|
---|
34 | private final @NonNull BigDecimal highValue;
|
---|
35 | private final @NonNull BigDecimal highUtility;
|
---|
36 |
|
---|
37 | @JsonCreator
|
---|
38 | /**
|
---|
39 | *
|
---|
40 | * @param lowValue the low value of the {@link Range}
|
---|
41 | * @param lowUtility the utility of the {@link #lowValue}
|
---|
42 | * @param highValue the high value of the {@link Range}. Must be
|
---|
43 | * >lowValue.
|
---|
44 | * @param highUtility the utility of the {@link #highValue}
|
---|
45 | */
|
---|
46 | public NumberValueSetUtilities(
|
---|
47 | @JsonProperty("lowValue") @NonNull BigDecimal lowValue,
|
---|
48 | @JsonProperty("lowUtility") @NonNull BigDecimal lowUtility,
|
---|
49 | @JsonProperty("highValue") @NonNull BigDecimal highValue,
|
---|
50 | @JsonProperty("highUtility") @NonNull BigDecimal highUtility) {
|
---|
51 | if (lowValue == null || highValue == null || lowUtility == null
|
---|
52 | || highUtility == null) {
|
---|
53 | throw new NullPointerException(
|
---|
54 | "arguments lowValue, lowUtility, highValue and highUtility must be non-null");
|
---|
55 | }
|
---|
56 | if (!isInZeroOne(lowUtility)) {
|
---|
57 | throw new IllegalArgumentException("lowUtility must be in [0,1]");
|
---|
58 | }
|
---|
59 | if (!isInZeroOne(highUtility)) {
|
---|
60 | throw new IllegalArgumentException("highUtility must be in [0,1]");
|
---|
61 | }
|
---|
62 | if (highValue.compareTo(lowValue) <= 0) {
|
---|
63 | throw new IllegalArgumentException("highValue must be > lowValue");
|
---|
64 | }
|
---|
65 | this.lowValue = lowValue;
|
---|
66 | this.highValue = highValue;
|
---|
67 | this.lowUtility = lowUtility;
|
---|
68 | this.highUtility = highUtility;
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public @NonNull BigDecimal getUtility(@NonNull Value value) {
|
---|
73 | if (!(value instanceof NumberValue)) {
|
---|
74 | return BigDecimal.ZERO;
|
---|
75 | }
|
---|
76 | @NonNull
|
---|
77 | BigDecimal x = ((NumberValue) value).getValue();
|
---|
78 | if (x.compareTo(lowValue) < 0 || x.compareTo(highValue) > 0)
|
---|
79 | return BigDecimal.ZERO;
|
---|
80 | // we need to be careful to avoid round errors from divides.
|
---|
81 | // so we return lowU + deltaU * (x-lowV) /deltaV
|
---|
82 | @NonNull
|
---|
83 | BigDecimal deltaU = highUtility.subtract(lowUtility);
|
---|
84 | @NonNull
|
---|
85 | BigDecimal deltaV = highValue.subtract(lowValue);
|
---|
86 |
|
---|
87 | return lowUtility.add(deltaU.multiply(
|
---|
88 | x.subtract(lowValue).divide(deltaV, 8, RoundingMode.HALF_UP)));
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | public String isFitting(@NonNull ValueSet valueset) {
|
---|
93 | if (!(valueset instanceof NumberValueSet)) {
|
---|
94 | return "The utilities are for a number valueset but the given values are "
|
---|
95 | + valueset;
|
---|
96 | }
|
---|
97 | @NonNull
|
---|
98 | NumberValueSet numvalset = (NumberValueSet) valueset;
|
---|
99 | if (numvalset.getRange().getLow().compareTo(lowValue) != 0) {
|
---|
100 | return "the utilities are specified down to " + lowValue
|
---|
101 | + " but the valueset starts at "
|
---|
102 | + numvalset.getRange().getLow();
|
---|
103 | }
|
---|
104 | if (numvalset.getRange().getHigh().compareTo(highValue) != 0) {
|
---|
105 | return "the utilities are specified up to " + highValue
|
---|
106 | + " but the valueset ends at "
|
---|
107 | + numvalset.getRange().getHigh();
|
---|
108 | }
|
---|
109 |
|
---|
110 | return null;
|
---|
111 | }
|
---|
112 |
|
---|
113 | @NonNull
|
---|
114 | /**
|
---|
115 | * @return the lowest value
|
---|
116 | */
|
---|
117 | public BigDecimal getLowValue() {
|
---|
118 | return lowValue;
|
---|
119 | }
|
---|
120 |
|
---|
121 | @NonNull
|
---|
122 | /**
|
---|
123 | *
|
---|
124 | * @return the highest value
|
---|
125 | */
|
---|
126 | public BigDecimal getHighValue() {
|
---|
127 | return highValue;
|
---|
128 | }
|
---|
129 |
|
---|
130 | @NonNull
|
---|
131 | /**
|
---|
132 | *
|
---|
133 | * @return the utility of the lowest value
|
---|
134 | */
|
---|
135 | public BigDecimal getLowUtility() {
|
---|
136 | return lowUtility;
|
---|
137 | }
|
---|
138 |
|
---|
139 | @NonNull
|
---|
140 | /**
|
---|
141 | *
|
---|
142 | * @return the utility of the highest value
|
---|
143 | */
|
---|
144 | public BigDecimal getHighUtility() {
|
---|
145 | return highUtility;
|
---|
146 | }
|
---|
147 |
|
---|
148 | @Override
|
---|
149 | public @NonNull String toString() {
|
---|
150 | return "NumberValueSetUtilities(" + lowValue + "->" + lowUtility + ","
|
---|
151 | + highValue + "->" + highUtility + ")";
|
---|
152 | }
|
---|
153 |
|
---|
154 | @Override
|
---|
155 | public int hashCode() {
|
---|
156 | final int prime = 31;
|
---|
157 | int result = 1;
|
---|
158 | result = prime * result
|
---|
159 | + ((highUtility == null) ? 0 : highUtility.hashCode());
|
---|
160 | result = prime * result
|
---|
161 | + ((highValue == null) ? 0 : highValue.hashCode());
|
---|
162 | result = prime * result
|
---|
163 | + ((lowUtility == null) ? 0 : lowUtility.hashCode());
|
---|
164 | result = prime * result
|
---|
165 | + ((lowValue == null) ? 0 : lowValue.hashCode());
|
---|
166 | return result;
|
---|
167 | }
|
---|
168 |
|
---|
169 | @Override
|
---|
170 | public boolean equals(Object obj) {
|
---|
171 | if (this == obj)
|
---|
172 | return true;
|
---|
173 | if (obj == null)
|
---|
174 | return false;
|
---|
175 | if (getClass() != obj.getClass())
|
---|
176 | return false;
|
---|
177 | NumberValueSetUtilities other = (NumberValueSetUtilities) obj;
|
---|
178 | if (highUtility == null) {
|
---|
179 | if (other.highUtility != null)
|
---|
180 | return false;
|
---|
181 | } else if (!highUtility.equals(other.highUtility))
|
---|
182 | return false;
|
---|
183 | if (highValue == null) {
|
---|
184 | if (other.highValue != null)
|
---|
185 | return false;
|
---|
186 | } else if (!highValue.equals(other.highValue))
|
---|
187 | return false;
|
---|
188 | if (lowUtility == null) {
|
---|
189 | if (other.lowUtility != null)
|
---|
190 | return false;
|
---|
191 | } else if (!lowUtility.equals(other.lowUtility))
|
---|
192 | return false;
|
---|
193 | if (lowValue == null) {
|
---|
194 | if (other.lowValue != null)
|
---|
195 | return false;
|
---|
196 | } else if (!lowValue.equals(other.lowValue))
|
---|
197 | return false;
|
---|
198 | return true;
|
---|
199 | }
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Check if value is in range [0,1]
|
---|
203 | *
|
---|
204 | * @param value
|
---|
205 | * @return true if in range.
|
---|
206 | */
|
---|
207 | private static boolean isInZeroOne(@NonNull BigDecimal value) {
|
---|
208 | return value.compareTo(BigDecimal.ZERO) >= 0
|
---|
209 | && value.compareTo(BigDecimal.ONE) <= 0;
|
---|
210 |
|
---|
211 | }
|
---|
212 |
|
---|
213 | }
|
---|