import library

library(lpSolveAPI)

define the data sets

A new lpSolve linear program model object with m constraints and n decision variables can be created using the make.lp function. For example, the following command creates an lpSolve linear program model object with 3 constraints and 2 decision variables.

create an LP model with 3 constraints and 2 decision variables

 my.lp <- make.lp(7, 7)
#每周连休2天
work<-data.frame(weekday1=c(0,0,1,1,1,1,1), weekday2=c(1,0,0,1,1,1,1),weekday3=c(1,1,0,0,1,1,1),weekday4=c(1,1,1,0,0,1,1),weekday5=c(1,1,1,1,0,0,1),weekday6=c(1,1,1,1,1,0,0),weekday7=c(0,1,1,1,1,1,0),week_need=c(4,8,8,7,7,6,5))
#每周休1天
#work<-data.frame(weekday1=c(0,1,1,1,1,1,1), weekday2=c(1,0,1,1,1,1,1),weekday3=c(1,1,0,1,1,1,1),weekday4=c(1,1,1,0,1,1,1),weekday5=c(1,1,1,1,0,1,1),weekday6=c(1,1,1,1,1,0,1),weekday7=c(1,1,1,1,1,1,0),week_need=c(4,8,8,7,7,6,5))
for (i in 1:7){
  
  set.column(my.lp, i, work[,i])
}
set.constr.type(my.lp, rep(">=", 7))
set.objfn(my.lp, c(1,1,1,1,1,1,1))
RowNames <- c("Sunday", "Monday", "Tuesday","Wednesday", "Thursday", "Friday","Saturday")
ColNames <- c("team_Sunday", "team_Monday", "team_Tuesday","team_Wednesday", "team_Thursday", "team_Friday","team_Saturday")
dimnames(my.lp) <- list(RowNames, ColNames)
set.rhs(my.lp, work[,8])
set.type(my.lp, c(1:7), "integer")
lp.control(my.lp,sense='min')
## $anti.degen
## [1] "fixedvars" "stalling" 
## 
## $basis.crash
## [1] "none"
## 
## $bb.depthlimit
## [1] -50
## 
## $bb.floorfirst
## [1] "automatic"
## 
## $bb.rule
## [1] "pseudononint" "greedy"       "dynamic"      "rcostfixing" 
## 
## $break.at.first
## [1] FALSE
## 
## $break.at.value
## [1] -1e+30
## 
## $epsilon
##       epsb       epsd      epsel     epsint epsperturb   epspivot 
##      1e-10      1e-09      1e-12      1e-07      1e-05      2e-07 
## 
## $improve
## [1] "dualfeas" "thetagap"
## 
## $infinite
## [1] 1e+30
## 
## $maxpivot
## [1] 250
## 
## $mip.gap
## absolute relative 
##    1e-11    1e-11 
## 
## $negrange
## [1] -1e+06
## 
## $obj.in.basis
## [1] TRUE
## 
## $pivoting
## [1] "devex"    "adaptive"
## 
## $presolve
## [1] "none"
## 
## $scalelimit
## [1] 5
## 
## $scaling
## [1] "geometric"   "equilibrate" "integers"   
## 
## $sense
## [1] "minimize"
## 
## $simplextype
## [1] "dual"   "primal"
## 
## $timeout
## [1] 0
## 
## $verbose
## [1] "neutral"
my.lp
## Model name: 
##               team_Sunday     team_Monday    team_Tuesday  team_Wednesday   team_Thursday     team_Friday   team_Saturday       
## Minimize                1               1               1               1               1               1               1       
## Sunday                  0               1               1               1               1               1               0  >=  4
## Monday                  0               0               1               1               1               1               1  >=  8
## Tuesday                 1               0               0               1               1               1               1  >=  8
## Wednesday               1               1               0               0               1               1               1  >=  7
## Thursday                1               1               1               0               0               1               1  >=  7
## Friday                  1               1               1               1               0               0               1  >=  6
## Saturday                1               1               1               1               1               0               0  >=  5
## Kind                  Std             Std             Std             Std             Std             Std             Std       
## Type                  Int             Int             Int             Int             Int             Int             Int       
## Upper                 Inf             Inf             Inf             Inf             Inf             Inf             Inf       
## Lower                   0               0               0               0               0               0               0
solve(my.lp)
## [1] 0
#this return the proposed solution
get.objective(my.lp)
## [1] 10
get.variables(my.lp)
## [1] 2 0 0 3 0 4 1
get.constraints(my.lp)
## [1]  7  8 10  7  7  6  5