Example #1
0
def test_game(test_type, bet_strategies, action_models, bet_models):
    if test_type == 'nnvh':
        #        strategies = [3,4,3,4]
        #        bet_strategies = ['model', 'model', 'model', 'model']
        #        action_models = [None, nn_action_model, None, nn_action_model]
        #        bet_models = [hvh, nn_bet_model,hvh,nn_bet_model]
        strategies = [4, 3, 4, 3]
    elif test_type == 'nnvnn':
        strategies = [4, 4, 4, 4]

    n = 13
    wins_team1 = 0
    wins_team2 = 0
    score_team1 = 0
    score_team2 = 0
    #wins_team1 = [0 for i in range(10)]
    #wins_team2 = [0 for i in range(10)]
    #frac_won_by_nn = [0 for i in range(10)]

    for g in range(num_games):
        Total_Scores = [0 for p in range(4)]

        while True:
            #game = Game(n, strategies, bet_strategies, n, [action_model for i in range(4)], [bet_model for i in range(4)])
            game = Game(n,
                        strategies,
                        bet_strategies,
                        n,
                        action_model_objects=action_models,
                        bet_model_objects=bet_models)
            scores = game.playGame()

            for p in range(4):
                Total_Scores[p] += scores[p]
            score_team1 = score_team1 + Total_Scores[0] + Total_Scores[2]
            score_team2 = score_team2 + Total_Scores[1] + Total_Scores[3]
            if (Total_Scores[0] >= 41
                    and Total_Scores[2] >= 0) or (Total_Scores[0] >= 0
                                                  and Total_Scores[2] >= 41):
                wins_team1 += 1
                #print 'Team 1 won'
                break
            if (Total_Scores[1] >= 41
                    and Total_Scores[3] >= 0) or (Total_Scores[1] >= 0
                                                  and Total_Scores[3] >= 41):
                wins_team2 += 1
                #print 'Team 2 won'
                break
            #print scores
            #print Total_Scores
        print 'game: ' + str(g)
        #print Total_Scores
    print 'Team 1 won' + str(wins_team1)
    print 'Team 2 won' + str(wins_team2)
    #frac_won_by_nn[i] = wins_team2[i]*1.0/num_games
    #print frac_won_by_nn

    #plt.figure(2)
    #plt.plot(frac_won_by_nn)
    #plt.title('NN performance vs heuristic team')
    #plt.savefig('Plots/nn.png')
    return (wins_team1, wins_team2, score_team1, score_team2)
