예제 #1
0
파일: kuhn.py 프로젝트: seokhohong/cfr
    def test_three_players(self):
        deck = pydealer.Deck()
        cards = deck.get_list([
            '10 of Spades', 'Jack of Spades', 'Queen of Spades',
            'King of Spades'
        ])
        t, j, q, k = cards
        players = create_player_set(3)

        game = KuhnGame(players, cards, 1)

        root = game.create_root_node()

        TKQ_node = root.get_children()[(t, k, q)]
        JKQ_node = root.get_children()[(j, k, q)]

        for node in [TKQ_node, JKQ_node]:
            self.assertTrue(
                np.allclose(
                    node.play(PokerActions.RAISE_1).play(
                        PokerActions.FOLD).play(
                            PokerActions.FOLD).evaluation(),
                    np.array([2, -1, -1])))
            self.assertTrue(
                np.allclose(
                    node.play(PokerActions.CHECK).play(
                        PokerActions.RAISE_1).play(PokerActions.FOLD).play(
                            PokerActions.CALL).evaluation(),
                    np.array([-2, 3, -1])))
예제 #2
0
파일: main.py 프로젝트: seokhohong/cfr
def run_game():

    deck = pydealer.Deck()

    cards = deck.get_list(
        ['Jack of Spades', 'Queen of Spades', 'King of Spades'])
    players = create_player_set(2)

    game = KuhnGame(players, cards, 1)

    root = game.create_root_node()

    #random.seed(1)

    #chance_sampling_cfr = ChanceSamplingCFR(root, players)
    #chance_sampling_cfr.run(iterations=1000)
    #chance_sampling_cfr.compute_nash_equilibrium()
    # read Nash-Equilibrum via chance_sampling_cfr.nash_equilibrium member
    # try chance_sampling_cfr.value_of_the_game() function to get value of the game (-1/18)
    #print(chance_sampling_cfr.value_of_the_game())

    # vanilla cfr
    vanilla_cfr = VanillaCFR(root, players)

    root = game.create_root_node()
    j, q, k = cards

    KQ_node = root.get_children()[(k, q)]

    vanilla_cfr._cfr_utility_recursive(
        KQ_node.play(PokerActions.CHECK).play(PokerActions.CHECK), [1, 1])
    for i in tqdm(range(10)):
        vanilla_cfr.run(iterations=100)
        vanilla_cfr.compute_nash_equilibrium()
        print(vanilla_cfr.value_of_the_game())
예제 #3
0
def run_game():

    deck = pydealer.Deck()
    hand = pydealer.Stack()

    cards = deck.get_list([
        '10 of Spades', 'Jack of Spades', 'Queen of Spades', 'King of Spades'
    ])
    players = create_player_set(3)

    game = KuhnGame(players, cards, 1)

    root = game.create_root_node()

    #random.seed(1)

    #chance_sampling_cfr = ChanceSamplingCFR(root, players)
    #chance_sampling_cfr.run(iterations=1000)
    #chance_sampling_cfr.compute_nash_equilibrium()
    # read Nash-Equilibrum via chance_sampling_cfr.nash_equilibrium member
    # try chance_sampling_cfr.value_of_the_game() function to get value of the game (-1/18)
    #print(chance_sampling_cfr.value_of_the_game())

    # vanilla cfr
    vanilla_cfr = VanillaCFR(root, players)
    for i in tqdm(range(10)):
        vanilla_cfr.run(iterations=100)
        vanilla_cfr.compute_nash_equilibrium()
예제 #4
0
파일: kuhn.py 프로젝트: seokhohong/cfr
    def get_chance_node(self):
        deck = pydealer.Deck()
        cards = deck.get_list(
            ['Jack of Spades', 'Queen of Spades', 'King of Spades'])
        players = create_player_set(2)

        game = KuhnGame(players, cards, 1)

        return game.create_root_node()
