dfs-with-r / coach

Lineup optimization for daily fantasy sports
https://dfs-with-r.github.io/coach/
GNU General Public License v3.0
47 stars 13 forks source link

Dynamic "locks" variable #28

Closed meteoman2210 closed 4 years ago

meteoman2210 commented 4 years ago

hi @zamorarr...Love your site!

I'm trying to get a little sophisticated (it's not working) with your code.

Say I've created two locks that want to loop through:

c(68,67,69,26) 68 | Ben Roethlisberger 67 | Chase Claypool 69 | Diontae Johnson 26 | Ezekiel Elliott

c(8,9,3,28) 8 | Matt Ryan 9 | Russell Gage 3 | Hayden Hurst 28 | Noah Fant

Here's my problem...

If i explicitly assign the locks, no problem everything works ok.

Lineups<-optimize_generic(data, model, L = 20, stack_sizes = c(3,2), randomness = randomness,min_salary = 48500, max_exposure = 0.25, locks = c(68,67,69,26) )

However as I mentioned I want to loop on several locks.

So I set up a csv file "stacks.csv" c(68,67,69,26) c(8,9,3,28)

Read the files in...

stacks <- read.csv("stacks.csv")

Now I try to optimize assigning the locks to the first stack like...

Lineups<-optimize_generic(data, model, L = 20, stack_sizes = c(3,2), randomness = randomness,min_salary = 48500, max_exposure = 0.25, locks = stacks[1,] )

I get this error...

Error: The number of different indexes is 0 for this constraint

Is there a way to loop the "locks" variable?

Thank you.

Ray

zamorarr commented 4 years ago

Hi @meteoman2210

Thanks for using the package! It's a little hard to tell without seeing an example of stacks.csv. But my first instinct is that stacks[1,] isn't returning a vector but a list, since it's the first row of the data frame. Can you try using locks = unlist(stacks[1,]) is the function call instead?

If that doesn't work feel free to attach stacks.csv (or at least a version with a couple of sample rows) and I'll try and reproduce the error on my end.

meteoman2210 commented 4 years ago

Robert,

Thank you for the quick response!

I tried your suggestion and it didn't work.

I've attached the following for your consideration:

testCode.R - some test code for you to try data.csv - my data file Stacks.csv - my locks file

Thank you.

Ray

On Thu, Nov 12, 2020 at 5:36 AM Robert Zamora notifications@github.com wrote:

Hi @meteoman2210 https://github.com/meteoman2210

Thanks for using the package! It's a little hard to tell without seeing an example of stacks.csv. But my first instinct is that stacks[1,] isn't returning a vector but a list, since it's the first row of the data frame. Can you try using locks = unlist(stacks[1,]) is the function call instead?

If that doesn't work feel free to attach stacks.csv (or at least a version with a couple of sample rows) and I'll try and reproduce the error on my end.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/dfs-with-r/coach/issues/28#issuecomment-725993739, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADTS3BHB2FPSLZCVJ23C3ODSPO3CLANCNFSM4TSP4YNQ .

zamorarr commented 4 years ago

Hey Ray, could you try uploading those files again? I don't see them attached here. Unless I'm missing something.

meteoman2210 commented 4 years ago

Robert,

Here you go.

Ray

On Fri, Nov 13, 2020 at 10:41 AM Robert Zamora notifications@github.com wrote:

Hey Ray, could you try uploading those files again? I don't see them attached here. Unless I'm missing something.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/dfs-with-r/coach/issues/28#issuecomment-726834895, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADTS3BDNU2HOXHVRYSNTW2TSPVHQ3ANCNFSM4TSP4YNQ .

meteoman2210 commented 4 years ago

Here you go again.

meteoman2210 commented 4 years ago

Robert,

Here's the data.

Test code

`constraints <- list( "QB" = 1, "RB" = 2, "WR" = 3, "TE" = 1,

"RB/WR/TE" = 1,

"WR/TE" = 1,
"DST" = 1

)

Stacks <- read.csv("Stacks.csv") data <- read.csv("data.csv") data$player_id <- as.character(data$player_id) numStacks <- nrow(Stacks) randomness <- function(x) x+x*runif(n, min = -.05, max = .05) model <- model_generic(data, total_salary = 50000, roster_size = 9, max_from_team = 4) model <- add_generic_positions_constraint(model, data, constraints)

for(k in 1:numStacks){

Lineups <- optimize_generic(data, model, L = nLineups, stack_sizes = c(3,2),
                                                        randomness = randomness,min_salary = 48500, max_exposure = 0.25,locks = c(68,67,69,26))

} `

Stacks.csv

Stacks c(68,67,69,26), c(8,9,3,28), c(59,57,51,60), c(38,33,35,42)

