[英]R lpsolve get 2nd,3rd,Nth best solution
I am trying to solve an optimization problem using lpsolveapi in R.我正在尝试在 R 中使用 lpsolveapi 解决优化问题。 I am trying to get not only the best solution, but also get the next best (through nth) solutions.我不仅要获得最佳解决方案,还要获得次佳(通过第 n 个)解决方案。 The application is for fantasy football where you have a set number of players of varying size at 5 different positions, with an additional constraint of total salary that cannot be exceeded.该应用程序适用于梦幻足球,您在 5 个不同的位置拥有一定数量的不同体型的球员,并有一个不能超过的总薪水限制。 I have found a solution here , but am having trouble adapting to my problem.我在这里找到了解决方案,但无法适应我的问题。
I assume that I need to add a constraint with the value from get.objective(lp_model)
.我假设我需要使用来自get.objective(lp_model)
的值添加一个约束。 I tried doing that, but I do not think I have set it up correctly as I am only getting 1 solution still.我试过这样做,但我认为我没有正确设置它,因为我仍然只得到 1 个解决方案。
Ideally, I am looking for a solution that looks like this (with the appropriate number of players):理想情况下,我正在寻找一个看起来像这样的解决方案(具有适当数量的玩家):
> sols
[[1]]
[1] 0 1 0 1 0 0 0 0 0 0 1 0 1 0 1 1 0 1 0 1 1
[[2]]
[1] 1 1 0 1 1 0 0 0 0 0 0 0 1 1 1 0 1 1 0 0 0
Here is my code so far:到目前为止,这是我的代码:
library(lpSolveAPI)
Dataset <- matrix(c("DET","SF","NO","D.Prescott","A.Smith","C.Newton","L.Miller","C.McCaffrey","A.Kamara","D.Freeman","C.Hyde","C.Thompson","D.Murray","D.Bryant","D.Baldwin","A.Green","T.Hill","J.Landry","M.Crabtree","L.Fitzgerald","G.Tate","A.Seferian-Jenkins","V.Davis","D.Walker","J.Cook",1,1,1,2,2,2,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,3000,3200,3400,6700,6500,6300,6200,6500,6300,6600,5200,6100,5400,6400,6900,7400,6700,6300,6500,5900,6300,4900,4100,4900,4000,7.679411765,7.309444444,7.061764706,20.66725263,19.58133684,18.78134737,16.08178947,14.93878947,14.605,14.02315789,13.93152632,13.92768421,12.94,15.66052632,15.38147368,14.85521053,14.60457895,14.56222222,14.44427778,14.23257895,14.16161111,11.09,10.95147368,10.52473684,9.581222222), ncol=4)
colnames(Dataset) <- c('Name','Position2','Salary','Pts')
Dataset <- data.frame(Dataset)
Dataset$Position2 <- as.numeric(Dataset$Position2)
Dataset$Salary <- as.numeric(Dataset$Salary)
Dataset$Pts <- as.numeric(Dataset$Pts)
lp_model= make.lp(0, nrow(Dataset))
set.objfn(lp_model, Dataset$Pts)
add.constraint(lp_model, rep(1,nrow(Dataset)), "=", 9)
add.constraint(lp_model, as.vector(Dataset$Salary), "<=", 50000)
add.constraint(lp_model, Dataset$Position2==1, "=", 1)
add.constraint(lp_model, Dataset$Position2==2, "=", 1)
add.constraint(lp_model, Dataset$Position2==3, ">=", 2)
add.constraint(lp_model, Dataset$Position2==3, "<=", 3)
add.constraint(lp_model, Dataset$Position2==4, ">=", 3)
add.constraint(lp_model, Dataset$Position2==4, "<=", 4)
add.constraint(lp_model, Dataset$Position2==5, ">=", 1)
add.constraint(lp_model, Dataset$Position2==5, "<=", 2)
lp.control(lp_model, sense= "max")
set.type(lp_model, 1:nrow(Dataset), "binary")
lp_model
solve(lp_model)
get.variables(lp_model)
get.objective(lp_model)
get.constr.value((lp_model))
get.total.iter(lp_model)
get.solutioncount(lp_model)
rc<-solve(lp_model)
sols<-list()
obj0<-get.objective(lp_model)
counter = 0
# find more solutions
while(TRUE) {
counts <- matrix(obj0, ncol=nrow(Dataset))
counter = counter+1
sol <- round(get.variables(lp_model))
sols <- c(sols,list(sol))
add.constraint(lp_model,counts,"<",obj0)
rc<-solve(lp_model)
obj0<-get.objective(lp_model)
if (rc!=0) break;
if (counter == 5) break;
}
sols
I found a solution based on playing around with it.我找到了一个基于玩弄它的解决方案。 I switched back to the original answer from the above link and added a counter.我从上面的链接切换回原来的答案并添加了一个计数器。 Here is the code:这是代码:
rc<-solve(lp_model)
sols<-list()
obj0<-get.objective(lp_model)
while(TRUE) {
counter = counter+1
sol <- round(get.variables(lp_model))
sols <- c(sols,list(sol))
add.constraint(lp_model,2*sol-1,"<=", sum(sol)-1)
rc<-solve(lp_model)
#if (rc!=0) break;
#if (get.objective(lp_model)<obj0-1e-6) break;
if (counter == 5) break;
}
sols
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.