예제 #5
0
파일: kuhn.py 프로젝트: seokhohong/cfr
    def test_evaluation(self):
        deck = pydealer.Deck()
        cards = deck.get_list(
            ['Jack of Spades', 'Queen of Spades', 'King of Spades'])
        j, q, k = cards
        players = create_player_set(2)

        game = KuhnGame(players, cards, 1)

        root = game.create_root_node()

        KQ_node = root.get_children()[(k, q)]
        QJ_node = root.get_children()[(q, j)]
        KJ_node = root.get_children()[(k, j)]
        QK_node = root.get_children()[(q, k)]
        JQ_node = root.get_children()[(j, q)]
        JK_node = root.get_children()[(j, k)]

        for node in [KQ_node, QJ_node, KJ_node]:
            self.assertTrue(
                np.allclose(
                    node.play(PokerActions.RAISE_1).play(
                        PokerActions.FOLD).evaluation(), np.array([1, -1])))
            self.assertTrue(
                np.allclose(
                    node.play(PokerActions.RAISE_1).play(
                        PokerActions.CALL).evaluation(), np.array([2, -2])))
            self.assertTrue(
                np.allclose(
                    node.play(PokerActions.CHECK).play(
                        PokerActions.RAISE_1).play(
                            PokerActions.FOLD).evaluation(), np.array([-1,
                                                                       1])))
            self.assertTrue(
                np.allclose(
                    node.play(PokerActions.CHECK).play(
                        PokerActions.CHECK).evaluation(), np.array([1, -1])))

        for node in [QK_node, JQ_node, JK_node]:
            self.assertTrue(
                np.allclose(
                    node.play(PokerActions.RAISE_1).play(
                        PokerActions.FOLD).evaluation(), np.array([1, -1])))
            self.assertTrue(
                np.allclose(
                    node.play(PokerActions.RAISE_1).play(
                        PokerActions.CALL).evaluation(), np.array([-2, 2])))
            self.assertTrue(
                np.allclose(
                    node.play(PokerActions.CHECK).play(
                        PokerActions.RAISE_1).play(
                            PokerActions.FOLD).evaluation(), np.array([-1,
                                                                       1])))
            self.assertTrue(
                np.allclose(
                    node.play(PokerActions.CHECK).play(
                        PokerActions.CHECK).evaluation(), np.array([-1, 1])))
예제 #6
0
파일: kuhn.py 프로젝트: seokhohong/cfr
    def test_player_a_acts_first(self):

        deck = pydealer.Deck()
        cards = deck.get_list(
            ['Jack of Spades', 'Queen of Spades', 'King of Spades'])
        players = create_player_set(2)

        game = KuhnGame(players, cards, 1)
        root = game.create_root_node()

        for k in root.get_children():
            child = root.get_children()[k]
            assert child.get_player_to_move() == players[0]
예제 #7
0
파일: kuhn.py 프로젝트: seokhohong/cfr
    def fresh_kq_node(self):
        deck = pydealer.Deck()

        cards = deck.get_list(
            ['Jack of Spades', 'Queen of Spades', 'King of Spades'])
        players = create_player_set(2)

        game = KuhnGame(players, cards, 1)

        root = game.create_root_node()

        vanilla_cfr = VanillaCFR(root, players)
        j, q, k = cards

        KQ_node = root.get_children()[(k, q)]
        return vanilla_cfr, KQ_node
예제 #8
0
파일: kuhn.py 프로젝트: seokhohong/cfr
    def test_game_tree(self):
        deck = pydealer.Deck()
        cards = deck.get_list(
            ['Jack of Spades', 'Queen of Spades', 'King of Spades'])

        num_players = 2
        game = KuhnGame(create_player_set(num_players), cards, 1)

        # see if simulations run successfully
        for i in range(100):
            curr_node = game.create_root_node()
            while not curr_node.is_terminal():
                curr_node = random.choice(
                    list(curr_node.get_children().values()))
            self.assertTrue(
                len(np.nonzero(curr_node.evaluation())[0]) == num_players)
예제 #9
0
def fresh_tkq_node():
    deck = pydealer.Deck()

    cards = deck.get_list([
        '10 of Spades', 'Jack of Spades', 'Queen of Spades', 'King of Spades'
    ])
    players = create_player_set(3)

    game = KuhnGame(players, cards, num_deal=1)

    root = game.create_root_node()

    vanilla_cfr = VanillaCFR(root, players)
    t, j, q, k = cards

    TKQ_node = root.get_children()[(t, k, q)]
    return vanilla_cfr, TKQ_node
예제 #10
0
파일: kuhn.py 프로젝트: seokhohong/cfr
    def test_external_sampling(self):
        deck = pydealer.Deck()

        cards = deck.get_list(
            ['Jack of Spades', 'Queen of Spades', 'King of Spades'])
        players = create_player_set(2)

        game = KuhnGame(players, cards, num_deal=1)

        root = game.create_root_node()

        chance_cfr = ExternalSamplingCFR(root, players)
        chance_cfr.run(iterations=1000)
        game_value = chance_cfr.approximate_value_of_game(1000)

        epsilon = 0.01

        self.assertTrue(game_value[0] < -1. / 18 + epsilon)
        self.assertTrue(game_value[1] > 1. / 18 - epsilon)
