source: java2python/geniuswebtranslator/geniuswebsrc/geniusweb/bidspace/PartialSpaceFromUtility.java@ 818

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

#278 all code seems annotated @NonNull

File size: 3.4 KB
Line 
1package geniusweb.bidspace;
2
3import java.math.BigInteger;
4import java.security.SecureRandom;
5import java.util.HashMap;
6import java.util.HashSet;
7import java.util.Map;
8import java.util.Set;
9
10import org.eclipse.jdt.annotation.NonNull;
11
12import geniusweb.issuevalue.Bid;
13import geniusweb.profile.DefaultPartialOrdering;
14import geniusweb.profile.PartialOrdering;
15import geniusweb.profile.utilityspace.UtilitySpace;
16import tudelft.utilities.immutablelist.ListWithRemove;
17import tudelft.utilities.immutablelist.Tuple;
18import tudelft.utilities.immutablelist.Tuples;
19
20/**
21 * Simple demo converting a {@link UtilitySpace} into a {@link PartialOrdering}.
22 * Notice, a UtilitySpace IS a PartialOrdering, but a special one that is in
23 * fact a full ordering. This converter makes it a space that really has all
24 * info removed except for a few basic "isBetter" informations.
25 */
26public class PartialSpaceFromUtility extends DefaultPartialOrdering {
27 public PartialSpaceFromUtility(@NonNull UtilitySpace space, int nRankings) {
28 super(space.getName() + "_" + nRankings, space.getDomain(),
29 space.getReservationBid(), pickBetter(space, nRankings));
30 }
31
32 /**
33 * Pick subset of static isbetter rankings. Notice, ShuffledList currently
34 * can't handle really large lists which also limits this. This has to be
35 * fixed.
36 *
37 * @param space the space to sample from
38 * @param nRankings the number of rankings to be placed in the
39 * PartialOrdering.
40 * @return a subset creating a partial profile of the geiven UtilitySpace
41 */
42 private static @NonNull Map<@NonNull Bid, @NonNull Set<@NonNull Bid>> pickBetter(
43 @NonNull UtilitySpace space, int nRankings) {
44 final @NonNull Map<@NonNull Bid, @NonNull Set<@NonNull Bid>> eqOrBetter = new HashMap<>();
45
46 final @NonNull AllBidsList allbids = new AllBidsList(space.getDomain());
47 @NonNull
48 ListWithRemove<@NonNull Tuple<@NonNull Bid, @NonNull Bid>> available = new ListWithRemove<>(
49 new Tuples<@NonNull Bid, @NonNull Bid>(allbids, allbids));
50 for (int n = 0; n < nRankings; n = n + 1) {
51 final @NonNull BigInteger sel = rnd(available.size());
52 final @NonNull Tuple<@NonNull Bid, @NonNull Bid> tuple = available
53 .get(sel);
54 final @NonNull Bid bid1 = tuple.get1();
55 final @NonNull Bid bid2 = tuple.get2();
56 switch (space.getUtility(bid1).compareTo(space.getUtility(bid2))) {
57 case -1: // bid1 is worse
58 add(eqOrBetter, bid2, bid1);
59 break;
60 case 0:
61 add(eqOrBetter, bid2, bid1);
62 add(eqOrBetter, bid1, bid2);
63 break;
64 case 1:// bid1 is better
65 add(eqOrBetter, bid1, bid2);
66 break;
67 }
68 available = available.remove(sel);
69 }
70
71 return eqOrBetter;
72 }
73
74 /**
75 *
76 * @param eqOrBetter map to be extended with (key=bid1, value=bid2)
77 * @param bid1 the key to add a value to
78 * @param bid2 the value to be added
79 */
80 private static void add(
81 @NonNull Map<@NonNull Bid, @NonNull Set<@NonNull Bid>> eqOrBetter,
82 @NonNull Bid bid1, @NonNull Bid bid2) {
83 Set<@NonNull Bid> set = eqOrBetter.get(bid1);
84 if (set == null) {
85 set = new HashSet<Bid>();
86 }
87 set.add(bid2);
88 eqOrBetter.put(bid1, set);
89 }
90
91 /**
92 * Bit simplistic way to generate random in [0,n>. Simplistic because the
93 * mod() operator that we use causes bias in the random numbers.
94 *
95 * @param n the maximum value for the random number
96 * @return random in [0,n>
97 *
98 */
99 private static final @NonNull BigInteger rnd(@NonNull BigInteger n) {
100 return new BigInteger(n.bitLength(), new SecureRandom()).mod(n);
101 }
102}
Note: See TracBrowser for help on using the repository browser.