コード例 #1
0
def return_fixture_names_shortnames():
    df = create_data_frame()
    static, fixture = read_data.get_json()
    names = pd.DataFrame(static['teams'])['name']
    short_names = pd.DataFrame(static['teams'])['short_name']
    ids = pd.DataFrame(static['teams'])['id']
    return df, names, short_names, ids
コード例 #2
0
def create_data_frame():
    """
    create dataframe with fixture information.
    Rows: Team_id - 1
    Columns: Team (team name) 1 (GW number) 2 3 4 5
    :return:             Team            1            2   ...           38
            0          Arsenal  [FUL, A, 2]  [WHU, H, 2]  ...  [BHA, H, 2]
            1      Aston Villa  [MCI, A, 0]  [SHU, H, 3]  ...  [CHE, H, 3]

    """
    static, fixture = read_data.get_json()

    team_list = create_team_list()
    number_of_PL_teams = len(team_list)
    columns = [str(i) for i in range(0, len(team_list[0].fixtures_df.index) + 1)]
    columns[0] = 'Team'
    data = []
    temp_team = []
    dict_team_id_to_name = create_dict_with_team_ids_to_team_name(static['teams'], name_or_short_name="short_name")
    for team in range(number_of_PL_teams):
        temp_team.append(team_list[team].team)
    for gameweek in range(number_of_PL_teams):
        team_info = team_list[gameweek]
        temp_data = [team_info.team]
        for team_idx in range(len(columns) - 1):
            index_opp = team_info.fixtures_df['opponent_team'].index[team_idx]
            index_ah = team_info.fixtures_df['H/A'].index[team_idx]
            index_diff = team_info.fixtures_df['difficulty'].index[team_idx]
            opp = team_info.fixtures_df['opponent_team'][index_opp]
            ah = team_info.fixtures_df['H/A'][index_ah]
            diff = team_info.fixtures_df['difficulty'][index_diff]

            temp_data.append([dict_team_id_to_name[opp], ah, diff])
        data.append(temp_data)
    return pd.DataFrame(data=data, columns=columns)
コード例 #3
0
def create_team_list():
    static, fixture = read_data.get_json()
    # a = DataFetch()
    # b = a.get_current_fpl_info()
    b = static
    player_info = b['elements']
    # fixture_info = a.get_current_fixtures()
    fixture_info = fixture
    players = Players(player_info, fixture_info)
    team_list = players.create_all_teams()  # list teams, where Arsenal = team_list[0], ... Wolves = team_list[-1]
    dict_team_id_to_team_name = create_dict_with_team_ids_to_team_name(static['teams'], "name")
    for team_id in range(len(team_list)):
        team_list[team_id].team = dict_team_id_to_team_name[team_id + 1]
    # the list index is also the same as the team id minus 1. Arsenal = id=1 - 1, ...
    return team_list
