source: java2python/geniuswebtranslator/geniuswebsrc/geniusweb/profile/utilityspace/NumberValueSetUtilities.java@ 818

Last change on this file since 818 was 818, checked in by wouter, 6 months ago

#278 all code seems annotated @NonNull

File size: 5.9 KB
Line 
1package geniusweb.profile.utilityspace;
2
3import java.math.BigDecimal;
4import java.math.RoundingMode;
5
6import org.eclipse.jdt.annotation.NonNull;
7
8import com.fasterxml.jackson.annotation.JsonAutoDetect;
9import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
10import com.fasterxml.jackson.annotation.JsonCreator;
11import com.fasterxml.jackson.annotation.JsonProperty;
12
13import geniusweb.issuevalue.NumberValue;
14import geniusweb.issuevalue.NumberValueSet;
15import geniusweb.issuevalue.Value;
16import geniusweb.issuevalue.ValueSet;
17import 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)
25public 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 /**
38 *
39 * @param lowValue the low value of the {@link Range}
40 * @param lowUtility the utility of the {@link #lowValue}
41 * @param highValue the high value of the {@link Range}. Must be
42 * >lowValue.
43 * @param highUtility the utility of the {@link #highValue}
44 */
45 @JsonCreator
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 /**
114 * @return the lowest value
115 */
116 public @NonNull BigDecimal getLowValue() {
117 return lowValue;
118 }
119
120 /**
121 *
122 * @return the highest value
123 */
124 public @NonNull BigDecimal getHighValue() {
125 return highValue;
126 }
127
128 /**
129 *
130 * @return the utility of the lowest value
131 */
132 public @NonNull BigDecimal getLowUtility() {
133 return lowUtility;
134 }
135
136 /**
137 *
138 * @return the utility of the highest value
139 */
140 public @NonNull BigDecimal getHighUtility() {
141 return highUtility;
142 }
143
144 @Override
145 public @NonNull String toString() {
146 return "NumberValueSetUtilities(" + lowValue + "->" + lowUtility + ","
147 + highValue + "->" + highUtility + ")";
148 }
149
150 @Override
151 public int hashCode() {
152 final int prime = 31;
153 int result = 1;
154 result = prime * result
155 + ((highUtility == null) ? 0 : highUtility.hashCode());
156 result = prime * result
157 + ((highValue == null) ? 0 : highValue.hashCode());
158 result = prime * result
159 + ((lowUtility == null) ? 0 : lowUtility.hashCode());
160 result = prime * result
161 + ((lowValue == null) ? 0 : lowValue.hashCode());
162 return result;
163 }
164
165 @Override
166 public boolean equals(Object obj) {
167 if (this == obj)
168 return true;
169 if (obj == null)
170 return false;
171 if (getClass() != obj.getClass())
172 return false;
173 NumberValueSetUtilities other = (NumberValueSetUtilities) obj;
174 if (highUtility == null) {
175 if (other.highUtility != null)
176 return false;
177 } else if (!highUtility.equals(other.highUtility))
178 return false;
179 if (highValue == null) {
180 if (other.highValue != null)
181 return false;
182 } else if (!highValue.equals(other.highValue))
183 return false;
184 if (lowUtility == null) {
185 if (other.lowUtility != null)
186 return false;
187 } else if (!lowUtility.equals(other.lowUtility))
188 return false;
189 if (lowValue == null) {
190 if (other.lowValue != null)
191 return false;
192 } else if (!lowValue.equals(other.lowValue))
193 return false;
194 return true;
195 }
196
197 /**
198 * Check if value is in range [0,1]
199 *
200 * @param value
201 * @return true if in range.
202 */
203 private static boolean isInZeroOne(@NonNull BigDecimal value) {
204 return value.compareTo(BigDecimal.ZERO) >= 0
205 && value.compareTo(BigDecimal.ONE) <= 0;
206
207 }
208
209}
Note: See TracBrowser for help on using the repository browser.