1 | package geniusweb.profile.utilityspace;
|
---|
2 |
|
---|
3 | import java.math.BigDecimal;
|
---|
4 | import java.util.Collections;
|
---|
5 | import java.util.HashMap;
|
---|
6 | import java.util.HashSet;
|
---|
7 | import java.util.Map;
|
---|
8 |
|
---|
9 | import org.eclipse.jdt.annotation.NonNull;
|
---|
10 |
|
---|
11 | import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
---|
12 | import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
---|
13 |
|
---|
14 | import geniusweb.issuevalue.DiscreteValue;
|
---|
15 | import geniusweb.issuevalue.DiscreteValueSet;
|
---|
16 | import geniusweb.issuevalue.Value;
|
---|
17 | import geniusweb.issuevalue.ValueSet;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Links a set of values to utilities for that value. Does not link to some
|
---|
21 | * issue so may need further checking when used for an actual issue. Constructor
|
---|
22 | * guarantees that
|
---|
23 | * <ul>
|
---|
24 | * <li>All values in the provided map (the utilities) are in [0,1]
|
---|
25 | * <li>All keys are proper {@link DiscreteValue}s
|
---|
26 | * </ul>
|
---|
27 | */
|
---|
28 | @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
|
---|
29 | public class DiscreteValueSetUtilities implements ValueSetUtilities {
|
---|
30 |
|
---|
31 | private @NonNull HashMap<@NonNull DiscreteValue, @NonNull BigDecimal> valueUtilities = new HashMap<>();
|
---|
32 |
|
---|
33 | @SuppressWarnings("unused") // for deserializer
|
---|
34 | private DiscreteValueSetUtilities() {
|
---|
35 |
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * create new object based on the given mapping from values to utilities.
|
---|
40 | *
|
---|
41 | * @param valueUtils map with key {@link DiscreteValue}s and value a Double
|
---|
42 | * in the range [0,1].
|
---|
43 | * @throws NullPointerException if one of the args is null
|
---|
44 | * @throws IllegalArgumentException if values are not in range [0,1].
|
---|
45 | */
|
---|
46 | public DiscreteValueSetUtilities(
|
---|
47 | @NonNull Map<@NonNull DiscreteValue, @NonNull BigDecimal> valueUtils) {
|
---|
48 | if (valueUtils == null) {
|
---|
49 | throw new NullPointerException("valueUtils==null");
|
---|
50 | }
|
---|
51 | if (valueUtils.keySet().contains(null)) {
|
---|
52 | throw new NullPointerException(
|
---|
53 | "one of the keys in valueUtils is null");
|
---|
54 | }
|
---|
55 | /*#PY
|
---|
56 | * if True in (x==None or x<0 pr x>1 for x in self._valueUtils.values():
|
---|
57 | * raise ValueError("part weights must all be in [0,1]")
|
---|
58 | */
|
---|
59 | if (valueUtils.values().stream()
|
---|
60 | .anyMatch(v -> v == null || v.compareTo(BigDecimal.ZERO) < 0
|
---|
61 | || v.compareTo(BigDecimal.ONE) > 0)) {
|
---|
62 | throw new IllegalArgumentException(
|
---|
63 | "Weights in valueUtils must all be in [0,1]");
|
---|
64 | }
|
---|
65 | this.valueUtilities.putAll(valueUtils);
|
---|
66 |
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public @NonNull BigDecimal getUtility(@NonNull Value value) {
|
---|
71 | if (!valueUtilities.containsKey(value)) {
|
---|
72 | return BigDecimal.ZERO;
|
---|
73 | }
|
---|
74 | return valueUtilities.get(value);
|
---|
75 | }
|
---|
76 |
|
---|
77 | @NonNull
|
---|
78 | /**
|
---|
79 | * @return copy of the value-utility pair map.
|
---|
80 | */
|
---|
81 | public Map<@NonNull DiscreteValue, @NonNull BigDecimal> getUtilities() {
|
---|
82 | return Collections.unmodifiableMap(valueUtilities);
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | public String isFitting(@NonNull ValueSet valueset) {
|
---|
87 | if (!(valueset instanceof DiscreteValueSet)) {
|
---|
88 | return "The utilities are for a discrete valueset but the given values are "
|
---|
89 | + valueset;
|
---|
90 | }
|
---|
91 | final @NonNull DiscreteValueSet discvalueset = (DiscreteValueSet) valueset;
|
---|
92 | if (!valueUtilities.keySet()
|
---|
93 | .equals(new HashSet<>(discvalueset.getValues())))
|
---|
94 | return "The values in the set " + valueset
|
---|
95 | + " do not match the values mapped to utilities "
|
---|
96 | + valueUtilities.keySet();
|
---|
97 | return null;
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | public @NonNull String toString() {
|
---|
102 | return "DiscreteValueSetUtilities" + valueUtilities;
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public int hashCode() {
|
---|
107 | final int prime = 31;
|
---|
108 | int result = 1;
|
---|
109 | result = prime * result
|
---|
110 | + ((valueUtilities == null) ? 0 : valueUtilities.hashCode());
|
---|
111 | return result;
|
---|
112 | }
|
---|
113 |
|
---|
114 | @Override
|
---|
115 | public boolean equals(Object obj) {
|
---|
116 | if (this == obj)
|
---|
117 | return true;
|
---|
118 | if (obj == null)
|
---|
119 | return false;
|
---|
120 | if (getClass() != obj.getClass())
|
---|
121 | return false;
|
---|
122 | DiscreteValueSetUtilities other = (DiscreteValueSetUtilities) obj;
|
---|
123 | if (valueUtilities == null) {
|
---|
124 | if (other.valueUtilities != null)
|
---|
125 | return false;
|
---|
126 | } else if (!valueUtilities.equals(other.valueUtilities))
|
---|
127 | return false;
|
---|
128 | return true;
|
---|
129 | }
|
---|
130 |
|
---|
131 | }
|
---|