SlideShare a Scribd company logo
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
Prepared by Volkan OBAN
Source:
1-https://r-
forge.rproject.org/scm/viewvc.php/*checkout*/pkg/inst/doc/lpSolveAPI.pdf?revision=91&ro
ot=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

PDF
Functional programming from its fundamentals
Mauro Palsgraaf
 
PDF
Constraint Programming in Haskell
David Overton
 
PPTX
Numerical analysisgroup19
UltraviolenceKOBAIVA
 
PPTX
하스켈 프로그래밍 입문 4
Kwang Yul Seo
 
PPT
computer notes - Data Structures - 36
ecomputernotes
 
PDF
Comonads in Haskell
David Overton
 
PDF
Linear Types in Haskell
David Overton
 
PPTX
하스켈 프로그래밍 입문 3
Kwang Yul Seo
 
DOCX
Linear Programming wi̇th R - Examples
Dr. Volkan OBAN
 
PDF
Investigación de operaciones 026 programación lineal Solución Simplex con R S...
Jorge Pablo Rivas
 
PDF
Itroroduction to R language
chhabria-nitesh
 
PDF
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Dr. Volkan OBAN
 
PDF
Write an MPI program that implements a shell-sort like parallel algo.pdf
bharatchawla141
 
PPTX
Seminar on MATLAB
Dharmesh Tank
 
PDF
Dsp lab _eec-652__vi_sem_18012013
amanabr
 
PDF
Dsp lab _eec-652__vi_sem_18012013
Kurmendra Singh
 
PPT
MatlabIntro1234.ppt.....................
RajeshMadarkar
 
PPT
MatlabIntro.ppt
ShwetaPandey248972
 
PPT
MatlabIntro.ppt
konkatisandeepkumar
 
PPT
MatlabIntro.ppt
ssuser772830
 
PPT
MatlabIntro.ppt
Rajmohan Madasamy
 
PPT
Matlab intro
THEMASTERBLASTERSVID
 
PPTX
EPE821_Lecture3.pptx
Ihtisham Uddin
 
PDF
Matlab intro
Chaitanya Banoth
 
PPT
WIDI ediot autis dongok part 3.EDIOT LU LEMBOT LY
IrlanMalik
 
PPT
WIDI ediot autis dongok part 2.ediot lu lembot lu
IrlanMalik
 
PDF
Programming with matlab session 1
Infinity Tech Solutions
 
PPT
WIDI ediot autis dongok part 1.ediot lu lemot lu setan lu
IrlanMalik
 
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 (20)

DOCX
Linear Programming wi̇th R - Examples
Dr. Volkan OBAN
 
PDF
Investigación de operaciones 026 programación lineal Solución Simplex con R S...
Jorge Pablo Rivas
 
PDF
Itroroduction to R language
chhabria-nitesh
 
PDF
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Dr. Volkan OBAN
 
PDF
Write an MPI program that implements a shell-sort like parallel algo.pdf
bharatchawla141
 
PPTX
Seminar on MATLAB
Dharmesh Tank
 
PDF
Dsp lab _eec-652__vi_sem_18012013
amanabr
 
PDF
Dsp lab _eec-652__vi_sem_18012013
Kurmendra Singh
 
PPT
MatlabIntro1234.ppt.....................
RajeshMadarkar
 
PPT
MatlabIntro.ppt
ShwetaPandey248972
 
PPT
MatlabIntro.ppt
konkatisandeepkumar
 
PPT
MatlabIntro.ppt
ssuser772830
 
PPT
MatlabIntro.ppt
Rajmohan Madasamy
 
PPT
Matlab intro
THEMASTERBLASTERSVID
 
PPTX
EPE821_Lecture3.pptx
Ihtisham Uddin
 
PDF
Matlab intro
Chaitanya Banoth
 
PPT
WIDI ediot autis dongok part 3.EDIOT LU LEMBOT LY
IrlanMalik
 
PPT
WIDI ediot autis dongok part 2.ediot lu lembot lu
IrlanMalik
 
PDF
Programming with matlab session 1
Infinity Tech Solutions
 
PPT
WIDI ediot autis dongok part 1.ediot lu lemot lu setan lu
IrlanMalik
 
