source: java2python/geniuswebtranslator/geniuswebsrc/geniusweb/profile/utilityspace/SumOfGroupsUtilitySpace.java@ 811

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

#287 adding NonNull annotations to SumOfGroupsUtilitySpace

File size: 10.1 KB
Line 
1package geniusweb.profile.utilityspace;
2
3import java.math.BigDecimal;
4import java.util.Arrays;
5import java.util.HashMap;
6import java.util.HashSet;
7import java.util.LinkedList;
8import java.util.List;
9import java.util.Map;
10import java.util.Set;
11
12import org.eclipse.jdt.annotation.NonNull;
13
14import com.fasterxml.jackson.annotation.JsonAutoDetect;
15import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
16import com.fasterxml.jackson.annotation.JsonCreator;
17import com.fasterxml.jackson.annotation.JsonProperty;
18
19import geniusweb.issuevalue.Bid;
20import geniusweb.issuevalue.Domain;
21import geniusweb.issuevalue.NumberValueSet;
22import geniusweb.issuevalue.Value;
23import geniusweb.profile.DefaultProfile;
24
25/**
26 * This is a utility space that defines the utility of bids as a sum of the
27 * utilities of a number of subsets, or groups, of the issue values.
28 * <p>
29 * A group defines the utility of a non-empty subset of the issues in the
30 * domain. The parts are non-overlapping. Missing issue values have utility 0.
31 * <p>
32 * This space enables handling {@link UtilitySpace}s that are not simply linear
33 * additive, but with interacting issue values. For example, if you would like
34 * to have a car with good speakers but only if there is also a good hifi set,
35 * you can make a part that has {@link PartsUtilities} like
36 * <code> { { speakers:yes, hifi:yes }:1, {speakers, no: hifh:yes}:0.2, ...} </code>
37 * <p>
38 * NOTICE this space is completely discrete. There is no equivalent of the
39 * {@link NumberValueSet} here.
40 */
41@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE)
42public class SumOfGroupsUtilitySpace extends DefaultProfile
43 implements UtilitySpace {
44 /**
45 * the key is the part name, the value is the valuesetutilities for this
46 * issue. Should be immutable so do not return direct access to this field.
47 */
48 private final @NonNull Map<@NonNull String, @NonNull PartsUtilities> partUtilities = new HashMap<>();
49
50 /**
51 * @param domain the {@link Domain} in which this profile is
52 * defined.
53 * @param name the name of this profile. Must be simple name (a-Z,
54 * 0-9)
55 * @param partUtilities a map with key: part names (String) and value: the
56 * {@link PartsUtilities} for that part. There MUST
57 * NOT be a null part name. All values MUST NOT be
58 * null. The PartsUtilities must match the
59 * @param reservationBid the reservation bid. Only bids that are
60 * {@link #isPreferredOrEqual(Bid, Bid)} should be
61 * accepted. Can be null, meaning that there is no
62 * reservation bid and any agreement is better than no
63 * agreement.
64 * @throws NullPointerException if values are incorrectly null.
65 * @throws IllegalArgumentException if preconditions not met.
66 */
67 @JsonCreator
68 public SumOfGroupsUtilitySpace(
69 @JsonProperty("domain") @NonNull Domain domain,
70 @JsonProperty("name") @NonNull String name,
71 @JsonProperty("partUtilities") @NonNull Map<@NonNull String, @NonNull PartsUtilities> partUtilities,
72 @JsonProperty("reservationBid") Bid reservationBid) {
73 super(name, domain, reservationBid);
74 this.partUtilities.putAll(partUtilities);
75
76 String err = checkParts();
77 if (err != null) {
78 throw new IllegalArgumentException(err);
79 }
80 }
81
82 /**
83 * Copy settings in las. This will have the exact same utilities as the las
84 * but this then gives you the power to change it into a non-linear space by
85 * grouping.
86 *
87 * @param las the {@link LinearAdditive} to be converted/copied.
88 *
89 */
90 public SumOfGroupsUtilitySpace(@NonNull LinearAdditive las) {
91 this(las.getDomain(), las.getName(), las2parts(las),
92 las.getReservationBid());
93 }
94
95 @Override
96 public @NonNull BigDecimal getUtility(@NonNull Bid bid) {
97 //#PY return sum( [ self._util(partname, self._bid) for partname in self._partUtilities.keys() ] )
98 return partUtilities.keySet().stream()
99 .map(partname -> util(partname, bid))
100 .reduce(BigDecimal.ZERO, BigDecimal::add);
101 }
102
103 @Override
104 public @NonNull String toString() {
105 return "PartialAdditive[" + partUtilities + "," + getReservationBid()
106 + "]";
107 }
108
109 /**
110 *
111 * @param partnames the partnames to remove from this. There must be at
112 * least 2 parts
113 * @param newpartname the name of the new part that contains all partnames,
114 * grouped into 1 "issue". This name must not be an issue
115 * in this.
116 * @return new {@link SumOfGroupsUtilitySpace} that takes all partnames out
117 * of this, and makes a newaprtname that contains these.
118 */
119 public @NonNull SumOfGroupsUtilitySpace group(
120 @NonNull List<@NonNull String> partnames,
121 @NonNull String newpartname) {
122 final @NonNull Set<@NonNull String> allpartnames = partUtilities
123 .keySet();
124
125 if (partnames.size() < 2) {
126 throw new IllegalArgumentException(
127 "Group must contain at least 2 parts");
128 }
129 if (allpartnames.contains(newpartname)) {
130 throw new IllegalArgumentException(
131 "newpartname " + newpartname + " is already in use");
132 }
133 for (@NonNull
134 String name : partnames) {
135 if (!allpartnames.contains(name)) {
136 throw new IllegalArgumentException("Unknown part name " + name);
137 }
138 }
139
140 final @NonNull Map<@NonNull String, @NonNull PartsUtilities> newutils = new HashMap<>();
141
142 PartsUtilities newpartutils = null;
143
144 for (@NonNull
145 String name : partUtilities.keySet()) {
146 if (partnames.contains(name)) {
147 if (newpartutils == null) {
148 newpartutils = partUtilities.get(name);
149 } else {
150 newpartutils = newpartutils.add(partUtilities.get(name));
151 }
152 } else {
153 newutils.put(name, partUtilities.get(name));
154 }
155 }
156 if (newpartutils == null) // fixme better error message?
157 throw new IllegalArgumentException("Newpartutils remained null.");
158 newutils.put(newpartname, newpartutils);
159
160 return new SumOfGroupsUtilitySpace(getDomain(), getName(), newutils,
161 getReservationBid());
162 }
163
164 @Override
165 public int hashCode() {
166 final int prime = 31;
167 int result = super.hashCode();
168 result = prime * result
169 + ((partUtilities == null) ? 0 : partUtilities.hashCode());
170 return result;
171 }
172
173 @Override
174 public boolean equals(Object obj) {
175 if (this == obj)
176 return true;
177 if (!super.equals(obj))
178 return false;
179 if (getClass() != obj.getClass())
180 return false;
181 SumOfGroupsUtilitySpace other = (SumOfGroupsUtilitySpace) obj;
182 if (partUtilities == null) {
183 if (other.partUtilities != null)
184 return false;
185 } else if (!partUtilities.equals(other.partUtilities))
186 return false;
187 return true;
188 }
189
190 /***************************** private funcs ***************************/
191 /**
192 *
193 * @param partname the name of the part to get the utility of
194 * @param bid the bid
195 * @return weighted util of just the part: utilities[part].getUtility(part
196 * of bid)
197 */
198 private @NonNull BigDecimal util(@NonNull String partname,
199 @NonNull Bid bid) {
200 @NonNull
201 PartsUtilities partutils = partUtilities.get(partname);
202 @NonNull
203 ProductOfValue value = collectValues(bid, partutils);
204 return partutils.getUtility(value);
205 }
206
207 /**
208 *
209 * @param bid a full bid
210 * @param partutils the part utils for which a list of values from the bid
211 * is needed
212 * @return a list of values from the given bid, ordered as indicated in
213 * partutils
214 */
215 private @NonNull ProductOfValue collectValues(@NonNull Bid bid,
216 @NonNull PartsUtilities partutils) {
217 final @NonNull List<@NonNull Value> values = new LinkedList<>();
218 for (final @NonNull String issue : partutils.getIssues()) {
219 values.add(bid.getValue(issue));
220 }
221 return new ProductOfValue(values);
222 }
223
224 /**
225 *
226 * @param las a {@link LinearAdditive}
227 * @return a Map with partname-PartsUtilities. The partnames are identical
228 * to the issues in the given las.
229 */
230 private static @NonNull Map<@NonNull String, @NonNull PartsUtilities> las2parts(
231 @NonNull LinearAdditive las) {
232 final @NonNull Map<@NonNull String, @NonNull PartsUtilities> map = new HashMap<>();
233 for (@NonNull
234 String issue : las.getUtilities().keySet()) {
235 final @NonNull ValueSetUtilities valset = las.getUtilities()
236 .get(issue);
237 final @NonNull Map<@NonNull ProductOfValue, @NonNull BigDecimal> utilslist = new HashMap<>();
238 final @NonNull BigDecimal weight = las.getWeight(issue);
239 for (final @NonNull Value val : las.getDomain().getValues(issue)) {
240 final @NonNull BigDecimal util = valset.getUtility(val)
241 .multiply(weight);
242 if (BigDecimal.ZERO.compareTo(util) != 0) {
243 utilslist.put(new ProductOfValue(Arrays.asList(val)), util);
244 }
245 }
246 map.put(issue, new PartsUtilities(Arrays.asList(issue), utilslist));
247 }
248 return map;
249 }
250
251 /**
252 *
253 * @return error string, or null if no error (all parts seem fine)
254 */
255 private String checkParts() {
256
257 final @NonNull Set<@NonNull String> collectedIssues = new HashSet<>();
258 for (final @NonNull String partname : partUtilities.keySet()) {
259 final PartsUtilities part = partUtilities.get(partname);
260 if (part == null) {
261 return "partUtilities " + partname + " contains null value";
262 }
263 final @NonNull List<@NonNull String> issues = part.getIssues();
264 final @NonNull Set<@NonNull String> intersection = new HashSet<>(
265 collectedIssues);
266 intersection.retainAll(issues);
267 if (!intersection.isEmpty()) {
268 return "issues " + intersection + " occur multiple times";
269 }
270 collectedIssues.addAll(issues);
271 }
272
273 if (!collectedIssues.equals(getDomain().getIssuesValues())) {
274 return "parts must cover the domain issues "
275 + getDomain().getIssuesValues() + " but cover "
276 + collectedIssues;
277 }
278 if (getMaxUtility().compareTo(BigDecimal.ONE) > 0) {
279 return "Max utility of the space exceedds 1";
280 }
281 return null;
282
283 }
284
285 /**
286 *
287 * @return the max possible utility in this utility space.
288 */
289 private @NonNull BigDecimal getMaxUtility() {
290 //#PY return sum( [ partutils.getMaxUtility() for partutils in self._partUtilities.values() ] )
291 return partUtilities.values().stream()
292 .map(partutils -> partutils.getMaxUtility())
293 .reduce(BigDecimal.ZERO, BigDecimal::add);
294 }
295
296}
Note: See TracBrowser for help on using the repository browser.