예제 #1
0
 def test_competitor_pool(self):
     players = [
         LearningPlayer(name='random',
                        estimation_mode=LearningPlayer.NO_ESTIMATION)
     ]
     competitors = [
         LearningPlayer(name='random',
                        estimation_mode=LearningPlayer.ACTUAL_Q)
         for _ in range(2)
     ]
     simulator = Simulator(2, players, competitors)
     simulator.play_rounds()
     self.assertRaises(NoRecordsException, simulator.get_game_data)
예제 #2
0
    def test_features(self):
        players = [
            LearningPlayer(name='random',
                           estimation_mode=LearningPlayer.ACTUAL_Q)
            for _ in range(3)
        ]
        game = LandlordGame(players=players)
        while not game.is_round_over():
            curr_player = game.get_current_player()
            curr_features = curr_player._derive_features(game)
            curr_hand_vector = game.get_current_player().get_hand_vector(
                game, game.get_current_position())
            move = game.get_current_player().make_move(
                game, game.get_current_position())
            curr_move_vector = game.get_current_player().compute_move_vector(
                game.get_current_position(), game.get_landlord_position(),
                move)

            game.play_move(move)

            self.assertTrue(
                np.allclose(curr_features,
                            curr_player.record_history_matrices[-1]))
            self.assertTrue(
                np.allclose(curr_move_vector,
                            curr_player.record_move_vectors[-1]))
            self.assertTrue(
                np.allclose(curr_hand_vector,
                            curr_player.record_hand_vectors[-1]))
예제 #3
0
def load_net(net, models_dir='../models/'):
    return LearningPlayer(name=net,
                          net_dir=models_dir + net,
                          estimation_mode=LearningPlayer.ACTUAL_Q,
                          discount_factor=1,
                          epsilon=0,
                          learning_rate=0.3)
예제 #4
0
 def test_setup(self):
     players = [LearningPlayer('v1', None)] * 3
     game = LandlordGame(players=players)
     hands = {
         TurnPosition.FIRST: [Card.ACE] * 4 + [Card.KING] * 4 +
         [Card.QUEEN] * 4 + [Card.JACK] * 4 + [Card.THREE],
         TurnPosition.SECOND: [Card.TEN] * 4 + [Card.NINE] * 4 +
         [Card.EIGHT] * 4 + [Card.SEVEN] * 4 + [Card.THREE],
         TurnPosition.THIRD: [Card.FIVE] * 4 + [Card.FOUR] * 4 +
         [Card.SIX] * 4 + [Card.TWO] * 4 + [Card.THREE] * 2 +
         [Card.LITTLE_JOKER] + [Card.BIG_JOKER]
     }
     game._betting_complete = True
     game.force_setup(TurnPosition.THIRD, hands, 2)
     self.assertTrue(game.get_current_position() == TurnPosition.THIRD)
     game.play_move(None)
     self.assertTrue(
         game.get_current_position() == TurnPosition.THIRD.next())
     self.assertTrue(len(game.get_move_logs()) == 1)
     self.assertTrue(game.get_move_logs()[0][1] is None)
     game.play_move(
         SpecificMove(RankedMoveType(MoveType.BOMB, Card.KING),
                      cards=Counter({Card.KING: 4})))
     self.assertTrue(game.get_current_position() == TurnPosition.SECOND)
     feature_matrix = players[1]._derive_features(game)
     self.assertTrue(feature_matrix[0][-6] == 1)
     self.assertTrue(feature_matrix[0][-2] == 1)
     self.assertTrue(feature_matrix[1][10] == 4)
     self.assertTrue(np.sum(feature_matrix) == 7)
