def run(league, args, existing_rosters=None): if args.game not in _GAMES: raise Exception( 'You chose {} as DFS game. Available options are {}' .format(args.game, _GAMES) ) args.game = cons.DRAFT_KINGS \ if args.game == 'draftkings' or args.game == cons.DRAFT_KINGS \ else cons.FAN_DUEL solver = pywraplp.Solver( 'FD', pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING ) all_players = retrieve_players(args) # TODO - add NFL for FanDuel id:3 if args.league == 'NFL': 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, existing_rosters or [], ) opt_message = 'Optimized over {} players'.format(len(all_players)) print('\x1b[0;32;40m{}\x1b[0m'.format(opt_message)) if solution == solver.OPTIMAL: roster = RosterSelect().roster_gen(args.league) 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) return roster else: print( ''' No solution found for command line query. Try adjusting your query by taking away constraints. ''' ) return None
def run(league, args, existing_rosters=None, exposure_bounds=None): if exposure_bounds: exposure_args = get_exposure_args(existing_rosters=existing_rosters, exposure_bounds=exposure_bounds, n=int(args.i), use_random=args.random_exposure, random_seed=args.__dict__.get( 'random_exposure_seed', 0)) args.locked = exposure_args['locked'] args.banned = exposure_args['banned'] args.game = _get_game(args) all_players = retrieve_players(args) salary_max = _get_salary(args) salary_min = 0 roster_size = _get_roster_size(args) position_limits = _get_position_limits(args) general_position_limits = _get_general_position_limits(args) optimizer = Optimizer( players=all_players, existing_rosters=existing_rosters, settings=args, salary_min=salary_min, salary_max=salary_max, roster_size=roster_size, position_limits=position_limits, general_position_limits=general_position_limits, ) variables = optimizer.variables opt_message = 'Optimized over {} players'.format(len(all_players)) print('\x1b[0;32;40m{}\x1b[0m'.format(opt_message)) if optimizer.solve(): roster = RosterSelect().roster_gen(args.league) 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
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
def run(league, remove, args): if args.game not in _GAMES: raise Exception( 'You chose {} as DFS game. Available options are {}'.format( args.game, _GAMES)) args.game = cons.DRAFT_KINGS \ if args.game == 'draftkings' or args.game == cons.DRAFT_KINGS \ else cons.FAN_DUEL solver = pywraplp.Solver('FD', pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) all_players = retrieve_players(args, remove) # TODO - add NFL for FanDuel if args.league == 'NFL': 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) 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) return roster else: print(''' No solution found for command line query. Try adjusting your query by taking away constraints. ''') return None
def run(league, args, existing_rosters=None): args.game = _get_game(args) all_players = retrieve_players(args) salary = _get_salary(args) roster_size = _get_roster_size(args) position_limits = _get_position_limits(args) general_position_limits = _get_general_position_limits(args) optimizer = Optimizer( players=all_players, existing_rosters=existing_rosters, settings=args, salary=salary, roster_size=roster_size, position_limits=position_limits, general_position_limits=general_position_limits, ) variables = optimizer.variables opt_message = 'Optimized over {} players'.format(len(all_players)) print('\x1b[0;32;40m{}\x1b[0m'.format(opt_message)) if optimizer.solve(): roster = RosterSelect().roster_gen(args.league) 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) return roster else: print(''' No solution found for command line query. Try adjusting your query by taking away constraints. ''') return None
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): solver = pywraplp.Solver('FD', pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) all_players = [] with open('data/dk-salaries-current-week.csv', 'rb') as csvfile: csvdata = csv.DictReader(csvfile) for idx, row in enumerate(csvdata): if idx > 0: player = Player(row['Position'], row['Name'], row['Salary'], matchup=row['GameInfo']) if args.l == 'NBA': player.proj = float(row['AvgPointsPerGame']) player.team = row['teamAbbrev'] all_players.append(player) if league == 'NFL': with open('data/fan-pros.csv', 'rb') as csvfile: csvdata = csv.DictReader(csvfile) mass_hold = [['playername', 'points', 'cost', 'ppd']] for row in csvdata: holder = row player = filter(lambda x: x.name in row['playername'], all_players) if len(player) == 0: continue player[0].proj = float(row['points']) player[0].marked = 'Y' player[0].team = row['playername'].split(' ')[-2] 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('data/fan-pros.csv', 'rb') as csvdata: for row in csvdata: check = row break with open('data/fan-pros.csv', 'wb') as csvdata: if len(check) == 4: pass else: writer = csv.writer(csvdata, lineterminator='\n') writer.writerows(mass_hold) if league == 'NFL': _check_missing_players(all_players, args.sp, args.mp) # filter based on criteria and previously optimized all_players = filter(lambda x: x.name not in remove and \ x.proj >= int(args.lp) and \ x.cost <= int(args.ms) and \ x.team is not None, all_players) variables, solution = run_solver(solver, all_players, position_distribution) if solution == solver.OPTIMAL: roster = RosterSelect().roster_gen(args.l) 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: raise Exception('No solution error')
def run(position_distribution, league, remove, args, test_mode=False): csv_name = 'test' if test_mode else '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'], lock=(args.locked and row['Name'] in args.locked)) if args.l == 'NBA': player.proj = float(row['AvgPointsPerGame']) player.team = row['teamAbbrev'] 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 league == 'NFL': 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) if league == 'NFL': _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) 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): solver = pywraplp.Solver('FD', pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) all_players = [] with open('dk/data/dkk.csv', 'rb') as csvfile: csvdata = csv.DictReader(csvfile) for idx, row in enumerate(csvdata): if idx > 0: player = Player(row['Position'], row['Name'], row['Salary'], matchup=row['GameInfo']) if args.l == 'NBA': player.proj = float(row['AvgPointsPerGame']) player.team = row['teamAbbrev'] all_players.append(player) if league == 'NBA': with open('dk/data/fan-pros.csv', 'rb') as csvfile: csvdata = csv.DictReader(csvfile) mass_hold = [['player', 'projected', 'salary', 'cpp']] for row in csvdata: holder = row player = filter(lambda x: x.name in row['player'], all_players) if len(player) == 0: continue player[0].proj = float(row['projected']) player[0].marked = 'Y' player[0].team = row['playername'].split(' ')[-2] listify_holder = [ row['player'], row['projected'] ] if '0.0' not in row['projected'] or player[0].cost != 0: ppd = float(row['projected']) / float(player[0].cost) else: ppd = 0 listify_holder.extend([player[0].cost, ppd * 100000]) mass_hold.append(listify_holder) check = [] with open('dk/data/fan-pros.csv', 'rb') as csvdata: for row in csvdata: check = row break with open('dk/data/fan-pros.csv', 'wb') as csvdata: if len(check) == 4: pass else: writer = csv.writer(csvdata, lineterminator='\n') writer.writerows(mass_hold) if league == 'NFL': _check_missing_players(all_players, args.sp, args.mp) # filter based on criteria and previously optimized all_players = filter(lambda x: x.name not in remove and \ x.proj >= int(args.lp) and \ x.cost <= int(args.ms) and \ x.team is not None, all_players) variables, solution = run_solver(solver, all_players, position_distribution) if solution == solver.OPTIMAL: roster = RosterSelect().roster_gen(args.l) 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: raise Exception('No solution error')
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
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
def run(position_distribution, league, remove, args, test_mode=False): csv_name = 'test' if test_mode else '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 idx, row in enumerate(csvdata): if idx > 0: player = Player(row['Position'], row['Name'], row['Salary'], matchup=row['GameInfo']) if args.l == 'NBA': player.proj = float(row['AvgPointsPerGame']) player.team = row['teamAbbrev'] all_players.append(player) if league == 'NFL': with open(fnp.format(csv_name), 'rb') as csvfile: csvdata = csv.DictReader(csvfile) mass_hold = [['playername', 'points', 'cost', 'ppd']] for row in csvdata: player = filter(lambda x: x.name in row['playername'], all_players) if len(player) == 0: continue player[0].proj = float(row['points']) player[0].marked = 'Y' player[0].team = row['playername'].split(' ')[-2] 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) if league == 'NFL': _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. if isinstance(args.teams, str): args.teams = args.teams.split(' ') all_players = filter( qc.add_constraints(args, remove), all_players) variables, solution = run_solver(solver, all_players, position_distribution) if solution == solver.OPTIMAL: roster = RosterSelect().roster_gen(args.l) 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