[41] | 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 | /**
|
---|
| 29 | *
|
---|
| 30 | * @param name the issue name
|
---|
| 31 | * @param values the {@link ValueSet} of the issue
|
---|
| 32 | * @param utils the {@link ValueSetUtilities} of the issue profile
|
---|
| 33 | * @param weight the weight of the {@link ValueSetUtilities}
|
---|
| 34 | * @param precision the precision to compute with. Basically the number of
|
---|
| 35 | * decimal places used for the computations.
|
---|
| 36 | *
|
---|
| 37 | */
|
---|
| 38 | public IssueInfo(String name, ValueSet values, ValueSetUtilities utils,
|
---|
| 39 | BigDecimal weight, int precision) {
|
---|
| 40 | this.name = name;
|
---|
| 41 | this.values = values;
|
---|
| 42 | this.weightedUtils = computeWeightedUtils(utils, weight, precision);
|
---|
| 43 | this.interval = getRange();
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public ValueSet getValues() {
|
---|
| 47 | return values;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | public String getName() {
|
---|
| 51 | return name;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | /**
|
---|
| 55 | *
|
---|
| 56 | * @return weighted minimum and maximum utility achievable with this issue,
|
---|
| 57 | * rounded to the requested precision.
|
---|
| 58 | */
|
---|
| 59 | public Interval getInterval() {
|
---|
| 60 | return interval;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | *
|
---|
| 65 | * @param isMax if true the max {@link Value} is returned, else the min is
|
---|
| 66 | * returned.
|
---|
| 67 | * @return the extreme value, either the minimum if isMax=false or maximum
|
---|
| 68 | * if isMax=true
|
---|
| 69 | */
|
---|
| 70 | public Value getExtreme(boolean isMax) {
|
---|
| 71 | BigDecimal extremeutil = null;
|
---|
| 72 | Value extremeval = null;
|
---|
| 73 | for (Value val : values) {
|
---|
| 74 | BigDecimal util = weightedUtils.get(val);
|
---|
| 75 | if (extremeval == null) {
|
---|
| 76 | extremeutil = weightedUtils.get(val);
|
---|
| 77 | extremeval = val;
|
---|
| 78 | } else {
|
---|
| 79 | if (isMax) {
|
---|
| 80 | if (util.compareTo(extremeutil) > 0) {
|
---|
| 81 | extremeutil = util;
|
---|
| 82 | extremeval = val;
|
---|
| 83 | }
|
---|
| 84 | } else {
|
---|
| 85 | if (util.compareTo(extremeutil) < 0) {
|
---|
| 86 | extremeutil = util;
|
---|
| 87 | extremeval = val;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 | return extremeval;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | /**
|
---|
| 97 | * @param val the issue value to be evaluated
|
---|
| 98 | * @return weighted utility of given value, rounded to nearest value with
|
---|
| 99 | * the requested precision number of digits.
|
---|
| 100 | */
|
---|
| 101 | public BigDecimal getWeightedUtil(Value val) {
|
---|
| 102 | return weightedUtils.get(val);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /**
|
---|
| 106 | *
|
---|
| 107 | * @param interval an {@link Interval} of utility values.
|
---|
| 108 | * @return all values that are inside the interval.
|
---|
| 109 | */
|
---|
| 110 | protected List<Value> subset(Interval interval) {
|
---|
| 111 |
|
---|
| 112 | List<Value> selection = new ArrayList<>();
|
---|
| 113 | for (Value value : values) {
|
---|
| 114 | if (interval.contains(getWeightedUtil(value)))
|
---|
| 115 | selection.add(value);
|
---|
| 116 | }
|
---|
| 117 | return selection;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | /**
|
---|
| 121 | * Faster way to determine subset size, it does not create a list
|
---|
| 122 | *
|
---|
| 123 | * @param interval an {@link Interval} of utility values.
|
---|
| 124 | * @return size of the subset that you will get from calling subset
|
---|
| 125 | */
|
---|
| 126 | protected int subsetSize(Interval interval) {
|
---|
| 127 | int n = 0;
|
---|
| 128 | for (Value value : values)
|
---|
| 129 | if (interval.contains(getWeightedUtil(value)))
|
---|
| 130 | n++;
|
---|
| 131 | return n;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | /**
|
---|
| 135 | * @return the {@link Interval} (minimum and maximum) of the utility of the
|
---|
| 136 | * weighted utility of this issue, properly rounded to the
|
---|
| 137 | * {@link #precision}/
|
---|
| 138 | *
|
---|
| 139 | */
|
---|
| 140 | private Interval getRange() {
|
---|
| 141 | BigDecimal min = BigDecimal.ONE;
|
---|
| 142 | BigDecimal max = BigDecimal.ZERO;
|
---|
| 143 | for (Value value : values) {
|
---|
| 144 | BigDecimal util = getWeightedUtil(value);
|
---|
| 145 | if (util.compareTo(min) < 0)
|
---|
| 146 | min = util;
|
---|
| 147 | if (util.compareTo(max) > 0)
|
---|
| 148 | max = util;
|
---|
| 149 | }
|
---|
| 150 | return new Interval(min, max);
|
---|
| 151 |
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | private Map<Value, BigDecimal> computeWeightedUtils(
|
---|
| 155 | ValueSetUtilities utilities, BigDecimal w, int prec) {
|
---|
| 156 | Map<Value, BigDecimal> map = new HashMap<>();
|
---|
| 157 |
|
---|
| 158 | for (Value val : values) {
|
---|
| 159 | map.put(val, utilities.getUtility(val).multiply(w).setScale(prec,
|
---|
| 160 | RoundingMode.HALF_UP));
|
---|
| 161 | }
|
---|
| 162 | return map;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | }
|
---|