저는 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), ])
출력으로 갖고 싶은 것이 분명하지 않습니다. 원하는 최종 목표가 무엇인지 보여 주실 수 있습니까? – gented
에 원하는 출력이 추가되었습니다. 내 의도를 지우는 희망. – M3SSYM4RV1N
Ram의 답변을 참조하십시오. http://stackoverflow.com/questions/19250787/either-or-constraints-in-lpsolveapi –