Learn practical skills, build real-world projects, and advance your career
library(lpSolveAPI)

lprec <- make.lp(0, 8)
lp.control(lprec, sense= "maximize")  

set.objfn(lprec, c(0, 0, 0, 0, 0, 0, 0, 1))

add.constraint(lprec, c(-1,-1,0,0,0,-1,-1,1), "<=", 0)
add.constraint(lprec, c(0,-1,-1,0,-1,-1,0,1), "<=", 0)
add.constraint(lprec, c(0,0,-1,-2,-1,0,0,1), "<=", 0)
add.constraint(lprec, c(0,0,-1,-2,-1,0,0,1), "<=", 0)
add.constraint(lprec, c(0,-1,-1,0,-1,-1,0,1), "<=", 0)
add.constraint(lprec, c(-1,-1,0,0,0,-1,-1,1), "<=", 0)
add.constraint(lprec, c(1,1,1,1,1,1,1,0), "=", 1)

set.bounds(lprec, lower = c(0, 0, 0, 0, 0, 0, 0, -Inf))

RowNames <- c("Row1", "Row2", "Row3","Row4","Row5","Row6","Row7")
ColNames <- c("X1", "X2", "X3", "X4","X5","X6","X7", "v")
dimnames(lprec) <- list(RowNames, ColNames)

solve(lprec) # http://lpsolve.sourceforge.net/5.5/solve.htm
get.objective(lprec)
get.variables(lprec)
get.constraints(lprec)
lprec

# Player II°Øs game #
library(lpSolveAPI)
lprec <- make.lp(0, 7)
lp.control(lprec, sense= "minimize") 

set.objfn(lprec, c(0, 0, 0, 0, 0, 0, 1))
add.constraint(lprec, c(-1,0,0,0,0,-1,1), ">=", 0)
add.constraint(lprec, c(-1,-1,0,0,-1,-1,1), ">=", 0)
add.constraint(lprec, c(0,-1,-1,-1,-1,0,1), ">=", 0)
add.constraint(lprec, c(0,0,-2,-2,0,0,1), ">=", 0)
add.constraint(lprec, c(0,-1,-1,-1,-1,0,1), ">=", 0)
add.constraint(lprec, c(-1,-1,0,0,-1,-1,1), ">=", 0)
add.constraint(lprec, c(-1,0,0,0,0,-1,1), ">=", 0)
add.constraint(lprec, c(1,1,1,1,1,1,0), "=", 1)

set.bounds(lprec, lower = c(0, 0, 0, 0, 0, 0, -Inf))

RowNames <- c("Row1", "Row2", "Row3","Row4","Row5","Row6","Row7","Row8")
ColNames <- c("y1", "y2", "y3", "y4","y5","y6", "v")
dimnames(lprec) <- list(RowNames, ColNames)

solve(lprec) # http://lpsolve.sourceforge.net/5.5/solve.htm
get.objective(lprec)
get.variables(lprec)
get.constraints(lprec)
lprec
Model name: 
            X1    X2    X3    X4    X5    X6    X7     v       
Maximize     0     0     0     0     0     0     0     1       
Row1        -1    -1     0     0     0    -1    -1     1  <=  0
Row2         0    -1    -1     0    -1    -1     0     1  <=  0
Row3         0     0    -1    -2    -1     0     0     1  <=  0
Row4         0     0    -1    -2    -1     0     0     1  <=  0
Row5         0    -1    -1     0    -1    -1     0     1  <=  0
Row6        -1    -1     0     0     0    -1    -1     1  <=  0
Row7         1     1     1     1     1     1     1     0   =  1
Kind       Std   Std   Std   Std   Std   Std   Std   Std       
Type      Real  Real  Real  Real  Real  Real  Real  Real       
Upper      Inf   Inf   Inf   Inf   Inf   Inf   Inf   Inf       
Lower        0     0     0     0     0     0     0  -Inf       
Model name: 
            y1    y2    y3    y4    y5    y6     v       
Minimize     0     0     0     0     0     0     1       
Row1        -1     0     0     0     0    -1     1  >=  0
Row2        -1    -1     0     0    -1    -1     1  >=  0
Row3         0    -1    -1    -1    -1     0     1  >=  0
Row4         0     0    -2    -2     0     0     1  >=  0
Row5         0    -1    -1    -1    -1     0     1  >=  0
Row6        -1    -1     0     0    -1    -1     1  >=  0
Row7        -1     0     0     0     0    -1     1  >=  0
Row8         1     1     1     1     1     1     0   =  1
Kind       Std   Std   Std   Std   Std   Std   Std       
Type      Real  Real  Real  Real  Real  Real  Real       
Upper      Inf   Inf   Inf   Inf   Inf   Inf   Inf       
Lower        0     0     0     0     0     0  -Inf       
install.packages("lpSolveAPI")
library(lpSolveAPI)

ProductModel <- make.lp(9, 9)

lp.control(ProductModel, sense= "maximize")
set.objfn(ProductModel, c(25, 10, 5, 21, 6, 1, 25, 10, 5))

set.row(ProductModel, 1, c(1,1,1), indices = c(1:3))
set.row(ProductModel, 2, c(1,1,1), indices = c(4:6))
set.row(ProductModel, 3, c(1,1,1), indices = c(7:9))
set.row(ProductModel, 4, c(1, -1, -1), indices = c(1:3))
set.row(ProductModel, 5, c(1, -1, -1), indices = c(4:6))
set.row(ProductModel, 6, c(0.6, -0.4, -0.4), indices = c(7:9))
set.row(ProductModel, 7, c(-0.3, 0.7, -0.3), indices = c(1:3)) 
set.row(ProductModel, 8, c(-0.4, 0.6, -0.4), indices =c(4:6))
set.row(ProductModel, 9, c(-1, 1, -1), indices =c(7:9))

set.rhs(ProductModel, c(4500,	3000,	3500,	0,	0,	0,	0,	0, 0))
set.constr.type(ProductModel, c("<=",	"<=",	"<=", ">=", ">=",	">=",	">=", ">=",">="))

RowNames <- c("C1", "C2", "C3", 'C4','C5', 'C6','C7','C8','C9')
ColNames <- c(1, 2, 3, 4 ,5, 6, 7, 8, 9)
dimnames(ProductModel) <- list(RowNames, ColNames)

solve(ProductModel)
get.objective(ProductModel)
get.variables(ProductModel)
get.constraints(ProductModel) 
ProductModel


solve(ProductModel)
get.objective(ProductModel)
get.variables(ProductModel)