Der*_*son 6 r mathematical-optimization solver maximize integer-programming
我是R的新手,我正在尝试为Excel中的Solver编写一个脚本.在下面的数据中,我列出了工作类型为AE的工人.每个工人都有工资和生产率.我想要做的是找到10个工人的最大产量,累计工资<100,000.限制是我需要总共10个工人,我需要2个来自工作类型AD,1个来自E,以及1个任何类型.
我用optim,IpSolve等搜索并搜索了一种方法,但是由于我的知识有限,我没有太多运气.
谢谢您的帮助!
Name Pos Salary Producton
Joe A 12001 13.1
Jim A 17753 23.5
Jill A 11447 14.8
Brian A 11447 14.8
Sally B 2171 1.2
Nancy B 4537 2.1
Francis B 2840 1.8
Ace B 2840 1.8
Bill C 3818 1.6
Ted C 11447 0.1
Henry C 2000 1.1
Kyle C 3818 1.6
Sam D 11447 0.1
Trevor D 2000 1.1
John D 4317 11.7
Jerome D 2000 1.1
Rebecca E 3818 1.6
Sunny E 11447 0.1
Britt E 2000 1.1
Sara E 4317 11.7
Run Code Online (Sandbox Code Playgroud)
使用lp在lpSolve包来解决潜在的整数规划问题.前5个约束分别是A,B,C,D和E职位的数量,第6个是要选择的人员数量,第7个是总工资.假设DF问题中显示的数据框试试这个:
library(lpSolve)
obj <- DF$Prod
con <- rbind(t(model.matrix(~ Pos + 0, DF)), rep(1, nrow(DF)), DF$Salary)
dir <- c(">=", ">=", ">=", ">=", ">=", "==", "<")
rhs <- c(2, 2, 2, 2, 1, 10, 100000)
result <- lp("max", obj, con, dir, rhs, all.bin = TRUE)
Run Code Online (Sandbox Code Playgroud)
这使:
> result
Success: the objective function is 84.7
> DF[result$solution == 1, ]
Name Pos Salary Producton
2 Jim A 17753 23.5
3 Jill A 11447 14.8
4 Brian A 11447 14.8
6 Nancy B 4537 2.1
8 Ace B 2840 1.8
9 Bill C 3818 1.6
12 Kyle C 3818 1.6
14 Trevor D 2000 1.1
15 John D 4317 11.7
20 Sara E 4317 11.7
Run Code Online (Sandbox Code Playgroud)
请注意,生产在问题中拼写错误,或者可能是错误的.
添加:
关于第二个最佳解决方案,想法是添加一个约束,使最佳解决方案不可行,但不排除其他可能的解决方案:
con2 <- rbind(con, result$solution)
dir2 <- c(dir, "<=")
rhs2 <- c(rhs, 9)
result2 <- lp("max", obj, con2, dir2, rhs2, all.bin = TRUE)
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我们得到以下具有与最佳解决方案相同的最佳目标值,因此它将同样好:
> result2
Success: the objective function is 84.7
> DF[result2$solution == 1, ]
Name Pos Salary Producton
2 Jim A 17753 23.5
3 Jill A 11447 14.8
4 Brian A 11447 14.8
6 Nancy B 4537 2.1
8 Ace B 2840 1.8
9 Bill C 3818 1.6
12 Kyle C 3818 1.6
15 John D 4317 11.7
16 Jerome D 2000 1.1
20 Sara E 4317 11.7
Run Code Online (Sandbox Code Playgroud)
还有一些论据lp允许它直接生成多种解决方案; 但是,帮助文件提到了一些错误,采取上述方法可能更安全.