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