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