简体   繁体   中英

R lpsolve get 2nd,3rd,Nth best solution

I am trying to solve an optimization problem using lpsolveapi in R. I am trying to get not only the best solution, but also get the next best (through nth) solutions. 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. 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) . I tried doing that, but I do not think I have set it up correctly as I am only getting 1 solution still.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM