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.

More Related Content

What's hot (20)

PDF
確率的主成分分析
Mika Yoshimura
 
PDF
CVPR 2022 Tutorial에 대한 쉽고 상세한 Diffusion Probabilistic Model
jaypi Ko
 
PDF
ICASSP読み会2020
Yuki Saito
 
PPTX
PRML5.5
hiroki yamaoka
 
PPTX
Prml nn
Shota Yasui
 
PPTX
ノンパラメトリックベイズを用いた逆強化学習
Shota Ishikawa
 
PDF
PRML輪読#3
matsuolab
 
PPT
Genetic algorithm
garima931
 
PPTX
PRML Chapter 5
Masahito Ohue
 
PDF
PyData.Tokyo Meetup #21 講演資料「Optuna ハイパーパラメータ最適化フレームワーク」太田 健
Preferred Networks
 
PPTX
統計的因果推論からCausalMLまで走り抜けるスライド
fusha
 
PDF
Dimensionality Reduction
mrizwan969
 
PDF
Deep generative model.pdf
Hyungjoo Cho
 
PDF
時系列分析による異常検知入門
Yohei Sato
 
PPTX
Reinforcement Learning(方策改善定理)
Masanori Yamada
 
PDF
Prophet入門【理論編】Facebookの時系列予測ツール
hoxo_m
 
KEY
Hierarchical Reinforcement Learning
David Jardim
 
PPTX
【DL輪読会】Hopfield network 関連研究について
Deep Learning JP
 
PPTX
Travelling Salesman Problem using Partical Swarm Optimization
Ilgın Kavaklıoğulları
 
PDF
「ランダムフォレスト回帰」のハイパーパラメーター
Jun Umezawa
 
確率的主成分分析
Mika Yoshimura
 
CVPR 2022 Tutorial에 대한 쉽고 상세한 Diffusion Probabilistic Model
jaypi Ko
 
ICASSP読み会2020
Yuki Saito
 
Prml nn
Shota Yasui
 
ノンパラメトリックベイズを用いた逆強化学習
Shota Ishikawa
 
PRML輪読#3
matsuolab
 
Genetic algorithm
garima931
 
PRML Chapter 5
Masahito Ohue
 
PyData.Tokyo Meetup #21 講演資料「Optuna ハイパーパラメータ最適化フレームワーク」太田 健
Preferred Networks
 
統計的因果推論からCausalMLまで走り抜けるスライド
fusha
 
Dimensionality Reduction
mrizwan969
 
Deep generative model.pdf
Hyungjoo Cho
 
時系列分析による異常検知入門
Yohei Sato
 
Reinforcement Learning(方策改善定理)
Masanori Yamada
 
Prophet入門【理論編】Facebookの時系列予測ツール
hoxo_m
 
Hierarchical Reinforcement Learning
David Jardim
 
【DL輪読会】Hopfield network 関連研究について
Deep Learning JP
 
Travelling Salesman Problem using Partical Swarm Optimization
Ilgın Kavaklıoğulları
 
「ランダムフォレスト回帰」のハイパーパラメーター
Jun Umezawa
 

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

DOCX
Linear programming wi̇th R
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
 
PPTX
Seminar on MATLAB
Dharmesh Tank
 
PDF
Matlab ch1 intro
Ragu Nathan
 
PDF
Dsp lab _eec-652__vi_sem_18012013
amanabr
 
PDF
Dsp lab _eec-652__vi_sem_18012013
Kurmendra Singh
 
PDF
lpSolve - R Library
David Faris
 
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
Quality Python Homework Help
Python Homework Help
 
PDF
Matlab intro
Chaitanya Banoth
 
PDF
High-Performance Haskell
Johan Tibell
 
PDF
Programming with matlab session 1
Infinity Tech Solutions
 
PPT
WIDI ediot autis dongok part 3.EDIOT LU LEMBOT LY
IrlanMalik
 
Linear programming wi̇th R
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
 
Seminar on MATLAB
Dharmesh Tank
 
Matlab ch1 intro
Ragu Nathan
 
Dsp lab _eec-652__vi_sem_18012013
amanabr
 
Dsp lab _eec-652__vi_sem_18012013
Kurmendra Singh
 
lpSolve - R Library
David Faris
 
