DimaKudosh / pydfs-lineup-optimizer

Daily Fantasy Sports lineup optimzer for all popular daily fantasy sports sites
MIT License
424 stars 157 forks source link

How to properly load in game information for positional constraints? #284

Closed McCampy closed 3 years ago

McCampy commented 3 years ago

Hi there again, I am trying to use the restrict positions for opposing team constraint but I can't seem to load in the game variable correctly. I'm not exactly sure how to give each player the home_team attribute because it doesn't seem like you can add is a kwarg. Any insight on fixing this would be great. Cheers.

CSV: FD.csv

Code:

import pandas as pd 
from pydfs_lineup_optimizer import get_optimizer, Site, Sport, Player

dffd = pd.read_csv(r'E:\DFS\CSV_Downloads\08-08-21\FD.csv')
dffd[['home_team','away_team']] = dffd['Game'].str.split('@', expand=True)

o = get_optimizer(Site.FANDUEL, Sport.BASEBALL)

def row_to_player(row):
    """Converts dataframe row to Player object

    Args:
         row (namedtuple): dataframe row

    Returns:
        Player
    """
    valid_kwargs = {
        'is_injured', 'max_exposure', 'min_exposure', 
        'projected_ownership', 'game_info', 'roster_order', 
        'min_deviation', 'max_deviation', 'is_confirmed_starter', 
        'fppg_floor', 'fppg_ceil'
    }

    kwargs = {}
    for name, value in row._asdict().items():
        if name in valid_kwargs:
            kwargs[name] = value    

    return Player (
        player_id=getattr(row, 'id') if hasattr(row, 'id') else row.first_name + ' ' + row.last_name, 
        first_name=row.first_name,
        last_name=row.last_name,
        positions=row.position.split('/'),
        team=row.team,
        salary=row.salary,
        fppg=row.fpts,
        game_info=row.Game,
        **kwargs
    )

players = [row_to_player(row) for row in dffd.itertuples(index=False)]
o.load_players(players)
o.restrict_positions_for_opposing_team(['P'], ['C', 'SS', 'OF', '1B', '2B', '3B'], 1)

for lineup in o.optimize(n=1):
    print(lineup)
    print(lineup.players)  # list of players
    print(lineup.fantasy_points_projection)
    print(lineup.salary_costs)

AttributeError: runfile('E:/DFS-Model/Simulations/untitled0.py', wdir='E:/DFS-Model/Simulations') Traceback (most recent call last):

File "E:\DFS-Model\Simulations\untitled0.py", line 52, in for lineup in o.optimize(n=1):

File "C:\Users\William\anaconda3\lib\site-packages\pydfs_lineup_optimizer\lineup_optimizer.py", line 387, in optimize constraint.apply(base_solver)

File "C:\Users\William\anaconda3\lib\site-packages\pydfs_lineup_optimizer\rules.py", line 398, in apply home_team_players = {player: variable for player, variable in self.players_dict.items()

File "C:\Users\William\anaconda3\lib\site-packages\pydfs_lineup_optimizer\rules.py", line 399, in if player.team == game.home_team}

AttributeError: 'str' object has no attribute 'home_team'

DimaKudosh commented 3 years ago

You got this error because game_info is string in your code. It should be GameInfo object

McCampy commented 3 years ago

Ah makes sense, thank you.