3

考虑到不同的限制条件,我正在尝试选择最好的梦幻足球队。我的目标是挑选能够最大化他们的投影总和的球员

约束是:

1) 团队必须包括:

-1四分卫

-2 RB

-2 WR

-1 TE

2) 玩家的风险不得超过 6

3) 玩家费用之和不得超过300。

我怎样才能做到这一点?R 中优化这些约束的最佳包/功能是什么?给定这些约束,最大化投影点的函数调用会是什么样子?仅供参考,我将搜索 100-300 名玩家。

提前致谢!这是一个小的示例数据集:

name <- c("Aaron Rodgers","Tom Brady","Arian Foster","Ray Rice","LeSean McCoy","Calvin Johnson","Larry Fitzgerald","Wes Welker","Rob Gronkowski","Jimmy Graham")

pos <- c("QB","QB","RB","RB","RB","WR","WR","WR","TE","TE")

pts <- c(167, 136, 195, 174, 144, 135, 89, 81, 114, 111) 

risk <- c(2.9, 3.4, 0.7, 1.1, 3.5, 5.0, 6.7, 4.7, 3.7, 8.8) 

cost <- c(60, 47, 63, 62, 40, 60, 50, 35, 40, 40) 

mydata <- data.frame(name, pos, pts, risk, cost) 
4

1 回答 1

8

你的约束和目标是线性的,但你的变量是二元的:每个玩家是否应该被选中。所以你的问题比线性规划(LP)更普遍,它是混合整数规划(MIP)。在 CRAN 的优化任务视图中,查找他们的 MIP 部分。

CPLEX 是您可能无法访问的商业求解器,但 GLPK 是免费的。如果我是你,我可能会使用高级接口Rglpk

它需要你把你的问题以矩阵的形式,我建议你看一下文档和例子。


编辑:这是一个实现:

# We are going to solve:
# maximize f'x subject to A*x <dir> 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, b a vector, and <dir> a vector of "<=", "==", or ">=",
#   defining your linear constraints.

# number of variables
num.players <- length(name)
# objective:
f <- pts
# the variable are booleans
var.types <- rep("B", num.players)
# the constraints
A <- rbind(as.numeric(pos == "QB"), # num QB
           as.numeric(pos == "RB"), # num RB
           as.numeric(pos == "WR"), # num WR
           as.numeric(pos == "TE"), # num TE
           diag(risk),              # player's risk
           cost)                    # total cost

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

b <- c(1,
       2,
       2,
       1,
       rep(6, num.players),
       300)

library(Rglpk)
sol <- Rglpk_solve_LP(obj = f, mat = A, dir = dir, rhs = b,
                      types = var.types, max = TRUE)
sol
# $optimum
# [1] 836                      ### <- the optimal total points

# $solution
#  [1] 1 0 1 0 1 1 0 1 1 0     ### <- a `1` for the selected players

# $status
# [1] 0                        ### <- an optimal solution has been found

# your dream team
name[sol$solution == 1]
# [1] "Aaron Rodgers"  "Arian Foster"   "LeSean McCoy"
# [4] "Calvin Johnson" "Wes Welker"     "Rob Gronkowski
于 2013-03-01T00:18:06.630 回答