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_uniques_nba(): roster = run( rule_set=rules.DK_NBA_RULE_SET, player_pool=mock_nba_pool, ) second_roster = run( rule_set=rules.DK_NBA_RULE_SET, player_pool=mock_nba_pool, optimizer_settings=OptimizerSettings(existing_rosters=[roster], ), ) third_roster = run( rule_set=rules.DK_NBA_RULE_SET, player_pool=mock_nba_pool, optimizer_settings=OptimizerSettings( existing_rosters=[roster], uniques=2, ), ) players = roster.sorted_players() second_players = second_roster.sorted_players() third_players = third_roster.sorted_players() crossover_a = list(set(players).intersection(second_players)) crossover_b = list(set(players).intersection(third_players)) ntools.assert_equal(len(crossover_a), rules.DK_NBA_RULE_SET.roster_size - 1) ntools.assert_equal(len(crossover_b), rules.DK_NBA_RULE_SET.roster_size - 2)
def test_if_one_then_two(): mock_nba_pool = construct_pool() # Base case roster = run( rule_set=rules.DK_NBA_RULE_SET, player_pool=mock_nba_pool, verbose=True, ) names = {p.name for p in roster.players} ntools.assert_equal( True, 'A1' in names and 'A4' not in names and 'A9' not in names) settings = OptimizerSettings(custom_rules=[ # Always play A1 with A9 and A4 CustomRule(group_a=lambda p: p.name == 'A1', group_b=lambda p: p.name == 'A9' or p.name == 'A4') ]) # TODO - add test where A1 is not in optimized lineup roster = run( rule_set=rules.DK_NBA_RULE_SET, player_pool=mock_nba_pool, verbose=True, optimizer_settings=settings, ) names = {p.name for p in roster.players} # Without this rule, A4 and A9 would never appear in the optimized # lineup. Both have a 0 point projection. ntools.assert_equal(True, 'A1' in names and 'A9' in names and 'A4' in names)
def test_never_two(): mock_nba_pool = construct_pool() # Base case roster = run( rule_set=rules.DK_NBA_RULE_SET, player_pool=mock_nba_pool, verbose=True, ) names = {p.name for p in roster.players} ntools.assert_equal(True, 'A101' in names and 'A100' in names) # Never play two players together settings = OptimizerSettings(custom_rules=[ CustomRule(group_a=lambda p: p, group_b=lambda p: p.name == 'A100' or p.name == 'A101', comparison=lambda sum, a, b: sum(b) <= 1) ]) roster = run( rule_set=rules.DK_NBA_RULE_SET, player_pool=mock_nba_pool, verbose=True, optimizer_settings=settings, ) names = {p.name for p in roster.players} # Without this rule, A4 and A9 would never appear in the optimized # lineup. Both have a 0 point projection. ntools.assert_equal(True, 'A101' not in names and 'A100' in names)
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_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_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_multi_roster_nba(): roster = run( rule_set=rules.DK_NBA_RULE_SET, player_pool=mock_nba_pool, ) second_roster = run( rule_set=rules.DK_NBA_RULE_SET, player_pool=mock_nba_pool, optimizer_settings=OptimizerSettings(existing_rosters=[roster], ), ) ntools.assert_not_equal(roster, None) ntools.assert_not_equal(second_roster, None) ntools.assert_false(roster.exact_equal(second_roster))
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_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_nba_dk_with_csv(): roster = run( rule_set=rules.DK_NBA_RULE_SET, player_pool=mock_nba_pool, verbose=True, ) ntools.assert_not_equal(roster, None)
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_if_one_then_one(): mock_nba_pool = construct_pool() # Base case roster = run( rule_set=rules.DK_NBA_RULE_SET, player_pool=mock_nba_pool, verbose=True, ) names = {p.name for p in roster.players} ntools.assert_equal(True, 'A1' in names and 'A4' not in names) settings = OptimizerSettings(custom_rules=[ # Always play A1 with A9 and A4 CustomRule(group_a=lambda p: p.name == 'A1', group_b=lambda p: p.name == 'A4', comparison=lambda s_sum, a, b: s_sum(a) == s_sum(b)) ]) roster = run( rule_set=rules.DK_NBA_RULE_SET, player_pool=mock_nba_pool, verbose=True, optimizer_settings=settings, ) names = {p.name for p in roster.players} # Without this rule, A1 and A4 are not in the same lineup. ntools.assert_equal(True, 'A1' in names and 'A4' in names) # Confirm now that the rule prevents one without the other mock_nba_pool = construct_pool() for p in mock_nba_pool: if p.name == 'A1': # A1 normally would always be in the lineup # due to highest projection among PGs p.proj = 502 if p.name == 'A4': # A4 projection is so negative it should # never be in any lineup p.proj = -1_000_000 roster = run( rule_set=rules.DK_NBA_RULE_SET, player_pool=mock_nba_pool, verbose=True, optimizer_settings=settings, ) names = {p.name for p in roster.players} ntools.assert_equal(True, 'A1' not in names and 'A4' not in names)
def test_nfl_dk_mock(): roster = run( rule_set=rules.DK_NFL_RULE_SET, player_pool=mock_nfl_pool, ) ntools.assert_not_equal(roster, None) ntools.assert_equal(roster.projected(), 420.0)
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_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_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_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_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_nfl_dk_showdown_mock(): mock_dk_pool = _build_mock_player_pool() roster = run(rule_set=rules.DK_NFL_SHOWDOWN_RULE_SET, player_pool=mock_dk_pool, optimizer_settings=OptimizerSettings(showdown_teams=('X', 'Y'), ), verbose=True) ntools.assert_not_equal(roster, None) ntools.assert_equal(roster.projected(), 421)
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_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_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_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 test_csgo_mock(): mock_dk_pool = _build_mock_player_pool() roster = run( # CSGO limits three per team rule_set=rules.DK_CSGO_SHOWDOWN, player_pool=mock_dk_pool, optimizer_settings=OptimizerSettings(showdown_teams=('X', 'Y'), ), verbose=True) ntools.assert_not_equal(roster, None) players = roster.players ntools.assert_equal(len([x for x in players if x.team == 'Y']), 3)
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_nfl_showdown_ban_general(): mock_dk_pool = _build_mock_player_pool() roster = run(rule_set=rules.DK_NFL_SHOWDOWN_RULE_SET, player_pool=mock_dk_pool, optimizer_settings=OptimizerSettings( showdown_teams=('X', 'Y'), no_defense_against_captain=True, ), constraints=LineupConstraints(banned=['A1'], ), verbose=True) ntools.assert_not_equal(roster, None) ntools.assert_equal(roster.projected(), 334.0) ntools.assert_true('A1' not in [x.name for x in roster.players])
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')