[519] | 1 | package geniusweb.bidspace;
|
---|
| 2 |
|
---|
| 3 | import java.math.BigDecimal;
|
---|
[810] | 4 | import java.math.BigInteger;
|
---|
[519] | 5 | import java.math.RoundingMode;
|
---|
| 6 | import java.util.ArrayList;
|
---|
| 7 | import java.util.HashMap;
|
---|
| 8 | import java.util.List;
|
---|
| 9 | import java.util.Map;
|
---|
| 10 |
|
---|
[810] | 11 | import org.eclipse.jdt.annotation.NonNull;
|
---|
| 12 |
|
---|
[519] | 13 | import geniusweb.issuevalue.Value;
|
---|
| 14 | import geniusweb.issuevalue.ValueSet;
|
---|
| 15 | import geniusweb.profile.utilityspace.ValueSetUtilities;
|
---|
| 16 |
|
---|
| 17 | /**
|
---|
| 18 | * Tool class to collect all relevant info about one issue (from LinearAdditive)
|
---|
| 19 | * in one class. Used for internally grouping data for more efficient
|
---|
| 20 | * processing. This class may change in the future, not recommended for direct
|
---|
| 21 | * use.
|
---|
| 22 | * <p>
|
---|
| 23 | * immutable
|
---|
| 24 | */
|
---|
| 25 | public class IssueInfo {
|
---|
[810] | 26 | private final @NonNull String name;
|
---|
| 27 | private final @NonNull ValueSet values;
|
---|
| 28 | private final @NonNull Interval interval;
|
---|
| 29 | private final @NonNull Map<@NonNull Value, @NonNull BigDecimal> weightedUtils;
|
---|
[519] | 30 |
|
---|
| 31 | /**
|
---|
| 32 | *
|
---|
| 33 | * @param name the issue name
|
---|
| 34 | * @param values the {@link ValueSet} of the issue
|
---|
| 35 | * @param utils the {@link ValueSetUtilities} of the issue profile
|
---|
| 36 | * @param weight the weight of the {@link ValueSetUtilities}
|
---|
| 37 | * @param precision the precision to compute with. Basically the number of
|
---|
| 38 | * decimal places used for the computations.
|
---|
| 39 | *
|
---|
| 40 | */
|
---|
[810] | 41 | public IssueInfo(@NonNull String name, @NonNull ValueSet values,
|
---|
| 42 | @NonNull ValueSetUtilities utils, @NonNull BigDecimal weight,
|
---|
| 43 | int precision) {
|
---|
| 44 | if (name == null || values == null || utils == null || weight == null)
|
---|
| 45 | throw new NullPointerException(
|
---|
| 46 | "name values utils and weight must be not null.");
|
---|
| 47 | if (values.size() == BigInteger.ZERO)
|
---|
| 48 | throw new IllegalArgumentException("Values must not be empty");
|
---|
[519] | 49 | this.name = name;
|
---|
| 50 | this.values = values;
|
---|
| 51 | this.weightedUtils = computeWeightedUtils(utils, weight, precision);
|
---|
| 52 | this.interval = getRange();
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[810] | 55 | public @NonNull ValueSet getValues() {
|
---|
[519] | 56 | return values;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[810] | 59 | public @NonNull String getName() {
|
---|
[519] | 60 | return name;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | *
|
---|
| 65 | * @return weighted minimum and maximum utility achievable with this issue,
|
---|
| 66 | * rounded to the requested precision.
|
---|
| 67 | */
|
---|
[810] | 68 | public @NonNull Interval getInterval() {
|
---|
[519] | 69 | return interval;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | /**
|
---|
| 73 | *
|
---|
| 74 | * @param isMax if true the max {@link Value} is returned, else the min is
|
---|
| 75 | * returned.
|
---|
| 76 | * @return the extreme value, either the minimum if isMax=false or maximum
|
---|
[810] | 77 | * if isMax=true.
|
---|
| 78 | * @throws IllegalStateException if there are no values
|
---|
[519] | 79 | */
|
---|
[810] | 80 | public @NonNull Value getExtreme(boolean isMax) {
|
---|
[519] | 81 | BigDecimal extremeutil = null;
|
---|
| 82 | Value extremeval = null;
|
---|
| 83 | for (Value val : values) {
|
---|
| 84 | BigDecimal util = weightedUtils.get(val);
|
---|
| 85 | if (extremeval == null) {
|
---|
| 86 | extremeutil = weightedUtils.get(val);
|
---|
| 87 | extremeval = val;
|
---|
| 88 | } else {
|
---|
| 89 | if (isMax) {
|
---|
| 90 | if (util.compareTo(extremeutil) > 0) {
|
---|
| 91 | extremeutil = util;
|
---|
| 92 | extremeval = val;
|
---|
| 93 | }
|
---|
| 94 | } else {
|
---|
| 95 | if (util.compareTo(extremeutil) < 0) {
|
---|
| 96 | extremeutil = util;
|
---|
| 97 | extremeval = val;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
[810] | 103 | if (extremeval == null)
|
---|
| 104 | throw new IllegalStateException(
|
---|
| 105 | "Extreme does not exist as IssueInfo values is empty");
|
---|
| 106 |
|
---|
[519] | 107 | return extremeval;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | /**
|
---|
| 111 | * @param val the issue value to be evaluated
|
---|
| 112 | * @return weighted utility of given value, rounded to nearest value with
|
---|
| 113 | * the requested precision number of digits.
|
---|
| 114 | */
|
---|
[810] | 115 | public BigDecimal getWeightedUtil(@NonNull Value val) {
|
---|
[519] | 116 | return weightedUtils.get(val);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | /**
|
---|
| 120 | *
|
---|
| 121 | * @param interval an {@link Interval} of utility values.
|
---|
| 122 | * @return all values that are inside the interval.
|
---|
| 123 | */
|
---|
[810] | 124 | protected @NonNull List<@NonNull Value> subset(@NonNull Interval interval) {
|
---|
[519] | 125 |
|
---|
[810] | 126 | @NonNull
|
---|
| 127 | List<@NonNull Value> selection = new ArrayList<>();
|
---|
[519] | 128 | for (Value value : values) {
|
---|
| 129 | if (interval.contains(getWeightedUtil(value)))
|
---|
| 130 | selection.add(value);
|
---|
| 131 | }
|
---|
| 132 | return selection;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /**
|
---|
| 136 | * Faster way to determine subset size, it does not create a list
|
---|
| 137 | *
|
---|
| 138 | * @param interval an {@link Interval} of utility values.
|
---|
| 139 | * @return size of the subset that you will get from calling subset
|
---|
| 140 | */
|
---|
[810] | 141 | protected int subsetSize(@NonNull Interval interval) {
|
---|
[519] | 142 | int n = 0;
|
---|
| 143 | for (Value value : values)
|
---|
| 144 | if (interval.contains(getWeightedUtil(value)))
|
---|
[579] | 145 | n = n + 1;
|
---|
[519] | 146 | return n;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | /**
|
---|
| 150 | * @return the {@link Interval} (minimum and maximum) of the utility of the
|
---|
| 151 | * weighted utility of this issue, properly rounded to the
|
---|
| 152 | * {@link #precision}/
|
---|
| 153 | *
|
---|
| 154 | */
|
---|
[810] | 155 | private @NonNull Interval getRange() {
|
---|
[519] | 156 | BigDecimal min = BigDecimal.ONE;
|
---|
| 157 | BigDecimal max = BigDecimal.ZERO;
|
---|
| 158 | for (Value value : values) {
|
---|
| 159 | BigDecimal util = getWeightedUtil(value);
|
---|
| 160 | if (util.compareTo(min) < 0)
|
---|
| 161 | min = util;
|
---|
| 162 | if (util.compareTo(max) > 0)
|
---|
| 163 | max = util;
|
---|
| 164 | }
|
---|
| 165 | return new Interval(min, max);
|
---|
| 166 |
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[810] | 169 | private @NonNull Map<@NonNull Value, @NonNull BigDecimal> computeWeightedUtils(
|
---|
[519] | 170 | ValueSetUtilities utilities, BigDecimal w, int prec) {
|
---|
[810] | 171 | @NonNull
|
---|
| 172 | Map<@NonNull Value, @NonNull BigDecimal> map = new HashMap<>();
|
---|
[519] | 173 |
|
---|
[810] | 174 | for (@NonNull
|
---|
| 175 | Value val : values) {
|
---|
[519] | 176 | map.put(val, utilities.getUtility(val).multiply(w).setScale(prec,
|
---|
| 177 | RoundingMode.HALF_UP));
|
---|
| 178 | }
|
---|
| 179 | return map;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | }
|
---|