I am trying to use lpSolve to assign students to groups. Each student has ranked their interested in the group from first (most interest) to third (least interest). Students are listed as rows and their preferences as columns:
desires <- matrix(c(1,2,3,1,2,3,3,1,2,3,2,1,1,3,2),ncol=3,byrow=T)
If I use the following script:
nr <- nrow(desires)
nc <- ncol(desires)
columns <- t(sapply(1:nc,function(x) rep(c(0,1,0),c(nr*(x-1),nr,nr*(nc-x)))))
rows <- t(sapply(1:nr, function(x) rep(rep(c(0, 1, 0), c(x-1, 1, nr-x)), nc)))
mod <- lp("min", as.vector(desires), rbind(columns, rows), ">=", rep(1, nr+nc), binary.vec=rep(TRUE, nr*nc))
I get the following result:
matrix(mod$solution,ncol=nc)
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 1 0 0
[3,] 0 1 0
[4,] 0 0 1
[5,] 1 0 0
My problem is that group 1, column 1, has three members while the other groups only have one. How can I constrain the optimization so that each group as 1-2 members and each student is assigned to only one group? Thanks.