我正在尝试向 R 中的混合整数编程模型添加一个约束,以便每个人只被分配一个角色。
我有一个看起来像这样的数据框:
ID Name Role PreferenceScore
----- ------- --------- ------------------
1 Abby Chef 10
1 Abby Waiter 8
1 Abby Greeter 9
2 Bob Chef 7
2 Bob Waiter 8
2 Bob Greeter 3
3 Carly Chef 5
3 Carly Waiter 8
3 Carly Greeter 4
... ... ... ...
20 David Chef 2
20 David Waiter 3
20 David Greeter 8
我正在尝试使用 MIPmodel 根据每个人的偏好(数字越大越好)为每个人分配一个角色。每个角色最多可以有8人,总共有20人。
这是我到目前为止所拥有的:
library(dplyr)
library(ompr)
library(ompr.roi)
library(ROI)
library(ROI.plugin.glpk)
teamData <- read.csv("filename")
teamData$wait <- if_else(teamData$jobType == "Waiter", 1, 0)
teamData$chef <- if_else(teamData$jobType == "Chef", 1, 0)
teamData$greet <- if_else(teamData$jobType == "Greeter", 1, 0)
p <- nrow(teamData)
v <- as.numeric(teamData$PreferenceScore)
maxTeamSize <- 8
role <- teamData$Role
chef_job <- teamData$chef
waiter_job <- teamData$wait
greeter_job <- teamData$greet
name <- teamData$Name
# Build the model
model <- MIPModel() %>%
add_variable(x[i], i=1:p, type = "binary") %>%
set_objective(sum_expr(x[i] * v[i], i=1:p)) %>%
add_constraint(sum_expr(chef_job[i], i=1:p) <= 8) %>%
add_constraint(sum_expr(waiter_job[i], i=1:p) <= 8) %>%
add_constraint(sum_expr(greeter_job[i], i=1:p) <= 8) # %>%
# add_constraint(sum_expr(count(name[i])) == 1)
solved <- solve_model(model, with_ROI("glpk"))
result <- solved %>%
get_solution(x[i]) %>%
select(i) %>%
rowwise() %>%
mutate(Pref = v[i], Role = role[i], teamData$Name[i]) %>%
ungroup
result
我现在的主要问题是我无法弄清楚如何添加约束,以便每个人在解决方案中只有一个角色(即每个人只能是厨师、服务员或迎宾员)
任何指针将不胜感激。