예제 #5
0
    def test_player_game(self):
        players = [
            LearningPlayer(name='random',
                           estimation_mode=LearningPlayer.ACTUAL_Q)
        ] * 3
        game = LandlordGame(players=players)
        hands = {
            TurnPosition.FIRST: [Card.ACE] * 4 + [Card.KING] * 4 +
            [Card.QUEEN] * 4 + [Card.JACK] * 4 + [Card.THREE],
            TurnPosition.SECOND: [Card.TEN] * 4 + [Card.NINE] * 4 +
            [Card.EIGHT] * 4 + [Card.SEVEN] * 4 + [Card.THREE],
            TurnPosition.THIRD: [Card.FIVE] * 4 + [Card.FOUR] * 4 +
            [Card.SIX] * 4 + [Card.TWO] * 4 + [Card.THREE] * 2 +
            [Card.LITTLE_JOKER] + [Card.BIG_JOKER]
        }
        game._betting_complete = True
        game.force_setup(TurnPosition.THIRD, hands, 3)
        game.main_game()
        players[0].compute_future_q(game)
        self.assertTrue(np.sum(np.abs(game.get_scores())) > 0)
        # game is over
        self.assertTrue(np.abs(players[0]._record_future_q[-1]) > 0.5)

        features = players[0]._derive_features(game)
        self.assertTrue(
            np.sum(features[:, players[0].get_feature_index('I_AM_LANDLORD')])
            != 0)
        # it is possible this guy never plays, eventually
        self.assertTrue(
            np.sum(features[:, players[0].
                            get_feature_index('I_AM_BEFORE_LANDLORD')]) != 0)
예제 #6
0
 def test_bet_3(self):
     players = [LearningPlayer('v1')] * 3
     game = LandlordGame(players=players)
     game.play_move(BetMove(0))
     game.play_move(BetMove(3))
     self.assertFalse(game.is_round_over())
     self.assertTrue(game.is_betting_complete())
     self.assertTrue(game.get_bet_amount() == 3)
예제 #7
0
 def test_bet_1(self):
     players = [LearningPlayer('v1')] * 3
     game = LandlordGame(players=players)
     game.play_move(BetMove(0))
     game.play_move(BetMove(0))
     self.assertFalse(game.is_round_over())
     self.assertFalse(game.is_betting_complete())
     self.assertFalse(game.move_ends_game(BetMove(1)))
     self.assertTrue(game.move_ends_game(BetMove(0)))
     game.play_move(BetMove(0))
     self.assertTrue(game.is_round_over())
     self.assertTrue(game.is_betting_complete())
예제 #8
0
 def test_endgame_scenario(self):
     players = [LearningPlayer('v1')] * 3
     game = LandlordGame(players=players)
     hands = {
         TurnPosition.FIRST: [Card.ACE] * 1,
         TurnPosition.SECOND: [Card.TEN] * 1,
         TurnPosition.THIRD: [Card.JACK, Card.QUEEN]
     }
     game._betting_complete = True
     game.force_setup(TurnPosition.THIRD, hands, 2)
     game.main_game()
     self.assertTrue(TurnPosition.FIRST in game.get_winners())
     self.assertTrue(len(game.get_move_logs()) == 2)
예제 #9
0
 def test_landlord_bombing(self):
     players = [LearningPlayer('v1')] * 3
     game = LandlordGame(players=players)
     hands = {
         TurnPosition.FIRST: [Card.ACE] * 4 + [Card.THREE],
         TurnPosition.SECOND: [Card.TEN] * 4 + [Card.THREE],
         TurnPosition.THIRD: [Card.FIVE] * 4 + [Card.THREE]
     }
     game._betting_complete = True
     game.force_setup(TurnPosition.THIRD, hands, 2)
     game.play_move(
         SpecificMove(RankedMoveType(MoveType.BOMB, Card.FIVE),
                      Counter({Card.FIVE: 4})))
     self.assertTrue(game._bet_amount == 4)
예제 #10
0
 def test_bet_2(self):
     players = [LearningPlayer('v1')] * 3
     game = LandlordGame(players=players)
     game.play_move(BetMove(0))
     self.assertEqual(len(game.get_legal_moves()), 4)
     one_better = game.get_current_position()
     game.play_move(BetMove(1))
     self.assertFalse(game.is_round_over())
     self.assertFalse(game.is_betting_complete())
     game.play_move(BetMove(0))
     self.assertFalse(game.is_round_over())
     self.assertTrue(game.is_betting_complete())
     self.assertTrue(game.get_bet_amount() == 1)
     self.assertEqual(game.get_current_position(), one_better)
