带lpSolve的R团队花名册优化 [英] R Team Roster Optimization w/ lpSolve

查看:124
本文介绍了带lpSolve的R团队花名册优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是R的新手,有一个我想解决的特殊的幻想运动队优化问题.我已经看到其他帖子使用lpSolve来解决类似问题,但是我似乎无法绕过代码.下面的数据表示例.每个球员都在一个团队中,扮演特定的角色,有薪水,并且每场比赛产生平均得分.我需要的限制是我需要8个球员.任何一支球队最多只能有3名球员.每个角色(至少5个)中必须至少有一个玩家.而且累计薪水不得超过10,000美元.

I am new to R and have a particular fantasy sports team optimization problem I would like to solve. I have seen other posts use lpSolve for similar problems but I can not seem to wrap my head around the code. Example data table below. Every player is on a team, plays a particular role, has a salary, and has avg points produced per game. The constraints that I need are I need exactly 8 players. No more than 3 players may come from any one team. There must be at least one player for each role (of 5). And cumulative salary must not exceed $10,000.

Team    Player   Role      Avgpts    Salary
Bears   A        T         22        930
Bears   B        M         19        900
Bears   C        B         30        1300
Bears   D        J         25        970
Bears   E        S         20        910
Jets    F        T         21        920
Jets    G        M         26        980
[...]   

在R中,我写以下内容

> obj = DF$AVGPTS
> con = rbind(t(model.matrix(~ Role + 0, DF)), rep(1,nrow(DF)), DF$Salary)
> dir = c(">=",">=",">=",">=",">=","==","<=")
> rhs = c(1,1,1,1,1,8,10000)
> result = lp("max", obj, con, dir, rhs, all.bin = TRUE)

此代码在产生最佳幻想团队 时效果很好,任何一支团队最多只能有3名球员.这是我遇到的问题,我怀疑它与con参数有关.任何帮助表示赞赏.

This code works fine in producing the optimal fantasy team without the limitation of no more than 3 players may come from any one team. This is where I am stuck and I suspect it relates to the con argument. Any help is appreciated.

推荐答案

如果您添加了与在con中扮演角色的方式类似的方法,该怎么办?

What if you added something similar to the way you did the roles to con?

如果添加t(model.matrix(~ Team + 0, DF)),您将在约束中具有每个团队的指标.对于您给出的示例:

If you add t(model.matrix(~ Team + 0, DF)) you'll have indicators for each team in your constraint. For the example you gave:

> con <- rbind(t(model.matrix(~ Role + 0,DF)), t(model.matrix(~ Team + 0, DF)), rep(1,nrow(DF)), DF$Salary)
> con
            1   2    3   4   5   6   7
RoleB       0   0    1   0   0   0   0
RoleJ       0   0    0   1   0   0   0
RoleM       0   1    0   0   0   0   1
RoleS       0   0    0   0   1   0   0
RoleT       1   0    0   0   0   1   0
TeamBears   1   1    1   1   1   0   0
TeamJets    0   0    0   0   0   1   1
            1   1    1   1   1   1   1
          930 900 1300 970 910 920 980

我们现在需要更新dirrhs来解决此问题:

We now need to update dir and rhs to account for this:

dir <- c(">=",">=",">=",">=",">=",rep('<=',n_teams),"<=","<=")
rhs <- c(1,1,1,1,1,rep(3,n_teams),8,10000)

在适当设置n_teams的情况下.

With n_teams set appropriately.

这篇关于带lpSolve的R团队花名册优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