I have an optimised time allocation lp code in R using LpSolverAPI. The code works fine with the given constraints which are :
The time allotted per job must be met The time a worker works cannot exceed the paid work time
But, i need to add a "priority" while the time is allocated to each job. Meaning, there are jobs that need to be completed first/ must be given first priority while allocating workers.
The code i have is below.
The time must be allotted to job4, job2, job3 and finally job1 (as per priority_matrix) If no time is workers are free then lower priority jobs can remain un allocated.
With the below code (without priority) no time is allocated to job 4.
library(lpSolveAPI)
jobs <- c(1,1,1,1)
workermax <- c(8,6,9)
jobmax <- c(6,6,5,6)
priority_matrix <- as.matrix(c(4,2,3,1),ncol = 1, bycol = T) ##priority of each job
rownames(priority_matrix) <- c("Job1","Job2","Job3","Job4")
scheduler_input2 <- matrix(c(8, 0, 0, 0,6,0,8,0,9,0,6,0), nrow=4 , byrow =T)
obj.vals <- scheduler_input2
colnames(obj.vals) <- c("worker1","worker2","worker3")
rownames(obj.vals) <- c("Job1","Job2","Job3","Job4")
nworkers <- ncol(scheduler_input2)
njobs <- nrow(scheduler_input2) #4
ncol = nworkers*njobs
distribution<- make.lp(ncol=ncol)
set.type(distribution, columns=1:ncol, type = c("integer"))
set.objfn(distribution, obj.vals)
lp.control(distribution,sense='max')
#cosntraint1
time_per_job <- function (job_index) {
skill_cols <- (0:(nworkers-1))*njobs + job_index
add.constraint(distribution, rep(1,nworkers), indices=skill_cols,type="<=", rhs=jobmax[job_index])
}
lapply(1:njobs, time_per_job)
#cosntraint2
max_hrs_by_worker <- function (DA_index) {
DA_cols <- (DA_index-1)*njobs + (1:njobs) #relevant columns for a given room
add.constraint(distribution, xt=jobs, indices=DA_cols,type="<=",rhs=workermax[DA_index])
}
lapply(1:nworkers, max_hrs_by_worker)
solve(distribution)
get.objective(distribution)
distribution<-matrix(get.variables(distribution), njobs,nworkers)
Thanks in advance.