def main():
    """
    fill the player_prediction db table
    """
    parser = argparse.ArgumentParser(description="fill player predictions")
    parser.add_argument("--weeks_ahead",
                        help="how many weeks ahead to fill",
                        type=int)
    parser.add_argument("--gameweek_start",
                        help="first gameweek to look at",
                        type=int)
    parser.add_argument("--gameweek_end",
                        help="last gameweek to look at",
                        type=int)
    parser.add_argument("--ep_filename",
                        help="csv filename for FPL expected points")
    parser.add_argument("--season",
                        help="season, in format e.g. '1819'",
                        default=CURRENT_SEASON)
    parser.add_argument("--num_thread",
                        help="number of threads to parallelise over",
                        type=int)
    parser.add_argument(
        "--no_bonus",
        help="don't include bonus points",
        action="store_true",
    )
    parser.add_argument(
        "--no_cards",
        help="don't include points lost to yellow and red cards",
        action="store_true",
    )
    parser.add_argument(
        "--no_saves",
        help="don't include save points for goalkeepers",
        action="store_true",
    )
    parser.add_argument(
        "--sampling",
        help="If set use fit the model using sampling with numpyro",
        action="store_true",
    )

    args = parser.parse_args()
    if args.weeks_ahead and (args.gameweek_start or args.gameweek_end):
        print(
            "Please specify either gameweek_start and gameweek_end, OR weeks_ahead"
        )
        raise RuntimeError("Inconsistent arguments")
    if args.weeks_ahead and args.season != CURRENT_SEASON:
        print(
            "For past seasons, please specify gameweek_start and gameweek_end")
        raise RuntimeError("Inconsistent arguments")
    if args.weeks_ahead:
        gw_range = get_gameweeks_array(args.weeks_ahead)
    elif args.gameweek_start and args.gameweek_end:
        gw_range = list(range(args.gameweek_start, args.gameweek_end))
    elif args.gameweek_start:  # by default go three weeks ahead
        gw_range = list(range(args.gameweek_start, args.gameweek_start + 3))
    else:
        gw_range = list(range(NEXT_GAMEWEEK, NEXT_GAMEWEEK + 3))
    num_thread = args.num_thread or None
    include_bonus = not args.no_bonus
    include_cards = not args.no_cards
    include_saves = not args.no_saves
    if args.sampling:
        player_model = NumpyroPlayerModel()
    else:
        player_model = ConjugatePlayerModel()

    set_multiprocessing_start_method(num_thread)

    with session_scope() as session:
        session.expire_on_commit = False

        tag = make_predictedscore_table(
            gw_range=gw_range,
            season=args.season,
            num_thread=num_thread,
            include_bonus=include_bonus,
            include_cards=include_cards,
            include_saves=include_saves,
            player_model=player_model,
            dbsession=session,
        )

        # print players with top predicted points
        get_top_predicted_points(
            gameweek=gw_range,
            tag=tag,
            season=args.season,
            per_position=True,
            n_players=5,
            dbsession=session,
        )
