多重整数线性规划中的约束 [英] Constraints in R Multiple Integer Linear Programming

查看:274
本文介绍了多重整数线性规划中的约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一些代码在R优化我的幻想足球阵容,但我有一些约束的困难。



名单必须包括:



1 QB
2 RB
2 WR
1 TE
1 FLEX(RB,WR或TE)
$ 200以下的总成本



我的问题是,我的代码想选择FLEX位置作为已经选择为WR,RB或TE的播放器。这里是我使用的代码,我有一个表,我导入与播放器,位置,点和成本的列。在表中,任何RB,WR或TE都与位置复制为FLEX。我试图改变设置pos ==FLEX的行到pos ==WR|| pos ==RB|| pos ==TE,那没有工作,我唯一的其他想法是运行代码,如果它复制FLEX播放器,我从源表中删除它。这是一个痛苦,虽然。



任何想法都非常感激。

  name<  -  mydata $ name 
pos< - mydata $ pos
pts< - mydata $ pts
cost< - mydata $ cost

num.players< - length (名称)

f< - pts

var.types< - rep(B,num.players)

A& - rbind(as.numeric(pos ==QB)
,as.numeric(pos ==RB)
,as.numeric(pos ==WR)
,as.numeric(pos ==TE)
,as.numeric(pos ==FLEX)
,cost)

dir < ==
,==
,==
,==
,==
,&

b < - c(1
,2
,2
,1
,1
,200)
b $ b library(Rglpk)

sol< - Rglpk_solve_LP(obj = f
,mat = A
,dir = dir
,rhs = b
,types = var.types
,max = TRUE)
sol

name [sol $ solution == 1]

解决方案

您可以重写:

  1 QB 2 RB 2 WR 1 TE 1 FLEX(RB,WR或TE)

As

  num(QB)== 1 
2 <= num ; = 3
2 <= num(WR)<= 3
1 num(RB)+ num(WR) (TE)== 6


I am working on some code in R to optimize my fantasy football lineup but I am having some difficulty with one constraint. I basically have a list of players, their position, expected points, and cost.

Roster must include:

1 QB 2 RB 2 WR 1 TE 1 FLEX (either a RB, WR, or TE) Total cost under $200

My issue is that my code wants to pick the FLEX position as player it already selected as a WR, RB or TE. Here is the code I am using, I have a table that I imported with columns for player, position, points, and cost. In the table, any RB, WR, or TE is duplicated with the position as FLEX. I tried to change the line that sets pos=="FLEX" to pos=="WR"||pos=="RB"||pos=="TE" and that did not work, my only other idea is to run the code and if it duplicates the FLEX player I delete it from the source table. That is a bit of a pain though.

Any ideas are greatly appreciated.

name <- mydata$name
pos <- mydata$pos
pts <- mydata$pts
cost <- mydata$cost

num.players <- length(name)

f <- pts

var.types <- rep("B", num.players)

A <- rbind(as.numeric(pos=="QB")
         , as.numeric(pos=="RB")
         , as.numeric(pos=="WR")
         , as.numeric(pos=="TE")
         , as.numeric(pos=="FLEX")
         ,cost)

dir <- c("=="
        ,"=="
        ,"=="
        ,"=="
        ,"=="
        ,"<=")

b <- c(1
     , 2
     , 2
     , 1
     , 1
     , 200)

library(Rglpk)

sol <- Rglpk_solve_LP(obj = f
                , mat = A
                , dir = dir
                , rhs = b
                , types = var.types
                , max=TRUE)
sol

name[sol$solution == 1]

解决方案

You can rewrite:

1 QB 2 RB 2 WR 1 TE 1 FLEX (either a RB, WR, or TE) 

As

num(QB) == 1
2 <= num(RB) <= 3
2 <= num(WR) <= 3
1 <= num(TE) <= 2
num(RB) + num(WR) + num(TE) == 6

这篇关于多重整数线性规划中的约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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