diff --git a/aima-core/src/main/java/aima/core/gametheory/Mechanisms.java b/aima-core/src/main/java/aima/core/gametheory/Mechanisms.java index 6e3ef71d9..b7a7857e9 100644 --- a/aima-core/src/main/java/aima/core/gametheory/Mechanisms.java +++ b/aima-core/src/main/java/aima/core/gametheory/Mechanisms.java @@ -27,27 +27,45 @@ private Mechanisms() { } /** + * The Vickrey auction with no reserve price. + * + * @see #vickreyAuction(List, double) + */ + public static Pair vickreyAuction(List> 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 Pair vickreyAuction(List> bids) { - if (bids.isEmpty()) { - return null; - } - Pair winner = bids.get(0); + public static Pair vickreyAuction(List> bids, double reserve) { + Pair winner = null; List ranked = new ArrayList<>(); for (Pair 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); } diff --git a/aima-core/src/test/java/aima/test/core/unit/gametheory/GameTheoryTest.java b/aima-core/src/test/java/aima/test/core/unit/gametheory/GameTheoryTest.java index c4d0d0643..f835cb736 100644 --- a/aima-core/src/test/java/aima/test/core/unit/gametheory/GameTheoryTest.java +++ b/aima-core/src/test/java/aima/test/core/unit/gametheory/GameTheoryTest.java @@ -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 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