Data.csv row_id,player_id,player,team,opp_team,location,position,salary,fpts_avg,fpts_proj 1,15713493,Chase Edmonds,ARI,MIA,ARI,RB,6800,11.97,20.5 2,15714190,Cardinals,ARI,MIA,ARI,DST,2900,7,8.1 3,15714011,Hayden Hurst,ATL,DEN,ATL,TE,4100,10.36,16.8 4,15713701,Julio Jones,ATL,DEN,ATL,WR,7200,19.57,34.3 5,15714196,Falcons,ATL,DEN,ATL,DST,2500,2.75,6.2 6,15713503,Todd Gurley,ATL,DEN,ATL,RB,6200,15.6,25.4 7,15713705,Calvin Ridley,ATL,DEN,ATL,WR,7000,20.01,34.4 8,15713434,Matt Ryan,ATL,DEN,ATL,QB,6400,20.54,32.3 9,15713779,Russell Gage,ATL,DEN,ATL,WR,4500,9.82,20.2 10,15713791,Willie Snead,BAL,IND,IND,WR,4100,7.21,16.5 11,15714183,Ravens,BAL,IND,IND,DST,3500,11.43,9.2 12,15713539,Gus Edwards,BAL,IND,IND,RB,4800,6.07,12.7 13,15713535,J.K. Dobbins,BAL,IND,IND,RB,4900,8.84,14.9 14,15713699,Stefon Diggs,BUF,SEA,BUF,WR,7400,18.81,28.9 15,15713429,Josh Allen,BUF,SEA,BUF,QB,7000,25.07,36 16,15714199,Bills,BUF,SEA,BUF,DST,2400,4.75,8.7 17,15713483,Christian McCaffrey,CAR,KC,KC,RB,8500,26.65,28.9 18,15713437,Teddy Bridgewater,CAR,KC,KC,QB,6000,17.92,25.3 19,15713723,Robby Anderson,CAR,KC,KC,WR,6300,17.01,24.7 20,15714201,Panthers,CAR,KC,KC,DST,2100,3.75,6.9 21,15713737,DJ Moore,CAR,KC,KC,WR,5600,14.52,23.5 22,15714186,Bears,CHI,TEN,TEN,DST,3200,6.25,6.5 23,15713709,Allen Robinson,CHI,TEN,TEN,WR,6900,17.12,28.5 24,15713513,David Montgomery,CHI,TEN,TEN,RB,5700,13.15,19.9 25,15714202,Cowboys,DAL,PIT,DAL,DST,2000,2.5,7.7 26,15713497,Ezekiel Elliott,DAL,PIT,DAL,RB,6600,17.76,26.4 27,15714194,Broncos,DEN,ATL,ATL,DST,2700,5.71,6.3 28,15714001,Noah Fant,DEN,ATL,ATL,TE,4600,12.23,17.8 29,15713521,Phillip Lindsay,DEN,ATL,ATL,RB,5200,10.28,15.4 30,15713527,D'Andre Swift,DET,MIN,MIN,RB,5000,13.14,26.2 31,15714200,Lions,DET,MIN,MIN,DST,2300,2.14,6.8 32,15714187,Texans,HOU,JAX,JAX,DST,3100,2.57,7.6 33,15713739,Brandin Cooks,HOU,JAX,JAX,WR,5500,13.1,28.9 34,15713713,Will Fuller,HOU,JAX,JAX,WR,6800,17,26.8 35,15714009,Darren Fells,HOU,JAX,JAX,TE,4200,8.75,17.8 36,15713777,Randall Cobb,HOU,JAX,JAX,WR,4500,11.31,18.2 37,15713515,David Johnson,HOU,JAX,JAX,RB,5600,13.47,18.8 38,15713428,Deshaun Watson,HOU,JAX,JAX,QB,7100,24.5,33.1 39,15714185,Colts,IND,BAL,IND,DST,3300,11.71,9.5 40,15714023,Trey Burton,IND,BAL,IND,TE,3500,10.98,20.3 41,15713507,Jonathan Taylor,IND,BAL,IND,RB,6000,13.57,20.5 42,15713489,James Robinson,JAX,HOU,JAX,RB,7000,20.09,30.9 43,15713747,DJ Chark,JAX,HOU,JAX,WR,5200,12.18,25.5 44,15714197,Jaguars,JAX,HOU,JAX,DST,2500,2.57,6.1 45,15713793,Keelan Cole,JAX,HOU,JAX,WR,4000,12.37,21.4 46,15713993,Travis Kelce,KC,CAR,KC,TE,7200,18.75,27.4 47,15714182,Chiefs,KC,CAR,KC,DST,3700,9.25,9.4 48,15713763,Mecole Hardman,KC,CAR,KC,WR,4800,9.79,20 49,15713821,Demarcus Robinson,KC,CAR,KC,WR,3200,6.09,13.8 50,15714013,Hunter Henry,LAC,LV,LAC,TE,4000,9.63,13.7 51,15713529,Justin Jackson,LAC,LV,LAC,RB,4900,9.32,16.8 52,15714188,Chargers,LAC,LV,LAC,DST,3000,4.86,8.1 53,15713431,Justin Herbert,LAC,LV,LAC,QB,6800,27,38 54,15713707,Keenan Allen,LAC,LV,LAC,WR,7000,18.69,31 55,15713751,Mike Williams,LAC,LV,LAC,WR,5100,11.72,27.3 56,15713789,Hunter Renfrow,LV,LAC,LAC,WR,4100,9.7,17.8 57,15713761,Henry Ruggs,LV,LAC,LAC,WR,4900,8.64,19.2 58,15713501,Josh Jacobs,LV,LAC,LAC,RB,6300,16.34,31.2 59,15713995,Darren Waller,LV,LAC,LAC,TE,5800,14.61,27.6 60,15713439,Derek Carr,LV,LAC,LAC,QB,5700,19.85,26.8 61,15713787,Preston Williams,MIA,ARI,ARI,WR,4100,8.83,19.6 62,15714192,Dolphins,MIA,ARI,ARI,DST,2800,9.86,8.3 63,15713931,Jakeem Grant,MIA,ARI,ARI,WR,3000,5.26,8.7 64,15713765,Sterling Shepard,NYG,WAS,WAS,WR,4800,12.38,17.9 65,15714193,Giants,NYG,WAS,WAS,DST,2700,6.25,7.4 66,15713753,Darius Slayton,NYG,WAS,WAS,WR,5000,12.9,26.8 67,15713733,Chase Claypool,PIT,DAL,DAL,WR,5700,15.23,37.9 68,15713433,Ben Roethlisberger,PIT,DAL,DAL,QB,6500,18.1,22.3 69,15713755,Diontae Johnson,PIT,DAL,DAL,WR,5000,11.03,26.2 70,15713491,James Conner,PIT,DAL,DAL,RB,6900,17.04,26.3 71,15713839,James Washington,PIT,DAL,DAL,WR,3000,6.79,14.8 72,15714003,Eric Ebron,PIT,DAL,DAL,TE,4500,9.04,15.3 73,15713725,JuJu Smith-Schuster,PIT,DAL,DAL,WR,6200,13.37,22.6 74,15714181,Steelers,PIT,DAL,DAL,DST,4900,10.57,9.2 75,15713697,DK Metcalf,SEA,BUF,BUF,WR,7800,22,38.4 76,15713833,David Moore,SEA,BUF,BUF,WR,3100,8.99,17.2 77,15714191,Seahawks,SEA,BUF,BUF,DST,2800,5.57,6.5 78,15713711,Tyler Lockett,SEA,BUF,BUF,WR,6800,22.07,48.5 79,15713427,Russell Wilson,SEA,BUF,BUF,QB,7600,32.01,38.7 80,15714015,Jonnu Smith,TEN,CHI,TEN,TE,3900,11.31,22.3 81,15714189,Titans,TEN,CHI,TEN,DST,3000,3.71,7.3 82,15713487,Derrick Henry,TEN,CHI,TEN,RB,7900,22.23,38.5 83,15714184,WAS Football Team,WAS,NYG,WAS,DST,3400,6.71,8.7 84,15713719,Terry McLaurin,WAS,NYG,WAS,WR,6500,17,26.9 85,15713859,Isaiah Wright,WAS,NYG,WAS,WR,3000,5.45,7.4 86,15714019,Logan Thomas,WAS,NYG,WAS,TE,3700,8.54,15.2 87,15713444,Kyle Allen,WAS,NYG,WAS,QB,5300,14.84,18.7 88,15713533,DeeJay Dallas,SEA,BUF,BUF,RB,5000,19.3,8 89,15714031,Greg Olsen,SEA,BUF,BUF,TE,3100,11.5,8 90,15713425,Patrick Mahomes,KC,CAR,KC,QB,8100,40.7,41

