SlideShare a Scribd company logo
Prepared by Volkan OBAN
LINEAR PROGRAMMING WİTH R— lpsolve and IpSolveAPI Package:
The lpSolveAPI package provides an R API for the lp solve library, a mixed integer linear pro
gramming (MILP) solver with support for pure linear, (mixed) integer/binary, semi-continuou
s and special ordered sets (SOS) models. The lp solve library uses the revised simplex method
to solve pure linear programs and uses the branch-and-bound algorithm to handle integer varia
bles, semi-continuous variables and special ordered sets.
Example:
maximize
P = (110)(1.30)x + (30)(2.00)y = 143x + 60y
subject to
120x + 210y <= 15000
110x + 30y <= 4000
x + y <= 75
x >= 0
y >= 0
Using R to solve
 Install lpsolve library
> install.packages("lpSolveAPI")
 Load lpsolve library
> library("lpSolveAPI")
 Represent our problem
> lprec <- make.lp(0, 2)
> lp.control(lprec, sense="max")
> set.objfn(lprec, c(143, 60))
> add.constraint(lprec, c(120, 210), "<=", 15000)
> add.constraint(lprec, c(110, 30), "<=", 4000)
> add.constraint(lprec, c(1, 1), "<=", 75)
 Display the lpsolve matrix
> lprec
Model name:
C1 C2
Maximize 143 60
R1 120 210 <= 15000
R2 110 30 <= 4000
R3 1 1 <= 75
Kind Std Std
Type Real Real
Upper Inf Inf
Lower 0 0
 Solve
> solve(lprec)
[1] 0
 Get maximum profit
> get.objective(lprec)
[1] 6315.625
 Get the solution
> get.variables(lprec)
[1] 21.875 53.125
Thus, to achieve the maximum profit ($6315.625), the farmer
Example:
Model name:
C1 C2
Minimize -2 -1
R1 1 3 <= 4
R2 1 1 <= 2
R3 2 0 <= 3
>install.packages("lpSolveAPI")
library(lpSolveAPI)
>
> my.lp <- make.lp(3, 2)
>
> my.lp
Model name:
C1 C2
Minimize 0 0
R1 0 0 free 0
R2 0 0 free 0
R3 0 0 free 0
Kind Std Std
Type Real Real
Upper Inf Inf
Lower 0 0
> set.column(my.lp, 1, c(1, 1, 2))
>
> set.column(my.lp, 2, c(3, 1, 0))
>
> set.objfn(my.lp, c(-2, -1))
>
> set.constr.type(my.lp, rep("<=", 3))
>
> set.rhs(my.lp, c(4, 2, 3))
> my.lp
Model name:
C1 C2
Minimize -2 -1
R1 1 3 <= 4
R2 1 1 <= 2
R3 2 0 <= 3
Kind Std Std
Type Real Real
Upper Inf Inf
Lower 0 0
> solve(my.lp)
[1] 0
>
> get.objective(my.lp)
[1] -3.5
>
> get.variables(my.lp)
[1] 1.5 0.5
>
> get.constraints(my.lp)
[1] 3 2 3
Example:
First, we create an LPMO with 3 constraints and 4 decision variables.
> lprec <- make.lp(3, 4)
Next we set the values in the
first column. > set.column(lprec, 1, c(0, 0.24, 12.68))
The lp solve library is capable of using a sparse vectors to represent the constraints matrix. In
the remaining three columns, only the nonzero entries are set.
> set.column(lprec, 2, 78.26, indices = 1)
> set.column(lprec, 3, c(11.31, 0.08), indices = 2:3)
> set.column(lprec, 4, c(2.9, 0.9), indices = c(1, 3))
Next, we set the objective function, constraint types and right-hand-side.
> set.objfn(lprec, c(1, 3, 6.24, 0.1))
> set.constr.type(lprec, c(">=", "<=", ">="))
> set.rhs(lprec, c(92.3, 14.8, 4))
By default, all decision variables are created as real with range [0,∞). Thus we must change
the type of the decision variables x2 and x3.
> set.type(lprec, 2, "integer")
> set.type(lprec, 3, "binary")
We still need to set the range constraints on x1 and x4.
> set.bounds(lprec, lower = c(28.6, 18), columns = c(1, 4))
> set.bounds(lprec, upper = 48.98, columns = 4) Finally, we name the decision variables and
the constraints.
> RowNames <- c("THISROW", "THATROW", "LASTROW")
> ColNames <- c("COLONE", "COLTWO", "COLTHREE", "COLFOUR")
> dimnames(lprec) <- list(RowNames, ColNames)
Lets take a look at what we have done so far.
[1] 92.3000 6.8640 391.2928
Source:
1-
https://rforge.rproject.org/scm/viewvc.php/*checkout*/pkg/inst/doc/lpSolveAPI.pdf?revision
=91&root=lpsolve&pathrev=91
2-http://icyrock.com/blog/2013/12/linear-programming-in-r-using-lpsolve/
3-Modeling and Solving Linear Programming with R.
Ad

