1 | package geniusweb.profile.utilityspace;
|
---|
2 |
|
---|
3 | import java.math.BigDecimal;
|
---|
4 | import java.util.Collections;
|
---|
5 | import java.util.HashMap;
|
---|
6 | import java.util.LinkedList;
|
---|
7 | import java.util.List;
|
---|
8 | import java.util.Map;
|
---|
9 |
|
---|
10 | import org.eclipse.jdt.annotation.NonNull;
|
---|
11 |
|
---|
12 | import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
---|
13 | import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
---|
14 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
15 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
16 |
|
---|
17 | import geniusweb.issuevalue.Value;
|
---|
18 | import geniusweb.issuevalue.ValueSet;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Contains utilities of a ProductOfValue of a {@link SumOfGroupsUtilitySpace}.
|
---|
22 | * So this is similar to a {@link DiscreteValueSetUtilities} but instead of
|
---|
23 | * issues this contains an (ordered) list of issues. This object serializes to
|
---|
24 | * something like <code>
|
---|
25 | * {"partsutils":
|
---|
26 | * {"issues":["issue1","issue2"],
|
---|
27 | * "utilslist":[{"values":["low","low"],"util":0.3},{"values":["high","high"],"util":0.9}]}}
|
---|
28 | * </code> The issues field contains a list of N issues in the domain, The
|
---|
29 | * utilslist contains a list of dictionaries, with "values" containing a list of
|
---|
30 | * N issue values, in the same order as the issues list and with "util"
|
---|
31 | * containng the utility value of that set of values.
|
---|
32 | */
|
---|
33 | @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE)
|
---|
34 | public class PartsUtilities implements ValueSetUtilities {
|
---|
35 | private final @NonNull List<@NonNull String> issues;
|
---|
36 | // the real but not-serializable values (because Map can only have String
|
---|
37 | // keys in jackson)
|
---|
38 | private transient final @NonNull Map<@NonNull ProductOfValue, @NonNull BigDecimal> utilities = new HashMap<>();
|
---|
39 | // copy only for serializing
|
---|
40 | private final @NonNull List<@NonNull PartUtil> utilslist = new LinkedList<>();
|
---|
41 |
|
---|
42 | /**
|
---|
43 | *
|
---|
44 | * @param issues list of issues
|
---|
45 | * @param utils with keys: list of values and value: utility value for that
|
---|
46 | * list of values. All list-of-values missing from the map are
|
---|
47 | * assumed to have utility 0. This includes partial bids where
|
---|
48 | * list-of-values contain null objects.
|
---|
49 | */
|
---|
50 | public PartsUtilities(@NonNull List<@NonNull String> issues,
|
---|
51 | @NonNull Map<@NonNull ProductOfValue, @NonNull BigDecimal> utils) {
|
---|
52 | if (issues == null || utils == null) {
|
---|
53 | throw new IllegalArgumentException(
|
---|
54 | "issues and utils must be not null");
|
---|
55 | }
|
---|
56 |
|
---|
57 | this.issues = issues;
|
---|
58 | this.utilities.putAll(utils);
|
---|
59 |
|
---|
60 | checkUtilities();
|
---|
61 | for (final @NonNull ProductOfValue pval : utils.keySet()) {
|
---|
62 | utilslist.add(new PartUtil(pval.getValues(), utils.get(pval)));
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | @JsonCreator
|
---|
67 | public PartsUtilities(
|
---|
68 | @JsonProperty("issues") @NonNull List<@NonNull String> issues,
|
---|
69 | @JsonProperty("utilslist") @NonNull List<@NonNull PartUtil> utilslist) {
|
---|
70 | this(issues, list2map(utilslist));
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | @NonNull
|
---|
75 | /**
|
---|
76 | *
|
---|
77 | * @param pval the {@link ProductOfValue} value
|
---|
78 | * @return the utility of the value, in the same order as
|
---|
79 | * {@link #getIssues()}. Returns 0 if there is no utility set for
|
---|
80 | * the given combination of values. Notice, partial bids will
|
---|
81 | * usually have utility 0.
|
---|
82 | *
|
---|
83 | */
|
---|
84 | public BigDecimal getUtility(@NonNull Value pval) {
|
---|
85 | final @NonNull BigDecimal val = utilities.get(pval);
|
---|
86 | if (val == null)
|
---|
87 | return BigDecimal.ZERO;
|
---|
88 | return val;
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | public String isFitting(@NonNull ValueSet valueset) {
|
---|
93 | return null; // what can we tests here?
|
---|
94 | }
|
---|
95 |
|
---|
96 | @NonNull
|
---|
97 | /**
|
---|
98 | * @return the issues that are contained here.
|
---|
99 | */
|
---|
100 | public List<@NonNull String> getIssues() {
|
---|
101 | return issues;
|
---|
102 | }
|
---|
103 |
|
---|
104 | @NonNull
|
---|
105 | /**
|
---|
106 | *
|
---|
107 | * @return map with all available values and their utilities. The map and
|
---|
108 | * its contents should all be immutable.
|
---|
109 | */
|
---|
110 | public Map<@NonNull ProductOfValue, @NonNull BigDecimal> getUtilities() {
|
---|
111 | return Collections.unmodifiableMap(utilities);
|
---|
112 | }
|
---|
113 |
|
---|
114 | @NonNull
|
---|
115 | /**
|
---|
116 | *
|
---|
117 | * @param other another {@link PartsUtilities} map. The issues in the other
|
---|
118 | * map must be different from {@link #issues}.
|
---|
119 | * @return new PartsUtils, with the powermap of all combinations of one
|
---|
120 | * element from this and one from the other map, and with the
|
---|
121 | * utilities computed as the sum of thisvalue + othervalue.
|
---|
122 | */
|
---|
123 | public PartsUtilities add(@NonNull PartsUtilities other) {
|
---|
124 | for (final @NonNull String issue : issues) {
|
---|
125 | if (other.issues.contains(issue)) {
|
---|
126 | throw new IllegalArgumentException(
|
---|
127 | "Issue " + issue + " exists already");
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | final @NonNull List<@NonNull String> combinedissues = new LinkedList<>(
|
---|
132 | issues);
|
---|
133 | combinedissues.addAll(other.issues);
|
---|
134 |
|
---|
135 | final @NonNull Map<@NonNull ProductOfValue, @NonNull BigDecimal> combinedvalues = new HashMap<>();
|
---|
136 | for (final @NonNull ProductOfValue productOfValue : this.utilities
|
---|
137 | .keySet()) {
|
---|
138 | for (final @NonNull ProductOfValue otherProductOfValue : other.utilities
|
---|
139 | .keySet()) {
|
---|
140 | final @NonNull BigDecimal combinedutil = getUtility(
|
---|
141 | productOfValue)
|
---|
142 | .add(other.getUtility(otherProductOfValue));
|
---|
143 | combinedvalues.put(productOfValue.merge(otherProductOfValue),
|
---|
144 | combinedutil);
|
---|
145 | }
|
---|
146 | }
|
---|
147 | return new PartsUtilities(combinedissues, combinedvalues);
|
---|
148 |
|
---|
149 | }
|
---|
150 |
|
---|
151 | @Override
|
---|
152 | public int hashCode() {
|
---|
153 | final int prime = 31;
|
---|
154 | int result = 1;
|
---|
155 | result = prime * result + ((issues == null) ? 0 : issues.hashCode());
|
---|
156 | result = prime * result
|
---|
157 | + ((utilities == null) ? 0 : utilities.hashCode());
|
---|
158 | return result;
|
---|
159 | }
|
---|
160 |
|
---|
161 | @Override
|
---|
162 | public boolean equals(Object obj) {
|
---|
163 | if (this == obj)
|
---|
164 | return true;
|
---|
165 | if (obj == null)
|
---|
166 | return false;
|
---|
167 | if (getClass() != obj.getClass())
|
---|
168 | return false;
|
---|
169 | PartsUtilities other = (PartsUtilities) obj;
|
---|
170 | if (issues == null) {
|
---|
171 | if (other.issues != null)
|
---|
172 | return false;
|
---|
173 | } else if (!issues.equals(other.issues))
|
---|
174 | return false;
|
---|
175 | if (utilities == null) {
|
---|
176 | if (other.utilities != null)
|
---|
177 | return false;
|
---|
178 | } else if (!utilities.equals(other.utilities))
|
---|
179 | return false;
|
---|
180 | return true;
|
---|
181 | }
|
---|
182 |
|
---|
183 | @Override
|
---|
184 | public @NonNull String toString() {
|
---|
185 | return "PartsUtilities[" + issues + "," + utilities + "]";
|
---|
186 | }
|
---|
187 |
|
---|
188 | private void checkUtilities() {
|
---|
189 | /*#PY
|
---|
190 | * if True in (x==None or x<0 pr x>1 for x in self._utilities.values():
|
---|
191 | * raise ValueError("part weights must all be in [0,1]")
|
---|
192 | */
|
---|
193 | if (utilities.values().stream()
|
---|
194 | .anyMatch(v -> v == null || v.compareTo(BigDecimal.ZERO) < 0
|
---|
195 | || v.compareTo(BigDecimal.ONE) > 0)) {
|
---|
196 | throw new IllegalArgumentException(
|
---|
197 | "part weights must all be in [0,1]");
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | private static @NonNull Map<@NonNull ProductOfValue, @NonNull BigDecimal> list2map(
|
---|
202 | @NonNull List<@NonNull PartUtil> list) {
|
---|
203 | final @NonNull Map<@NonNull ProductOfValue, @NonNull BigDecimal> map = new HashMap<>();
|
---|
204 | for (final @NonNull PartUtil partutil : list) {
|
---|
205 | map.put(new ProductOfValue(partutil.getValues()),
|
---|
206 | partutil.getUtil());
|
---|
207 | }
|
---|
208 | return map;
|
---|
209 | }
|
---|
210 |
|
---|
211 | @NonNull
|
---|
212 | /**
|
---|
213 | *
|
---|
214 | * @return the max utility of all values contained here.
|
---|
215 | */
|
---|
216 | public BigDecimal getMaxUtility() {
|
---|
217 | @NonNull
|
---|
218 | BigDecimal maxutil = BigDecimal.ZERO;
|
---|
219 | for (final @NonNull ProductOfValue vakye : utilities.keySet()) {
|
---|
220 | if (utilities.get(vakye).compareTo(maxutil) > 0) {
|
---|
221 | maxutil = utilities.get(vakye);
|
---|
222 | }
|
---|
223 | }
|
---|
224 | return maxutil;
|
---|
225 | }
|
---|
226 |
|
---|
227 | }
|
---|
228 |
|
---|
229 | @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE)
|
---|
230 | /**
|
---|
231 | * contains list of values, and the utility for this combi of values. The values
|
---|
232 | * make only sense with a corresponding list of issues, but that is managed in
|
---|
233 | * (see {@link PartsUtilities}.
|
---|
234 | * <p>
|
---|
235 | * immutable.
|
---|
236 | */
|
---|
237 | class PartUtil {
|
---|
238 | private final @NonNull List<@NonNull Value> values;
|
---|
239 |
|
---|
240 | private final @NonNull BigDecimal util;
|
---|
241 |
|
---|
242 | @JsonCreator
|
---|
243 | public PartUtil(
|
---|
244 | @JsonProperty("values") @NonNull List<@NonNull Value> values,
|
---|
245 | @JsonProperty("util") @NonNull BigDecimal util) {
|
---|
246 | this.values = values;
|
---|
247 | this.util = util;
|
---|
248 | }
|
---|
249 |
|
---|
250 | public @NonNull List<@NonNull Value> getValues() {
|
---|
251 | return values;
|
---|
252 | }
|
---|
253 |
|
---|
254 | public @NonNull BigDecimal getUtil() {
|
---|
255 | return util;
|
---|
256 | }
|
---|
257 | }
|
---|