예제 #11
0
 def test_llord_winning(self):
     players = [LearningPlayer(name='random')] * 3
     game = LandlordGame(players=players)
     hands = {
         TurnPosition.FIRST: [Card.ACE] * 4 + [Card.KING] * 4 +
         [Card.QUEEN] * 4 + [Card.JACK] * 4 + [Card.THREE],
         TurnPosition.SECOND: [Card.TEN] * 4 + [Card.NINE] * 4 +
         [Card.EIGHT] * 4 + [Card.SEVEN] * 4 + [Card.THREE],
         TurnPosition.THIRD: [Card.FIVE] * 4
     }
     game._betting_complete = True
     game.force_setup(TurnPosition.THIRD, hands, 3)
     game.main_game()
     self.assertTrue(TurnPosition.THIRD in game.get_winners())
     self.assertTrue(len(game.get_move_logs()) == 1)
예제 #12
0
    def test_full_game(self):
        players = [LearningPlayer(name='random') for _ in range(3)]
        game = LandlordGame(players=players)
        game.play_round()

        while np.sum(np.abs(game.get_scores())) == 0:
            players = [LearningPlayer(name='random') for _ in range(3)]
            game = LandlordGame(players=players)
            game.play_round()

        # game is over
        for i in range(3):
            # print(players[i].record_future_q[-1])
            # self.assertTrue(np.abs(players[i].record_future_q[-1]) > 0.5)

            features = players[i]._derive_features(game)
            self.assertTrue(
                np.sum(features[:, players[i].get_feature_index('I_AM_LANDLORD'
                                                                )]) != 0)
            # it is possible this guy never plays, eventually
            self.assertTrue(
                np.sum(features[:, players[i].
                                get_feature_index('I_AM_BEFORE_LANDLORD')]) !=
                0)
예제 #13
0
    def test_simulator_actual_q(self):
        players = [
            LearningPlayer(name='random',
                           estimation_mode=LearningPlayer.ACTUAL_Q)
            for _ in range(5)
        ]
        simulator = Simulator(2, players)
        simulator.play_rounds()

        history_matrices, move_vectors, hand_vectors, qs = simulator.get_game_data(
        )
        self.assertTrue(len(hand_vectors) == len(move_vectors))
        self.assertTrue(
            history_matrices[0].shape[0] == LearningPlayer.TIMESTEPS)
        self.assertTrue(len(history_matrices) == qs.shape[0])
        self.assertTrue(len(move_vectors) == len(history_matrices))
예제 #14
0
 def test_landlord_game_ending(self):
     players = [LearningPlayer('v1')] * 3
     game = LandlordGame(players=players)
     hands = {
         TurnPosition.FIRST: [Card.ACE] * 4,
         TurnPosition.SECOND: [Card.TEN] * 4,
         TurnPosition.THIRD: [Card.FIVE] * 4
     }
     game._betting_complete = True
     game.force_setup(TurnPosition.THIRD, hands, 2)
     self.assertTrue(
         game.move_ends_game(
             SpecificMove(RankedMoveType(MoveType.BOMB, Card.FIVE),
                          Counter({Card.FIVE: 4}))))
     self.assertFalse(
         game.move_ends_game(
             SpecificMove(RankedMoveType(MoveType.BOMB, Card.TEN),
                          Counter({Card.TEN: 4}))))