Example #2
0
def create_bet_data(num_batches, batch_size, strategy_var, organization_var):
    num_batches = num_batches
    batch_size = batch_size
    Bets = []
    Tricks = []
    strategy_var = strategy_var
    organization = organization_var
    datatype = organization

    #strategy_var = 'Greedy_v_Heuristic'
    #strategy_var = 'Greedy_v_Greedy'
    #strategy_var = 'Heuristic_v_Heuristic'
    #strategy_var = 'Heuristic_v_Greedy'

    #organization ='standard'
    #set data organization
    #organization = 'binary'
    #organization = 'sorted'
    #organization = 'interleave'
    #organization = 'interleave_sorted'

    def loss_bet(y_true, y_pred):
        return K.mean(y_true + K.sign(y_pred - y_true) * y_pred)

    # Returns our custom loss function
    def get_loss_bet():
        # Our custom loss function: if we make our bet (y_true >= y_pred), the loss
        # is the amount we could have gotten if we'd bet y_true, i.e., it's
        # y_true - y_pred. If we didn't make our bet, then our loss is what we
        # could have gotten minus what we lost, i.e., y_true + y_pred
        # (since -1*(-bet) = bet)
        return loss_bet

    betting_model_objects = [None, None, None, None]
    players_to_learn_from = range(4)
    if strategy_var == 'Greedy_v_Greedy':
        nameString = './Data/Greedy_v_Greedy.csv'
        gameTypeString = 'Greedy_v_Greedy'
        strategies = [2, 2, 2, 2]
        #doesn't matter how they are betting since we are learning. Just bet heuristic
        model_vector = ['heuristic', 'heuristic', 'heuristic', 'heuristic']
        #model_vector = ['model','model','model','model']
        #betting_model_objects = [keras.models.load_model('./Models/Greedy_v_Greedy_bet_data' + datatype + '.h5', custom_objects={'get_loss_bet': get_loss_bet, 'loss_bet': loss_bet}) for i in range(4)]

    elif strategy_var == 'Greedy_v_Heuristic':
        nameString = './Data/Greedy_v_Heuristic.csv'
        gameTypeString = 'Greedy_v_Heuristic'
        strategies = [2, 3, 2, 3]
        model_vector = ['heuristic', 'heuristic', 'heuristic', 'heuristic']
        model_vector = ['heuristic', 'heuristic', 'heuristic', 'heuristic']
        players_to_learn_from = [0, 2]
        #model_vector = ['heuristic','heuristic','heuristic','heuristic']

    elif strategy_var == 'Heuristic_v_Heuristic':
        nameString = './Data/Heuristic_v_Heuristic.csv'
        gameTypeString = 'Heuristic_v_Heuristic'
        strategies = [3, 3, 3, 3]
        model_vector = ['heuristic', 'heuristic', 'heuristic', 'heuristic']
        #betting_model_objects = [keras.models.load_model('./Models/Heuristic_v_Heuristic_bet_' + datatype + '.h5', custom_objects={'get_loss_bet': get_loss_bet, 'loss_bet': loss_bet}) for i in range(4)]

    elif strategy_var == 'Heuristic_v_Greedy':
        nameString = './Data/Heuristic_v_Greedy.csv'
        gameTypeString = 'Heuristic_v_Greedy'
        strategies = [3, 2, 3, 2]
        players_to_learn_from = [0, 2]
        model_vector = ['heuristic', 'heuristic', 'heuristic', 'heuristic']

    x_size = 26

    if organization == 'binary':
        nameString = './Data/' + gameTypeString + '_bet_data_' + organization + '.csv'
        x_size = 52
    elif organization == 'interleave':
        nameString = './Data/' + gameTypeString + '_bet_data_' + organization + '.csv'
    elif organization == 'sorted':
        nameString = './Data/' + gameTypeString + '_bet_data_' + organization + '.csv'
    elif organization == 'interleave_sorted':
        nameString = './Data/' + gameTypeString + '_bet_data_' + organization + '.csv'
    elif organization == 'matrix':
        x_size = 52
        nameString = './Data/' + gameTypeString + '_bet_data_' + organization + '.csv'
    y_size = 1

    models = []
    num_learn_from = len(players_to_learn_from)
    #print num_learn_from
    for batch in range(num_batches):
        dataarray = np.zeros(
            (num_learn_from * batch_size, x_size + y_size)).astype(int)
        for t in range(batch_size):
            #print progress
            if t % (batch_size / 10) == 1:
                print '{}0 percent of batch complete: batch size is {} and we are on batch {} of {}'.format(
                    t, batch_size, batch, num_batches)
            #generate a new game
            game = Game(13, strategies, model_vector, betting_model_objects)
            scores = game.playGame()
            #print players_to_learn_from
            for i in range(len(players_to_learn_from)):
                p = players_to_learn_from[i]
                #print(p)
                #scount tricks taken and get suits and card vals
                tricks = sum(game.T[t] == p for t in range(13))
                hand = game.initialHands[p]
                vals = np.zeros(13).astype(int)
                suits = np.zeros(13).astype(int)
                x_binary = np.zeros(52).astype(int)
                vals_sorted = np.zeros(13).astype(int)
                suits_sorted = np.zeros(13).astype(int)
                for c in range(13):
                    card = hand.cards[c]
                    value = card.value
                    suit = card.suit
                    vals[c] = value
                    suits[c] = suit
                    x_binary[value + 4 * suit] = 1
                hand.sort()
                for c in range(13):
                    card = hand.cards[c]
                    value = card.value
                    suit = card.suit
                    vals_sorted[c] = value
                    suits_sorted[c] = suit
                #add x and y data to array
                #x_obs = vals + suits
                x_obs = np.concatenate([vals, suits])
                #x_sorted = vals_sorted + suits_sorted
                x_sorted = np.concatenate([vals_sorted, suits_sorted])
                x_interleave = np.array([
                    val for pair in zip(vals, suits) for val in pair
                ]).astype(int)
                x_interleave_sorted = np.array([
                    val for pair in zip(vals_sorted, suits_sorted)
                    for val in pair
                ]).astype(int)
                x_matrix = hand.get_cards_matrix_order()
                if organization == 'interleave sorted':
                    x_obs = x_interleave_sorted
                elif organization == 'interleave':
                    x_obs = x_interleave
                elif organization == 'binary':
                    x_obs = x_binary.astype(int)
                elif organization == 'sorted':
                    x_obs = x_sorted
                elif organization == 'matrix':
                    x_obs = x_matrix.flatten()
                y_obs = tricks
                dataarray[num_learn_from * t + i, 0:x_size] = x_obs
                dataarray[num_learn_from * t + i,
                          x_size:x_size + y_size] = y_obs
        #now ready to append current array
        with open(nameString, "a") as output:
            np.savetxt(output, dataarray, delimiter=',', fmt='%i')
