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