コード例 #4
0
def find_best_rotation_combos2(data, GW_start, GW_end, teams_to_check=5, teams_to_play=3, team_names=[-1],
                              teams_in_solution=[], teams_not_in_solution=[],
                              top_teams_adjustment=False, one_double_up=False, home_away_adjustment=True,
                              include_extra_good_games=False, num_to_print=20):
    """
    Find the best rotation combo for "teams_to_check" number of team where "team_to_play" number of them must play each gameweek.
    :param GW_start: start count from this gameweek
    :param GW_end: end counting in this gameweek
    :param teams_to_check: how many teams to check (1-5)
    :param teams_to_play: how many of the teams_to_check you want to play in each gameweek. ( team_to_play < teams_to_check)
    teams_to_check = 5 and teams_to_play = 3 will give the best 5 teams to use in your team if you must use at least 3 each round
    :param team_names: which teams to check from. ([-1]: all teams, ["Arsenal", "Spurs"]: check only some teams)
    :param teams_in_solution: ["Liverpool"]: liverpool must be in the optimal solution. []: no extra dependencies.
    :param home_away_adjustment: wheter to give home/away match score -/+ 0.1 points. Default=true
    :param num_to_print: how many of the best results to print to screen
    :return: combos_with_score [[score, [team_ids], [team_names]], ... ]   ([22.2, [1, 4, 11], ['Arsenal', 'Burnley', 'Liverpool']])
    """

    print("Teams to check: ", teams_to_check)
    print("Teams to play: ", teams_to_play)
    print("Double up from one team: ", one_double_up)
    if team_names[0] != -1:
        if teams_to_check > len(team_names):
            print("Teams to check must be >= to number of input teams")
            return -1
    if teams_to_play > teams_to_check:
        print("Teams to play must be smaller than teams_to_check.")
        return -1
    if len(teams_in_solution) > teams_to_check:
        print("Teams_in_solution must be smaller than teams_to_play.")
        return -1
    if one_double_up:
        if teams_to_check < 2:
            print("Teams to check must be >= 1")
            return -1

    # create fixture dataframe. Each element: ['ARS', 'H', 3]
    # df = create_data_frame()
    df, names, short_names, ids = return_fixture_names_shortnames()


    # adjust the fixture difficulty
    if home_away_adjustment > 0:
        l = 0
        #df = adjust_df_for_home_away(df, home_advantage=home_away_adjustment)

    if top_teams_adjustment:
        l = 0
        #df = adjust_df_for_difficult_teams(df)

    static, fixture = read_data.get_json()
    dict_with_team_name_to_team_ids = create_dict_with_team_name_to_team_ids(static['teams'])

    team_ids = []
    for team_name in team_names:
        if team_name == -1:
            number_of_teams = df.shape[0]
            team_ids = np.arange(1, number_of_teams + 1)
            break
        team_id = dict_with_team_name_to_team_ids[team_name]
        team_ids.append(team_id)

    number_of_GW = GW_end - GW_start + 1

    dict_team_id_to_fixtures = {}
    dict_team_id_to_home_away = {}
    dict_team_id_to_opponent = {}


    for idx, team_id in enumerate(team_ids):
        info = fixture_score_one_team(df, team_id, GW_start, GW_end)[3]
        info[info == 0] = 10
        dict_team_id_to_fixtures[team_id] = info
        dict_team_id_to_home_away[team_id] = fixture_score_one_team(df, team_id, GW_start, GW_end)[8]
        dict_team_id_to_opponent[team_id] = fixture_score_one_team(df, team_id, GW_start, GW_end)[2]




    dict_with_team_ids_to_team_name = create_dict_with_team_ids_to_team_name(static['teams'])

    dict_with_team_ids_to_team_short_name = create_dict_with_team_ids_to_team_name(static['teams'], "short_name")


    if len(teams_in_solution) > 0:
        print("This/these team(s) must be in the solution: ", teams_in_solution, "\n")
    if len(teams_not_in_solution) > 0:
        print("This/these team(s) can not be in the solution: ", teams_not_in_solution, "\n")

    ids_must_be_in_solution = create_list_with_team_ids_from_list_with_team_names(static['teams'], teams_in_solution)
    ids_must_not_be_in_solution = create_list_with_team_ids_from_list_with_team_names(static['teams'], teams_not_in_solution)

    if one_double_up:
        # allow combinations with one double up from one team
        unique_team_ids = [[*comb] for comb in combinations(team_ids, teams_to_check - 1) if
                           all(elem in [*comb] for elem in ids_must_be_in_solution)]
        temp_unique_team_ids = []
        for unique_team_id in unique_team_ids:
            for id in team_ids:
                temp_unique_team_ids.append(unique_team_id + [id])
        unique_team_ids = temp_unique_team_ids

    if not one_double_up:
        # create unique team combos
        unique_team_ids = [[*comb] for comb in combinations(team_ids, teams_to_check) if
                           all(elem in [*comb] for elem in ids_must_be_in_solution)]

    # remove all combinations where teams from teams_not_in_solution exists
    temp_unique_team_ids = []
    for comb in unique_team_ids:
        if len([i for i in comb if i in ids_must_not_be_in_solution]) == 0:
            temp_unique_team_ids.append(comb)
    unique_team_ids = temp_unique_team_ids


    combos_with_score = []
    combos_with_score_new = []
    for team_combos in unique_team_ids:
        # team_combos = [1, 3]
        team_total_score = 0
        team_total_score_new = 0
        extra_fixtures = 0
        home_games = 0
        #two_D_lis = two_D_list(len(team_combos), number_of_GW)
        two_D_list_new = two_D_list(len(team_combos), number_of_GW)
        for GW_idx, GW in enumerate(range(number_of_GW)):
            #GW_home_scores = []
            GW_home_scores_new = []
            #GW_scores = []
            GW_scores_new = []
            for team_idx, team_id in enumerate(team_combos):
                # team_id = 6
                #FDR = dict_team_id_to_fixtures[team_id][GW]
                #H_A = dict_team_id_to_home_away[team_id][GW]
                #Opponent = dict_team_id_to_opponent[team_id][GW]
                team_name = dict_with_team_ids_to_team_name[team_id]
                #GW_home_scores.append([FDR, H_A])
                #GW_scores.append(FDR)
                #team_object = FDR_team(team_name, Opponent.upper(),
                #                       FDR, H_A, 0)
                #two_D_lis[team_idx][GW_idx] = team_object
                temp_team_data_dict = create_FDR_dict(data[int(team_id - 1)], 10)
                data_gw = temp_team_data_dict[GW + GW_start]
                gws_this_round = len(data_gw)
                temp_score = 0
                team_object_new = []
                if gws_this_round > 1:
                    for i in range(gws_this_round):
                        temp_score += data_gw[i][2]
                        team_object_new.append(FDR_team(team_name, data_gw[i][0].upper(),
                                                        data_gw[i][2], data_gw[i][1], 0))
                else:
                    temp_score += data_gw[0][2]
                    team_object_new.append(FDR_team(team_name, data_gw[0][0].upper(),
                                           data_gw[0][2], data_gw[0][1], 0))
                temp_score = temp_score / gws_this_round ** 2
                GW_scores_new.append(temp_score)
                two_D_list_new[team_idx][GW_idx] = team_object_new
                H_A = 1
                # fix this for later feature regarding h_a advantage
                GW_home_scores_new.append([temp_score, H_A])

            #Use_Not_Use_idx = np.array(GW_scores).argsort()[:teams_to_play]
            #for k in Use_Not_Use_idx:
            #    two_D_lis[k][GW_idx].Use_Not_Use = 1

            Use_Not_Use_idx = np.array(GW_scores_new).argsort()[:teams_to_play]
            for k in Use_Not_Use_idx:
                two_D_list_new[k][GW_idx][0].Use_Not_Use = 1



            #print("Argsort: ", np.array(GW_scores).argsort()[:teams_to_play], GW_scores)

            # team_total_score += np.sum(sorted(GW_scores, key=float)[:teams_to_play])
            #sorted_scores = np.array(sorted(GW_home_scores, key=lambda l: l[0], reverse=False))
            #home_games += np.sum(sorted_scores[:teams_to_play, 1])
            #team_total_score += np.sum(sorted_scores[:teams_to_play, 0])

            sorted_scores_new = np.array(sorted(GW_home_scores_new, key=lambda l: l[0], reverse=False))
            team_total_score_new += np.sum(sorted_scores_new[:teams_to_play, 0])

            #print(team_combos, team_total_score, home_games, GW_home_scores, teams_to_play)
            # if there are more good games than
            if include_extra_good_games:
                if teams_to_play < len(team_combos):
                    extra_scores = sorted_scores_new[teams_to_play:, 0]
                    for extra_score in extra_scores:
                        if extra_score <= 2.9:
                            team_total_score -= 0.1
                            extra_fixtures += 1

        combo_names = []
        for team_id in team_combos:
            combo_names.append(dict_with_team_ids_to_team_name[team_id])

       # combos_with_score.append(
        #    [round(team_total_score / number_of_GW / teams_to_play, 4), team_combos, combo_names, extra_fixtures,
        #     home_games, two_D_lis])

        combos_with_score_new.append(
            [round(team_total_score_new / number_of_GW / teams_to_play, 4), team_combos, combo_names, extra_fixtures,
             home_games, two_D_list_new])
        #print("HEHEH", combo_names, team_total_score)

    # sort all the combos by the team_total_score. Best fixture will be first element and so on.
    #insertion_sort(combos_with_score, len(combos_with_score), element_to_sort=0, min_max="min")
    insertion_sort(combos_with_score_new, len(combos_with_score_new), element_to_sort=0, min_max="min")

    #print("\n\nRank \t Avg difficulty/game \t\t Teams")
    #for idx, i in enumerate(combos_with_score[:num_to_print]):
    #    print(str(idx + 1) + "\t\t" + str(i[0]) + "\t\t" + str(', '.join(i[2])))
    #print("\n")
    #print(combos_with_score[0])
    #for GW in range(number_of_GW):
    #    for TEAM in range(teams_to_check):
    #        ob = combos_with_score[0][-1][TEAM][GW]
    #        print("GW: ", GW_start + GW, ob.team_name, ob.FDR, ob.opponent_team_name_short, ob.Use_Not_Use)
    return combos_with_score_new
