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