source: java2python/geniuswebtranslator/geniuswebsrc/geniusweb/bidspace/IssueInfo.java@ 830

Last change on this file since 830 was 825, checked in by wouter, 6 months ago

#291 move annotation to above the javadoc

File size: 5.3 KB
Line 
1package geniusweb.bidspace;
2
3import java.math.BigDecimal;
4import java.math.BigInteger;
5import java.math.RoundingMode;
6import java.util.ArrayList;
7import java.util.HashMap;
8import java.util.List;
9import java.util.Map;
10
11import org.eclipse.jdt.annotation.NonNull;
12
13import geniusweb.issuevalue.Value;
14import geniusweb.issuevalue.ValueSet;
15import 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 */
25public 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 @NonNull
65 /**
66 *
67 * @return weighted minimum and maximum utility achievable with this issue,
68 * rounded to the requested precision.
69 */
70 public Interval getInterval() {
71 return interval;
72 }
73
74 @NonNull
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
80 * if isMax=true.
81 * @throws IllegalStateException if there are no values
82 */
83 public Value getExtreme(boolean isMax) {
84 BigDecimal extremeutil = null;
85 Value extremeval = null;
86 for (final @NonNull Value val : values) {
87 final @NonNull BigDecimal util = weightedUtils.get(val);
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 }
106 if (extremeval == null)
107 throw new IllegalStateException(
108 "Extreme does not exist as IssueInfo values is empty");
109
110 return extremeval;
111 }
112
113 @NonNull
114 /**
115 * @param val the issue value to be evaluated
116 * @return weighted utility of given value, rounded to nearest value with
117 * the requested precision number of digits. returns 0 if value is
118 * unknown.
119 */
120 public BigDecimal getWeightedUtil(@NonNull Value val) {
121 BigDecimal util = weightedUtils.get(val);
122 return util == null ? BigDecimal.ZERO : util;
123 }
124
125 @NonNull
126 /**
127 *
128 * @param interval an {@link Interval} of utility values.
129 * @return all values that are inside the interval.
130 */
131 protected List<@NonNull Value> subset(@NonNull Interval interval) {
132
133 final @NonNull List<@NonNull Value> selection = new ArrayList<>();
134 for (final @NonNull Value value : values) {
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 */
147 protected int subsetSize(@NonNull Interval interval) {
148 int n = 0;
149 for (final @NonNull Value value : values)
150 if (interval.contains(getWeightedUtil(value)))
151 n = n + 1;
152 return n;
153 }
154
155 @NonNull
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 */
162 private Interval getRange() {
163 @NonNull
164 BigDecimal min = BigDecimal.ONE;
165 @NonNull
166 BigDecimal max = BigDecimal.ZERO;
167 for (final @NonNull Value value : values) {
168 final @NonNull BigDecimal util = getWeightedUtil(value);
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
178 private @NonNull Map<@NonNull Value, @NonNull BigDecimal> computeWeightedUtils(
179 @NonNull ValueSetUtilities utilities, @NonNull BigDecimal w,
180 int prec) {
181 final @NonNull Map<@NonNull Value, @NonNull BigDecimal> map = new HashMap<>();
182
183 for (final @NonNull Value val : values) {
184 map.put(val, utilities.getUtility(val).multiply(w).setScale(prec,
185 RoundingMode.HALF_UP));
186 }
187 return map;
188 }
189
190}
Note: See TracBrowser for help on using the repository browser.