diff --git a/aima/game_theory.py b/aima/game_theory.py index ecd659ed2..c73205d3f 100644 --- a/aima/game_theory.py +++ b/aima/game_theory.py @@ -216,21 +216,24 @@ def beats(a, b): # a majority of voters rank a above b return next((a for a in candidates if all(a == b or beats(a, b) for b in candidates)), None) -def vickrey_auction(bids): +def vickrey_auction(bids, reserve=0): """ [Section 18.4] - Sealed-bid second-price (Vickrey) auction: the highest bidder wins but pays - only the second-highest bid. 'bids' maps each bidder to their bid. Returns the - (winner, price) pair. Because the winner does not pay their own bid, bidding - one's true value is a dominant strategy (the mechanism is truth-revealing). - With no bid there is no sale, and the result is (None, None); between equal - highest bids the first bidder wins, and pays that same bid. + Sealed-bid second-price (Vickrey) auction: 'bids' maps each bidder to their + bid, 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, 0 by default. Because the + price does not depend on the winner's own bid, bidding one's true value is a + dominant strategy (the mechanism is truth-revealing). Returns the (winner, + price) pair; with no bid left there is no sale, i.e., (None, None), and + between equal highest bids the first bidder wins, and pays that same bid. """ - if not bids: + valid = {bidder: bid for bidder, bid in bids.items() if bid >= reserve} + if not valid: return None, None - ranked = sorted(bids.values(), reverse=True) - winner = max(bids, key=bids.get) - price = ranked[1] if len(ranked) > 1 else ranked[0] + ranked = sorted(valid.values(), reverse=True) + winner = max(valid, key=valid.get) + price = max(reserve, ranked[1]) if len(ranked) > 1 else reserve return winner, price diff --git a/tests/test_game_theory.py b/tests/test_game_theory.py index b32320258..d71c0674c 100644 --- a/tests/test_game_theory.py +++ b/tests/test_game_theory.py @@ -103,6 +103,17 @@ def test_vickrey_auction(): assert price == 8 # but pays the second-highest bid +def test_vickrey_auction_with_reserve_price(): + # a single bidder pays the reserve price, 0 by default + assert vickrey_auction({'a': 10}) == ('a', 0) + assert vickrey_auction({'a': 10}, reserve=5) == ('a', 5) + # a bid below the reserve is not taken + assert vickrey_auction({'a': 4}, reserve=5) == (None, None) + # the second highest bid is below the reserve, so the winner pays the reserve + assert vickrey_auction({'a': 10, 'b': 3}, reserve=5) == ('a', 5) + assert vickrey_auction({'a': 10, 'b': 8}, reserve=5) == ('a', 8) + + # --------------------------------------------------------------------------- # Additional cases taken from the book and the 3rd-edition solutions manual # --------------------------------------------------------------------------- @@ -201,7 +212,7 @@ def test_voting_single_ballot_and_auction_single_bid(): assert plurality_winner([['b', 'a']]) == 'b' assert borda_winner([['b', 'a']]) == 'b' assert condorcet_winner([['b', 'a']]) == 'b' - assert vickrey_auction({'x': 10}) == ('x', 10) + assert vickrey_auction({'x': 10}) == ('x', 0) # a single bidder pays the reserve def test_contract_net_without_bids():