source: profile/src/main/java/geniusweb/profile/utilityspace/DiscreteValueSetUtilities.java@ 31

Last change on this file since 31 was 31, checked in by bart, 3 years ago

New protocols Learn and APPLearn. Fixed memory leak.

File size: 3.6 KB
Line 
1package geniusweb.profile.utilityspace;
2
3import java.math.BigDecimal;
4import java.util.Collections;
5import java.util.HashMap;
6import java.util.HashSet;
7import java.util.Map;
8
9import com.fasterxml.jackson.annotation.JsonAutoDetect;
10import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
11import com.fasterxml.jackson.annotation.JsonTypeName;
12
13import geniusweb.issuevalue.DiscreteValue;
14import geniusweb.issuevalue.DiscreteValueSet;
15import geniusweb.issuevalue.Value;
16import geniusweb.issuevalue.ValueSet;
17
18/**
19 * Links a set of values to utilities for that value. Does not link to some
20 * issue so may need further checking when used for an actual issue. Constructor
21 * guarantees that
22 * <ul>
23 * <li>All values in the provided map (the utilities) are in [0,1]
24 * <li>All keys are proper {@link DiscreteValue}s
25 * </ul>
26 */
27@JsonTypeName("discreteutils")
28@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
29public class DiscreteValueSetUtilities implements ValueSetUtilities {
30
31 private HashMap<DiscreteValue, 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 Map<DiscreteValue, 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 if (valueUtils.values().stream()
56 .anyMatch(v -> v == null || v.compareTo(BigDecimal.ZERO) < 0
57 || v.compareTo(BigDecimal.ONE) > 0)) {
58 throw new IllegalArgumentException(
59 "Weights in valueUtils must all be in [0,1]");
60 }
61 this.valueUtilities.putAll(valueUtils);
62
63 }
64
65 @Override
66 public BigDecimal getUtility(Value value) {
67 if (!valueUtilities.containsKey(value)) {
68 return BigDecimal.ZERO;
69 }
70 return valueUtilities.get(value);
71 }
72
73 /**
74 * @return copy of the value-utility pair map.
75 */
76 public Map<DiscreteValue, BigDecimal> getUtilities() {
77 return Collections.unmodifiableMap(valueUtilities);
78 }
79
80 @Override
81 public String isFitting(ValueSet valueset) {
82 if (!(valueset instanceof DiscreteValueSet)) {
83 return "The utilities are for a discrete valueset but the given values are "
84 + valueset;
85 }
86 DiscreteValueSet discvalueset = (DiscreteValueSet) valueset;
87 if (!valueUtilities.keySet()
88 .equals(new HashSet<>(discvalueset.getValues())))
89 return "The values in the set " + valueset
90 + " do not match the values mapped to utilities "
91 + valueUtilities.keySet();
92 return null;
93 }
94
95 @Override
96 public String toString() {
97 return "DiscreteValueSetUtilities" + valueUtilities;
98 }
99
100 @Override
101 public int hashCode() {
102 final int prime = 31;
103 int result = 1;
104 result = prime * result
105 + ((valueUtilities == null) ? 0 : valueUtilities.hashCode());
106 return result;
107 }
108
109 @Override
110 public boolean equals(Object obj) {
111 if (this == obj)
112 return true;
113 if (obj == null)
114 return false;
115 if (getClass() != obj.getClass())
116 return false;
117 DiscreteValueSetUtilities other = (DiscreteValueSetUtilities) obj;
118 if (valueUtilities == null) {
119 if (other.valueUtilities != null)
120 return false;
121 } else if (!valueUtilities.equals(other.valueUtilities))
122 return false;
123 return true;
124 }
125
126}
Note: See TracBrowser for help on using the repository browser.