示例#2
0
def main():
    """
    The main function, to be used as entrypoint.
    """
    parser = argparse.ArgumentParser(
        description="Try some different transfer strategies")
    parser.add_argument("--weeks_ahead", help="how many weeks ahead", type=int)
    parser.add_argument("--gw_start",
                        help="first gameweek to consider",
                        type=int)
    parser.add_argument("--gw_end", help="last gameweek to consider", type=int)
    parser.add_argument("--tag",
                        help="specify a string identifying prediction set")
    parser.add_argument(
        "--wildcard_week",
        help="play wildcard in the specified week. Choose 0 for 'any week'.",
        type=int,
        default=-1,
    )
    parser.add_argument(
        "--free_hit_week",
        help="play free hit in the specified week. Choose 0 for 'any week'.",
        type=int,
        default=-1,
    )
    parser.add_argument(
        "--triple_captain_week",
        help=
        "play triple captain in the specified week. Choose 0 for 'any week'.",
        type=int,
        default=-1,
    )
    parser.add_argument(
        "--bench_boost_week",
        help="play bench_boost in the specified week. Choose 0 for 'any week'.",
        type=int,
        default=-1,
    )
    parser.add_argument("--num_free_transfers",
                        help="how many free transfers do we have",
                        type=int)
    parser.add_argument(
        "--max_hit",
        help="maximum number of points to spend on additional transfers",
        type=int,
        default=8,
    )
    parser.add_argument(
        "--allow_unused",
        help="if set, include strategies that waste free transfers",
        action="store_true",
    )
    parser.add_argument(
        "--num_iterations",
        help="how many iterations to use for Wildcard/Free Hit optimization",
        type=int,
        default=100,
    )
    parser.add_argument("--num_thread",
                        help="how many threads to use",
                        type=int,
                        default=4)
    parser.add_argument(
        "--season",
        help="what season, in format e.g. '2021'",
        type=str,
        default=CURRENT_SEASON,
    )
    parser.add_argument(
        "--profile",
        help="For developers: Profile strategy execution time",
        action="store_true",
    )
    parser.add_argument(
        "--fpl_team_id",
        help="specify fpl team id",
        type=int,
        required=False,
    )
    args = parser.parse_args()

    fpl_team_id = args.fpl_team_id or None

    sanity_check_args(args)
    season = args.season
    # default weeks ahead is not specified (or gw_end is not specified) is three
    if args.weeks_ahead:
        gameweeks = get_gameweeks_array(args.weeks_ahead)
    elif args.gw_start:
        if args.gw_end:
            gameweeks = list(range(args.gw_start, args.gw_end))
        else:
            gameweeks = list(range(args.gw_start, args.gw_start + 3))
    else:
        gameweeks = list(range(get_next_gameweek(), get_next_gameweek() + 3))

    num_iterations = args.num_iterations
    if args.num_free_transfers:
        num_free_transfers = args.num_free_transfers
    else:
        num_free_transfers = None  # will work it out in run_optimization
    tag = args.tag or get_latest_prediction_tag(season=season)
    max_total_hit = args.max_hit
    allow_unused_transfers = args.allow_unused
    num_thread = args.num_thread
    profile = args.profile or False
    chip_gameweeks = {
        "wildcard": args.wildcard_week,
        "free_hit": args.free_hit_week,
        "triple_captain": args.triple_captain_week,
        "bench_boost": args.bench_boost_week,
    }

    if not check_tag_valid(tag, gameweeks, season=season):
        print(
            "ERROR: Database does not contain predictions",
            "for all the specified optimsation gameweeks.\n",
            "Please run 'airsenal_run_prediction' first with the",
            "same input gameweeks and season you specified here.",
        )
        sys.exit(1)

    set_multiprocessing_start_method(num_thread)

    with warnings.catch_warnings():
        warnings.simplefilter("ignore", TqdmWarning)
        run_optimization(
            gameweeks,
            tag,
            season,
            fpl_team_id,
            chip_gameweeks,
            num_free_transfers,
            max_total_hit,
            allow_unused_transfers,
            2,
            num_iterations,
            num_thread,
            profile,
        )
示例#3
0
def run_pipeline(
    num_thread,
    weeks_ahead,
    fpl_team_id,
    clean,
    apply_transfers,
    wildcard_week,
    free_hit_week,
    triple_captain_week,
    bench_boost_week,
):
    """
    Run the full pipeline, from setting up the database and filling
    with players, teams, fixtures, and results (if it didn't already exist),
    then updating with the latest info, then running predictions to get a
    score estimate for every player, and finally optimization, to choose
    the best squad.
    """
    if fpl_team_id is None:
        fpl_team_id = fetcher.FPL_TEAM_ID
    print("Running for FPL Team ID {}".format(fpl_team_id))
    if not num_thread:
        num_thread = multiprocessing.cpu_count()
    set_multiprocessing_start_method(num_thread)

    with session_scope() as dbsession:
        if check_clean_db(clean, dbsession):
            click.echo("Setting up Database..")
            setup_ok = setup_database(fpl_team_id, dbsession)
            if not setup_ok:
                raise RuntimeError("Problem setting up initial db")
            click.echo("Database setup complete..")
            update_attr = False
        else:
            click.echo("Found pre-existing AIrsenal database.")
            update_attr = True
        click.echo("Updating database..")
        update_ok = update_database(fpl_team_id, update_attr, dbsession)
        if not update_ok:
            raise RuntimeError("Problem updating db")
        click.echo("Database update complete..")
        click.echo("Running prediction..")
        predict_ok = run_prediction(num_thread, weeks_ahead, dbsession)
        if not predict_ok:
            raise RuntimeError("Problem running prediction")
        click.echo("Prediction complete..")
        if NEXT_GAMEWEEK == 1:
            click.echo("Generating a squad..")
            new_squad_ok = run_make_squad(weeks_ahead, fpl_team_id, dbsession)
            if not new_squad_ok:
                raise RuntimeError("Problem creating a new squad")
        else:
            click.echo("Running optimization..")
            chips_played = setup_chips(wildcard_week, free_hit_week,
                                       triple_captain_week, bench_boost_week)
            opt_ok = run_optimize_squad(num_thread, weeks_ahead, fpl_team_id,
                                        dbsession, chips_played)
            if not opt_ok:
                raise RuntimeError("Problem running optimization")

        click.echo("Optimization complete..")
        if apply_transfers:
            click.echo("Applying suggested transfers...")
            transfers_ok = make_transfers(fpl_team_id)
            if not transfers_ok:
                raise RuntimeError("Problem applying the transfers")
            click.echo("Setting Lineup...")
            lineup_ok = set_starting_11(fpl_team_id)
            if not lineup_ok:
                raise RuntimeError("Problem setting the lineup")
        click.echo("Pipeline finished OK!")