Recommended

PPTX
Introduction to dynamic programming
Amisha Narsingani
 
PDF
Economic interpretations of Linear Programming Problem
RAVI PRASAD K.J.
 
DOCX
Linear programming wi̇th R
Dr. Volkan OBAN
 
PDF
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Dr. Volkan OBAN
 
PDF
lpSolve - R Library
David Faris
 
PDF
Network Planning and Optimization
EM Legacy
 
PDF
Linear programming optimization in r
Ashwini Mathur
 
PDF
Optimum Engineering Design - Day 4 - Clasical methods of optimization
SantiagoGarridoBulln
 
DOCX
01.steps to solve the lp problem using excel solver
Dheeraj Shetty
 
PPTX
Linear Programming Problem
A. Dally Maria Evangeline
 
PDF
Investigación de operaciones 026 programación lineal Solución Simplex con R S...
Jorge Pablo Rivas
 
DOCX
Solving Optimization Problems using the Matlab Optimization.docx
whitneyleman54422
 
PDF
Mathematical linear programming notes
Tarig Gibreel
 
PPTX
Applied optimization in Matlab for Power System.pptx
HenryKim20273
 
PDF
35000120030_Aritra Kundu_Operations Research.pdf
JuliusCaesar36
 
PDF
Linearprog, Reading Materials for Operational Research
Derbew Tesfa
 
PPTX
Simplex method material for operation .pptx
bizuayehuadmasu1
 
PPTX
Linear programming problem
POOJA UDAYAN
 
PDF
Mixed-integer and Disjunctive Programming - Ignacio E. Grossmann
CAChemE
 
PPTX
QA CHAPTER II.pptx
Teshome48
 
