Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NFL Stack with Bring Back #413

Open
cdioriodfs opened this issue Jul 6, 2023 · 1 comment
Open

NFL Stack with Bring Back #413

cdioriodfs opened this issue Jul 6, 2023 · 1 comment

Comments

@cdioriodfs
Copy link

Is it possible to have a 3 man stack and a bring back player that is either RB, WR, or TE?

optimizer.add_stack(PositionsStack(['QB', 'WR', ('WR', 'TE')], for_teams=stack_teams))
optimizer.force_positions_for_opposing_team(('QB','WR)) <-- I know this would work for just WR but how can you get it to choose one of the 3 opposing positions?

bringback_options = [('QB', 'WR'), ('QB', 'RB'), ('QB', 'TE')]
selected_bringback = random.choice(bringback_options)
optimizer.force_positions_for_opposing_team(selected_bringback)

I have tried something like this, but it is then randomly choosing one tuple per QB, not lineup.

@BenikaH
Copy link

BenikaH commented Oct 10, 2024

I have a solution I'm using if interested. I kept getting solver errors using the force_positions function. You'll have to update the GameStack function in Stacks.py. Comment the current one out, in case you don't like the lineups and paste this. Make sure that GameStack is imported already.

class GameStack(BaseStack):
    def __init__(
            self,
            size: int,
            max_exposure: Optional[float] = None,
            min_from_team: int = 1,
            positions_for_team: Optional[List[str]] = None,  # Positions for home team
            positions_for_opposing_team: Optional[List[str]] = None,  # Positions for away team
            teams: Optional[List[str]] = None  # Specify teams to include in stacking
    ):
        self.size = size
        self.max_exposure = max_exposure
        self.min_from_team = min_from_team
        self.positions_for_team = positions_for_team  # Store positions for home team
        self.positions_for_opposing_team = positions_for_opposing_team  # Store positions for away team
        self.teams = teams  # Store specific teams to include

    def build_stacks(self, players, optimizer):
        players_by_teams = get_players_grouped_by_teams(players)
        all_groups: List[BaseGroup] = []

        for game in optimizer.games:
            # Check if the current game's teams are in the specified teams list (if provided)
            if self.teams and game.home_team not in self.teams and game.away_team not in self.teams:
                continue  # Skip this game if neither team is in the specified list

            home_team_players = players_by_teams.get(game.home_team, [])
            away_team_players = players_by_teams.get(game.away_team, [])

            # Filter players by specified positions for home and opposing teams if provided
            if self.positions_for_team:
                home_team_players = [player for player in home_team_players if player.positions[0] in self.positions_for_team]
            if self.positions_for_opposing_team:
                away_team_players = [player for player in away_team_players if player.positions[0] in self.positions_for_opposing_team]

            # Build the groups for home and away teams
            groups = [PlayersGroup(
                players=home_team_players,
                min_from_group=self.min_from_team,
            ), PlayersGroup(
                players=away_team_players,
                min_from_group=self.min_from_team,
            ), PlayersGroup(
                players=[*home_team_players, *away_team_players],
                min_from_group=self.size,
            )]

            # Create the nested group and append to all_groups
            nested_group = NestedPlayersGroup(
                groups=groups,
                max_exposure=self.max_exposure,
            )
            all_groups.append(nested_group)

        return [OptimizerStack(groups=all_groups)]

    def validate(self, optimizer) -> None:
        if floor(self.size / 2) < self.min_from_team:
            raise LineupOptimizerException('min_from_team is greater than stack size')

You can then add the GameStack like so.

optimizer.add_stack(GameStack(
                size=4,
                min_from_team=3,
                positions_for_team=['QB', 'WR'],  # Home team positions
                positions_for_opposing_team=['WR', 'RB', 'TE'],  # Opposing team positions
                teams=['KC','TB'],  # Only stack players from Kansas City and Tampa Bay games
                max_exposure=0.3
            ))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants