对于我的梦幻联盟,我总是在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)”
2条答案
按热度按时间agxfikkp1#
你需要在
lpSolve::lp
中构造const.mat
,你可以使用model.matrix
来实现:注意所有变量都是二进制的(0 -未选择,1 -已选择)。2已选择的玩家和总幻想点数:
mhd8tkvw2#
这就是解决办法