2016-10-11 7 views
1

저는 R을 배우기 시작했고 코드에서 어떻게 처리해야할지 모르겠습니다.R을 사용하여 최적의 프로젝트 팀 찾기 R

프로젝트에 할당 할 수있는 개인 풀과 함께 data.frame을 만듭니다. 이 프로젝트에는 BA 1 개, PM 1 개, SA 2 개, 이 필요하고 추가 직원은 SA 또는 BA 일 수 있습니다. 각 사람은 그들과 관련된 평가와 비용을 가지고 있습니다. 비용을 일정 기준 이하로 유지하면서 최대 등급이 필요합니다.

위의 시나리오에서 굵게 표시된 부분을 파악하는 방법이 확실하지 않습니다. 아래 코드는 작동하지만 추가 BA/SA를 고려하지 않습니다.

(이 자체 연구이다 .. 할당되지 숙제)

마지막 행은 SA BA 또는 위치 중 하나 일 수있다-EDIT 원하는 출력 .

name  position rating cost BA PM SA 
Matt  SA  95 9500 0 0 1  
Aaron  BA  85 4700 1 0 0  
Stephanie SA  95 9200 0 0 1  
Molly  PM  88 5500 0 1 0  
Jake  SA  74 5300 0 0 1 

코드 : 제가 질문을 잘 가지고있는 경우

#load libraries 
library(lpSolve) 

# create data.frame 
name = c("Steve", "Jeremy", "Matt", "Aaron", "Stephanie", "Molly", "Jake", "Tony", "Jay", "Katy", "Alison") 
position = c("BA", "PM", "SA", "BA", "SA", "PM", "SA", "SA", "PM", "BA", "SA") 
rating = c(75, 90, 95, 85, 95, 88, 74, 81, 55, 65, 68) 
cost = c(5000, 8000, 9500, 4700, 9200, 5500, 5300, 7300, 3300, 4100, 4400) 
df = data.frame(name, position, rating, cost) 

# create restrictions 
num_ba = 1 
num_pm = 1 
num_sa = 2 
max_cost = 35000 

# create vectors to constrain by position 
df$BA = ifelse(df$position == "BA", 1, 0) 
df$PM = ifelse(df$position == "PM", 1, 0) 
df$SA = ifelse(df$position == "SA", 1, 0) 

# vector to optimize against 
objective = df$rating 

# constraint directions 
const_dir <- c("=", "=", "=", "<=") 

# matrix 
const_mat = matrix(c(df$BA, df$PM, df$SA, df$cost), 4, byrow=TRUE) 
const_rhs = c(num_ba, num_pm, num_sa, max_cost) 

#solve 
x = lp("max", objective, const_mat, const_dir, const_rhs, all.bin=TRUE, all.int=TRUE) 
print(df[which(x$solution==1), ]) 
+0

출력으로 갖고 싶은 것이 분명하지 않습니다. 원하는 최종 목표가 무엇인지 보여 주실 수 있습니까? – gented

+0

에 원하는 출력이 추가되었습니다. 내 의도를 지우는 희망. – M3SSYM4RV1N

+1

Ram의 답변을 참조하십시오. http://stackoverflow.com/questions/19250787/either-or-constraints-in-lpsolveapi –

답변

1

,이 일할 수 :

library(lpSolve) 

# create data.frame 
name = c("Steve", "Jeremy", "Matt", "Aaron", "Stephanie", "Molly", "Jake", "Tony", "Jay", "Katy", "Alison") 
position = c("BA", "PM", "SA", "BA", "SA", "PM", "SA", "SA", "PM", "BA", "SA") 
rating = c(75, 90, 95, 85, 95, 88, 74, 81, 55, 65, 68) 
cost = c(5000, 8000, 9500, 4700, 9200, 5500, 5300, 7300, 3300, 4100, 4400) 

df = data.frame(name, position, rating, cost) 

# create restrictions 
num_pm = 1 
min_num_ba = 1 
min_num_sa = 2 
tot_saba = 4 
max_cost = 35000 

# create vectors to constrain by position 
df$PM = ifelse(df$position == "PM", 1, 0) 
df$minBA = ifelse(df$position == "BA", 1, 0) 
df$minSA = ifelse(df$position == "SA", 1, 0) 
df$SABA = ifelse(df$position %in% c("SA","BA"), 1, 0) 

# vector to optimize against 
objective = df$rating 

# constraint directions 
const_dir <- c("==", ">=", "<=", "==", "<=") 

# matrix 
const_mat = matrix(c(df$PM, df$minBA, df$minSA, df$SABA, df$cost), 5, byrow=TRUE) 
const_rhs = c(num_pm, min_num_ba,min_num_sa, tot_saba, max_cost) 

#solve 
x = lp("max", objective, const_mat, const_dir, const_rhs, all.bin=TRUE, all.int=TRUE) 
print(df[which(x$solution==1), ]) 

제가 몇 가지 제약 조건을 수정하고 새를 추가하고 있어요 : BA 수는> = 1이어야합니다. SA> = 2, 의 수 BA와 SA의 합은 이어야합니다. 따라서은 4가되어야합니다. 5 명을 선택하십시오.

이 그러나 영업 이익으로 쓴 것보다 다른 솔루션 제공 : 연산 결과가 437 동안

 name position rating cost PM minBA minSA SABA 
1  Steve  BA  75 5000 0  1  0 1 
3  Matt  SA  95 9500 0  0  1 1 
4  Aaron  BA  85 4700 0  1  0 1 
5 Stephanie  SA  95 9200 0  0  1 1 
6  Molly  PM  88 5500 1  0  0 0 

그러나,이 솔루션의 평가를 합산하면, (438)를 제공을, 그래서 이것은 올바른합니다.

HTH.

+0

음, 이제 아마 몇 가지 제약 사항을 피할 수있을 것입니다. 실제로는 4 개만 필요합니다. PM = 1, BA> = 1, SA> = 2, BA + SA == 4. 나중에 답안을 다시 작성하겠습니다. – lbusett

+0

감사합니다.이 말이 맞습니다! 또한 실습을 위해 제약 조건을 오프라인으로 최적화하는 방법에 대해서도 설명합니다. – M3SSYM4RV1N

+0

도와 줘서 기쁩니다 - 개선되고 단순한 버전입니다. – lbusett