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