幻想足球线性规划在R与RGLPK [英] Fantasy football linear programming in R with RGLPK

查看:313
本文介绍了幻想足球线性规划在R与RGLPK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长时间监听器第一次呼叫者SO ..
我问一个问题,以前已经问过非常相似,但我不相信我足够聪明解密如何实施的解决方案,对于这个我道歉。这里是我发现的问题的链接:
R中的约束多整数线性规划



我超过了我的投资幻想点(FPTS_PREDICT_RF),受到50,000工资帽,同时最小化一个风险计算,我已经想出了。



现在,问题出在flex位置。该团队需要由9个职位组成,
1 QB
2 RB
3 WR
1 TE
1 DEF
1 FLEX



flex可以是RB,WR或TE。

那么,我们可以得到:
1 QB
2- 3 RB
3-4 WR
1-2 TE
1 DEF



我试图实现约束#RB + #WR + #TE == 7。



以下是相关代码:

  library(Rglpk)



#变量数量
num.players < - length(final $ PLAYER)
#objective :
obj< - final $ FPTS_PREDICT_RF
#vars表示为布尔值
var.types< - rep(B,num.players)

matrix< - rbind(as.numeric(final $ position ==QB),#num QB
as.numeric(final $ position ==RB),#num RB
as.numeric(final $ position ==WR),#num WR
as.numeric(final $ position ==TE),#num TE
as.numeric(final $位置==DEF),#num DEF
diag(final $ riskNormalized),#player's risk
final $ Salary)#total cost

direction< ==,
< =,
< =,
< =,
==,
rep < =,num.players),
< =)

rhs< - c(1,#Quartbacks
3,#Running Backs
2,#Wide Receivers
1,#Tight Ends
1,#Defense
rep(10,num.players),#HERE,您需要输入一个数字, b $ b #risk你愿意,1是低风险,
#10是高风险。 10是最大。
50000)#默认情况下,您会花50K,所以请不要离开这个号码。

sol< - Rglpk_solve_LP(obj = obj,mat = matrix,dir = direction,rhs = rhs,
types = var.types,max = TRUE)
sol#投射的幻想点

有人可以帮助我实现这个约束吗?



编辑:链接到数据集final是csv格式:
https://www.dropbox.com/s/qp35wc4d380hep1/final.csv?dl=0



另一个问题:对于任何你梦幻足球运动员在那里,我计算我的风险因素直接从SD的玩家的历史幻想点,并在[0,10]的支持下规范化这个数字。

解决方案

您可以通过添加以下约束来做到这一点: / p>


  • RB数量= 2

  • RB数量<= 3

  • WR数> = 3

  • WR数<= 4

  • TE数> = 1

  • TE数量<= 2

  • RB数量+ WR + TEs == 7



以下是更新后的代码:

  b 
$ b#变量数
num.players < - length(final $ PLAYER)
#objective:
obj < - final $ FPTS_PREDICT_RF
# vars表示为布尔值
var.types< - rep(B,num.players)
#约束
矩阵< - rbind(as.numeric ==QB),#num QB
as.numeric(final $ position ==RB),#num RB
as.numeric(final $ position ==RB),# num RB
as.numeric(final $ position ==WR),#num WR
as.numeric(final $ position ==WR),#num WR
as。数字(final $ position ==TE),#num TE
as.numeric(final $ position ==TE),#num TE
as.numeric c(RB,WR,TE)),#Num RB / WR / TE
as.numeric(final $ position ==DEF),#num DEF
diag final $ riskNormalized),#player's risk
final $ Salary)#total cost
direction< - c(==,
> =,
; =,
> =,
< =,
> =,
< =,
=,
==,
rep(< =,num.players),
< =)
rhs < - c ,#Quartbacks
2,#RB Min
3,#RB Max
3,#WR Min
4,#WR Max
1,#TE Min
2,#TE Max
7,#RB / WR / TE
1,#Defense
rep(10,num.players),#HERE,您需要输入一个数字表示您愿意如何
#risk,1是低风险,
#10是高风险。 10是最大。
50000)#默认情况下,您会花50K,所以请不要离开这个号码。

sol< - Rglpk_solve_LP(obj = obj,mat = matrix,dir = direction,rhs = rhs,
types = var.types,max = TRUE)

最后,您可以通过子集 final 来评估您的解决方案:

  final [sol $ solution == 1,] 
#X PLAYER FPTS_PREDICT_LIN FPTS_PREDICT_RF工资位置
#1 1 AJ绿色20.30647 20.885558 5900 WR
#17 18 Andre Holmes 13.26369 15.460503 4100 WR
#145 156 Giovani Bernard 17.05857 19.521157 6100 RB
#148 160 Greg Olsen 17.08808 17.831687 5500 TE
#199 222 Jordy Nelson 22.12326 24.077787 7800 WR
#215 239 Kelvin Benjamin 16.12116 17.132573 5000 WR
#233 262 Le'Veon Bell 20.51564 18.565763 6300 RB
#303 340 Ryan Tannehill 17.92518 19.134305 6700 QB
#362 3641 SD 5.00000 6.388666 2600 DEF
#风险风险正规化
#1 5.131601 3.447990
#17 9.859006 6.624396
#145 9.338094 6.274388
#148 6.517376 4.379111
#199 9.651055 6.484670
#215 7.081162 4.757926
#233 6.900656 4.636641
#303 4.857983 3.264143
#362 2.309401 0.000000

对于这个问题数据,您选择了一个宽接收器到flex位置。


long time listener first time caller to S.O... I am asking a question that has been asked very similarly before, however I don't believe I am smart enough to decipher how to implement the solution, for this I apologize. Here is the link to the question I found: Constraints in R Multiple Integer Linear Programming