Linear Programming wi̇th R - Examples
Dr. Volkan OBAN
 
Investigación de operaciones 026 programación lineal Solución Simplex con R S...
Jorge Pablo Rivas
 
Itroroduction to R language
chhabria-nitesh
 
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Dr. Volkan OBAN
 
Write an MPI program that implements a shell-sort like parallel algo.pdf
bharatchawla141
 
Seminar on MATLAB
Dharmesh Tank
 
Dsp lab _eec-652__vi_sem_18012013
amanabr
 
Dsp lab _eec-652__vi_sem_18012013
Kurmendra Singh
 
MatlabIntro1234.ppt.....................
RajeshMadarkar
 
MatlabIntro.ppt
ShwetaPandey248972
 
MatlabIntro.ppt
konkatisandeepkumar
 
MatlabIntro.ppt
ssuser772830
 
MatlabIntro.ppt
Rajmohan Madasamy
 
Matlab intro
THEMASTERBLASTERSVID
 
EPE821_Lecture3.pptx
Ihtisham Uddin
 
Matlab intro
Chaitanya Banoth
 
WIDI ediot autis dongok part 3.EDIOT LU LEMBOT LY
IrlanMalik
 
WIDI ediot autis dongok part 2.ediot lu lembot lu
IrlanMalik
 
Programming with matlab session 1
Infinity Tech Solutions
 
WIDI ediot autis dongok part 1.ediot lu lemot lu setan lu
IrlanMalik
 

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)

PDF
International Journal of Advanced Information Technology (IJAIT)
ijait
 
PDF
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Shabista Imam
 
PPTX
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
resming1
 
PPTX
Introduction to Python Programming Language
merlinjohnsy
 
PPTX
Bitumen Emulsion by Dr Sangita Ex CRRI Delhi
grilcodes
 
PDF
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Diego López-de-Ipiña González-de-Artaza
 
PDF
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
Shabista Imam
 
PPTX
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
PDF
System design handwritten notes guidance
Shabista Imam
 
PPTX
MATERIAL SCIENCE LECTURE NOTES FOR DIPLOMA STUDENTS
SAMEER VISHWAKARMA
 
PDF
Rapid Prototyping for XR: Lecture 2 - Low Fidelity Prototyping.
Mark Billinghurst
 
PDF
Complete University of Calculus :: 2nd edition
Shabista Imam
 
PDF
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 
PPTX
Industrial internet of things IOT Week-3.pptx
KNaveenKumarECE
 
PDF
FSE-Journal-First-Automated code editing with search-generate-modify.pdf
cl144
 
PDF
Modern multi-proposer consensus implementations
François Garillot
 
PPTX
NEW Strengthened Senior High School Gen Math.pptx
DaryllWhere
 
PPT
20CE404-Soil Mechanics - Slide Share PPT
saravananr808639
 
PPTX
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
 
PDF
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
International Journal of Advanced Information Technology (IJAIT)
ijait
 
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Shabista Imam
 
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
resming1
 
Introduction to Python Programming Language
merlinjohnsy
 
Bitumen Emulsion by Dr Sangita Ex CRRI Delhi
grilcodes
 
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Diego López-de-Ipiña González-de-Artaza
 
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
Shabista Imam
 
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
System design handwritten notes guidance
Shabista Imam
 
MATERIAL SCIENCE LECTURE NOTES FOR DIPLOMA STUDENTS
SAMEER VISHWAKARMA
 
Rapid Prototyping for XR: Lecture 2 - Low Fidelity Prototyping.
Mark Billinghurst
 
Complete University of Calculus :: 2nd edition
Shabista Imam
 
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 
Industrial internet of things IOT Week-3.pptx
KNaveenKumarECE
 
FSE-Journal-First-Automated code editing with search-generate-modify.pdf
cl144
 
Modern multi-proposer consensus implementations
François Garillot
 
NEW Strengthened Senior High School Gen Math.pptx
DaryllWhere
 
20CE404-Soil Mechanics - Slide Share PPT
saravananr808639
 
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
 
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
Ad

Linear programming wi̇th R

  • 1. 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")
  • 2.  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
  • 3. 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
  • 5. 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))
  • 6. 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 Prepared by Volkan OBAN