def run_multi( iterations: int, rule_set: RuleSet, player_pool: list, constraints: LineupConstraints = LineupConstraints(), player_settings: PlayerPoolSettings = PlayerPoolSettings(), optimizer_settings: OptimizerSettings = OptimizerSettings(), verbose=False, exposure_bounds: List[dict] = list(), exposure_random_seed=None, ) -> [List[Roster], list]: # set the random seed globally for random lineup exposure random.seed(exposure_random_seed) rosters = [] for _ in range(0, iterations): exposure_dict = get_exposure_args( existing_rosters=optimizer_settings.existing_rosters, exposure_bounds=exposure_bounds, n=iterations, use_random=bool(exposure_random_seed), random_seed=exposure_random_seed, ) roster = run( rule_set=rule_set, player_pool=player_pool, optimizer_settings=optimizer_settings, player_settings=player_settings, exposure_dict=exposure_dict, constraints=constraints, verbose=verbose, ) if roster: optimizer_settings.existing_rosters += [roster] if roster: rosters.append(roster) else: break # clear ban/lock to reset exposure between iterations reset_player_ban_lock(player_pool) exposure_diffs = {} if rosters and verbose: print(get_exposure_table(rosters, exposure_bounds)) print() print(get_exposure_matrix(rosters)) print() exposure_diffs = check_exposure(rosters, exposure_bounds) for n, d in exposure_diffs.items(): if d < 0: print('{} is UNDER exposure by {} lineups'.format(n, d)) else: print('{} is OVER exposure by {} lineups'.format(n, d)) return rosters, exposure_diffs
def optimize(all_players: list, player_settings: PlayerPoolSettings = PlayerPoolSettings(), constraints: LineupConstraints = LineupConstraints()): lineup_players = [] all_players = list( filter(add_pickem_contraints(player_settings), all_players)) if constraints.has_group_constraints(): raise NotImplementedError('Groups are not supported for pickem') for p in all_players: if constraints.is_banned(p.name): p.ban = True for t in TIERS: best = sorted( [p for p in all_players if p.tier == t and not p.ban], key=lambda p: p.proj, reverse=True, )[0] lineup_players.append(best) lineup = TieredLineup(lineup_players) for p in all_players: if constraints.is_locked(p.name): p.lock = True setattr(lineup, p.tier, p) return lineup
def test_constraint_set_eq(): lcs1 = LineupConstraints() lcs1.add_group_constraint(['Spencer Ware', 'Amari Cooper'], 1) lcs1.ban(['Packers']) lcs1.add_group_constraint(['Eli Manning', 'Russell Wilson', 'Doug Martin'], (1, 3)) lcs1.lock(['Will Fuller']) lcs2 = LineupConstraints() lcs2.add_group_constraint(['Spencer Ware', 'Amari Cooper'], 1) lcs2.ban(['Packers']) lcs2.add_group_constraint(['Eli Manning', 'Russell Wilson', 'Doug Martin'], (1, 3)) lcs2.lock(['Will Fuller']) ntools.assert_equal(lcs1, lcs2)
def test_banned_players(): players = _generate_test_player_data() optimized = optimize(players, player_settings=PlayerPoolSettings(), constraints=LineupConstraints(banned=['A', 'C'])) ntools.assert_equal(optimized.total, 49 + 47 + 46 + 44 + 42 + 40)
def test_constraint_contains(): lcs = LineupConstraints() lcs.add_group_constraint(['A', 'B'], 1) lcs.ban(['C']) lcs.add_group_constraint(['E', 'F', 'G'], (1, 3)) lcs.lock(['H']) for c in ['A', 'B', 'C', 'E', 'F', 'G', 'H']: ntools.assert_equal(c in lcs, True)
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_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_nfl_showdown_lock_captain(): 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( position_locked=['A2 CPT X'], ), verbose=True ) ntools.assert_not_equal(roster, None) ntools.assert_equal(roster.projected(), 370.5) cpt = [x for x in roster.players if x.pos == 'CPT'][0] ntools.assert_equal('A2', cpt.name)
def test_nfl_showdown_ban_specific(): 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( position_banned=['A1 CPT X'], ), verbose=True ) ntools.assert_not_equal(roster, None) ntools.assert_equal(roster.projected(), 386.0) flex = [ x for x in roster.players if x.pos == 'FLEX' and x.name == 'A1' ][0] ntools.assert_equal('A1', flex.name)
def test_bad_group_zero_min(): lcs = LineupConstraints() lcs.add_group_constraint(['Spencer Ware', 'Amari Cooper'], (0, 1))
def test_bad_group_duplicate_bounds(): lcs = LineupConstraints() lcs.add_group_constraint(['Spencer Ware', 'Amari Cooper'], (1, 1))
def test_bad_group_shadow_ban_hi_lo_bound(): lcs = LineupConstraints() lcs.add_group_constraint(['Spencer Ware', 'Amari Cooper'], (0, 0))
def test_dup_group_rule2(): lcs = LineupConstraints() lcs.add_group_constraint(['Eli Manning', 'Russell Wilson', 'Doug Martin'], (1, 2)) lcs.add_group_constraint(['Eli Manning', 'Russell Wilson', 'Doug Martin'], (1, 2))
def test_constraint_string_args(): lcs = LineupConstraints() lcs.ban('Sam Bradford') lcs.lock('Will Fuller') ntools.assert_equal(len(lcs), 2)
def test_dup_group_rule(): lcs = LineupConstraints() lcs.add_group_constraint(['Spencer Ware', 'Amari Cooper'], 1) lcs.add_group_constraint(['Spencer Ware', 'Amari Cooper'], 1)
def test_bad_group_bounds_type(): lcs = LineupConstraints() lcs.add_group_constraint(['Spencer Ware', 'Amari Cooper'], '1')
def test_group_ban_conflict(): lcs = LineupConstraints() lcs.add_group_constraint(['Eli Manning', 'Doug Martin'], (1, 2)) lcs.ban(['Eli Manning'])
def test_empty_lock(): lcs = LineupConstraints() lcs.lock([])
def run(rule_set: RuleSet, player_pool: list, constraints: LineupConstraints = LineupConstraints(), optimizer_settings: OptimizerSettings = OptimizerSettings(), player_settings: PlayerPoolSettings = PlayerPoolSettings(), exposure_dict: dict = dict(), roster_gen: Roster = None, verbose=False) -> Roster: players = pool.filter_pool( player_pool, player_settings, ) if rule_set.game_type == 'showdown': if optimizer_settings.no_offense_against_defense: print('WARNING:') print('no_offense_against_defense setting ignored for showdown') print('game types. Use no_defense_against_captain instead.') print() optimizer = Optimizer( players=players, rule_set=rule_set, settings=optimizer_settings, lineup_constraints=constraints, exposure_dict=exposure_dict, ) variables = optimizer.variables if optimizer.solve(): if roster_gen: roster = roster_gen() else: roster = RosterSelect().roster_gen(rule_set.league) for i, player in enumerate(players): if variables[i].solution_value() == 1: roster.add_player(player) if verbose: print('Optimal roster for: {}'.format(rule_set.league)) print(roster) return roster if verbose: print(''' No solution found. Try adjusting your query by taking away constraints. OPTIMIZER CONSTRAINTS: {} LINEUP CONSTRAINTS: {} PLAYER POOL SETTINGS: {} PLAYER COUNT: {} '''.format(optimizer_settings, constraints, player_settings, len(players or []))) return None
def test_empty_group(): lcs = LineupConstraints() lcs.add_group_constraint([], 1)
def test_single_player_group(): lcs = LineupConstraints() lcs.add_group_constraint(['Spencer Ware'], 1)
def test_bad_group_too_many_bounds(): lcs = LineupConstraints() lcs.add_group_constraint(['Spencer Ware', 'Amari Cooper'], (1, 1, 2))
def test_bad_group_negative_min(): lcs = LineupConstraints() lcs.add_group_constraint(['Spencer Ware', 'Amari Cooper'], (-1, 1))
def test_bad_group_max_set(): lcs = LineupConstraints() lcs.add_group_constraint(['Spencer Ware', 'Amari Cooper'], (1, 3))
def test_locked_players(): players = _generate_test_player_data() optimized = optimize(players, constraints=LineupConstraints(locked=['B', 'D'])) ntools.assert_equal(optimized.total, 49 + 47 + 46 + 44 + 42 + 40)
def test_bad_group_dup_player(): lcs = LineupConstraints() lcs.add_group_constraint(['Amari Cooper', 'Amari Cooper'], 1)
def test_empty_ban(): lcs = LineupConstraints() lcs.ban([])
def test_lock_ban_conflict(): lcs = LineupConstraints() lcs.ban(['Will Fuller']) lcs.lock(['Will Fuller'])