MatlabIntro1234.ppt.....................
RajeshMadarkar
 
MatlabIntro.ppt
ShwetaPandey248972
 
MatlabIntro.ppt
konkatisandeepkumar
 
MatlabIntro.ppt
ssuser772830
 
MatlabIntro.ppt
Rajmohan Madasamy
 
Matlab intro
THEMASTERBLASTERSVID
 
Quality Python Homework Help
Python Homework Help
 
Matlab intro
Chaitanya Banoth
 
High-Performance Haskell
Johan Tibell
 
Programming with matlab session 1
Infinity Tech Solutions
 
WIDI ediot autis dongok part 3.EDIOT LU LEMBOT LY
IrlanMalik
 
Ad

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
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
PPTX
apidays Helsinki & North 2025 - Vero APIs - Experiences of API development in...
apidays
 
PDF
Product Management in HealthTech (Case Studies from SnappDoctor)
Hamed Shams
 
PPTX
apidays Helsinki & North 2025 - Running a Successful API Program: Best Practi...
apidays
 
PPTX
BinarySearchTree in datastructures in detail
kichokuttu
 
PDF
What does good look like - CRAP Brighton 8 July 2025
Jan Kierzyk
 
PDF
Data Retrieval and Preparation Business Analytics.pdf
kayserrakib80
 
PPTX
SlideEgg_501298-Agentic AI.pptx agentic ai
530BYManoj
 
PPTX
apidays Helsinki & North 2025 - API access control strategies beyond JWT bear...
apidays
 
PDF
apidays Helsinki & North 2025 - How (not) to run a Graphql Stewardship Group,...
apidays
 
PDF
apidays Singapore 2025 - From API Intelligence to API Governance by Harsha Ch...
apidays
 
PPTX
apidays Singapore 2025 - From Data to Insights: Building AI-Powered Data APIs...
apidays
 
PDF
apidays Singapore 2025 - Building a Federated Future, Alex Szomora (GSMA)
apidays
 
PDF
OOPs with Java_unit2.pdf. sarthak bookkk
Sarthak964187
 
PPTX
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
PPTX
Listify-Intelligent-Voice-to-Catalog-Agent.pptx
nareshkottees
 
PDF
Research Methodology Overview Introduction
ayeshagul29594
 
PDF
OPPOTUS - Malaysias on Malaysia 1Q2025.pdf
Oppotus
 
PPTX
apidays Helsinki & North 2025 - From Chaos to Clarity: Designing (AI-Ready) A...
apidays
 
PDF
Driving Employee Engagement in a Hybrid World.pdf
Mia scott
 
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
apidays Helsinki & North 2025 - Vero APIs - Experiences of API development in...
apidays
 
Product Management in HealthTech (Case Studies from SnappDoctor)
Hamed Shams
 
apidays Helsinki & North 2025 - Running a Successful API Program: Best Practi...
apidays
 
BinarySearchTree in datastructures in detail
kichokuttu
 
What does good look like - CRAP Brighton 8 July 2025
Jan Kierzyk
 
Data Retrieval and Preparation Business Analytics.pdf
kayserrakib80
 
SlideEgg_501298-Agentic AI.pptx agentic ai
530BYManoj
 
apidays Helsinki & North 2025 - API access control strategies beyond JWT bear...
apidays
 
apidays Helsinki & North 2025 - How (not) to run a Graphql Stewardship Group,...
apidays
 
apidays Singapore 2025 - From API Intelligence to API Governance by Harsha Ch...
apidays
 
apidays Singapore 2025 - From Data to Insights: Building AI-Powered Data APIs...
apidays
 
apidays Singapore 2025 - Building a Federated Future, Alex Szomora (GSMA)
apidays
 
OOPs with Java_unit2.pdf. sarthak bookkk
Sarthak964187
 
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
Listify-Intelligent-Voice-to-Catalog-Agent.pptx
nareshkottees
 
Research Methodology Overview Introduction
ayeshagul29594
 
OPPOTUS - Malaysias on Malaysia 1Q2025.pdf
Oppotus
 
apidays Helsinki & North 2025 - From Chaos to Clarity: Designing (AI-Ready) A...
apidays
 
Driving Employee Engagement in a Hybrid World.pdf
Mia scott
 

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