1 | package genius.core.uncertainty;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 |
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.Iterator;
|
---|
7 | import java.util.Random;
|
---|
8 |
|
---|
9 | import agents.org.apache.commons.lang.StringUtils;
|
---|
10 | import genius.core.Bid;
|
---|
11 | import genius.core.BidIterator;
|
---|
12 | import genius.core.Domain;
|
---|
13 |
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Provides a (total) ranking of bids: b1 <= b2 <= ... <= bn
|
---|
17 | */
|
---|
18 | public class BidRanking implements Iterable<Bid> {
|
---|
19 | /** Ordered from low to high */
|
---|
20 | private final List<Bid> bidOrder;
|
---|
21 | private final double lowUtility;
|
---|
22 | private final double highUtility;
|
---|
23 | protected Random random = new Random();
|
---|
24 |
|
---|
25 | /**
|
---|
26 | *
|
---|
27 | * @param bidOrder
|
---|
28 | * bids, Ordered from low to high utility for me. Must not be
|
---|
29 | * empty.
|
---|
30 | * @param lowUtil
|
---|
31 | * A suggestion for the utility of the first (worst) bid in the
|
---|
32 | * list. Must be in [0,1]
|
---|
33 | * @param highUtil
|
---|
34 | * Suggested utility for me of the last (best) bid in the list.
|
---|
35 | * Must be in [lowUtil,1]
|
---|
36 | */
|
---|
37 | public BidRanking(List<Bid> bidOrder, double lowUtil, double highUtil) {
|
---|
38 | if (bidOrder == null || bidOrder.isEmpty()) {
|
---|
39 | throw new IllegalArgumentException(
|
---|
40 | "bid order must contain at least one value.");
|
---|
41 | }
|
---|
42 | if (lowUtil < 0 || lowUtil > 1) {
|
---|
43 | throw new IllegalArgumentException("low utility must be in [0,1]");
|
---|
44 | }
|
---|
45 | if (highUtil < lowUtil || lowUtil > 1) {
|
---|
46 | throw new IllegalArgumentException(
|
---|
47 | "low utility must be in [" + lowUtil + ",1]");
|
---|
48 | }
|
---|
49 | this.bidOrder = bidOrder;
|
---|
50 | this.lowUtility = lowUtil;
|
---|
51 | this.highUtility = highUtil;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public Bid getMinimalBid() {
|
---|
55 | return bidOrder.get(0);
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | *
|
---|
60 | * @return The utility of the first (worst) bid in the list.
|
---|
61 | * In [0,1]
|
---|
62 | */
|
---|
63 | public Double getLowUtility() {
|
---|
64 | return lowUtility;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | *
|
---|
69 | * @return The utility of the last (best) bid in the list.
|
---|
70 | * In [lowUtil,1].
|
---|
71 | */
|
---|
72 | public Double getHighUtility() {
|
---|
73 | return highUtility;
|
---|
74 | }
|
---|
75 |
|
---|
76 | public Bid getMaximalBid() {
|
---|
77 | return bidOrder.get(bidOrder.size() - 1);
|
---|
78 | }
|
---|
79 |
|
---|
80 | public int indexOf(Bid b) {
|
---|
81 | return bidOrder.indexOf(b);
|
---|
82 | }
|
---|
83 |
|
---|
84 | public List<OutcomeComparison> getPairwiseComparisons() {
|
---|
85 | ArrayList<OutcomeComparison> comparisons = new ArrayList<OutcomeComparison>();
|
---|
86 | for (int i = 0; i < bidOrder.size() - 1; i++)
|
---|
87 | comparisons.add(new OutcomeComparison(bidOrder.get(i),
|
---|
88 | bidOrder.get(i + 1), -1));
|
---|
89 | return comparisons;
|
---|
90 |
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * This function computes the sum of pairwise distances between bids in the bidRanking.
|
---|
95 | * @return double
|
---|
96 | */
|
---|
97 | public double getTotalVarDistance() {
|
---|
98 | double totalVarDist = 0;
|
---|
99 | for (int i = 0; i<bidOrder.size(); i++) {
|
---|
100 | for (int j = i+1; j<bidOrder.size(); j++) {
|
---|
101 | totalVarDist += bidOrder.get(i).getDistance(bidOrder.get(j));
|
---|
102 | System.out.println(bidOrder.get(i).getDistance(bidOrder.get(j)));
|
---|
103 | }
|
---|
104 | }
|
---|
105 | double correcting_factor = (double)2/((double)(Math.pow(bidOrder.size(),2) - bidOrder.size()));
|
---|
106 | return totalVarDist*correcting_factor;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Computes the added TV distance brought by the bid in input.
|
---|
111 | * @param Bid b
|
---|
112 | * @return double
|
---|
113 | */
|
---|
114 |
|
---|
115 | public double addedTV(Bid b) {
|
---|
116 | double addedDist = 0;
|
---|
117 | if (bidOrder.contains(b))
|
---|
118 | return 0;
|
---|
119 | for (int i=0; i<bidOrder.size(); i++) {
|
---|
120 | addedDist += b.getDistance(bidOrder.get(i));
|
---|
121 | }
|
---|
122 | //double correcting_factor = (double)2/((double)(Math.pow(bidOrder.size(),2) - (double)bidOrder.size()));
|
---|
123 | return addedDist;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * This function returns the bid in the domain which is the farthest apart from the bids in the bidRanking.
|
---|
128 | * Note: There can be possibly many such bids, the function just return the first one it finds.
|
---|
129 | * @return Bid
|
---|
130 | */
|
---|
131 | public Bid getTVMaximizer(){
|
---|
132 | Domain domain = this.getMaximalBid().getDomain();
|
---|
133 | double max = 0;
|
---|
134 | BidIterator bidIterator = new BidIterator(domain);
|
---|
135 | Bid maximizer = bidIterator.next();
|
---|
136 | while(bidIterator.hasNext()) {
|
---|
137 | Bid nextBid = bidIterator.next();
|
---|
138 | if(this.addedTV(nextBid) > max) {
|
---|
139 | max = this.addedTV(nextBid);
|
---|
140 | maximizer = nextBid;
|
---|
141 | }
|
---|
142 | }
|
---|
143 | return maximizer;
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * This function returns the bid in the domain which is the closest from the bids in the bidRanking.
|
---|
148 | * Note: There can be possibly many such bids, the function just return the first one it finds.
|
---|
149 | * @return Bid
|
---|
150 | */
|
---|
151 | public Bid getTVMinimizer(){
|
---|
152 | Domain domain = this.getMaximalBid().getDomain();
|
---|
153 | double min = domain.getNumberOfPossibleBids();
|
---|
154 | BidIterator bidIterator = new BidIterator(domain);
|
---|
155 | Bid maximizer = bidIterator.next();
|
---|
156 | while(bidIterator.hasNext()) {
|
---|
157 | Bid nextBid = bidIterator.next();
|
---|
158 | if(this.addedTV(nextBid) < min) {
|
---|
159 | min = this.addedTV(nextBid);
|
---|
160 | maximizer = nextBid;
|
---|
161 | }
|
---|
162 | }
|
---|
163 | return maximizer;
|
---|
164 | }
|
---|
165 | /**
|
---|
166 | * Returns a random bid not in the bid rankin
|
---|
167 | * @return rdm bid
|
---|
168 | */
|
---|
169 | public Bid getRandomBid(){
|
---|
170 | Domain domain = this.getMaximalBid().getDomain();
|
---|
171 | Bid rdm = domain.getRandomBid(random);
|
---|
172 | while (bidOrder.contains(rdm)){
|
---|
173 | rdm = domain.getRandomBid(random);
|
---|
174 | }
|
---|
175 | return rdm;
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | public List<Bid> getBidOrder()
|
---|
180 | {
|
---|
181 | return bidOrder;
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * The size equals 1 + the number of comparisons
|
---|
187 | *
|
---|
188 | * @return
|
---|
189 | */
|
---|
190 | public int getSize() {
|
---|
191 | return bidOrder.size();
|
---|
192 | }
|
---|
193 |
|
---|
194 | public int getAmountOfComparisons() {
|
---|
195 | return getSize() - 1;
|
---|
196 | }
|
---|
197 |
|
---|
198 | @Override
|
---|
199 | public String toString() {
|
---|
200 | return StringUtils.join(bidOrder.iterator(), " <= ");
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Iterates the bids from low to high
|
---|
205 | */
|
---|
206 | @Override
|
---|
207 | public Iterator<Bid> iterator() {
|
---|
208 | return bidOrder.iterator();
|
---|
209 | }
|
---|
210 | }
|
---|