コード例 #5
0
def find_best_rotation_combos(GW_start, GW_end, teams_to_check=5, teams_to_play=3, team_names=[-1],
                              teams_in_solution=[], teams_not_in_solution=[],
                              top_teams_adjustment=False, one_double_up=False, home_away_adjustment=True,
                              include_extra_good_games=False, num_to_print=20):
    """
    Find the best rotation combo for "teams_to_check" number of team where "team_to_play" number of them must play each gameweek.
    :param GW_start: start count from this gameweek
    :param GW_end: end counting in this gameweek
    :param teams_to_check: how many teams to check (1-5)
    :param teams_to_play: how many of the teams_to_check you want to play in each gameweek. ( team_to_play < teams_to_check)
    teams_to_check = 5 and teams_to_play = 3 will give the best 5 teams to use in your team if you must use at least 3 each round
    :param team_names: which teams to check from. ([-1]: all teams, ["Arsenal", "Spurs"]: check only some teams)
    :param teams_in_solution: ["Liverpool"]: liverpool must be in the optimal solution. []: no extra dependencies.
    :param home_away_adjustment: wheter to give home/away match score -/+ 0.1 points. Default=true
    :param num_to_print: how many of the best results to print to screen
    :return: combos_with_score [[score, [team_ids], [team_names]], ... ]   ([22.2, [1, 4, 11], ['Arsenal', 'Burnley', 'Liverpool']])
    """
    print("Teams to check: ", teams_to_check)
    print("Teams to play: ", teams_to_play)
    print("Double up from one team: ", one_double_up)
    if team_names[0] != -1:
        if teams_to_check > len(team_names):
            print("Teams to check must be >= to number of input teams")
            return -1
    if teams_to_play > teams_to_check:
        print("Teams to play must be smaller than teams_to_check.")
        return -1
    if len(teams_in_solution) > teams_to_check:
        print("Teams_in_solution must be smaller than teams_to_play.")
        return -1
    if one_double_up:
        if teams_to_check < 2:
            print("Teams to check must be >= 1")
            return -1

    # create fixture dataframe. Each element: ['ARS', 'H', 3]
    df = create_data_frame()
    # adjust the fixture difficulty
    if home_away_adjustment > 0:
        l = 0
        #df = adjust_df_for_home_away(df, home_advantage=home_away_adjustment)

    if top_teams_adjustment:
        l = 0
        #df = adjust_df_for_difficult_teams(df)

    static, fixture = read_data.get_json()
    dict_with_team_name_to_team_ids = create_dict_with_team_name_to_team_ids(static['teams'])

    team_ids = []
    for team_name in team_names:
        if team_name == -1:
            number_of_teams = df.shape[0]
            team_ids = np.arange(1, number_of_teams + 1)
            break

        team_id = dict_with_team_name_to_team_ids(team_name)
        team_ids.append(team_id)

    number_of_GW = GW_end - GW_start + 1

    dict_team_id_to_fixtures = {}
    dict_team_id_to_home_away = {}
    for idx, team_id in enumerate(team_ids):
        info = fixture_score_one_team(df, team_id, GW_start, GW_end)[3]
        info[info == 0] = 10
        dict_team_id_to_fixtures[team_id] = info
        dict_team_id_to_home_away[team_id] = fixture_score_one_team(df, team_id, GW_start, GW_end)[8]


    dict_with_team_ids_to_team_name = create_dict_with_team_ids_to_team_name(static['teams'])


    if len(teams_in_solution) > 0:
        print("This/these team(s) must be in the solution: ", teams_in_solution, "\n")
    if len(teams_not_in_solution) > 0:
        print("This/these team(s) can not be in the solution: ", teams_not_in_solution, "\n")

    ids_must_be_in_solution = create_list_with_team_ids_from_list_with_team_names(static['teams'], teams_in_solution)
    ids_must_not_be_in_solution = create_list_with_team_ids_from_list_with_team_names(static['teams'], teams_not_in_solution)

    #if one_double_up:
        # allow combinations with one double up from one team
    #    unique_team_ids = [[*comb] for comb in combinations(team_ids, teams_to_check - 1) if
    #                       all(elem in [*comb] for elem in ids_must_be_in_solution)]
    #    temp_unique_team_ids = []
    #    for unique_team_id in unique_team_ids:
    #        for id in team_ids:
    #            temp_unique_team_ids.append(unique_team_id + [id])
    #    unique_team_ids = temp_unique_team_ids

    if not one_double_up:
        # create unique team combos
        unique_team_ids = [[*comb] for comb in combinations(team_ids, teams_to_check) if
                           all(elem in [*comb] for elem in ids_must_be_in_solution)]

    # remove all combinations where teams from teams_not_in_solution exists
    temp_unique_team_ids = []
    for comb in unique_team_ids:
        if len([i for i in comb if i in ids_must_not_be_in_solution]) == 0:
            temp_unique_team_ids.append(comb)
    unique_team_ids = temp_unique_team_ids

    combos_with_score = []
    for team_combos in unique_team_ids:
        team_total_score = 0
        extra_fixtures = 0
        home_games = 0
        for GW in range(number_of_GW):
            GW_home_scores = []
            for team_id in team_combos:
                GW_home_scores.append([dict_team_id_to_fixtures[team_id][GW], dict_team_id_to_home_away[team_id][GW]])

            # team_total_score += np.sum(sorted(GW_scores, key=float)[:teams_to_play])

            sorted_scores = np.array(sorted(GW_home_scores, key=lambda l: l[0], reverse=False))
            home_games += np.sum(sorted_scores[:teams_to_play, 1])
            team_total_score += np.sum(sorted_scores[:teams_to_play, 0])

            # if there are more good games than
            if include_extra_good_games:
                if teams_to_play < len(team_combos):
                    extra_scores = sorted_scores[teams_to_play:, 0]
                    for extra_score in extra_scores:
                        if extra_score <= 2.9:
                            team_total_score -= 0.1
                            extra_fixtures += 1

        combo_names = []
        for team_id in team_combos:
            combo_names.append(dict_with_team_ids_to_team_name[team_id])
        combos_with_score.append(
            [round(team_total_score / number_of_GW / teams_to_play, 4), team_combos, combo_names, extra_fixtures,
             home_games])
    # sort all the combos by the team_total_score. Best fixture will be first element and so on.
    insertion_sort(combos_with_score, len(combos_with_score), element_to_sort=0, min_max="min")
    print("\n\nRank \t Avg difficulty/game \t\t Teams")
    for idx, i in enumerate(combos_with_score[:num_to_print]):
        print(str(idx + 1) + "\t\t" + str(i[0]) + "\t\t" + str(', '.join(i[2])))
    print("\n")
    return combos_with_score