DimaKudosh / pydfs-lineup-optimizer

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

Add function to play a WR on the opposing team of the Team stack for each lineup #102

Closed awiden91 closed 4 years ago

awiden91 commented 4 years ago

Let's say I want to play Falcons players Matt Ryan (QB) and Julio Jones (WR) as a team stack, the Falcons are playing the Texans this week, so could the optimizer pick one WR from the Texans to also put in that lineup? Appreciate the help!

sansbacon commented 4 years ago

You can use something like the code below in the interim . . .

players = optimizer.players

# dict of all games with two entries per game
games = {'CHI': 'DET',
                'DET': 'CHI',
                . . . }

qbs = [p for p in players if p.position == 'QB']
wrs = [p for p in players if p.position == 'WR']

qb_wrs = {p.full_name: [[wr for wr in wrs if wr.team == qb.team],
                                       [wr for wr in wrs if wr.team == games.get(qb.team)]]
                 for qb in qbs}

# loop through all the qbs (you can obviously filter)
for i in range (len(qbs)):
    qb_name = qbs[i].full_name
    qb = optimizer.get_player_by_name(qb_name)
    # inner loop gives you 10 combos of qb-wr-opp_wr
    # and then 3 optimal lineups for each combo
    for j in range(10):
        optimizer.add_player_to_lineup(qb)
        wr_name = random.choice(qb_wrs.get(qb.full_name)[0])
        wr = optimizer.get_player_by_name(wr_name)
        optimizer.add_player_to_lineup(wr)
        opp_wr_name = random.choice(qb_wrs.get(qb.full_name)[1])
        opp_wr = optimizer.get_player_by_name(opp_wr_name)
        optimizer.add_player_to_lineup(opp_wr)
        for lineup in optimizer.optimize(3):
            print(lineup)
        optimizer.remove_player_from_lineup(wr)
        optimizer.remove_player_from_lineup(opp_wr)
    optimizer.remove_player_from_lineup(qb)
awiden91 commented 4 years ago

Thank you so much for the response. I am still learning Python so would I put this in my sublime text editor or change the actual code? Below is what I am using and wanted to add a line like the bold line below where I can have something like optimizer.set_positions_for_opposing_team(['WR']) Is this at all possible? I still have a lot of learning to do but really appreciate your help and assistance with this!

from pydfs_lineup_optimizer import get_optimizer, Site, Sport, CSVLineupExporter optimizer = get_optimizer(Site.DRAFTKINGS, Sport.FOOTBALL) optimizer.load_players_from_csv("Week5-2.csv") optimizer.set_positions_for_same_team(['QB','WR']) optimizer.set_min_salary_cap(49900)

fant = optimizer.get_player_by_name('Noah Fant') # Get player fant.max_exposure = 0.2 # Set exposures for player

kirk_cousins = optimizer.get_player_by_name('Kirk Cousins') # Get player kirk_cousins.max_exposure = 0.05 # Set exposures for player

optimizer.add_player_to_lineup(godwin) optimizer.add_player_to_lineup(dalvin_cook) # Lock player

ryan = optimizer.get_player_by_name('Matt Ryan') # Get player ryan.max_exposure = 0.15 # Set exposures for player

Deshaun = optimizer.get_player_by_name('Deshaun Watson') # Get player Deshaun.max_exposure = 0.20 # Set exposures for player titans = optimizer.get_player_by_name('Titans') # Get player titans.max_exposure = 0.25 # Set exposures for player

for lineup in optimizer.optimize(n=18, max_exposure=0.70): print(lineup)

exporter = CSVLineupExporter(optimizer.optimize(18)) exporter.export('Final Results.csv')

sansbacon commented 4 years ago

There is no method like the bolded line in the current library.

It seems like you are trying to generate tournament lineups. In that case, I think it makes more sense to run the optimizer over a number of different sets of projections for various stacks. That is, assuming that this stack hits the Xth percentile (say 85th) for the QB and WR, what would be the optimal lineup?

If you are trying to optimize on one set of mean projections I don't think you will generate good tournament lineups. This also solves the problem of maximum exposure. If you have multiple projection sets representing a range of outcomes, it is much less likely that a player will end up in all of your lineups.

awiden91 commented 4 years ago

Gotcha, gotcha. I appreciate all your assistance with this!

DimaKudosh commented 4 years ago

I've added new rule in new release, it's example how you can do this: optimizer.force_positions_for_opposing_team(('QB', 'WR'))