1 | package genius.core.uncertainty;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.Collections;
|
---|
5 | import java.util.List;
|
---|
6 | import java.util.Random;
|
---|
7 | import java.util.stream.Collectors;
|
---|
8 |
|
---|
9 | import genius.core.Bid;
|
---|
10 | import genius.core.bidding.BidDetails;
|
---|
11 | import genius.core.bidding.BidDetailsSorterUtility;
|
---|
12 | import genius.core.boaframework.SortedOutcomeSpace;
|
---|
13 |
|
---|
14 | public class ComparisonGenerator {
|
---|
15 | private SortedOutcomeSpace outcomeSpace;
|
---|
16 |
|
---|
17 | public ComparisonGenerator(SortedOutcomeSpace outcomeSpace) {
|
---|
18 | this.outcomeSpace = outcomeSpace;
|
---|
19 | }
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * TODO DOC
|
---|
23 | *
|
---|
24 | * @param uncertaintyPercentage
|
---|
25 | * @param seed
|
---|
26 | * if nonzero, this seed is used to pick the random bids
|
---|
27 | * @return
|
---|
28 | */
|
---|
29 | public BidRanking generateComparisonsByUncertaintyPercentage(
|
---|
30 | double uncertaintyPercentage, long seed) {
|
---|
31 | int amountOfOutcomes = (int) (uncertaintyPercentage
|
---|
32 | * (outcomeSpace.getAllOutcomes().size()));
|
---|
33 | return generateRankingByAmount(amountOfOutcomes, seed);
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | *
|
---|
38 | * @param amountOfOutcomes
|
---|
39 | * number of outcomes needed.
|
---|
40 | * @param seed
|
---|
41 | * if nonzero, this seed is used to pick the random bids
|
---|
42 | * @return amountOfOutcomes randomly picked bids, sorted from low to high
|
---|
43 | * utility, NOTICE this algorithm first generates a list with ALL
|
---|
44 | * bids, and iterates multiple times over this list, which can be
|
---|
45 | * prohibitively expensive.
|
---|
46 | */
|
---|
47 | public BidRanking generateRankingByAmount(int amountOfOutcomes, long seed) {
|
---|
48 | List<BidDetails> selectedBidsWithUtilities = new ArrayList<BidDetails>();
|
---|
49 | List<BidDetails> allBids = outcomeSpace.getAllOutcomes();
|
---|
50 | BidDetails minBid = outcomeSpace.getMinBidPossible();
|
---|
51 | BidDetails maxBid = outcomeSpace.getMaxBidPossible();
|
---|
52 | double minUtil = minBid.getMyUndiscountedUtil();
|
---|
53 | double maxUtil = maxBid.getMyUndiscountedUtil();
|
---|
54 | int numberOfBids = amountOfOutcomes - 2;
|
---|
55 |
|
---|
56 | //Here we make sure that the max and min bid are in the ranking to avoid an agent cheating by
|
---|
57 | //discovering utilities of bids added at the ranking extremities. We deal with the case <= 2 first.
|
---|
58 | if (numberOfBids <= 0) {
|
---|
59 | List<Bid> baseRanking = new ArrayList<Bid>();
|
---|
60 | baseRanking.add(minBid.getBid());
|
---|
61 | baseRanking.add(maxBid.getBid());
|
---|
62 | return new BidRanking(baseRanking,minUtil,maxUtil);
|
---|
63 | }
|
---|
64 | allBids.remove(maxBid);
|
---|
65 | allBids.remove(minBid);
|
---|
66 | if (seed == 0) {
|
---|
67 | Collections.shuffle(allBids);
|
---|
68 | } else {
|
---|
69 | Collections.shuffle(allBids, new Random(seed));
|
---|
70 | }
|
---|
71 | selectedBidsWithUtilities = allBids.subList(0, numberOfBids);
|
---|
72 | Collections.sort(selectedBidsWithUtilities,
|
---|
73 | new BidDetailsSorterUtility().reversed());
|
---|
74 | List<Bid> bids = new ArrayList<Bid>();
|
---|
75 | selectedBidsWithUtilities.stream()
|
---|
76 | .forEach(bid -> bids.add(bid.getBid()));
|
---|
77 | //Add min and max bids to the ranking.
|
---|
78 | bids.add(0,minBid.getBid());
|
---|
79 | bids.add(maxBid.getBid());
|
---|
80 | return new BidRanking(bids, minUtil, maxUtil);
|
---|
81 | }
|
---|
82 |
|
---|
83 | public List<OutcomeComparison> generateComparisonsFromRankedOutcomeSet(
|
---|
84 | List<BidDetails> selectedBids) {
|
---|
85 | List<OutcomeComparison> comparisons = new ArrayList<OutcomeComparison>();
|
---|
86 | for (int i = 0; i < selectedBids.size() - 1; i++) {
|
---|
87 | comparisons.add(new OutcomeComparison(selectedBids.get(i),
|
---|
88 | selectedBids.get(i + 1)));
|
---|
89 | }
|
---|
90 | return comparisons;
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * TODO DOC
|
---|
95 | *
|
---|
96 | * @param amount
|
---|
97 | * @param error
|
---|
98 | * @param seed
|
---|
99 | * if nonzero, this seed is used to pick the random bids
|
---|
100 | * @return
|
---|
101 | */
|
---|
102 | public List<OutcomeComparison> generateComparisonsWithError(int amount,
|
---|
103 | double error, long seed) {
|
---|
104 | List<OutcomeComparison> comparisons = generateRankingByAmount(amount,
|
---|
105 | seed).getPairwiseComparisons();
|
---|
106 | int wrongComparisonsAmount = (int) (error * comparisons.size());
|
---|
107 | for (int i = 0; i < wrongComparisonsAmount; i++) {
|
---|
108 | if (comparisons.get(i).getComparisonResult() == -1)
|
---|
109 | comparisons.get(i).setComparisonResult(+1);
|
---|
110 | else
|
---|
111 | comparisons.get(i).setComparisonResult(-1);
|
---|
112 | }
|
---|
113 | return comparisons;
|
---|
114 | }
|
---|
115 |
|
---|
116 | public SortedOutcomeSpace getOutcomeSpace() {
|
---|
117 | return outcomeSpace;
|
---|
118 | }
|
---|
119 |
|
---|
120 | public List<BidDetails> getAllBids() {
|
---|
121 | return outcomeSpace.getAllOutcomes();
|
---|
122 | }
|
---|
123 | }
|
---|