source: java2python/geniuswebtranslator/geniuswebsrc/geniusweb/bidspace/BidsWithUtility.java@ 804

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

#278 added NonNull annotation in many places in the geniusweb code

File size: 9.0 KB
Line 
1package geniusweb.bidspace;
2
3import java.math.BigDecimal;
4import java.math.BigInteger;
5import java.util.Collections;
6import java.util.HashMap;
7import java.util.List;
8import java.util.Map;
9import java.util.function.Function;
10import java.util.stream.Collectors;
11
12import org.eclipse.jdt.annotation.NonNull;
13
14import geniusweb.issuevalue.Bid;
15import geniusweb.issuevalue.Domain;
16import geniusweb.issuevalue.Value;
17import geniusweb.profile.utilityspace.LinearAdditive;
18import tudelft.utilities.immutablelist.AbstractImmutableList;
19import tudelft.utilities.immutablelist.FixedList;
20import tudelft.utilities.immutablelist.ImmutableList;
21import tudelft.utilities.immutablelist.JoinedList;
22import tudelft.utilities.immutablelist.MapList;
23import tudelft.utilities.immutablelist.Tuple;
24
25/**
26 * Tool class containing functions dealing with utilities of all bids in a given
27 * {@link LinearAdditive}. This class caches previously computed values to
28 * accelerate the calls and subsequent calls. Re-use the object to keep/reuse
29 * the cache.
30 * <h2>Rounding</h2> Internally, utilities of bids are rounded to the given
31 * precision. This may cause inclusion/exclusion of some bids in the results.
32 * See {@link #BidsWithUtility(LinearAdditive, int)} for more details
33 */
34public class BidsWithUtility {
35
36 private final List<IssueInfo> issueInfo;
37 private final int precision; // #digits used for Intervals
38
39 /**
40 * cache. Key = call arguments for {@link #get(int, Interval)}. Value=return
41 * value of that call.
42 */
43 private final Map<Tuple<Integer, Interval>, ImmutableList<Bid>> cache = new HashMap<>();
44 private ImmutableList<Bid> emptylist = new FixedList<>();
45
46 /**
47 * Default constructor, uses default precision 6. This value seems practical
48 * for the common range of issues, utilities and weights. See
49 * {@link #BidsWithUtility(LinearAdditive, int)} for more details on the
50 * precision.
51 *
52 * @param space the {@link LinearAdditive} to analyze
53 */
54 public BidsWithUtility(LinearAdditive space) {
55 this(space, 6);
56 }
57
58 /**
59 *
60 * @param space the {@link LinearAdditive} to analyze
61 * @param precision the number of digits to use for computations. In
62 * practice, 6 seems a good default value.
63 * <p>
64 * All utilities * weight are rounded to this number of
65 * digits. This value should match the max number of
66 * (digits used in the weight of an issue + number of
67 * digits used in the issue utility). To determine the
68 * optimal value, one may consider the step size of the
69 * issues, and the range of interest. For instance if the
70 * utility function has values 1/3 and 2/3, then these have
71 * an 'infinite' number of relevant digits. But if the goal
72 * is to search bids between utility 0.1 and 0.2, then
73 * computing in 2 digits might already be sufficient.
74 * <p>
75 * This algorithm has memory and space complexity O(
76 * |nissues| 10^precision ). For spaces up to 7 issues, 7
77 * digits should be feasible; for 9 issues, 6 digits may be
78 * the maximum.
79 */
80 public BidsWithUtility(LinearAdditive space, int precision) {
81 this(getInfo(space, precision), precision);
82 }
83
84 /**
85 *
86 * @param issuesInfo List of the relevant issues (in order of relevance) and
87 * all info of each issue.
88 * @param precision the number of digits used in Intervals.
89 */
90 public BidsWithUtility(List<IssueInfo> issuesInfo, int precision) {
91 if (issuesInfo == null || issuesInfo.isEmpty()) {
92 throw new IllegalArgumentException(
93 "sortedissues list must contain at least 1 element");
94 }
95
96 this.issueInfo = issuesInfo;
97 this.precision = precision;
98
99 }
100
101 /**
102 * @return the (rounded) utility {@link Interval} of this space: minimum and
103 * maximum achievable utility.
104 */
105 public Interval getRange() {
106 return getRange(issueInfo.size() - 1);
107 }
108
109 /**
110 *
111 * @param range the minimum and maximum utility required of the bids. to be
112 * included (both ends inclusive).
113 * @return a list with bids that have a (rounded) utility inside range.
114 * possibly empty.
115 */
116 public ImmutableList<Bid> getBids(Interval range) {
117 return get(issueInfo.size() - 1, range.round(precision));
118 }
119
120 public List<IssueInfo> getInfo() {
121 return Collections.unmodifiableList(issueInfo);
122 }
123
124 /**
125 *
126 * @param isMax the extreme bid required
127 * @return the extreme bid, either the minimum if isMax=false or maximum if
128 * isMax=true
129 */
130 public Bid getExtremeBid(boolean isMax) {
131 Map<String, Value> map = new HashMap<>();
132 for (IssueInfo info : issueInfo) {
133 map.put(info.getName(), info.getExtreme(isMax));
134 }
135 return new Bid(map);
136 }
137
138 /**
139 * Create partial BidsWithUtil list considering only issues 0..n, with
140 * utilities in given range.
141 * <h2>Memory use</h2> Memory use of the return value can be large, if large
142 * domains are given and the requested interval contains large numbers of
143 * bids. To give some idea, here are some typical memory uses:
144 * <table border="1">
145 * <tr>
146 * <td>domain</td>
147 * <td>mem use (MB) of returned object</td>
148 * <td>nr. of selected bids</td>
149 * </tr>
150 * <tr>
151 * <td>jobs1</td>
152 * <td>0.028</td>
153 * <td>23</td>
154 * </tr>
155 * <tr>
156 * <td>7issues1</td>
157 * <td>34</td>
158 * <td>346327</td>
159 * </tr>
160 * <tr>
161 * <td>9issues1</td>
162 * <td>98</td>
163 * <td>37160666</td>
164 * </tr>
165 * </table>
166 *
167 * @param n the number of issueRanges to consider, we consider 0..n here.
168 * The recursion decreases n until n=0
169 * @param goal the minimum and maximum utility required of the bids. to be
170 * included (both ends inclusive)
171 * @return BidsWithUtil list, possibly empty.
172 */
173 protected ImmutableList<Bid> get(int n, Interval goal) {
174 if (goal == null) {
175 throw new NullPointerException("Interval=null");
176 }
177
178 // clamp goal into what is reachable. Avoid caching empty
179 goal = goal.intersect(getRange(n));
180 if (goal.isEmpty()) {
181 return new FixedList<>();
182 }
183
184 Tuple<Integer, Interval> cachetuple = new Tuple<>(n, goal);
185 ImmutableList<Bid> cached = cache.get(cachetuple);
186 if (cached != null) {
187 // hits++;
188 return cached;
189 }
190
191 ImmutableList<Bid> result = checkedGet(n, goal);
192 cache.put(cachetuple, result);
193 return result;
194 }
195
196 private static List<IssueInfo> getInfo(LinearAdditive space2,
197 int precision) {
198 Domain dom = space2.getDomain();
199 /*#PY
200 * return [ IssueInfo(issue, dom.getValues(issue),
201 * space2.getUtilities().get(issue),
202 * space2.getWeight(issue), precision))
203 * for issue in space2.getDomain().getIssues() ]
204 */
205 return space2.getDomain().getIssuesValues().stream()
206 .map(issue -> new IssueInfo(issue, dom.getValues(issue),
207 space2.getUtilities().get(issue),
208 space2.getWeight(issue), precision))
209 .collect(Collectors.toList());
210 }
211
212 private ImmutableList<Bid> checkedGet(int n, Interval goal) {
213 IssueInfo info = issueInfo.get(n);
214 // issue is the first issuesWithRange.
215 String issue = info.getName();
216
217 if (n == 0)
218 return new OneIssueSubset(info, goal);
219
220 // make new list, joining all sub-lists
221 ImmutableList<Bid> fulllist = emptylist;
222 for (Value val : info.getValues()) {
223 BigDecimal weightedutil = info.getWeightedUtil(val);
224 Interval subgoal = goal.subtract(weightedutil);
225 // recurse: get list of bids for the subspace
226 ImmutableList<Bid> partialbids = get(n - 1, subgoal);
227
228 ImmutableList<Bid> fullbids = new MapList<Bid, Bid>(
229 (Function<Bid, Bid>) pbid -> pbid
230 .merge(new Bid(issue, val)),
231 partialbids);
232 if (!fullbids.size().equals(BigInteger.ZERO))
233 fulllist = new JoinedList<Bid>(fullbids, fulllist);
234 }
235 return fulllist;
236 }
237
238 /**
239 * @param n the maximum issuevalue utility to include. Use n=index of last
240 * issue s= (#issues in the domain - 1) for the full range of this
241 * domain.
242 * @return Interval (min, max) of the total weighted utility Interval of
243 * issues 0..n. All weighted utilities have been rounded to the set
244 * {@link #precision}
245 */
246 private Interval getRange(int n) {
247 Interval value = Interval.ZERO;
248 for (int i = 0; i <= n; i = i + 1) {
249 value = value.add(issueInfo.get(i).getInterval());
250 }
251 return value;
252 }
253
254}
255
256/**
257 * List of all one-issue bids that have utility inside given interval.
258 */
259class OneIssueSubset extends AbstractImmutableList<Bid> {
260 private final IssueInfo info;
261 private final Interval interval;
262 private BigInteger size;
263
264 /**
265 *
266 * @param info the {@link IssueInfo}
267 * @param interval a utility interval (weighted)
268 */
269 OneIssueSubset(IssueInfo info, Interval interval) {
270 this.info = info;
271 this.interval = interval;
272 this.size = BigInteger.valueOf(info.subsetSize(interval));
273 }
274
275 @Override
276 public @NonNull Bid get(@NonNull BigInteger index) {
277 return new Bid(info.getName(),
278 info.subset(interval).get(index.intValue()));
279 }
280
281 @Override
282 public @NonNull BigInteger size() {
283 return size;
284 }
285
286}
Note: See TracBrowser for help on using the repository browser.