【问题标题】:Fantasy football linear programming in R with RGLPK使用 RGLPK 在 R 中进行梦幻足球线性规划
【发布时间】:2014-10-21 19:44:40
【问题描述】:

长时间的听众第一次打电话给 S.O... 我问了一个以前非常相似的问题,但是我不相信我足够聪明来破译如何实施解决方案,为此我深表歉意。这是我发现的问题的链接: Constraints in R Multiple Integer Linear Programming

我正在最大限度地提高我预计的幻想点数 (FPTS_PREDICT_RF),但工资上限为 50,000,同时将我提出的“风险”计算降至最低。

现在,问题在于“弹性”位置。团队需要由9个位置组成, 1个四分卫 2RB 3 写 1个TE 1 防御 1 弹性

flex 可以是 RB、WR 或 TE。
所以,我们可以有: 1个四分卫 2-3 RB 3-4 写 1-2 TE 1 防御

我正在尝试实现 #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

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

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

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

【问题讨论】:

  • 请发布final,以便您的示例可重现。我认为您需要做的就是为#RB &gt;= 2#RB &lt;= 3#WR &gt;= 3#WR &lt;= 4#TE &gt;= 1#TE &lt;= 2#RB+#WR+#TE == 7 添加单独的约束。
  • 另一件事——你的最后一个问题更适合 stats.stackexchange.com。
  • 我已经包含了 csv 的链接。再次感谢您的快速回复,对不起,我对这个论坛很天真,下次我会在统计数据上发布这样的问题。然而,我对如何实现该约束感到困惑。我能够用笔和纸在物理上写出一个拉格朗日最大问题,但是在将其翻译成这段代码时我一无所知。再次,非常感谢!
  • 你是个好人查理布朗。
  • @alpha 保管箱链接现在已损坏;你还在某处有那个excel文件的副本吗?很乐意将其上传到其他地方并为您编辑问题。我是 Twitter 上的@andrewgjohnson

标签: r constraints mathematical-optimization linear-programming


【解决方案1】:

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

  • RB 数量 >= 2
  • RB 数量
  • WR 数 >= 3
  • WR 的数量
  • TE 的数量 >= 1
  • TE 数量
  • RBs + 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

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

【讨论】:

  • 知道如何强制一名球员进入阵容吗?我可以看到如何通过在模型矩阵中将它们归零来删除它们,但不确定如何强制包含。
  • @tcash21 您只需将行final$PLAYER == "Player Name" 绑定到matrix,将"=" 添加到direction,并将1 添加到rhs
  • 太棒了,谢谢!这与使用lpSolve 的方式类似吗?
  • @tcash21 是的,这两个包的输入几乎相同。
猜你喜欢
  • 2017-04-22
  • 2017-08-16
  • 2011-11-27
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-07
  • 2016-07-13
相关资源
最近更新 更多