zamorarr commented 4 years ago

Hey Ray,

So I think the problem is with stacks.csv. At least with the file your provided, I couldn't read in the file correctly. Everything else worked fine. I've attached some working code where I specified the locks you gave me directly in the code as a list. You should be able to run this and get results. Let me know if it doesn't work.

Do you have a lot of locks to iterate over? If not, you can just continue to specify them in the code as I have. Otherwise I can work on another version where we specify the locks in a different file.

library(coach)

# define the constraints
constraints <- list(
  "QB" = 1,
  "RB" = 2,
  "WR" = 3,
  "TE" = 1,
  # "RB/WR/TE" = 1,
  "WR/TE" = 1,
  "DST" = 1
)

# specify the locks. I couldn't read the csv file you provided
# so I just entered them directly in the code. 
locks <- list(
  c(68,67,69,26),
  c(8,9,3,28),
  c(59,57,51,60),
  c(38,33,35,42)
)

# read in the data
data <- read.csv("data.csv", stringsAsFactors = FALSE)
data$player_id <- as.character(data$player_id)
n <- nrow(data)

# set parameters
numLineups <- 10
numLocks <- length(locks)
randomness <- function(x) x+x*runif(n, min = -.05, max = .05)

# build model
model <- model_generic(data, total_salary = 50000, roster_size = 9, max_from_team = 4)
model <- add_generic_positions_constraint(model, data, constraints)

# generate lineups
Lineups <- vector("list", numLocks)
for(k in 1:numLocks) {
  Lineups[[k]] <- optimize_generic(
    data, model, L = numLineups, stack_sizes = c(3,2),
    randomness = randomness, min_salary = 48500, max_exposure = 0.25, locks = locks[[k]])

}

Lineups
meteoman2210 commented 4 years ago

Robert thank you very much. Much appreciated. Ray