Beispiel #1
0
def run(league, remove, args):
    solver = pywraplp.Solver(
        'FD',
        pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING
    )

    all_players = retrieve_players(args, remove)

    flex_args = {}
    if args.no_double_te == _YES:
        flex_args['te_upper'] = 1
    if args.flex_position == 'RB':
        flex_args['rb_min'] = 3
    if args.flex_position == 'WR':
        flex_args['wr_min'] = 4

    cons.POSITIONS['NFL'] = cons.get_nfl_positions(**flex_args)

    variables, solution = run_solver(
        solver,
        all_players,
        args
    )

    if solution == solver.OPTIMAL:
        roster = RosterSelect().roster_gen(args.league)
        if args.source != _DK_AVG:
            roster.projection_source = \
                scrapers.scrape_dict[args.source]['readable']

        for i, player in enumerate(all_players):
            if variables[i].solution_value() == 1:
                roster.add_player(player)

        print('Optimal roster for: {}'.format(league))
        print(roster)
        print()

        return roster
    else:
        print(
            '''
            No solution found for command line query.
            Try adjusting your query by taking away constraints.
            '''
        )
        return None
Beispiel #2
0
def run(league, remove, args):
    solver = pywraplp.Solver('FD',
                             pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)

    all_players = []

    with open(args.salary_file, 'rb') as csvfile:
        csvdata = csv.DictReader(csvfile)

        def generate_player(pos, row):
            return Player(pos,
                          row['Name'],
                          row['Salary'],
                          possible_positions=row['Position'],
                          multi_position=('/' in row['Position']),
                          team=row['teamAbbrev'],
                          matchup=row['GameInfo'],
                          average_score=float(row.get('AvgPointsPerGame', 0)),
                          lock=(args.locked and row['Name'] in args.locked))

        for row in csvdata:
            for pos in row['Position'].split('/'):
                all_players.append(generate_player(pos, row))

    if args.w and args.season and args.historical == _YES:
        print('Fetching {} season data for all players...'.format(args.season))
        for p in all_players:
            p.set_historical(int(args.w), int(args.season))

    if args.po_location and args.po:
        with open(args.po_location, 'rb') as csvfile:
            csvdata = csv.DictReader(csvfile)
            for row in csvdata:
                player = filter(lambda p: p.name in row['Name'], all_players)
                if player:
                    player[0].projected_ownership_pct = float(row['%'])

    with open(args.projection_file, 'rb') as csvfile:
        csvdata = csv.DictReader(csvfile)

        # hack for weird defensive formatting
        def name_match(row):
            def match_fn(p):
                if p.pos == 'DST':
                    return p.name.strip() in row['playername']
                return p.name in row['playername']

            return match_fn

        for row in csvdata:
            matching_players = filter(name_match(row), all_players)

            if len(matching_players) == 0:
                continue

            for p in matching_players:
                p.proj = float(row['points'])
                p.marked = 'Y'

    _check_missing_players(all_players, args.sp, args.mp)

    # filter based on criteria and previously optimized
    # do not include DST or TE projections in min point threshold.
    all_players = filter(qc.add_constraints(args, remove), all_players)

    if args.no_double_te == _YES:
        cons.POSITIONS['NFL'] = cons.get_nfl_positions(te_upper=1)

    variables, solution = run_solver(solver, all_players, args)

    if solution == solver.OPTIMAL:
        roster = RosterSelect().roster_gen(args.l)
        roster.projection_source = \
            scrapers.scrape_dict[args.source]['readable']

        for i, player in enumerate(all_players):
            if variables[i].solution_value() == 1:
                roster.add_player(player)

        print "Optimal roster for: %s" % league
        print roster
        print

        return roster
    else:
        print("No solution found for command line query. " +
              "Try adjusting your query by taking away constraints.")
        return None
