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