Example #3
0
genetic_parameters['bet_params'] = np.random.rand(52)
genetic_parameters['state_params'] = {}
genetic_parameters['prob_param']=1
genetic_parameters['action_params'] = {}
genetic_parameters['urgency_param'] = .5
genetic_param_list = [genetic_parameters for i in range(4)]


#games = [Game(13, strategies) for i in range(num_tests)]
for i in range(num_tests):
    #print i
    #if i% 10000 ==1:
     #   print i
    game = Game(13, strategies, model_vector, genetic_parameter_list=genetic_param_list)
    #game = games[i]
    scores = game.playGame()
    tricks = game.getTricks()
    #print game.initialbets
    #print tricks
    #print scores
    #print(scores)
    odd_score = scores[1]+scores[3]
    even_score = scores[0]+scores[2]
    odd_tricks = tricks[1] + tricks[3]
    even_tricks = tricks[0] + tricks[2]
    total_score_even += even_score
    total_score_odd += odd_score
    total_tricks_even += even_tricks
    total_tricks_odd += odd_tricks

    if even_score < odd_score:
Example #4
0
def test_game(test_type):
    if test_type == 'hvg':
        strategies = [2,3,2,3]
        bet_models = [hvh, nn_bet_model,hvh,nn_bet_model]
        bet_strategies = ['model', 'model', 'model', 'model']
        action_models = [None,None,None,None]
        num_iters =1
        
    elif test_type == 'hvr':
        strategies = [1,3,1,3]
        bet_models = [gvg, hvh,gvg,hvh]
        bet_strategies = ['none', 'model', 'none', 'model']
        action_models = [None,None,None,None]
        num_iters = 1
        
    elif test_type == 'gvr':
        strategies = [1,2,1,2]
        bet_models = [gvg, gvh,gvh,gvg]
        bet_strategies = ['none', 'heuristic', 'none', 'heuristic']
        action_models = [None,None,None,None]
        num_iters = 1
        
    elif test_type == 'nnvh':
        strategies = [4,3,4,3]
        bet_strategies = ['model', 'model', 'model', 'model']
        action_models = [nn_action_model, None, nn_action_model, None]
        bet_models = [nn_bet_model, hvh, nn_bet_model, hvh]
        num_iters = 1

    elif test_type == 'hvnn':
        strategies = [3, 4, 3, 4]
        bet_strategies = ['model', 'model', 'model', 'model']
        action_models = [None, nn_action_model,None,  nn_action_model]
        bet_models = [hvh, nn_bet_model, hvh, nn_bet_model]
        num_iters = 1

    elif test_type == 'gvnn':
        strategies = [2,4,2,4]
        bet_strategies = ['model', 'model', 'model', 'model']
        action_models = [None, nn_action_model, None, nn_action_model]
        bet_models = [gvg, nn_bet_model,gvg,nn_bet_model]
        num_iters = 1
    elif test_type == 'nnvg':
        strategies = [4,2,4,2]
        bet_strategies = ['model', 'model', 'model', 'model']
        action_models = [ nn_action_model, None, nn_action_model,None]
        bet_models = [ nn_bet_model,gvg,nn_bet_model,gvg]
        num_iters = 1

    elif test_type == 'nnvr':
        strategies = [1,4,1,4]
        bet_strategies = ['none', 'model', 'none', 'model']
        action_models = [None, nn_action_model, None, nn_action_model]
        bet_models = [gvg, nn_bet_model,gvg,nn_bet_model]
        num_iters = 1

    n=13
    wins_team1 = [0 for i in range(num_iters)]
    wins_team2 = [0 for i in range(num_iters)]
    score_team1 = [0 for i in range(num_iters)]
    score_team2 = [0 for i in range(num_iters)]

    num_iters = 4


    for i in range(1):
        for g in range(num_games):
            Total_Scores = [0 for p in range(4)]

            while True:
                game = Game(n, strategies, bet_strategies, n, [None, None, None, None], action_models, bet_models)
                scores = game.playGame()

                for p in range(4):
                    Total_Scores[p] += scores[p]
                score_team1[i] = score_team1[i] + Total_Scores[0] + Total_Scores[2]
                score_team2[i] = score_team2[i] + Total_Scores[1] + Total_Scores[3]
                if (Total_Scores[0] >= 41 and Total_Scores[2] >= 0) or (Total_Scores[0] >= 0 and Total_Scores[2] >= 41):
                       wins_team1[i] += 1
                       break
                if (Total_Scores[1] >= 41 and Total_Scores[3] >= 0) or (Total_Scores[1] >= 0 and Total_Scores[3] >= 41):
                       wins_team2[i] += 1
                       break
        print 'Team 1 won' + str(wins_team1[i])
        print 'Team 2 won' + str(wins_team2[i])

    return (wins_team1, wins_team2)