예제 #15
0
 def test_sweep(self):
     players = [LearningPlayer('v1')] * 3
     game = LandlordGame(players=players)
     hands = {
         TurnPosition.FIRST: [Card.ACE] * LandlordGame.DEAL_SIZE,
         TurnPosition.SECOND: [Card.TEN] * LandlordGame.DEAL_SIZE,
         TurnPosition.THIRD: [Card.FIVE] * 4
     }
     game._betting_complete = True
     game.force_setup(TurnPosition.THIRD, hands, 2)
     game.play_move(
         SpecificMove(RankedMoveType(MoveType.BOMB, Card.FIVE),
                      Counter({Card.FIVE: 4})))
     self.assertTrue(game.peasants_have_no_plays())
     self.assertTrue(game.get_scores()[TurnPosition.THIRD] == 2 * 2 * 2 *
                     LandlordGame.SWEEP_MULTIPLIER)
     self.assertEqual(game.get_r(), 24)
     self.assertEqual(game.get_winbased_r(), 1)
예제 #16
0
 def test_player_move(self):
     players = [LearningPlayer(name='random')] * 3
     game = LandlordGame(players=players)
     hands = {
         TurnPosition.FIRST: [Card.ACE] * 4 + [Card.KING] * 4 +
         [Card.QUEEN] * 4 + [Card.JACK] * 4 + [Card.THREE],
         TurnPosition.SECOND: [Card.TEN] * 4 + [Card.NINE] * 4 +
         [Card.EIGHT] * 4 + [Card.SEVEN] * 4 + [Card.THREE],
         TurnPosition.THIRD: [Card.FIVE] * 4 + [Card.FOUR] * 4 +
         [Card.SIX] * 4 + [Card.TWO] * 4 + [Card.THREE] * 2 +
         [Card.LITTLE_JOKER] + [Card.BIG_JOKER]
     }
     game._betting_complete = True
     game.force_setup(TurnPosition.THIRD, hands, 3)
     game2 = copy(game)
     game.play_move(
         SpecificMove(RankedMoveType(MoveType.BOMB, Card.FIVE),
                      Counter({Card.FIVE: 4})))
     self.assertNotEqual(game2.get_hand(TurnPosition.THIRD),
                         game.get_hand(TurnPosition.THIRD))
예제 #17
0
 def test_peasant_winning(self):
     players = [LearningPlayer(name='random')] * 3
     game = LandlordGame(players=players)
     hands = {
         TurnPosition.FIRST: [Card.ACE] * 4,
         TurnPosition.SECOND: [Card.TEN] + [Card.THREE],
         TurnPosition.THIRD: [Card.FIVE] * 3 + [Card.THREE] + [Card.FOUR]
     }
     game._betting_complete = True
     game.force_setup(TurnPosition.THIRD, hands, 3)
     hand_vector = players[0].get_hand_vector(game, TurnPosition.FIRST)
     self.assertTrue(hand_vector[11] == 4)
     self.assertTrue(hand_vector[-2] == 2)
     self.assertTrue(hand_vector[-3] == 5)
     self.assertTrue(hand_vector[-1] == 4)
     # self.assertTrue(np.sum(hand_vector) == 4)
     game.main_game()
     self.assertTrue(TurnPosition.THIRD not in game.get_winners())
     self.assertTrue(TurnPosition.SECOND in game.get_winners())
     self.assertTrue(TurnPosition.FIRST in game.get_winners())
     self.assertTrue(len(game.get_move_logs()) == 2)
예제 #18
0
 def test_betting(self):
     players = [LearningPlayer('v1')] * 3
     game = LandlordGame(players=players)
     game.force_current_position(TurnPosition.SECOND)
     game.force_kitty([Card.LITTLE_JOKER, Card.BIG_JOKER, Card.THREE])
     game._make_bet_move(BetMove(2))
     game._make_bet_move(None)
     self.assertEqual(game.get_last_played(), BetMove(2))
     game._make_bet_move(BetMove(3))
     hands = {
         TurnPosition.FIRST: [Card.ACE] * 4 + [Card.KING] * 4 +
         [Card.QUEEN] * 4 + [Card.JACK] * 4 + [Card.THREE],
         TurnPosition.SECOND: [Card.TEN] * 4 + [Card.NINE] * 4 +
         [Card.EIGHT] * 4 + [Card.SEVEN] * 4 + [Card.THREE],
         TurnPosition.THIRD: [Card.FIVE] * 4 + [Card.FOUR] * 4 +
         [Card.SIX] * 4 + [Card.TWO] * 4 + [Card.THREE] * 1
     }
     game.force_setup(TurnPosition.FIRST, hands, 2)
     game.play_move(
         SpecificMove(RankedMoveType(MoveType.BOMB, Card.ACE),
                      cards=Counter({Card.ACE: 4})))
     feature_matrix = players[1]._derive_features(game)
     self.assertTrue(feature_matrix[0][-3] == 2)
     self.assertTrue(np.sum(players[1]._derive_features(game)) == 16)
