R中的幻想足球线性编程与RGLPK

时间:2014-10-21 19:44:40

标签: r constraints mathematical-optimization linear-programming

长时间听众第一次打电话到S.O ... 我问的问题之前有过非常类似的问题,但我不相信我足够聪明地破译如何实施解决方案,为此我道歉。这是我发现的问题的链接: Constraints in R Multiple Integer Linear Programming

我超出了我预计的幻想点(FPTS_PREDICT_RF),受到50,000工资上限的限制,同时最小化了我提出的“风险”计算。

现在,问题出在“弹性”位置。团队需要由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)



# 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

有人可以帮我实现这个约束吗? 非常感谢任何帮助!

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

侧面问题:对于你们中的任何一个幻想足球运动员,我正在直接从S.D.计算我的“风险”因素。玩家的历史幻想点,并在[0,10]的支持下将这个数字标准化。你能想出一个更好的方法来计算给定的球员风险吗?

1 个答案:

答案 0 :(得分:9)

您可以通过添加以下约束来完成此操作:

  • RB的数量&gt; = 2
  • RB的数量<= 3
  • WR的数量&gt; = 3
  • WR的数量&lt; = 4
  • TE的数量> = 1
  • TE的数量<= 2
  • RB的数量+ WRs + TEs == 7

这是更新后的代码:

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)

最后,您可以通过对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

对于此问题数据,您已选择宽接收器到弹性位置。