PDF
Optimization Problems Solved by Different Platforms Say Optimum Tool Box (Mat...
IRJET Journal
 
PPTX
Linear programming manzoor nabi
Manzoor Wani
 
PPTX
Solver.pptx
MohitBhatnagar22
 
PPTX
UNIT-2 Quantitaitive Anlaysis for Mgt Decisions.pptx
MinilikDerseh1
 
PDF
Linear programming class 12 investigatory project
Divyans890
 
PDF
Diagnosing Infeasibilities in IMPL
Alkis Vazacopoulos
 
PPTX
linear programming
Jazz Bhatti
 
PPTX
Linear Programing.pptx
AdnanHaleem
 
PDF
Conference Paper:IMAGE PROCESSING AND OBJECT DETECTION APPLICATION: INSURANCE...
Dr. Volkan OBAN
 
PDF
Covid19py Python Package - Example
Dr. Volkan OBAN
 

More Related Content

Similar to Linear Programming wi̇th R - Examples (20)

DOCX
01.steps to solve the lp problem using excel solver
Dheeraj Shetty
 
PPTX
Linear Programming Problem
A. Dally Maria Evangeline
 
PDF
Investigación de operaciones 026 programación lineal Solución Simplex con R S...
Jorge Pablo Rivas
 
DOCX
Solving Optimization Problems using the Matlab Optimization.docx
whitneyleman54422
 
PDF
Mathematical linear programming notes
Tarig Gibreel
 
PPTX
Applied optimization in Matlab for Power System.pptx
HenryKim20273
 
PDF
35000120030_Aritra Kundu_Operations Research.pdf
JuliusCaesar36
 
PDF
Linearprog, Reading Materials for Operational Research
Derbew Tesfa
 
PPTX
Simplex method material for operation .pptx
bizuayehuadmasu1
 
PPTX
Linear programming problem
POOJA UDAYAN
 
PDF
Mixed-integer and Disjunctive Programming - Ignacio E. Grossmann
CAChemE
 
PPTX
QA CHAPTER II.pptx
Teshome48
 
PDF
Optimization Problems Solved by Different Platforms Say Optimum Tool Box (Mat...
IRJET Journal
 
PPTX
Linear programming manzoor nabi
Manzoor Wani
 
PPTX
Solver.pptx
MohitBhatnagar22
 
PPTX
UNIT-2 Quantitaitive Anlaysis for Mgt Decisions.pptx
MinilikDerseh1
 
PDF
Linear programming class 12 investigatory project
Divyans890
 
PDF
Diagnosing Infeasibilities in IMPL
Alkis Vazacopoulos
 
PPTX
linear programming
Jazz Bhatti
 
PPTX
Linear Programing.pptx
AdnanHaleem
 
01.steps to solve the lp problem using excel solver
Dheeraj Shetty
 
Linear Programming Problem
A. Dally Maria Evangeline
 
Investigación de operaciones 026 programación lineal Solución Simplex con R S...
Jorge Pablo Rivas
 
Solving Optimization Problems using the Matlab Optimization.docx
whitneyleman54422
 
Mathematical linear programming notes
Tarig Gibreel
 
Applied optimization in Matlab for Power System.pptx
HenryKim20273
 
35000120030_Aritra Kundu_Operations Research.pdf
JuliusCaesar36
 
Linearprog, Reading Materials for Operational Research
Derbew Tesfa
 
Simplex method material for operation .pptx
bizuayehuadmasu1
 
Linear programming problem
POOJA UDAYAN
 
Mixed-integer and Disjunctive Programming - Ignacio E. Grossmann
CAChemE
 
QA CHAPTER II.pptx
Teshome48
 
Optimization Problems Solved by Different Platforms Say Optimum Tool Box (Mat...
IRJET Journal
 
Linear programming manzoor nabi
Manzoor Wani
 
Solver.pptx
MohitBhatnagar22
 
UNIT-2 Quantitaitive Anlaysis for Mgt Decisions.pptx
MinilikDerseh1
 
Linear programming class 12 investigatory project
Divyans890
 
Diagnosing Infeasibilities in IMPL
Alkis Vazacopoulos
 
linear programming
Jazz Bhatti
 
Linear Programing.pptx
AdnanHaleem
 

More from Dr. Volkan OBAN (20)

PDF
Conference Paper:IMAGE PROCESSING AND OBJECT DETECTION APPLICATION: INSURANCE...
Dr. Volkan OBAN
 
PDF
Covid19py Python Package - Example
Dr. Volkan OBAN
 
PDF
Object detection with Python
Dr. Volkan OBAN
 
PDF
Python - Rastgele Orman(Random Forest) Parametreleri
Dr. Volkan OBAN
 
DOCX
"optrees" package in R and examples.(optrees:finds optimal trees in weighted ...
Dr. Volkan OBAN
 
DOCX
k-means Clustering in Python
Dr. Volkan OBAN
 
DOCX
Naive Bayes Example using R
Dr. Volkan OBAN
 
DOCX
R forecasting Example
Dr. Volkan OBAN
 
DOCX
k-means Clustering and Custergram with R
Dr. Volkan OBAN
 
PDF
Data Science and its Relationship to Big Data and Data-Driven Decision Making
Dr. Volkan OBAN
 
DOCX
Data Visualization with R.ggplot2 and its extensions examples.
Dr. Volkan OBAN
 
PDF
Scikit-learn Cheatsheet-Python
Dr. Volkan OBAN
 
PDF
Python Pandas for Data Science cheatsheet
Dr. Volkan OBAN
 
PDF
Pandas,scipy,numpy cheatsheet
Dr. Volkan OBAN
 
PPTX
ReporteRs package in R. forming powerpoint documents-an example
Dr. Volkan OBAN
 
PPTX
ReporteRs package in R. forming powerpoint documents-an example
Dr. Volkan OBAN
 
DOCX
R-ggplot2 package Examples
Dr. Volkan OBAN
 
DOCX
R Machine Learning packages( generally used)
Dr. Volkan OBAN
 
DOCX
treemap package in R and examples.
Dr. Volkan OBAN
 
DOCX
Mosaic plot in R.
Dr. Volkan OBAN
 
Conference Paper:IMAGE PROCESSING AND OBJECT DETECTION APPLICATION: INSURANCE...
Dr. Volkan OBAN
 
Covid19py Python Package - Example
Dr. Volkan OBAN
 
Object detection with Python
Dr. Volkan OBAN
 
Python - Rastgele Orman(Random Forest) Parametreleri
Dr. Volkan OBAN
 
"optrees" package in R and examples.(optrees:finds optimal trees in weighted ...
Dr. Volkan OBAN
 
k-means Clustering in Python
Dr. Volkan OBAN
 
Naive Bayes Example using R
Dr. Volkan OBAN
 
R forecasting Example
Dr. Volkan OBAN
 
k-means Clustering and Custergram with R
Dr. Volkan OBAN
 
Data Science and its Relationship to Big Data and Data-Driven Decision Making
Dr. Volkan OBAN
 
Data Visualization with R.ggplot2 and its extensions examples.
Dr. Volkan OBAN
 
Scikit-learn Cheatsheet-Python
Dr. Volkan OBAN
 
Python Pandas for Data Science cheatsheet
Dr. Volkan OBAN
 
Pandas,scipy,numpy cheatsheet
Dr. Volkan OBAN
 
ReporteRs package in R. forming powerpoint documents-an example
Dr. Volkan OBAN
 
ReporteRs package in R. forming powerpoint documents-an example
Dr. Volkan OBAN
 
R-ggplot2 package Examples
Dr. Volkan OBAN
 
R Machine Learning packages( generally used)
Dr. Volkan OBAN
 
treemap package in R and examples.
Dr. Volkan OBAN
 
Mosaic plot in R.
Dr. Volkan OBAN
 
Ad

Recently uploaded (20)

PPTX
UPS and Big Data intro to Business Analytics.pptx
sanjum5582
 
PPTX
定制OCAD学生卡加拿大安大略艺术与设计大学成绩单范本,OCAD成绩单复刻
taqyed
 
PPTX
美国毕业证范本中华盛顿大学学位证书CWU学生卡购买
Taqyea
 
PDF
11_L2_Defects_and_Trouble_Shooting_2014[1].pdf
gun3awan88
 
PPTX
最新版美国威斯康星大学河城分校毕业证(UWRF毕业证书)原版定制
taqyea
 
PPSX
PPT1_CB_VII_CS_Ch3_FunctionsandChartsinCalc.ppsx
animaroy81
 
PDF
Predicting Titanic Survival Presentation
praxyfarhana
 
PPT
Reliability Monitoring of Aircrfat commerce
Rizk2
 
PDF
Residential Zone 4 for industrial village
MdYasinArafat13
 
PPTX
RESEARCH-FINAL-GROUP-3, about the final .pptx
gwapokoha1
 
PPTX
Communication_Skills_Class10_Visual.pptx
namanrastogi70555
 
PDF
Microsoft Power BI - Advanced Certificate for Business Intelligence using Pow...
Prasenjit Debnath
 
PPTX
25 items quiz for practical research 1 in grade 11
leamaydayaganon81
 
PPTX
Mynd company all details what they are doing a
AniketKadam40952
 
PPTX
PPT2 W1L2.pptx.........................................
palicteronalyn26
 
PPTX
ppt somu_Jarvis_AI_Assistant_presen.pptx
MohammedumarFarhan
 
PPTX
英国毕业证范本利物浦约翰摩尔斯大学成绩单底纹防伪LJMU学生证办理学历认证
taqyed
 
PPTX
Flextronics Employee Safety Data-Project-2.pptx
kilarihemadri
 
PDF
ilide.info-tg-understanding-culture-society-and-politics-pr_127f984d2904c57ec...
jed P
 
PPTX
Indigo dyeing Presentation (2).pptx as dye
shreeroop1335
 
UPS and Big Data intro to Business Analytics.pptx
sanjum5582
 
定制OCAD学生卡加拿大安大略艺术与设计大学成绩单范本,OCAD成绩单复刻
taqyed
 
美国毕业证范本中华盛顿大学学位证书CWU学生卡购买
Taqyea
 
11_L2_Defects_and_Trouble_Shooting_2014[1].pdf
gun3awan88
 
最新版美国威斯康星大学河城分校毕业证(UWRF毕业证书)原版定制
taqyea
 
PPT1_CB_VII_CS_Ch3_FunctionsandChartsinCalc.ppsx
animaroy81
 
Predicting Titanic Survival Presentation
praxyfarhana
 
Reliability Monitoring of Aircrfat commerce
Rizk2
 
Residential Zone 4 for industrial village
MdYasinArafat13
 
RESEARCH-FINAL-GROUP-3, about the final .pptx
gwapokoha1
 
Communication_Skills_Class10_Visual.pptx
namanrastogi70555
 
Microsoft Power BI - Advanced Certificate for Business Intelligence using Pow...
Prasenjit Debnath
 
25 items quiz for practical research 1 in grade 11
leamaydayaganon81
 
Mynd company all details what they are doing a
AniketKadam40952
 
PPT2 W1L2.pptx.........................................
palicteronalyn26
 
ppt somu_Jarvis_AI_Assistant_presen.pptx
MohammedumarFarhan
 
英国毕业证范本利物浦约翰摩尔斯大学成绩单底纹防伪LJMU学生证办理学历认证
taqyed
 
Flextronics Employee Safety Data-Project-2.pptx
kilarihemadri
 
ilide.info-tg-understanding-culture-society-and-politics-pr_127f984d2904c57ec...
jed P
 
Indigo dyeing Presentation (2).pptx as dye
shreeroop1335
 
Ad

Linear Programming wi̇th R - Examples

  • 1. Prepared by Volkan OBAN LINEAR PROGRAMMING WİTH R— lpsolve and IpSolveAPI Package: The lpSolveAPI package provides an R API for the lp solve library, a mixed integer linear pro gramming (MILP) solver with support for pure linear, (mixed) integer/binary, semi-continuou s and special ordered sets (SOS) models. The lp solve library uses the revised simplex method to solve pure linear programs and uses the branch-and-bound algorithm to handle integer varia bles, semi-continuous variables and special ordered sets. Example: maximize P = (110)(1.30)x + (30)(2.00)y = 143x + 60y subject to 120x + 210y <= 15000 110x + 30y <= 4000 x + y <= 75 x >= 0 y >= 0 Using R to solve  Install lpsolve library
  • 2. > install.packages("lpSolveAPI")  Load lpsolve library > library("lpSolveAPI")  Represent our problem > lprec <- make.lp(0, 2) > lp.control(lprec, sense="max") > set.objfn(lprec, c(143, 60)) > add.constraint(lprec, c(120, 210), "<=", 15000) > add.constraint(lprec, c(110, 30), "<=", 4000) > add.constraint(lprec, c(1, 1), "<=", 75)  Display the lpsolve matrix > lprec Model name: C1 C2 Maximize 143 60 R1 120 210 <= 15000 R2 110 30 <= 4000
  • 3. R3 1 1 <= 75 Kind Std Std Type Real Real Upper Inf Inf Lower 0 0  Solve > solve(lprec) [1] 0  Get maximum profit > get.objective(lprec) [1] 6315.625  Get the solution > get.variables(lprec) [1] 21.875 53.125 Thus, to achieve the maximum profit ($6315.625), the farmer
  • 4. Example: Model name: C1 C2 Minimize -2 -1 R1 1 3 <= 4 R2 1 1 <= 2 R3 2 0 <= 3 >install.packages("lpSolveAPI") library(lpSolveAPI) > > my.lp <- make.lp(3, 2) > > my.lp Model name: C1 C2 Minimize 0 0 R1 0 0 free 0 R2 0 0 free 0 R3 0 0 free 0 Kind Std Std Type Real Real Upper Inf Inf Lower 0 0 > set.column(my.lp, 1, c(1, 1, 2)) > > set.column(my.lp, 2, c(3, 1, 0)) > > set.objfn(my.lp, c(-2, -1)) > > set.constr.type(my.lp, rep("<=", 3)) > > set.rhs(my.lp, c(4, 2, 3)) > my.lp Model name: C1 C2 Minimize -2 -1 R1 1 3 <= 4 R2 1 1 <= 2 R3 2 0 <= 3 Kind Std Std Type Real Real Upper Inf Inf Lower 0 0
  • 5. > solve(my.lp) [1] 0 > > get.objective(my.lp) [1] -3.5 > > get.variables(my.lp) [1] 1.5 0.5 > > get.constraints(my.lp) [1] 3 2 3 Example: First, we create an LPMO with 3 constraints and 4 decision variables. > lprec <- make.lp(3, 4) Next we set the values in the first column. > set.column(lprec, 1, c(0, 0.24, 12.68)) The lp solve library is capable of using a sparse vectors to represent the constraints matrix. In the remaining three columns, only the nonzero entries are set. > set.column(lprec, 2, 78.26, indices = 1) > set.column(lprec, 3, c(11.31, 0.08), indices = 2:3) > set.column(lprec, 4, c(2.9, 0.9), indices = c(1, 3)) Next, we set the objective function, constraint types and right-hand-side. > set.objfn(lprec, c(1, 3, 6.24, 0.1)) > set.constr.type(lprec, c(">=", "<=", ">=")) > set.rhs(lprec, c(92.3, 14.8, 4)) By default, all decision variables are created as real with range [0,∞). Thus we must change the type of the decision variables x2 and x3.
  • 6. > set.type(lprec, 2, "integer") > set.type(lprec, 3, "binary") We still need to set the range constraints on x1 and x4. > set.bounds(lprec, lower = c(28.6, 18), columns = c(1, 4)) > set.bounds(lprec, upper = 48.98, columns = 4) Finally, we name the decision variables and the constraints. > RowNames <- c("THISROW", "THATROW", "LASTROW") > ColNames <- c("COLONE", "COLTWO", "COLTHREE", "COLFOUR") > dimnames(lprec) <- list(RowNames, ColNames) Lets take a look at what we have done so far. [1] 92.3000 6.8640 391.2928