def test_force_combo(): # no combo players = salary_download.generate_players_from_csvs( salary_file_location=salary_file, projection_file_location=projection_file, game=rules.DRAFT_KINGS, ) roster = run(rule_set=rules.DK_NFL_RULE_SET, player_pool=players, player_settings=PlayerPoolSettings(locked=['Sam Bradford'], ), optimizer_settings=OptimizerSettings( stack_team='NE', stack_count=5, )) qb = roster.sorted_players()[0] team_count = len([x for x in roster.sorted_players() if x.team == qb.team]) ntools.assert_equals(team_count, 1) # QB/WR combo players = salary_download.generate_players_from_csvs( salary_file_location=salary_file, projection_file_location=projection_file, game=rules.DRAFT_KINGS, ) roster = run(rule_set=rules.DK_NFL_RULE_SET, player_pool=players, optimizer_settings=OptimizerSettings(force_combo=True, )) qb = roster.sorted_players()[0] ntools.assert_equal(qb.pos, 'QB')
def test_te_combo(): # use lock and ban to make a non-globally optimal QB/TE combo optimal players = salary_download.generate_players_from_csvs( salary_file_location=salary_file, projection_file_location=projection_file, game=rules.DRAFT_KINGS, ) roster = run( rule_set=rules.DK_NFL_RULE_SET, player_pool=players, optimizer_settings=OptimizerSettings( force_combo=True, combo_allow_te=True, ), constraints=LineupConstraints( banned=['Kellen Davis'], locked=['Philip Rivers'], ), verbose=True, ) qb = roster.sorted_players()[0] ntools.assert_equal(qb.pos, 'QB') team_count = len([ x for x in roster.sorted_players() if x.team == qb.team and x.pos == 'TE' ]) ntools.assert_equals(team_count, 1) # make sure WR/QB still works players = salary_download.generate_players_from_csvs( salary_file_location=salary_file, projection_file_location=projection_file, game=rules.DRAFT_KINGS, ) roster = run( rule_set=rules.DK_NFL_RULE_SET, player_pool=players, optimizer_settings=OptimizerSettings( force_combo=True, combo_allow_te=True, ), constraints=LineupConstraints(locked=['Andrew Luck'], ), verbose=True, ) qb = roster.sorted_players()[0] ntools.assert_equal(qb.pos, 'QB') team_count = len([ x for x in roster.sorted_players() if x.team == qb.team and x.pos == 'WR' ]) ntools.assert_equals(team_count, 1)
def test_pickem_nba_upload(): salary_file_location = '{}/data/dk-nba-pickem-salaries.csv'.format( CURRENT_DIR) players = salary_download.generate_players_from_csvs( game=rules.DRAFT_KINGS, salary_file_location=salary_file_location, ruleset=rules.DK_NBA_PICKEM_RULE_SET, ) rosters = [p_optimize(all_players=players, )] pid_file = '{}/data/dk-nba-pickem-pids.csv'.format(CURRENT_DIR) upload_file = '{}/data/current-upload.csv'.format(CURRENT_DIR) uploader = uploaders.DraftKingsNBAPickemUploader( pid_file=pid_file, upload_file=upload_file, ) uploader.write_rosters(rosters) row = None with open(upload_file, 'r') as csvfile: reader = csv.reader(csvfile, delimiter=',') for idx, row in enumerate(reader): if idx == 0: continue assert_equal(row, [ '11839390', '11839397', '11839400', '11839405', '11839420', '11839422', ])
def test_dk_nba_use_proj(): players = salary_download.generate_players_from_csvs( salary_file_location=salaries, projection_file_location=projections, game=DRAFT_KINGS, ) ntools.assert_equals(players[0].proj, 62.29)
def test_deterministic_exposure_limits(): iterations = 2 players = salary_download.generate_players_from_csvs( salary_file_location=salary_file, projection_file_location=projection_file, game=rules.DRAFT_KINGS, ) rosters, exposure_diffs = run_multi(iterations=2, rule_set=rules.DK_NFL_RULE_SET, player_pool=players, exposure_bounds=[ { 'name': 'Andrew Luck', 'min': 0.5, 'max': 0.7 }, { 'name': 'Alshon Jeffery', 'min': 1, 'max': 1 }, ]) ntools.assert_equal(len(rosters), iterations) ntools.assert_equal(len(exposure_diffs), 0) players = [p.name for p in rosters[0].players] ntools.assert_true('Andrew Luck' in players) ntools.assert_true('Alshon Jeffery' in players) players = [p.name for p in rosters[1].players] ntools.assert_true('Andrew Luck' not in players) ntools.assert_true('Alshon Jeffery' in players)
def _get_first_written_row( game: str, salary_file_location: str, rule_set: rules.RuleSet, pid_file: str, Uploader: Type[uploaders.CSVUploader], ) -> list: players = salary_download.generate_players_from_csvs( game=game, salary_file_location=salary_file_location, ruleset=rule_set, ) roster = optimize.run( rule_set=rule_set, player_pool=players, verbose=True, ) upload_file = '{}/data/current-upload.csv'.format(CURRENT_DIR) uploader = Uploader( pid_file=pid_file, upload_file=upload_file, ) uploader.write_rosters([roster]) row = None with open(upload_file, 'r') as csvfile: reader = csv.reader(csvfile, delimiter=',') for idx, row in enumerate(reader): if idx == 0: continue return row
def test_custom_stack(): players = salary_download.generate_players_from_csvs( salary_file_location=salary_file, projection_file_location=projection_file, game=rules.DRAFT_KINGS, ) roster = run( rule_set=rules.DK_NFL_RULE_SET, player_pool=players, optimizer_settings=OptimizerSettings(stacks=[ Stack( team='NE', count=5, stack_lock_pos=['QB'], stack_eligible_pos=['WR'], ) ]), verbose=True, ) ne_players_count = len( [p for p in roster.sorted_players() if p.team == 'NE']) ntools.assert_equals(5, ne_players_count) ne_def = len([ p for p in roster.sorted_players() if p.team == 'NE' and p.pos == 'DST' ]) ntools.assert_equals(ne_def, 0) wides = len([ p for p in roster.sorted_players() if p.team == 'NE' and p.pos == 'WR' ]) ntools.assert_equals(wides, 4)
def test_no_opposing_def_dk_nfl(): players = salary_download.generate_players_from_csvs( salary_file_location=salary_file, projection_file_location=projection_file, game=rules.DRAFT_KINGS, ) roster = run( rule_set=rules.DK_NFL_RULE_SET, player_pool=players, optimizer_settings=OptimizerSettings(no_offense_against_defense=True), constraints=LineupConstraints(locked=['Bengals']), verbose=True) for p in roster.players: if p.pos in rules.DK_NFL_RULE_SET.offensive_positions: ntools.assert_not_equal(p.team, 'CIN') # force impossible lineup roster = run( rule_set=rules.DK_NFL_RULE_SET, player_pool=players, optimizer_settings=OptimizerSettings(no_offense_against_defense=True), constraints=LineupConstraints(locked=['Bengals', 'Ryan Fitzpatrick']), verbose=True) ntools.assert_equal(roster, None)
def test_no_opposing_def_fd_nfl(): players = salary_download.generate_players_from_csvs( salary_file_location=fd_nfl_salary_file, game=rules.FAN_DUEL, ) roster = run( rule_set=rules.FD_NFL_RULE_SET, player_pool=players, optimizer_settings=OptimizerSettings(no_offense_against_defense=True), constraints=LineupConstraints(locked=['Jacksonville Jaguars']), verbose=True) ntools.assert_not_equal(roster, None) for p in roster.players: if p.pos in rules.DK_NFL_RULE_SET.offensive_positions: ntools.assert_not_equal(p.team, 'IND') # force impossible lineup roster = run( rule_set=rules.FD_NFL_RULE_SET, player_pool=players, optimizer_settings=OptimizerSettings(no_offense_against_defense=True), constraints=LineupConstraints( locked=['Eric Ebron', 'Jacksonville Jaguars']), verbose=True) ntools.assert_equal(roster, None)
def test_custom_rules(): # Minimum stack size custom_rules = [] player_pool = salary_download.generate_players_from_csvs( salary_file_location=salary_file, game=rules.DRAFT_KINGS, ruleset=rules.DK_MLB_RULE_SET, ) for p in player_pool: if p.team == 'ATL' and p.pos == '1B': p.proj = 1_000 def comp(sum, a, b): return sum(b) >= sum(a) + 2 # It appears to me closures do not work with ortools # (ex. passing p.team == t) custom_rules.append( CustomRule( # Given 1B in optimized lineup group_a=lambda p: p.pos == '1B' and p.team == 'ATL', # Ensure the stack is four players group_b=lambda p: '1B' not in p.pos and p.team == 'ATL', # batters only comparison=comp, ) ) custom_rules.append( CustomRule( # Given 1B in optimized lineup group_a=lambda p: p.pos == '1B' and p.team == 'BOS', # Ensure the stack is four players group_b=lambda p: '1B' not in p.pos and p.team == 'BOS', # batters only comparison=comp, ) ) settings = OptimizerSettings( custom_rules=custom_rules ) roster = run( rule_set=rules.DK_MLB_RULE_SET, player_pool=player_pool, verbose=True, optimizer_settings=settings, ) team_for_first = [p for p in roster.players if p.pos == '1B'][0].team total = len([p for p in roster.players if p.team == team_for_first and 'P' not in p.pos]) assert total > 3, f"{total} below 4"
def test_force_combo(): # no combo players = salary_download.generate_players_from_csvs( salary_file_location=salary_file, projection_file_location=projection_file, game=rules.DRAFT_KINGS, ) roster = run( rule_set=rules.DK_NFL_RULE_SET, player_pool=players, optimizer_settings=OptimizerSettings( stacks=[Stack(team='NE', count=5)]), constraints=LineupConstraints(locked=['Sam Bradford'], ), ) qb = roster.sorted_players()[0] team_count = len([x for x in roster.sorted_players() if x.team == qb.team]) ntools.assert_equals(team_count, 1) # QB/WR combo players = salary_download.generate_players_from_csvs( salary_file_location=salary_file, projection_file_location=projection_file, game=rules.DRAFT_KINGS, ) roster = run( rule_set=rules.DK_NFL_RULE_SET, player_pool=players, optimizer_settings=OptimizerSettings(force_combo=True, ), constraints=LineupConstraints(banned=['Ryan Fitzpatrick']), verbose=True, ) qb = roster.sorted_players()[0] ntools.assert_equal(qb.pos, 'QB') wr_team_count = len([ x for x in roster.sorted_players() if x.team == qb.team and x.pos == 'WR' ]) ntools.assert_equals(wr_team_count, 1) te_team_count = len([ x for x in roster.sorted_players() if x.team == qb.team and x.pos == 'TE' ]) ntools.assert_equals(te_team_count, 0)
def test_nfl_fd(): players = salary_download.generate_players_from_csvs( salary_file_location=fd_nfl_salary_file, game=rules.FAN_DUEL, ) roster = run( rule_set=rules.FD_NFL_RULE_SET, player_pool=players, ) ntools.assert_not_equals(roster, None)
def test_tennis_lineup(): player_pool = salary_download.generate_players_from_csvs( salary_file_location=salary_file, game=rules.DRAFT_KINGS, ) roster = run( rule_set=rules.DK_TEN_CLASSIC_RULE_SET, player_pool=player_pool, verbose=True, ) ntools.assert_not_equal(roster, None)
def test_soccer_dk(): player_pool = salary_download.generate_players_from_csvs( salary_file_location=salary_file, game=rules.DRAFT_KINGS, ) roster = run( rule_set=rules.DK_SOCCER_RULE_SET, player_pool=player_pool, verbose=True, ) ntools.assert_not_equal(roster, None)
def test_nfl_dk(): players = salary_download.generate_players_from_csvs( salary_file_location=salary_file, projection_file_location=projection_file, game=rules.DRAFT_KINGS, ) roster = run( rule_set=rules.DK_NFL_RULE_SET, player_pool=players, ) ntools.assert_not_equals(roster, None)
def test_no_double_te(): players = salary_download.generate_players_from_csvs( salary_file_location=salary_file, projection_file_location=projection_file, game=rules.DRAFT_KINGS, ) roster = run(rule_set=rules.DK_NFL_RULE_SET, player_pool=players, player_settings=PlayerPoolSettings(locked=['Rob Gronkowski'])) qb = roster.sorted_players()[0] ntools.assert_equal(qb.pos, 'QB')
def test_nfl_fd(): players = salary_download.generate_players_from_csvs( salary_file_location=fd_nfl_salary_file, game=rules.FAN_DUEL, ) roster = run(rule_set=rules.FD_NFL_RULE_SET, player_pool=players, verbose=True) ntools.assert_not_equal(roster, None) ntools.assert_equal(roster.projected(), 155.0172712846236)
def test_no_mutate_side_Effect(): players = salary_download.generate_players_from_csvs( salary_file_location=fd_nfl_salary_file, game=rules.FAN_DUEL, ) run(rule_set=rules.FD_NFL_RULE_SET, player_pool=players, optimizer_settings=OptimizerSettings(no_offense_against_defense=True), constraints=LineupConstraints(locked=['Tom Brady']), verbose=True) brady = next((p for p in players if p.name == 'Tom Brady')) ntools.assert_equal(brady.lock, False)
def test_f1_identifier(): player_pool = salary_download.generate_players_from_csvs( salary_file_location=salary_file, game=rules.DRAFT_KINGS, ruleset=rules.DK_F1_SHOWDOWN, ) roster = run( rule_set=rules.DK_F1_SHOWDOWN, player_pool=player_pool, verbose=True, ) ntools.assert_not_equal(roster.identifier, None)
def _generate_players(self) -> list: salary_file = self.file_locations['salaries'] projection_file = self._adjust_projections() rule = getattr(rules, self.SITE_MAP[self.site]['rule']) try: return salary_download.generate_players_from_csvs( salary_file_location=salary_file, projection_file_location=projection_file, game=rule, ) except MissingPlayersException as e: raise ("Error: {}".format(e))
def test_multi_position(): players = salary_download.generate_players_from_csvs( salary_file_location=salary_file, projection_file_location=projection_file, game=rules.DRAFT_KINGS, ) roster = run(rule_set=rules.DK_NFL_RULE_SET, player_pool=players, constraints=LineupConstraints(locked=['Eli Manning'], ), verbose=True) ntools.assert_not_equal(roster, None) multi_pos = [p for p in roster.players if p.name == 'Eli Manning'] ntools.assert_equal(len(multi_pos), 1) ntools.assert_equal(multi_pos[0].pos, 'TE')
def test_f1_dk(): player_pool = salary_download.generate_players_from_csvs( salary_file_location=salary_file, game=rules.DRAFT_KINGS, ruleset=rules.DK_F1_SHOWDOWN, ) roster = run( rule_set=rules.DK_F1_SHOWDOWN, player_pool=player_pool, verbose=True, ) ntools.assert_not_equal(roster, None) for p in roster.sorted_players(): ntools.assert_equal(type(p), ShowdownPlayer)
def test_random_exposure_limits(): iterations = 10 players = salary_download.generate_players_from_csvs( salary_file_location=salary_file, projection_file_location=projection_file, game=rules.DRAFT_KINGS, ) rosters, exposure_diffs = run_multi( iterations=iterations, exposure_random_seed=42, rule_set=rules.DK_NFL_RULE_SET, player_pool=players, ) ntools.assert_equal(len(rosters), iterations) ntools.assert_equal(len(exposure_diffs), 0)
def test_stack(): players = salary_download.generate_players_from_csvs( salary_file_location=salary_file, projection_file_location=projection_file, game=rules.DRAFT_KINGS, ) roster = run(rule_set=rules.DK_NFL_RULE_SET, player_pool=players, optimizer_settings=OptimizerSettings( stack_team='NE', stack_count=5, )) ne_players_count = len( [p for p in roster.sorted_players() if p.team == 'NE']) ntools.assert_equals(5, ne_players_count)
def test_mlb_dk(): player_pool = salary_download.generate_players_from_csvs( salary_file_location=salary_file, game=rules.DRAFT_KINGS, ruleset=rules.DK_MLB_RULE_SET, ) roster = run( rule_set=rules.DK_MLB_RULE_SET, player_pool=player_pool, verbose=True, ) # Test general position limits ntools.assert_not_equal(roster, None) ntools.assert_true('RP' in [x.pos for x in roster.players])
def test_respect_ban(): players = salary_download.generate_players_from_csvs( salary_file_location=salary_file, projection_file_location=projection_file, game=rules.DRAFT_KINGS, ) roster = run( rule_set=rules.DK_NFL_RULE_SET, player_pool=players, verbose=True, constraints=LineupConstraints(banned=['Eli Manning'], ), ) for player in roster.sorted_players(): ntools.assert_not_equal(player.name, 'Eli Manning')
def test_soccer_dk_no_opp_d(): player_pool = salary_download.generate_players_from_csvs( salary_file_location=salary_file, game=rules.DRAFT_KINGS, ) roster = run( rule_set=rules.DK_SOCCER_RULE_SET, player_pool=player_pool, constraints=LineupConstraints( locked=['Maxi Gomez'], ), optimizer_settings=OptimizerSettings( no_offense_against_defense=False, ), verbose=True, ) cel_off_players = [ p for p in roster.players if p.team == 'CEL' and p.pos in ['M', 'F'] ] lgn_d_players = [ p for p in roster.players if p.team == 'LGN' and p.pos in ['D', 'GK'] ] ntools.assert_equal(len(cel_off_players), 2) ntools.assert_equal(len(lgn_d_players), 2) roster = run( rule_set=rules.DK_SOCCER_RULE_SET, player_pool=player_pool, constraints=LineupConstraints( locked=['Maxi Gomez'], ), optimizer_settings=OptimizerSettings( no_offense_against_defense=True, ), verbose=True, ) cel_off_players = [ p for p in roster.players if p.team == 'CEL' and p.pos in ['M', 'F'] ] lgn_d_players = [ p for p in roster.players if p.team == 'LGN' and p.pos in ['D', 'GK'] ] ntools.assert_equal(len(cel_off_players), 2) ntools.assert_equal(len(lgn_d_players), 0)
def test_respect_lock(): players = salary_download.generate_players_from_csvs( salary_file_location=salary_file, projection_file_location=projection_file, game=rules.DRAFT_KINGS, ) roster = run( rule_set=rules.DK_NFL_RULE_SET, player_pool=players, verbose=True, constraints=LineupConstraints(locked=['Andrew Luck'], ), ) qb = roster.sorted_players()[0] ntools.assert_equal(qb.pos, 'QB') ntools.assert_equal(qb.name, 'Andrew Luck')
def test_multi_roster_nfl(): players = salary_download.generate_players_from_csvs( salary_file_location=salary_file, projection_file_location=projection_file, game=rules.DRAFT_KINGS, ) roster = run(rule_set=rules.DK_NFL_RULE_SET, player_pool=players, verbose=True) second_roster = run(rule_set=rules.DK_NFL_RULE_SET, player_pool=players, optimizer_settings=OptimizerSettings( existing_rosters=[roster], ), verbose=True) ntools.assert_not_equal(roster, None) ntools.assert_not_equal(second_roster, None) ntools.assert_false(roster.exact_equal(second_roster))
def test_respect_group1(): players = salary_download.generate_players_from_csvs( salary_file_location=salary_file, projection_file_location=projection_file, game=rules.DRAFT_KINGS, ) grouped_players = ('DeAndre Hopkins', 'Amari Cooper', 'Sammy Watkins') roster = run( rule_set=rules.DK_NFL_RULE_SET, player_pool=players, verbose=True, constraints=LineupConstraints(groups=[[grouped_players, 2]], ), ) group_count = len( [x for x in roster.sorted_players() if x.name in grouped_players]) ntools.assert_equal(group_count, 2)