2016-08-17 1 views
1
나는 previous question

R 팀 명단 제약 - 나는 적어도 3 플레이어에서가 너무 최적화하고 싶습니다 팀

의 기반을 구축을 위해 노력하는 데 문제

에서 적어도 X 플레이어를 선택해야합니다 같은 팀이지만 어떤 팀이든 상관하지 않습니다.

아래 코드에서 나는 Bears (또는 내가 지정한 다른 팀)에서 3 명의 플레이어를 선택하도록 강요 할 수 있습니다. 같은 팀, 어떤 팀의 3 명의 선수와 함께 최적의 명단을 선택하는 방법은 무엇입니까?

library(Rglpk) 
DF <- data.frame(Team=c(rep("Bears",5), rep("Jets",5), rep("49ers", 5)), Player=c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O"), Role=c(rep(c("WR", "RB", "TE"),5)), Avgpts=c(22, 19, 30, 25, 20, 21, 26, 14, 21, 13, 11, 8, 4, 3, 5), Salary=c(930, 900, 1300, 970, 910, 920, 980, 720, 650, 589, 111, 1239, 145, 560, 780)) 
obj = DF$Avgpts 
con = rbind(as.numeric(DF$Role=="WR"), as.numeric(DF$Role=="RB"), as.numeric(DF$Role=="TE"), as.numeric(DF$Team == "Bears"), DF$Salary) 
dir = c("==","==","==","==","<=") 
rhs = c(1,1,1,3,100000) 
sol <- Rglpk_solve_LP(obj = obj 
       , mat = con 
       , dir = dir 
       , rhs = rhs 
       , types = rep("B", length(DF$Team)) 
       , max=TRUE) 

solution <- DF[sol$solution==1,] 
+0

일반적으로 링크가있는 것이 좋지만 코드는 독자적으로 실행해야합니다. 이는'DF'를 만들고'library (lpSolve) '를 호출하는 것을 의미합니다. – Frank

+1

죄송합니다. 예제를 변경하여 실행합니다. 현재 Bears에서 3 명의 선수를 선택하기 위해 하드 코딩 된 조건이 있지만, 어떤 팀이든 상관없이 같은 시간에 최고의 3 명의 선수를 선택하고 싶습니다. – spantz

답변

0

용어의 일부가 잘못 표시되면 용서해주세요.하지만 여기에 나와있는 해결책이 있습니다. 각 플레이어는 기둥으로 취급되며 각 팀마다 기둥이 있습니다. 한 팀에 원하는 선수의 최소 수와 동일한 각 팀 = 팀마다 더미 변수를 넣었습니다.

library("lpSolveAPI") 
DF <- data.frame(Team=c(rep("Bears",5), rep("Jets",5), rep("49ers", 5)), Player=c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O"), Role=c(rep(c("WR", "RB", "TE"),5)), Avgpts=c(22, 19, 30, 25, 20, 21, 26, 14, 21, 13, 11, 8, 4, 3, 5), Salary=c(930, 900, 1300, 970, 910, 920, 980, 720, 650, 589, 111, 1239, 145, 560, 780)) 

ncol <- nrow(DF) # of players in DF 
nteams <- length(unique(DF$Team)) 
teams <- unique(DF$Team) 

lp_rowpicker <- make.lp(ncol=(ncol+nteams)) 

obj_vals <- DF[, "Avgpts"] 
set.objfn(lp_rowpicker, c(obj_vals, rep(0, nteams))) #dummy 0s for team variable 
lp.control(lp_rowpicker,sense='max') 
set.type(lp_rowpicker, columns=1:(ncol+nteams), type = "binary") 
add.constraint(lp_rowpicker, xt=c(DF$Salary, rep(0, nteams)), type="<=", rhs=35000) 
add.constraint(lp_rowpicker, xt=c(as.numeric(DF$Role=="WR"), rep(0, nteams)), type="=", rhs=1) 
add.constraint(lp_rowpicker, xt=c(as.numeric(DF$Role=="RB"), rep(0, nteams)), type="=", rhs=1) 
add.constraint(lp_rowpicker, xt=c(as.numeric(DF$Role=="TE"), rep(0, nteams)), type="=", rhs=1) 

나는 다음 하나로 설정 팀 열 수 내가 최적의 솔루션에서 원하는 팀의 총 수를 뺀 팀의 수와 동일한 제약 조건을 설정합니다. 이 경우 데이터 프레임에서 3 팀 중 1 팀을 찾고 있기 때문에 2 팀이 1로 설정되고 0으로 설정된 팀은 행의 최소 제한 조건을 충족시키기 위해 최소 3 명의 플레이어가 필요합니다. 수평.

#3 players total 
add.constraint(lp_rowpicker, xt=c(rep(1, ncol), rep(0, nteams)), type="=", rhs=3) 

# add a constraint that every team must have between 3 and 6 players. 
# put a dummy value of 3 in for each team 
# if the flag for the team column is 0 then 3 players must be selected (each with a value of 1 in that team's column. 
for (i in 1:nteams){ 
    team <- teams[i] 
    add.constraint(lp_rowpicker, lhs=3, xt=c(as.numeric(DF$Team==team), rep(0, i-1), 3, rep(0, nteams-i)), type="<=", rhs=7) 
} 

# one team will not have the dummy value in the team column, forcing at  least 3 players picked from the same team to meet the lhs of the above constraint 
add.constraint(lp_rowpicker, xt=c(rep(0, ncol), rep(1, nteams)), type="=", rhs=(nteams-1)) 

solve(lp_rowpicker) 
get.objective(lp_rowpicker) 
soln <- get.variables(lp_rowpicker)>0 
solution <- DF[soln[0:ncol],] 
print(solution[order(solution$Team),])