def run(position_distribution, league, remove, args):
    csv_name = ["test", "test"] if args.test_mode else ["data", "current"]
    solver = pywraplp.Solver("FD", pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)

    all_players = []

    with open(fns.format(*csv_name), "rb") as csvfile:
        csvdata = csv.DictReader(csvfile)

        for row in csvdata:
            player = Player(
                row["Position"],
                row["Name"],
                row["Salary"],
                team=row["teamAbbrev"],
                matchup=row["GameInfo"],
                average_score=float(row.get("AvgPointsPerGame", 0)),
                lock=(args.locked and row["Name"] in args.locked),
            )
            all_players.append(player)

    if args.w and args.season and args.historical == _YES:
        print ("Fetching {} season data for all players...".format(args.season))
        for p in all_players:
            p.set_historical(int(args.w), int(args.season))

    if args.po_location and args.po:
        with open(args.po_location, "rb") as csvfile:
            csvdata = csv.DictReader(csvfile)
            for row in csvdata:
                player = filter(lambda p: p.name in row["Name"], all_players)
                if player:
                    player[0].projected_ownership_pct = float(row["%"])

    with open(fnp.format(*csv_name), "rb") as csvfile:
        csvdata = csv.DictReader(csvfile)
        mass_hold = [["playername", "points", "cost", "ppd"]]

        # hack for weird defensive formatting
        def name_match(row):
            def match_fn(p):
                if p.pos == "DST":
                    return p.name.strip() in row["playername"]
                return p.name in row["playername"]

            return match_fn

        for row in csvdata:
            player = filter(name_match(row), all_players)

            if len(player) == 0:
                continue

            player[0].proj = float(row["points"])
            player[0].marked = "Y"
            listify_holder = [row["playername"], row["points"]]
            if "0.0" not in row["points"] or player[0].cost != 0:
                ppd = float(row["points"]) / float(player[0].cost)
            else:
                ppd = 0
            listify_holder.extend([player[0].cost, ppd * 100000])
            mass_hold.append(listify_holder)

    check = []
    with open(fns.format(*csv_name), "rb") as csvdata:
        for row in csvdata:
            check = row
            break

    with open(fnp.format(*csv_name), "wb") as csvdata:
        if len(check) == 4:
            pass
        else:
            writer = csv.writer(csvdata, lineterminator="\n")
            writer.writerows(mass_hold)

    _check_missing_players(all_players, args.sp, args.mp)

    # filter based on criteria and previously optimized
    # do not include DST or TE projections in min point threshold.
    all_players = filter(qc.add_constraints(args, remove), all_players)

    if args.no_double_te == _YES:
        cons.POSITIONS["NFL"] = cons.get_nfl_positions(te_upper=1)

    variables, solution = run_solver(solver, all_players, position_distribution, args)

    if solution == solver.OPTIMAL:
        roster = RosterSelect().roster_gen(args.l)
        roster.projection_source = scrapers.scrape_dict[args.source]["readable"]

        for i, player in enumerate(all_players):
            if variables[i].solution_value() == 1:
                roster.add_player(player)

        print "Optimal roster for: %s" % league
        print roster
        print

        return roster
    else:
        print ("No solution found for command line query. " + "Try adjusting your query by taking away constraints.")
        return None
Beispiel #4
0
def run(league, remove, args):
    solver = pywraplp.Solver('FD',
                             pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)

    all_players = []

    with open(args.salary_file, 'rb') as csv_file:
        csv_data = csv.DictReader(csv_file)

        def generate_player(pos, row):
            avg = float(row.get('AvgPointsPerGame', 0))
            player = Player(pos,
                            row['Name'],
                            row['Salary'],
                            possible_positions=row['Position'],
                            multi_position=('/' in row['Position']),
                            team=row['teamAbbrev'],
                            matchup=row['GameInfo'],
                            average_score=avg,
                            lock=(args.locked and row['Name'] in args.locked))
            if args.source == _DK_AVG:
                player.proj = avg

            return player

        for row in csv_data:
            for pos in row['Position'].split('/'):
                all_players.append(generate_player(pos, row))

    _set_historical_points(all_players, args)
    _set_player_ownership(all_players, args)

    if args.source != _DK_AVG:
        with open(args.projection_file, 'rb') as csvfile:
            csvdata = csv.DictReader(csvfile)

            # hack for weird defensive formatting
            def name_match(row):
                def match_fn(p):
                    if p.pos == 'DST':
                        return p.name.strip() in row['playername']
                    return p.name in row['playername']

                return match_fn

            for row in csvdata:
                matching_players = filter(name_match(row), all_players)

                if len(matching_players) == 0:
                    continue

                for p in matching_players:
                    p.proj = float(row['points'])
                    p.marked = 'Y'

    _check_missing_players(all_players, args.sp, args.mp)

    # filter based on criteria and previously optimized
    # do not include DST or TE projections in min point threshold.
    all_players = filter(qc.add_constraints(args, remove), all_players)

    flex_args = {}
    if args.no_double_te == _YES:
        flex_args['te_upper'] = 1
    if args.flex_position == 'RB':
        flex_args['rb_min'] = 3
    if args.flex_position == 'WR':
        flex_args['wr_min'] = 4

    cons.POSITIONS['NFL'] = cons.get_nfl_positions(**flex_args)

    variables, solution = run_solver(solver, all_players, args)

    if solution == solver.OPTIMAL:
        roster = RosterSelect().roster_gen(args.l)
        if args.source != _DK_AVG or args.proj:
            roster.projection_source = \
                scrapers.scrape_dict[args.source]['readable']

        for i, player in enumerate(all_players):
            if variables[i].solution_value() == 1:
                roster.add_player(player)

        print('Optimal roster for: %s' % league)
        print(roster)
        print()

        return roster
    else:
        print('''
            No solution found for command line query.
            Try adjusting your query by taking away constraints.
            ''')
        return None