[4] | 1 | package geniusweb.bidspace;
|
---|
| 2 |
|
---|
| 3 | import java.math.BigDecimal;
|
---|
| 4 | import java.math.RoundingMode;
|
---|
| 5 | import java.util.ArrayList;
|
---|
| 6 | import java.util.HashMap;
|
---|
| 7 | import java.util.List;
|
---|
| 8 | import java.util.Map;
|
---|
| 9 |
|
---|
| 10 | import geniusweb.issuevalue.Value;
|
---|
| 11 | import geniusweb.issuevalue.ValueSet;
|
---|
| 12 | import geniusweb.profile.utilityspace.ValueSetUtilities;
|
---|
| 13 |
|
---|
| 14 | /**
|
---|
| 15 | * Tool class to collect all relevant info about one issue (from LinearAdditive)
|
---|
| 16 | * in one class. Used for internally grouping data for more efficient
|
---|
| 17 | * processing. This class may change in the future, not recommended for direct
|
---|
| 18 | * use.
|
---|
| 19 | * <p>
|
---|
| 20 | * immutable
|
---|
| 21 | */
|
---|
| 22 | public class IssueInfo {
|
---|
| 23 | private final String name;
|
---|
| 24 | private final ValueSet values;
|
---|
| 25 | private final Interval interval;
|
---|
| 26 | private final Map<Value, BigDecimal> weightedUtils;
|
---|
| 27 |
|
---|
| 28 | public IssueInfo(String name, ValueSet values, ValueSetUtilities utils,
|
---|
| 29 | BigDecimal weight, int precision) {
|
---|
| 30 | this.name = name;
|
---|
| 31 | this.values = values;
|
---|
| 32 | this.weightedUtils = computeWeightedUtils(utils, weight, precision);
|
---|
| 33 | this.interval = getRange();
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | public ValueSet getValues() {
|
---|
| 37 | return values;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | public String getName() {
|
---|
| 41 | return name;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | *
|
---|
| 46 | * @return weighted minimum and maximum utility achievable with this issue,
|
---|
| 47 | * rounded to the {@link #precision}.
|
---|
| 48 | */
|
---|
| 49 | public Interval getInterval() {
|
---|
| 50 | return interval;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | /**
|
---|
| 54 | * @param val the issue value to be evaluated
|
---|
| 55 | * @return weighted utility of given value, rounded to nearest value with
|
---|
| 56 | * {@link #precision} digits.
|
---|
| 57 | */
|
---|
| 58 | public BigDecimal getWeightedUtil(Value val) {
|
---|
| 59 | return weightedUtils.get(val);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
| 63 | *
|
---|
| 64 | * @param interval an {@link Interval} of utility values.
|
---|
| 65 | * @return all values that are inside the interval.
|
---|
| 66 | */
|
---|
| 67 | protected List<Value> subset(Interval interval) {
|
---|
| 68 |
|
---|
| 69 | List<Value> selection = new ArrayList<>();
|
---|
| 70 | for (Value value : values) {
|
---|
| 71 | if (interval.contains(getWeightedUtil(value)))
|
---|
| 72 | selection.add(value);
|
---|
| 73 | }
|
---|
| 74 | return selection;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
| 78 | * Faster way to determine subset size, it does not create a list
|
---|
| 79 | *
|
---|
| 80 | * @param interval an {@link Interval} of utility values.
|
---|
| 81 | * @return size of the subset that you will get from calling subset
|
---|
| 82 | */
|
---|
| 83 | protected int subsetSize(Interval interval) {
|
---|
| 84 | int n = 0;
|
---|
| 85 | for (Value value : values)
|
---|
| 86 | if (interval.contains(getWeightedUtil(value)))
|
---|
| 87 | n++;
|
---|
| 88 | return n;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /**
|
---|
| 92 | * @return the {@link Interval} (minimum and maximum) of the utility of the
|
---|
| 93 | * weighted utility of this issue, properly rounded to the
|
---|
| 94 | * {@link #precision}/
|
---|
| 95 | *
|
---|
| 96 | */
|
---|
| 97 | private Interval getRange() {
|
---|
| 98 | BigDecimal min = BigDecimal.ONE;
|
---|
| 99 | BigDecimal max = BigDecimal.ZERO;
|
---|
| 100 | for (Value value : values) {
|
---|
| 101 | BigDecimal util = getWeightedUtil(value);
|
---|
| 102 | if (util.compareTo(min) < 0)
|
---|
| 103 | min = util;
|
---|
| 104 | if (util.compareTo(max) > 0)
|
---|
| 105 | max = util;
|
---|
| 106 | }
|
---|
| 107 | return new Interval(min, max);
|
---|
| 108 |
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | private Map<Value, BigDecimal> computeWeightedUtils(
|
---|
| 112 | ValueSetUtilities utilities, BigDecimal w, int prec) {
|
---|
| 113 | Map<Value, BigDecimal> map = new HashMap<>();
|
---|
| 114 |
|
---|
| 115 | for (Value val : values) {
|
---|
| 116 | map.put(val, utilities.getUtility(val).multiply(w).setScale(prec,
|
---|
| 117 | RoundingMode.HALF_UP));
|
---|
| 118 | }
|
---|
| 119 | return map;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | }
|
---|