Esempio n. 1
0
def test_compute_showdown():

    # Check that the same hole cards results in a draw.
    computed = Leduc.compute_showdown(Card(2, 3), Card(2, 4), Card(1, 0), {
        1: 10,
        2: 10
    })
    expected = {1: 0, 2: 0}
    assert computed == expected

    # Check that player 1 pair results in a win.
    computed = Leduc.compute_showdown(Card(2, 3), Card(1, 4), Card(2, 0), {
        1: 10,
        2: 10
    })
    expected = {1: 10, 2: -10}
    assert computed == expected

    # Check that player 2 pair results in a win.
    computed = Leduc.compute_showdown(Card(1, 3), Card(2, 4), Card(2, 0), {
        1: 10,
        2: 10
    })
    expected = {1: -10, 2: 10}
    assert computed == expected

    # Check that player 1 higher card and no pairs results in a win.
    computed = Leduc.compute_showdown(Card(1, 3), Card(0, 4), Card(2, 0), {
        1: 10,
        2: 10
    })
    expected = {1: 10, 2: -10}
    assert computed == expected

    # Check that player 2 higher card and no pairs results in a win.
    computed = Leduc.compute_showdown(Card(1, 3), Card(3, 4), Card(2, 0), {
        1: 10,
        2: 10
    })
    expected = {1: -10, 2: 10}
    assert computed == expected
Esempio n. 2
0
def test_best_response_cfr():
    """Test we can run 10 iterations of CFR on Leduc and then compute a best
    response.
    """
    cards = [Card(value, suit) for value in range(3) for suit in range(2)]
    game = Leduc(cards)

    strategy, exploitabilities, strategies = cfr(game, num_iters=10, use_chance_sampling=False)

    exploitability = compute_exploitability(game, strategy)

    print("Exploitability: {}".format(exploitability))
    assert exploitability > 0.0
Esempio n. 3
0
def test_leduc():
    """Test Leduc.
    """

    # Create a game of Leduc with 9 cards.
    cards = [Card(value, suit) for value in range(3) for suit in range(2)]
    game = Leduc(cards=cards)

    # Check we can play out a hand:
    node = game.root

    assert set(node.children.keys()) == set(cards)

    # 1 gets a (0, 0), 2 gets a (1, 0).
    node = node.children[Card(0, 0)]
    node = node.children[Card(1, 0)]

    # Player 1 checks
    node = node.children[1]

    # Player 2 bets
    node = node.children[2]

    # Player 1 calls
    node = node.children[1]

    assert set(node.children.keys()) == {
        Card(2, 0), Card(0, 1), Card(1, 1),
        Card(2, 1)
    }

    # The board is (1, 1).
    node = node.children[Card(1, 1)]

    # Player 2 to play
    assert node.player == 2

    # 4 bets
    node = node.children[2]
    node = node.children[2]
    node = node.children[2]
    node = node.children[2]

    # End of game. Player 2 wins with a pair.
    # The raise amount is 2 in the first round and 4 in the second. Both
    # players ante 1 chip initially. Thus, the pot is: 2 * 1 + 2 * 2 + 4 * 4
    #  = 22. Since 2 put in 11 to this pot, they gain 22 - 11 = 11. Player 1
    #  loses 11.
    assert node.utility == {1: -11, 2: 11}
Esempio n. 4
0
        help='The number of epochs to train the neural network for.')
    parser.add_argument(
        '--learning_rate',
        default=1e-4,
        help='The number of epochs to train the neural network for.')
    parser.add_argument('--dropout_rate',
                        default=None,
                        help='The dropout rate to use.')
    args = parser.parse_args()

    dropout_rate = None
    if args.dropout_rate:
        dropout_rate = float(args.dropout_rate)

    cards = get_deck(num_values=args.num_values, num_suits=args.num_suits)
    game = Leduc(cards)

    strategy, exploitabilities, strategies = cfr(
        game,
        num_iters=args.cfr_iters,
        use_chance_sampling=args.use_chance_sampling)

    exploitability = compute_exploitability(game, strategy)
    print("Exploitability of final strategy: {}".format(exploitability))

    leduc_nfsp = LeducNFSP(cards)
    state_vectors = leduc_nfsp._state_vectors
    state_dim = leduc_nfsp.state_dim
    action_dim = leduc_nfsp.action_dim

    # Now build a network.