Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions aima-core/src/main/java/aima/core/gametheory/Mechanisms.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,45 @@ private Mechanisms() {
}

/**
* The Vickrey auction with no reserve price.
*
* @see #vickreyAuction(List, double)
*/
public static <B> Pair<B, Double> vickreyAuction(List<Pair<B, Double>> bids) {
return vickreyAuction(bids, 0);
}

/**
* A sealed-bid second-price auction (Section 18.4): a bid below the reserve
* price of the seller is not taken, and the highest bidder wins and pays the
* second highest bid, but never less than the reserve; so a single bidder
* pays the reserve price.
*
* @param bids
* the pairs (bidder, bid).
* @param reserve
* the reserve price of the seller.
* @return the highest bidder (the first one in case of ties) and the price
* it pays, the second highest bid (its own bid if it is the only
* bidder), or null if there are no bids, i.e., nothing is sold (the
* book does not discuss this case).
* it pays, or null if no bid reaches the reserve price, i.e.,
* nothing is sold.
*/
public static <B> Pair<B, Double> vickreyAuction(List<Pair<B, Double>> bids) {
if (bids.isEmpty()) {
return null;
}
Pair<B, Double> winner = bids.get(0);
public static <B> Pair<B, Double> vickreyAuction(List<Pair<B, Double>> bids, double reserve) {
Pair<B, Double> winner = null;
List<Double> ranked = new ArrayList<>();
for (Pair<B, Double> b : bids) {
if (b.getSecond() > winner.getSecond()) {
if (b.getSecond() < reserve) {
continue;
}
if (winner == null || b.getSecond() > winner.getSecond()) {
winner = b;
}
ranked.add(b.getSecond());
}
if (winner == null) {
return null;
}
ranked.sort(Collections.reverseOrder());
double price = ranked.size() > 1 ? ranked.get(1) : ranked.get(0);
double price = ranked.size() > 1 ? Math.max(reserve, ranked.get(1)) : reserve;
return new Pair<>(winner.getFirst(), price);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,22 @@ public void testVickreyAuction() {
Arrays.asList(new Pair<>("a", 10.0), new Pair<>("b", 8.0), new Pair<>("c", 5.0)));
Assert.assertEquals("a", result.getFirst());
Assert.assertEquals(8, result.getSecond(), DELTA);
Assert.assertEquals(7, Mechanisms.vickreyAuction(Arrays.asList(new Pair<>("a", 7.0))).getSecond(), DELTA);
// a single bidder pays the reserve price, 0 by default
Assert.assertEquals(0, Mechanisms.vickreyAuction(Arrays.asList(new Pair<>("a", 7.0))).getSecond(), DELTA);
}

@Test
public void testVickreyAuctionWithReservePrice() {
Assert.assertEquals(5, Mechanisms.vickreyAuction(Arrays.asList(new Pair<>("a", 10.0)), 5).getSecond(), DELTA);
// a bid below the reserve price is not taken
Assert.assertNull(Mechanisms.vickreyAuction(Arrays.asList(new Pair<>("a", 4.0)), 5));
// the second highest bid is below the reserve price, which the winner pays
Pair<String, Double> result = Mechanisms
.vickreyAuction(Arrays.asList(new Pair<>("a", 10.0), new Pair<>("b", 3.0)), 5);
Assert.assertEquals("a", result.getFirst());
Assert.assertEquals(5, result.getSecond(), DELTA);
Assert.assertEquals(8, Mechanisms
.vickreyAuction(Arrays.asList(new Pair<>("a", 10.0), new Pair<>("b", 8.0)), 5).getSecond(), DELTA);
}

@Test
Expand Down