source: bidspace/src/main/java/geniusweb/bidspace/BidsWithUtility.java@ 10

Last change on this file since 10 was 10, checked in by bart, 4 years ago

Update 28 jan 2020

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