예제 #19
0
def load_net(net):
    return LearningPlayer(name=net,
                          net_dir='../models/' + net,
                          estimation_mode=LearningPlayer.ACTUAL_Q)
예제 #20
0
def load_net(net):
    return LearningPlayer(name=net,
                          net_dir='../models/' + net,
                          estimation_mode=LearningPlayer.NO_ESTIMATION)
예제 #21
0
def load_net(net):
    return LearningPlayer(name=net,
                          net_dir='../models/' + net,
                          estimation_mode=LearningPlayer.NO_ESTIMATION)


if __name__ == "__main__":
    '''
    players = [
        LearningPlayer_v1(name='3_29_sim8_model' + str(i), net_dir='../models/3_29_sim8_model' + str(i))
    for i in range(3)] + [LearningPlayer_v1(name='random') for _ in range(3)] + \
              [LearningPlayer_v1(name='3_29_sim7_model' + str(i), net_dir='../models/3_29_sim7_model' + str(i))
    for i in range(3)]
    '''
    players = [LearningPlayer('random', estimation_mode=LearningPlayer.NO_ESTIMATION) for i in range(1)] + \
              [load_net('4_1_sim3_model2'), load_net('4_1_sim4_model4'), load_net('4_1_sim4_model6'),
             load_net('4_1_sim3_model3'), load_net('4_1_sim3_model0'), load_net('4_2_sim4_model15')]

    for i in tqdm(range(1)):
        simulator = Simulator(200, players)
        simulator.play_rounds()

        results = simulator.get_results()

    stats = GameStats(players, results)

    stats.print_player_stats()

    #game = LandlordGame(players=players)
    #game.play_round()
예제 #22
0
 def test_best_montecarlo(self):
     players = [LearningPlayer(name='random')] * 3
     game = LandlordGame(players=players)
     game.play_round(debug=False)
예제 #23
0
 def load_net(net):
     return LearningPlayer(name=net,
                           net_dir='../models/' + net,
                           estimation_mode=LearningPlayer.BEST_SIMULATION,
                           mc_best_move_depth=3)
예제 #24
0
import cProfile
from pstats import SortKey
import pstats

from landlordai.game.player import LearningPlayer
from landlordai.sim.game_stats import GameStats
from landlordai.sim.simulate import Simulator

if __name__ == '__main__':

    def load_net(net):
        return LearningPlayer(name=net,
                              net_dir='../models/' + net,
                              estimation_mode=LearningPlayer.BEST_SIMULATION,
                              mc_best_move_depth=3)

    players = [LearningPlayer('random', estimation_mode=LearningPlayer.MONTECARLO_RANDOM) for i in range(1)] + \
              [load_net('4_1_sim3_model2'), load_net('4_1_sim4_model4'), load_net('4_1_sim4_model6'),
             load_net('4_1_sim3_model3'), load_net('4_1_sim3_model0')]

    pr = cProfile.Profile()
    pr.enable()

    simulator = Simulator(1, players)
    simulator.play_rounds()

    pr.disable()
    pr.create_stats()

    ps = pstats.Stats(pr).sort_stats('tottime')
    ps.print_stats()
예제 #25
0
 def load_best_sim_net(net):
     return LearningPlayer(name=net,
                           net_dir='../models/' + net,
                           estimation_mode=LearningPlayer.ACTUAL_Q,
                           epsilon=0,
                           discount_factor=1)