-
Notifications
You must be signed in to change notification settings - Fork 2
/
Lineup Optimizer.Rmd
278 lines (190 loc) · 6.18 KB
/
Lineup Optimizer.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
---
title: "Lineup Optimizer"
author: "Jason Lee"
date: "6/2/2018"
output: html_document
<<<<<<< Updated upstream
=======
>>>>>>> Stashed changes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Daily Fantasy Football
If you are like me and love fantasy football but aren't always positive which lineup is the opitmal lineup to use in a given week - Then this optimizer is for you!
***
#### Math
***
We are going to solve:
maximize f'x subject to A*x with constraints equal to b where:
* **x:** is the variable to solve for: a vector of 0 or 1:
+ 1 when the player is selected, 0 otherwise
* **f:** is your objective vector
* **A:** is a matrix
* **dir:** a vector of "<=", "==", or ">="
* **b:** a vector defining your linear constraints.
***
```{r load, message=FALSE, warning=FALSE}
# load library
library(Rglpk)
```
## Load Data
We need to get the weekly fantasy football prices and projected scores.
Here we have data from Yahoo's Daily fantasy tournments.
```{r data}
QB <- read.csv("Data/QBs-Table 1.csv",header=TRUE,stringsAsFactors=FALSE)
RB <- read.csv("Data/RBs-Table 1.csv",header=TRUE,stringsAsFactors=FALSE)
WR <- read.csv("Data/WRs-Table 1.csv",header=TRUE,stringsAsFactors=FALSE)
TE <- read.csv("Data/TEs-Table 1.csv",header=TRUE,stringsAsFactors=FALSE)
DEF <- read.csv("Data/DEFS-Table 1.csv",header=TRUE,stringsAsFactors=FALSE)
```
## Append and Combine Data
We need to tag each of the players with their position.
We also need to combine all the players into a single dataset.
```{r combine}
QB$pos <- rep("QB")
RB$pos <- rep("RB")
WR$pos <- rep("WR")
TE$pos <- rep("TE")
DEF$pos <- rep("DEF")
ALL <- rbind(QB, RB, WR, TE, DEF) # combind into one data.frame
```
#### View the data
To give you an idea of what data set you would need to input when you are customizing and updating the equation for your current week.
Here's what my dataset looks like to start
```{r}
head(ALL)
```
As you can see it is very simple. We only need 4 key elements to run the algorithm:
* **Player Name** - this could be a player name or playerID
* **Position** - Needs to be either QB, RB, WR, TE, or DEF
* **Cost** - how much the player costs for the given week
* **Projected Points** - you can choose whatever site or projections you want
+ I will have another project posted showing you how to create your own projections
## Set contraints
```{r setup1}
# count of all the players
num.players <- length(ALL)
# objective:
f <- ALL$projPts
# the variables are all booleans
var.types <- rep("B", num.players)
# the constraints
A <- rbind(as.numeric(ALL$pos == "QB"), # num QB
as.numeric(ALL$pos == "RB"), # num RB
as.numeric(ALL$pos == "WR"), # num WR
as.numeric(ALL$pos == "TE"), # num TE
as.numeric(ALL$pos == "DEF"), # num DEF
ALL$cost) # total cost
dir <- c("==",
"==",
"==",
"==",
"==",
"<=") # this is for the total team salary, which is why it is less than or equal
```
## Customize Constraints
Here is the part that needs to be adjusted depending on how you want your lineup to be and what the total salary limit is for the team.
* **QB:** It's normal to have only one QB but hey if there's a daily league with more you can change this.
* **RB:** There is usually 2 required plus a possible flex position.
+ If you want that flex position to be a RB then this needs to be at 3.
+ If not then set it to 2.
* **WR:** There is usually 3 required plus a possible flex position.
+ If you want that flex position to be a RB then this needs to be at 4.
+ If not then set it to 3.
* **TE:** There is usually 1 required plus a possible flex position.
+ If you want that flex position to be a RB then this needs to be at 2.
+ If not then set it to 1.
* **DEF:** There is usually one 1 defense required.
* **Salary:** The salary limit could be very different depending on the league.
+ Yahoo is $200
+ Draft Kings is $60,000
+ Fan Duel is $50,000
***
## 3 Examples
I will run the algorithm 3 different ways.
1. The flex position be an extra RB
2. The flex position be an extra WR
3. The flex position be an extra TE
***
## 3 Running Back Lineup
```{r numPlayers}
b <- c(1, # QB
3, # RB
3, # WR
1, # TE
1, # DEF
200) # cost
```
#### Results
The first thing to check is that the status = 0. If it does not then it did not find an optimal solution.
```{r Math}
# Solve the math problem
sol.3rb <- Rglpk_solve_LP(obj = f, mat = A, dir = dir, rhs = b,
types = var.types, max = TRUE)
# Check that Status = 0 and that there is an optimum value
sol.3rb
# View the selected players
ALL$name[sol.3rb$solution == 1]
```
##### These guys are your best possible lineup with 3 Running Backs
***
## 4 Wide Receivers Lineup
```{r WR}
b1 <- c(1, # QB
2, # RB
4, # WR
1, # TE
1, # DEF
200) # cost
```
#### Results
```{r 4Wr}
# Solve the math problem
sol.4wr <- Rglpk_solve_LP(obj = f, mat = A, dir = dir, rhs = b1,
types = var.types, max = TRUE)
# Check that Status = 0 and that there is an optimum value
sol.4wr
# View the selected players
ALL$name[sol.4wr$solution == 1]
```
##### These guys are your best possible lineup with 4 Wide Receivers.
***
## 2 Tight Ends Lineup
```{r TE}
b2 <- c(1, # QB
2, # RB
3, # WR
2, # TE
1, # DEF
200) # cost
```
#### Results
```{r 2TE}
# Solve the math problem
sol.2te <- Rglpk_solve_LP(obj = f, mat = A, dir = dir, rhs = b2,
types = var.types, max = TRUE)
# Check that Status = 0 and that there is an optimum value
sol.2te
# View the selected players
ALL$name[sol.2te$solution == 1]
```
##### These guys are your best possible lineup with 2 Tight Ends.
***
## Best Lineups
```{r Best}
# 3 RB lineup
sol.3rb$optimum
ALL$name[sol.3rb$solution == 1]
# 4 WR lineup
sol.4wr$optimum
ALL$name[sol.4wr$solution == 1]
# 2 TE lineup
sol.2te$optimum
ALL$name[sol.2te$solution == 1]
```
***
Obviously these lineups are from a few years ago but you can change the inputs and try to compete against me next season!
***
## Good Luck
***