我尝试使用 R 包 LPSolve 尤其是 lp.transport 函数来解决优化问题。在下面的虚构示例中,我有 5 个办公地点,我需要为最少数量的员工提供资源,并且我已经建立了一个成本矩阵来确定每个员工家到办公室的距离。我想尽量减少上班的总距离,同时满足每个办公室的最低员工人数。
最初,这是可行的,因为我平等对待所有员工 (1)。然而,当我根据每个员工的效率来评价他们时,问题就开始出现了。例如,我现在想说 officeX 需要相当于 2 名工程师,这可能由 4 名效率为 50% 的工程师或 1 名效率为 200% 的工程师组成。但是,当我这样做时,发现的解决方案会将员工分散到多个办公室,我需要的是一个额外的约束,因此强制员工只能在 1 个办公室。无论如何希望这是足够的背景,这是我的例子:
Employee <- c("Jim","John","Jonah","James","Jeremy","Jorge")
Office1 <- c(2.58321505105556, 5.13811249390279, 2.75943834864996,
6.73543614029559, 6.23080251653027, 9.00620341764497)
Office2 <- c(24.1757667923894, 19.9990724784926, 24.3538456922105,
27.9532073293925, 26.3310994833106, 14.6856664813007)
Office3 <- c(38.6957155251069, 37.9074293509861, 38.8271000719858,
40.3882569566947, 42.6658938732098, 34.2011184027657)
Office4 <- c(28.8754359274453, 30.396841941228, 28.9595182970988,
29.2042274337124, 33.3933900645023, 28.6340025144932)
Office5 <- c(49.8854888720157, 51.9164328512659, 49.948290261029,
49.4793138594302, 54.4908258333456, 50.1487397648236)
#create CostMatrix
costMat<-data.frame(Employee,Office1, Office2, Office3, Office4, Office5)
#efficiency is the worth of employees, eg if 1 they are working at 100%
#so if for example I wanted 5 Employees
#working in a office then I could choose 5 at 100% or 10 working at 50% etc...
efficiency<-c(0.8416298, 0.8207991, 0.7129663, 1.1406839, 1.3868177, 1.1989748)
#Uncomment next line to see the working version based on headcount
#efficiency<-c(1,1,1,1,1,1)
#Minimum is the minimum number of Employees we want in each office
minimum<-c(1, 1, 2, 1, 1)
#solve problem
opSol <-lp.transport(cost.mat = as.matrix(costMat[,-1]),
direction = "min",
col.signs = rep(">=",length(minimum)),
col.rhs = minimum,
row.signs = rep("==", length(efficiency)),
row.rhs = efficiency,
integers=NULL)
#view solution
opSol$solution
# My issue is one employee is being spread across multiple areas,
#what I really want is a extra constraint that says that in a row there
# can only be 1 non 0 value.