R优化找到最佳梦幻足球阵容

bd1hkmkf  于 2023-01-28  发布在  其他
关注(0)|答案(2)|浏览(108)

对于我的梦幻联盟,我总是在excel中手动完成,但我想用R编码,这样我就可以每年更容易地完成。
考虑到我的联赛特定的名册要求1 QB,2 RB,2 WR,1 TE,我应该起草谁,以最大限度地提高点列(预计幻想点得分),限制需要上述职位和花费200美元或更少的价值(价值列)?
我通常在excel solver中这样做,告诉我选谁,然后我试着选那些球员,如果我选不出(例如,如果一个球员出价太高,我负担不起),我会重新运行solver,告诉我下一个最佳策略。
下面是我的df格式的数据:

> dput(players)
structure(list(Player = c("John1", "John2", "John3", "John4", 
"John5", "John6", "John7", "John8", "John9", "John10", "John11", 
"John12", "John13", "John14", "John15", "John16"), Position = c("QB", 
"QB", "QB", "QB", "RB", "RB", "RB", "RB", "WR", "WR", "WR", "WR", 
"TE", "TE", "TE", "TE"), FantasyPoints = c(10, 8, 6, 4, 20, 15, 
10, 5, 30, 20, 10, 5, 50, 30, 20, 10), DraftValue = c(15, 10, 
8, 2, 50, 30, 25, 20, 40, 30, 20, 10, 50, 35, 20, 5)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -16L))

这是我目前的代码:

# Create objective function to maximize fantasy points
obj <- players$FantasyPoints

# Create constraints for number of players at each position
qb_constraint <- players$Position == "QB"
rb_constraint <- players$Position == "RB"
wr_constraint <- players$Position == "WR"
te_constraint <- players$Position == "TE"

# Create constraint for total draft budget
budget_constraint <- players$DraftValue <= 200

# Run linear programming problem
lp_model <- lp("max", obj, qb_constraint, "=", 1, rb_constraint, "=", 2, wr_constraint, "=", 2, te_constraint, "=", 1, budget_constraint, "<=", 200)

# Print optimal solution
print(players[lp_model$solution == 1, ])

但我在运行LP模型时遇到一个错误:“lp(“最大值”,对象j,qb_约束,“=",1,rb_约束,“=",2,:未使用的参数(200)”

agxfikkp

agxfikkp1#

你需要在lpSolve::lp中构造const.mat,你可以使用model.matrix来实现:

player_types <- c(QB = 1, RB = 2, WR = 2, TE = 1)
max_cost <- 200

res <- lp(
  direction = "max",
  objective.in = players$FantasyPoints,
  const.mat = rbind(
    t(model.matrix(~Position - 1, players))[paste0("Position", names(player_types)),],
    players$DraftValue
  ),
  const.dir = c(rep("==", length(player_types)), "<="),
  const.rhs = c(player_types, max_cost),
  all.bin = TRUE
)

注意所有变量都是二进制的(0 -未选择,1 -已选择)。2已选择的玩家和总幻想点数:

chosen_players <- players$Player[res$solution == 1]
chosen_points <- res$objval
mhd8tkvw

mhd8tkvw2#

这就是解决办法

# Define the objective function (maximize fantasy points)
obj <- players$FantasyPoints

# Prompt user to input number of QBs
num_qb <- as.numeric(readline("Enter the number of QBs: "))
num_rb <- as.numeric(readline("Enter the number of RBs: "))
num_wr <- as.numeric(readline("Enter the number of WRs: "))
num_te <- as.numeric(readline("Enter the number of TEs: "))
num_value <- as.numeric(readline("Enter your draft budget: "))
num_players <- as.numeric(readline("Adding in your flex spots, enter the total number of starters: "))

# Create a new column indicating the player's position
players$QB <- ifelse(players$Position == "QB", 1, 0)
players$RB <- ifelse(players$Position == "RB", 1, 0)
players$WR <- ifelse(players$Position == "WR", 1, 0)
players$TE <- ifelse(players$Position == "TE", 1, 0)
players$Total <- 1

# Define the constraints (position limits and draft value limit)
con <- matrix(c(
  # QB constraint
  players$QB,
  # RB constraint
  players$RB,
  # WR constraint
  players$WR,
  # TE constraint
  players$TE,
  # Draft value constraint
  players$DraftValue,
  #Total players constraint
  players$Total
), ncol = nrow(players), byrow = TRUE)

# Define the variables for the lp
dir <- c("<=", rep(">=",3),"<=","<=")
rhs <- c(num_qb, num_rb, num_wr, num_te, num_value, num_players)

# Solve the linear program
result <- lp("max", obj, con, dir, rhs, all.bin = TRUE)

# Print the optimal team
print(players[result$solution == 1,])

相关问题