利用R语言寻找最优项目团队
我刚刚开始学习R,遇到了一些我不确定如何在代码中处理的问题 我正在创建一个data.frame,其中包含可分配给项目的人员池。该项目需要一名BA、一名PM、两名SA、和一名可以是SA或BA的额外人员。每个人都有一个评级和与之相关的成本,我需要最大评级,同时将成本保持在某个阈值以下 我不确定如何实现上述场景中的粗体部分。。下面的代码正在运行,但未说明额外的BA/SA (这是自学..没有布置作业) 编辑所需输出,其中最后一行可以是SA或BA位置。利用R语言寻找最优项目团队,r,mathematical-optimization,lpsolve,R,Mathematical Optimization,Lpsolve,我刚刚开始学习R,遇到了一些我不确定如何在代码中处理的问题 我正在创建一个data.frame,其中包含可分配给项目的人员池。该项目需要一名BA、一名PM、两名SA、和一名可以是SA或BA的额外人员。每个人都有一个评级和与之相关的成本,我需要最大评级,同时将成本保持在某个阈值以下 我不确定如何实现上述场景中的粗体部分。。下面的代码正在运行,但未说明额外的BA/SA (这是自学..没有布置作业) 编辑所需输出,其中最后一行可以是SA或BA位置。 name position rating c
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
代码:
#加载库
图书馆(lpSolve)
#创建data.frame
name=c(“史蒂夫”、“杰里米”、“马特”、“艾伦”、“斯蒂芬妮”、“莫莉”、“杰克”、“托尼”、“杰伊”、“凯蒂”、“艾莉森”)
职位=c(“BA”、“PM”、“SA”、“BA”、“SA”、“PM”、“SA”、“SA”、“SA”、“PM”、“BA”、“SA”)
评级=c(75,90,95,85,95,88,74,81,55,65,68)
成本=成本(5000、8000、9500、4700、9200、5500、5300、7300、3300、4100、4400)
df=数据帧(名称、位置、等级、成本)
#设置限制
num_ba=1
num_pm=1
num_sa=2
最大成本=35000
#创建要按位置约束的向量
df$BA=ifelse(df$position==“BA”,1,0)
df$PM=ifelse(df$position==“PM”,1,0)
df$SA=ifelse(df$position==“SA”,1,0)
#要优化的向量
目标=df$评级
#约束方向
const_dir如果我答对了问题,这可能会起作用:
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), ])
然而,将此解决方案的评级相加得到438,而op结果是437,因此这应该是正确的
嗯 您想要什么作为输出并不十分清楚。你能说明你想要的最终目标是什么吗?增加了想要的输出。。希望这能澄清我的意图。看看拉姆在mmm的回答,我现在知道你可能可以避免一些限制:你实际上只需要四个:PM=1,BA>=1,SA>=2和BA+SA==4。我稍后会重新格式化Answare。谢谢,这很有意义!我还将致力于离线优化约束以供练习。很高兴能提供帮助-这是一个改进且更简单的版本。
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), ])
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