R
ini LP bisa kita
terapkan dengan menggunakan package lpSolve. Paket lpSolve dari
R
berisi beberapa fungsi untuk menyelesaikan masalah
program linier dan mendapatkan analisis statistik yang signifikan.Sebuah perusahaan memproduksi dua model kursi: 4P dan 3P. Model 4P membutuhkan 4 kaki, 1 kursi dan 1 punggung. Di sisi lain, model 3P membutuhkan 3 kaki dan 1 kursi. Perusahaan memiliki stok awal 200 kaki, 500 kursi dan 100 punggung. Jika perusahaan membutuhkan lebih banyak kaki, kursi, dan sandaran, perusahaan dapat membeli balok kayu standar, yang harganya 80 euro per balok. Perusahaan dapat memproduksi 10 kursi, 20 kaki dan 2 punggung dari balok kayu standar. Biaya produksi model 4P adalah 30 euro/kursi, sedangkan biaya model 3P adalah 40 euro/kursi. Akhirnya, perusahaan menginformasikan bahwa jumlah minimum kursi yang harus diproduksi adalah 1000 unit per bulan. Tentukan model program linier, yang meminimalkan total biaya (biaya produksi dua kursi, ditambah pembelian balok kayu baru)
# Import lpSolve package
library(lpSolve)
# Solution with lpSolve -----------
Biaya setiap unit 4P adalah $30
Biaya setiap unit 3P adalah $40
Biaya setiap unit balok kayu adalah $80
## Set the coefficients of the decision variables -> C
C <- c(30, 40, 80)
Baris pertama adalah untuk batasan Kursi. Dikatakan bahwa:
Baris kedua untuk batasan Kaki
Baris ketiga adalah untuk batasan Punggung
Baris keempat adalah untuk batasan produksi Min
# Create constraint martix B
A <- matrix(c(1, 1, -10,
4, 3, -20,
1, 0, -2,
1, 1, 0), nrow=4, byrow=TRUE)
# Right hand side for the constraints
B <- c(500, 200, 100, 1000)
# Direction of the constraints
constranints_direction <- c("<=", "<=", "<=", ">=")
# Find the optimal solution
optimum <- lp(direction="min",
objective.in = C,
const.mat = A,
const.dir = constranints_direction,
const.rhs = B,
all.int = T)
str(optimum)
## List of 28
## $ direction : int 0
## $ x.count : int 3
## $ objective : num [1:3] 30 40 80
## $ const.count : int 4
## $ constraints : num [1:5, 1:4] 1 1 -10 1 500 4 3 -20 1 200 ...
## ..- attr(*, "dimnames")=List of 2
## .. ..$ : chr [1:5] "" "" "" "const.dir.num" ...
## .. ..$ : NULL
## $ int.count : int 3
## $ int.vec : int [1:3] 1 2 3
## $ bin.count : int 0
## $ binary.vec : int 0
## $ num.bin.solns : int 1
## $ objval : num 48680
## $ solution : num [1:3] 420 580 161
## $ presolve : int 0
## $ compute.sens : int 0
## $ sens.coef.from : num 0
## $ sens.coef.to : num 0
## $ duals : num 0
## $ duals.from : num 0
## $ duals.to : num 0
## $ scale : int 196
## $ use.dense : int 0
## $ dense.col : int 0
## $ dense.val : num 0
## $ dense.const.nrow: int 0
## $ dense.ctr : num 0
## $ use.rw : int 0
## $ tmp : chr "Nobody will ever look at this"
## $ status : int 0
## - attr(*, "class")= chr "lp"
# Print status: 0 = success, 2 = no feasible solution
print(optimum$status)
## [1] 0
# Display the optimum values for x_4p, x_3p and x_w
best_sol <- optimum$solution
names(best_sol) <- c("x_4p", "x_3p", "x_w")
print(best_sol)
## x_4p x_3p x_w
## 420 580 161
# Check the value of objective function at optimal point
print(paste("Total cost: ", optimum$objval, sep=""))
## [1] "Total cost: 48680"
#*********************************************************
# Output #
#*********************************************************
# [1] 0
# x_4p x_3p x_w
# 420 580 161
# "Total cost: 48680"
rm(optimum, constranints_direction, best_sol)
#*********************************************************
# Solution with lpSolveAPI -------------------
# Let's try to solve the problem again using lpSolveAPI
# Use lpSolveAPI
require(lpSolveAPI)
## Loading required package: lpSolveAPI
# Set 4 constraints and 3 decision variables
lprec <- make.lp(nrow = 4, ncol = 3)
# Set the type of problem we are trying to solve
lp.control(lprec, 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"
# Set type of decision variables
set.type(lprec, 1:3, type=c("integer"))
# Set objective function coefficients vector C
set.objfn(lprec, C)
# Add constraints
add.constraint(lprec, A[1, ], "<=", B[1])
add.constraint(lprec, A[2, ], "<=", B[2])
add.constraint(lprec, A[3, ], "<=", B[3])
add.constraint(lprec, A[4, ], ">=", B[4])
# Display the LPsolve matrix
lprec
## Model name:
## C1 C2 C3
## Minimize 30 40 80
## R1 0 0 0 free 0
## R2 0 0 0 free 0
## R3 0 0 0 free 0
## R4 0 0 0 free 0
## R5 1 1 -10 <= 500
## R6 4 3 -20 <= 200
## R7 1 0 -2 <= 100
## R8 1 1 0 >= 1000
## Kind Std Std Std
## Type Int Int Int
## Upper Inf Inf Inf
## Lower 0 0 0
# Solve problem
solve(lprec)
## [1] 0
# Get the decision variables values
get.variables(lprec)
## [1] 420 580 161
# Get the value of the objective function
get.objective(lprec)
## [1] 48680
# Note that the default boundaries on the decision variable are c(0, 0, 0) and c(Inf, Inf, Inf)
get.bounds(lprec)
## $lower
## [1] 0 0 0
##
## $upper
## [1] Inf Inf Inf
# Boundaries can be set with following function
#lpSolveAPI::set.bounds()
#*********************************************************
# Output #
#*********************************************************
# [1] 420 580 161
# [1] 48680