热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

R与RGLPK的幻想足球线性编程-FantasyfootballlinearprogramminginRwithRGLPK

longtimelistenerfirsttimecallertoS.OIamaskingaquestionthathasbeenaskedverysimil

long time listener first time caller to S.O... I am asking a question that has been asked very similarly before, however I don't believe I am smart enough to decipher how to implement the solution, for this I apologize. Here is the link to the question I found: Constraints in R Multiple Integer Linear Programming

很长一段时间的听众第一次打电话给S.O ...我问的问题之前有过非常相似的问题,但是我不相信我足够聪明地破译如何实施解决方案,为此我道歉。以下是我发现的问题的链接:R多重整数线性规划中的约束

I am maxing over my projected fantasy points(FPTS_PREDICT_RF), subject to a 50,000 salary cap, while minimizing a 'risk' calculation that I have came up with.

我的预计幻想点数(FPTS_PREDICT_RF)最高,受制于50,000工资上限,同时最大限度地减少了我提出的“风险”计算。

Now, the problem lies in the "flex" position. The team needs to be made up of 9 positions, 1 QB 2 RB 3 WR 1 TE 1 DEF 1 FLEX

现在,问题出在“弹性”位置。该团队需要由9个职位组成,1个QB 2 RB 3 WR 1 TE 1 DEF 1 FLEX

The flex can be a RB, WR, or TE.
So, we can then have: 1 QB 2-3 RB 3-4 WR 1-2 TE 1 DEF

flex可以是RB,WR或TE。那么,我们可以得到:1 QB 2-3 RB 3-4 WR 1-2 TE 1 DEF

I am trying to implement the constraint that #RB + #WR + #TE ==7.

我正在尝试实现#RB + #WR + #TE == 7的约束。

Here is the relevant code:

这是相关的代码:

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

Can someone help me implement this constraint? Any help is much, much appreciated!

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

EDIT: Link to dataset 'final' is csv format: https://www.dropbox.com/s/qp35wc4d380hep1/final.csv?dl=0

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

SIDE QUESTION: For any of you fantasy footballers out there, I am calculating my 'risk' factor directly from the S.D. of the player's historical fantasy points, and normalizing this number over the support of [0,10]. Can you think of a better way to calculate a given players risk?

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

1 个解决方案

#1


8  

You can do this by adding the following constraints:

您可以通过添加以下约束来完成此操作:

  • The number of RBs >= 2
  • RB的数量> = 2

  • The number of RBs <= 3
  • RB的数量<= 3

  • The number of WRs >= 3
  • WR的数量> = 3

  • The number of WRs <= 4
  • WR的数量<= 4

  • The number of TEs >= 1
  • TE的数量> = 1

  • The number of TEs <= 2
  • TE的数量<= 2

  • The number of RBs + WRs + TEs == 7
  • RB的数量+ WRs + TEs == 7

Here's the updated code:

这是更新的代码:

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)

Finally, you can evaluate your solution by subsetting final:

最后,您可以通过子集化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

For this problem data you have selected a wide receiver to the flex position.

对于此问题数据,您已选择宽接收器到弹性位置。


推荐阅读
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 本文讨论了在openwrt-17.01版本中,mt7628设备上初始化启动时eth0的mac地址总是随机生成的问题。每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下,而openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址再生成eth0.1、eth0.2等,生成后的mac地址会保存在/etc/config/network下。 ... [详细]
  • 本文介绍了Python爬虫技术基础篇面向对象高级编程(中)中的多重继承概念。通过继承,子类可以扩展父类的功能。文章以动物类层次的设计为例,讨论了按照不同分类方式设计类层次的复杂性和多重继承的优势。最后给出了哺乳动物和鸟类的设计示例,以及能跑、能飞、宠物类和非宠物类的增加对类数量的影响。 ... [详细]
  • IOS开发之短信发送与拨打电话的方法详解
    本文详细介绍了在IOS开发中实现短信发送和拨打电话的两种方式,一种是使用系统底层发送,虽然无法自定义短信内容和返回原应用,但是简单方便;另一种是使用第三方框架发送,需要导入MessageUI头文件,并遵守MFMessageComposeViewControllerDelegate协议,可以实现自定义短信内容和返回原应用的功能。 ... [详细]
author-avatar
XC一米_623
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有