I am maxing over my projected fantasy points(FPTS_PREDICT_RF), subject to a 50,000 salary cap, while minimizing a 'risk' calculation that I have came up with.

Now, the problem lies in the "flex" position. The team needs to be made up of 9 positions, 1 QB 2 RB 3 WR 1 TE 1 DEF 1 FLEX

The flex can be a RB, WR, or TE.
So, we can then have: 1 QB 2-3 RB 3-4 WR 1-2 TE 1 DEF

I am trying to implement the constraint that #RB + #WR + #TE ==7.

Here is the relevant code:

library(Rglpk)



# number of variables
num.players <- length(final$PLAYER)
# objective:
obj <- final$FPTS_PREDICT_RF
# the vars are represented as booleans
var.types <- rep("B", num.players)
# the constraints
matrix <- rbind(as.numeric(final$position == "QB"), # num QB
           as.numeric(final$position == "RB"), # num RB
           as.numeric(final$position == "WR"), # num WR
           as.numeric(final$position == "TE"), # num TE
           as.numeric(final$position == "DEF"),# num DEF
           diag(final$riskNormalized),         # player's risk
           final$Salary)                       # total cost

direction <- c("==",
         "<=",
         "<=",
         "<=",
         "==",
         rep("<=", num.players),
         "<=")

rhs <- c(1, # Quartbacks
       3, # Running Backs
       2, # Wide Receivers
       1, # Tight Ends
       1, # Defense
       rep(10, num.players), #HERE, you need to enter a number that indicates how
                             #risk you are willing to be, 1 being low risk,
                             # 10 being high risk.  10 is max.
       50000)                # By default, you get 50K to spend, so leave this number alone. 

sol <- Rglpk_solve_LP(obj = obj, mat = matrix, dir = direction, rhs = rhs,
                      types = var.types, max = TRUE)
sol #Projected Fantasy Points

Can someone help me implement this constraint? Any help is much, much appreciated!

EDIT: Link to dataset 'final' is csv format: https://www.dropbox.com/s/qp35wc4d380hep1/final.csv?dl=0

SIDE QUESTION: For any of you fantasy footballers out there, I am calculating my 'risk' factor directly from the S.D. of the player's historical fantasy points, and normalizing this number over the support of [0,10]. Can you think of a better way to calculate a given players risk?

解决方案

You can do this by adding the following constraints:

  • The number of RBs >= 2
  • The number of RBs <= 3
  • The number of WRs >= 3
  • The number of WRs <= 4
  • The number of TEs >= 1
  • The number of TEs <= 2
  • The number of RBs + WRs + TEs == 7

Here's the updated code:

library(Rglpk)

# number of variables
num.players <- length(final$PLAYER)
# objective:
obj <- final$FPTS_PREDICT_RF
# the vars are represented as booleans
var.types <- rep("B", num.players)
# the constraints
matrix <- rbind(as.numeric(final$position == "QB"), # num QB
           as.numeric(final$position == "RB"), # num RB
           as.numeric(final$position == "RB"), # num RB
           as.numeric(final$position == "WR"), # num WR
           as.numeric(final$position == "WR"), # num WR
           as.numeric(final$position == "TE"), # num TE
           as.numeric(final$position == "TE"), # num TE
           as.numeric(final$position %in% c("RB", "WR", "TE")),  # Num RB/WR/TE
           as.numeric(final$position == "DEF"),# num DEF
           diag(final$riskNormalized),         # player's risk
           final$Salary)                       # total cost
direction <- c("==",
         ">=",
         "<=",
         ">=",
         "<=",
         ">=",
         "<=",
         "==",
         "==",
         rep("<=", num.players),
         "<=")
rhs <- c(1, # Quartbacks
       2, # RB Min
       3, # RB Max
       3, # WR Min
       4, # WR Max
       1, # TE Min
       2, # TE Max
       7, # RB/WR/TE
       1, # Defense
       rep(10, num.players), #HERE, you need to enter a number that indicates how
                             #risk you are willing to be, 1 being low risk,
                             # 10 being high risk.  10 is max.
       50000)                # By default, you get 50K to spend, so leave this number alone. 

sol <- Rglpk_solve_LP(obj = obj, mat = matrix, dir = direction, rhs = rhs,
                      types = var.types, max = TRUE)

Finally, you can evaluate your solution by subsetting final:

final[sol$solution==1,]
#        X          PLAYER FPTS_PREDICT_LIN FPTS_PREDICT_RF Salary position
# 1      1      A.J. Green         20.30647       20.885558   5900       WR
# 17    18    Andre Holmes         13.26369       15.460503   4100       WR
# 145  156 Giovani Bernard         17.05857       19.521157   6100       RB
# 148  160      Greg Olsen         17.08808       17.831687   5500       TE
# 199  222    Jordy Nelson         22.12326       24.077787   7800       WR
# 215  239 Kelvin Benjamin         16.12116       17.132573   5000       WR
# 233  262    Le'Veon Bell         20.51564       18.565763   6300       RB
# 303  340  Ryan Tannehill         17.92518       19.134305   6700       QB
# 362 3641              SD          5.00000        6.388666   2600      DEF
#         risk riskNormalized
# 1   5.131601       3.447990
# 17  9.859006       6.624396
# 145 9.338094       6.274388
# 148 6.517376       4.379111
# 199 9.651055       6.484670
# 215 7.081162       4.757926
# 233 6.900656       4.636641
# 303 4.857983       3.264143
# 362 2.309401       0.000000

For this problem data you have selected a wide receiver to the flex position.

这篇关于幻想足球线性规划在R与RGLPK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