예제 #11
0
파일: kuhn.py 프로젝트: seokhohong/cfr
    def test_external_sampling_2(self):
        deck = pydealer.Deck()

        cards = deck.get_list([
            '10 of Spades', 'Jack of Spades', 'Queen of Spades',
            'King of Spades'
        ])
        players = create_player_set(3)

        game = KuhnGame(players, cards, num_deal=1)

        root = game.create_root_node()

        chance_cfr = ExternalSamplingCFR(root, players)
        chance_cfr.run(iterations=2000)
        chance_cfr.compute_nash_equilibrium()

        game_value = chance_cfr.approximate_value_of_game(1000)
        self.assertTrue(game_value[0] > -3. / 48)
        self.assertTrue(game_value[0] < -1. / 48)
        self.assertTrue(4. / 48 > game_value[2] > 2. / 48)
예제 #12
0
def run_game():
    deck = pydealer.Deck()

    cards = deck.get_list(
        ['Jack of Spades', 'Queen of Spades', 'King of Spades'])
    players = create_player_set(2)

    game = KuhnGame(players, cards, 1)

    root = game.create_root_node()

    #players = create_player_set(2)
    #ldgame = LDGame(players, 1)
    #root = ldgame.create_root_node()

    cfr = ExternalSamplingCFR(root, players)
    for i in tqdm(range(1000)):
        cfr.run(iterations=1)
        #if i % 100 == 0:
        #    print(vanilla_cfr.compute_nash_equilibrium())
    print("Approx Value", cfr.approximate_value_of_game(1000))
    print(cfr._learned_strategy)
예제 #13
0
파일: kuhn.py 프로젝트: seokhohong/cfr
    def test_full_iteration(self):
        deck = pydealer.Deck()

        cards = deck.get_list([
            '10 of Spades', 'Jack of Spades', 'Queen of Spades',
            'King of Spades'
        ])
        players = create_player_set(3)

        game = KuhnGame(players, cards, num_deal=1)

        root = game.create_root_node()

        vanilla_cfr = VanillaCFR(root, players)

        val = np.zeros(3)
        for permutation in itertools.permutations(cards, 3):
            one_val = vanilla_cfr._cfr_utility_recursive(
                root.get_children()[permutation], np.ones(3))
            val += one_val

        self.assertTrue(
            np.allclose(val, np.array([-0.61, 0.26, 0.35]), atol=1E-1))
예제 #14
0
파일: kuhn.py 프로젝트: seokhohong/cfr
    def test_inf_sets(self):

        deck = pydealer.Deck()
        cards = deck.get_list(
            ['Jack of Spades', 'Queen of Spades', 'King of Spades'])
        j, q, k = cards
        players = create_player_set(2)

        game = KuhnGame(players, cards, 1)

        root = game.create_root_node()

        KQ_node = root.get_children()[(k, q)]
        QJ_node = root.get_children()[(q, j)]
        KJ_node = root.get_children()[(k, j)]
        QK_node = root.get_children()[(q, k)]
        JQ_node = root.get_children()[(j, q)]
        JK_node = root.get_children()[(j, k)]

        assert root.inf_set() == "."
        self.assertTrue('King of Spades' in KQ_node.inf_set())
        self.assertTrue(
            'Queen of Spades' in KQ_node.play(PokerActions.RAISE_1).inf_set())
예제 #15
0
파일: kuhn.py 프로젝트: seokhohong/cfr
    def test_tjk_12(self):
        deck = pydealer.Deck()

        cards = deck.get_list([
            '10 of Spades', 'Jack of Spades', 'Queen of Spades',
            'King of Spades'
        ])
        players = create_player_set(3)

        game = KuhnGame(players, cards, num_deal=1)

        root = game.create_root_node()

        vanilla_cfr = VanillaCFR(root, players)
        t, j, q, k = cards

        TJQ_node = root.get_children()[(t, j, q)]
        TJK_node = root.get_children()[(t, j, k)]

        starting_reach = np.ones(3)
        vanilla_cfr._cfr_utility_recursive(TJQ_node, starting_reach)
        val = vanilla_cfr._cfr_utility_recursive(
            TJK_node.play(PokerActions.RAISE_1).play(PokerActions.FOLD),
            starting_reach)
예제 #16
0
파일: kuhn.py 프로젝트: seokhohong/cfr
 def test_possible_hands(self):
     deck = pydealer.Deck()
     cards = deck.get_list(
         ['Jack of Spades', 'Queen of Spades', 'King of Spades'])
     game = KuhnGame(create_player_set(2), cards, 1)
     self.assertEqual(len(list(game.enumerate_possible_hands())), 6)