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 (9)

Operation research unit 3 Transportation problem
Operation research unit 3 Transportation problemOperation research unit 3 Transportation problem
Operation research unit 3 Transportation problem
Dr. L K Bhagi
 
Queueing Theory and its BusinessS Applications
Queueing Theory and its BusinessS ApplicationsQueueing Theory and its BusinessS Applications
Queueing Theory and its BusinessS Applications
Biswajit Bhattacharjee
 
Canonical form and Standard form of LPP
Canonical form and Standard form of LPPCanonical form and Standard form of LPP
Canonical form and Standard form of LPP
Sundar B N
 
English Literature Reading Skills - Layers of Meaning - Lord of the Flies Exa...
English Literature Reading Skills - Layers of Meaning - Lord of the Flies Exa...English Literature Reading Skills - Layers of Meaning - Lord of the Flies Exa...
English Literature Reading Skills - Layers of Meaning - Lord of the Flies Exa...
Apple Tree Education
 
Simplex method: Slack, Surplus & Artificial variable
Simplex method:  Slack, Surplus & Artificial variableSimplex method:  Slack, Surplus & Artificial variable
Simplex method: Slack, Surplus & Artificial variable
DevyaneeDevyanee2007
 
discrete-time-systems and discetre time fourier
discrete-time-systems and discetre time fourierdiscrete-time-systems and discetre time fourier
discrete-time-systems and discetre time fourier
pranavrajrkmv
 
Funtional analysis-BANACH SPACE
Funtional analysis-BANACH SPACEFuntional analysis-BANACH SPACE
Funtional analysis-BANACH SPACE
Manikanta satyala
 
0207 1 gradient
0207 1 gradient0207 1 gradient
0207 1 gradient
Jeonghun Yoon
 
K-MAP(KARNAUGH MAP)BY THE SILENT PROGRAMMER
K-MAP(KARNAUGH MAP)BY THE SILENT PROGRAMMERK-MAP(KARNAUGH MAP)BY THE SILENT PROGRAMMER
K-MAP(KARNAUGH MAP)BY THE SILENT PROGRAMMER
kunal kishore
 
Operation research unit 3 Transportation problem
Operation research unit 3 Transportation problemOperation research unit 3 Transportation problem
Operation research unit 3 Transportation problem
Dr. L K Bhagi
 
Queueing Theory and its BusinessS Applications
Queueing Theory and its BusinessS ApplicationsQueueing Theory and its BusinessS Applications
Queueing Theory and its BusinessS Applications
Biswajit Bhattacharjee
 
Canonical form and Standard form of LPP
Canonical form and Standard form of LPPCanonical form and Standard form of LPP
Canonical form and Standard form of LPP
Sundar B N
 
English Literature Reading Skills - Layers of Meaning - Lord of the Flies Exa...
English Literature Reading Skills - Layers of Meaning - Lord of the Flies Exa...English Literature Reading Skills - Layers of Meaning - Lord of the Flies Exa...
English Literature Reading Skills - Layers of Meaning - Lord of the Flies Exa...
Apple Tree Education
 
Simplex method: Slack, Surplus & Artificial variable
Simplex method:  Slack, Surplus & Artificial variableSimplex method:  Slack, Surplus & Artificial variable
Simplex method: Slack, Surplus & Artificial variable
DevyaneeDevyanee2007
 
discrete-time-systems and discetre time fourier
discrete-time-systems and discetre time fourierdiscrete-time-systems and discetre time fourier
discrete-time-systems and discetre time fourier
pranavrajrkmv
 
Funtional analysis-BANACH SPACE
Funtional analysis-BANACH SPACEFuntional analysis-BANACH SPACE
Funtional analysis-BANACH SPACE
Manikanta satyala
 
K-MAP(KARNAUGH MAP)BY THE SILENT PROGRAMMER
K-MAP(KARNAUGH MAP)BY THE SILENT PROGRAMMERK-MAP(KARNAUGH MAP)BY THE SILENT PROGRAMMER
K-MAP(KARNAUGH MAP)BY THE SILENT PROGRAMMER
kunal kishore
 

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

Linear programming wi̇th R
Linear programming wi̇th RLinear programming wi̇th R
Linear programming wi̇th R
Dr. Volkan OBAN
 
Investigación de operaciones 026 programación lineal Solución Simplex con R S...
Investigación de operaciones 026 programación lineal Solución Simplex con R S...Investigación de operaciones 026 programación lineal Solución Simplex con R S...
Investigación de operaciones 026 programación lineal Solución Simplex con R S...
Jorge Pablo Rivas
 
Itroroduction to R language
Itroroduction to R languageItroroduction to R language
Itroroduction to R language
chhabria-nitesh
 
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Dr. Volkan OBAN
 
Seminar on MATLAB
Seminar on MATLABSeminar on MATLAB
Seminar on MATLAB
Dharmesh Tank
 
Matlab ch1 intro
Matlab ch1 introMatlab ch1 intro
Matlab ch1 intro
Ragu Nathan
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
amanabr
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
Kurmendra Singh
 
lpSolve - R Library
lpSolve - R LibrarylpSolve - R Library
lpSolve - R Library
David Faris
 
MatlabIntro1234.ppt.....................
MatlabIntro1234.ppt.....................MatlabIntro1234.ppt.....................
MatlabIntro1234.ppt.....................
RajeshMadarkar
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
ShwetaPandey248972
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
Rajmohan Madasamy
 
Matlab intro
Matlab introMatlab intro
Matlab intro
THEMASTERBLASTERSVID
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
konkatisandeepkumar
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
ssuser772830
 
Quality Python Homework Help
Quality Python Homework HelpQuality Python Homework Help
Quality Python Homework Help
Python Homework Help
 
Matlab intro
Matlab introMatlab intro
Matlab intro
Chaitanya Banoth
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
Johan Tibell
 
Programming with matlab session 1
Programming with matlab session 1Programming with matlab session 1
Programming with matlab session 1
Infinity Tech Solutions
 
WIDI ediot autis dongok part 3.EDIOT LU LEMBOT LY
WIDI ediot autis dongok part 3.EDIOT LU LEMBOT LYWIDI ediot autis dongok part 3.EDIOT LU LEMBOT LY
WIDI ediot autis dongok part 3.EDIOT LU LEMBOT LY
IrlanMalik
 
Linear programming wi̇th R
Linear programming wi̇th RLinear programming wi̇th R
Linear programming wi̇th R
Dr. Volkan OBAN
 
Investigación de operaciones 026 programación lineal Solución Simplex con R S...
Investigación de operaciones 026 programación lineal Solución Simplex con R S...Investigación de operaciones 026 programación lineal Solución Simplex con R S...
Investigación de operaciones 026 programación lineal Solución Simplex con R S...
Jorge Pablo Rivas
 
Itroroduction to R language
Itroroduction to R languageItroroduction to R language
Itroroduction to R language
chhabria-nitesh
 
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Dr. Volkan OBAN
 
Matlab ch1 intro
Matlab ch1 introMatlab ch1 intro
Matlab ch1 intro
Ragu Nathan
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
amanabr
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
Kurmendra Singh
 
lpSolve - R Library
lpSolve - R LibrarylpSolve - R Library
lpSolve - R Library
David Faris
 
MatlabIntro1234.ppt.....................
MatlabIntro1234.ppt.....................MatlabIntro1234.ppt.....................
MatlabIntro1234.ppt.....................
RajeshMadarkar
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
Johan Tibell
 
WIDI ediot autis dongok part 3.EDIOT LU LEMBOT LY
WIDI ediot autis dongok part 3.EDIOT LU LEMBOT LYWIDI ediot autis dongok part 3.EDIOT LU LEMBOT LY
WIDI ediot autis dongok part 3.EDIOT LU LEMBOT LY
IrlanMalik
 
Ad

More from Dr. Volkan OBAN (20)

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

Recently uploaded (20)

apidays New York 2025 - CIAM in the wild by Michael Gruen (Layr)
apidays New York 2025 - CIAM in the wild by Michael Gruen (Layr)apidays New York 2025 - CIAM in the wild by Michael Gruen (Layr)
apidays New York 2025 - CIAM in the wild by Michael Gruen (Layr)
apidays
 
Media_Literacy_Index_of_Media_Sector_Employees.pdf
Media_Literacy_Index_of_Media_Sector_Employees.pdfMedia_Literacy_Index_of_Media_Sector_Employees.pdf
Media_Literacy_Index_of_Media_Sector_Employees.pdf
OlhaTatokhina1
 
apidays New York 2025 - Building Agentic Workflows with FDC3 Intents by Nick ...
apidays New York 2025 - Building Agentic Workflows with FDC3 Intents by Nick ...apidays New York 2025 - Building Agentic Workflows with FDC3 Intents by Nick ...
apidays New York 2025 - Building Agentic Workflows with FDC3 Intents by Nick ...
apidays
 
MEDIA_LITERACY_INDEX_OF_EDUCATORS_ENG.pdf
MEDIA_LITERACY_INDEX_OF_EDUCATORS_ENG.pdfMEDIA_LITERACY_INDEX_OF_EDUCATORS_ENG.pdf
MEDIA_LITERACY_INDEX_OF_EDUCATORS_ENG.pdf
OlhaTatokhina1
 
apidays New York 2025 - Boost API Development Velocity with Practical AI Tool...
apidays New York 2025 - Boost API Development Velocity with Practical AI Tool...apidays New York 2025 - Boost API Development Velocity with Practical AI Tool...
apidays New York 2025 - Boost API Development Velocity with Practical AI Tool...
apidays
 
apidays New York 2025 - Why I Built Another Carbon Measurement Tool for LLMs ...
apidays New York 2025 - Why I Built Another Carbon Measurement Tool for LLMs ...apidays New York 2025 - Why I Built Another Carbon Measurement Tool for LLMs ...
apidays New York 2025 - Why I Built Another Carbon Measurement Tool for LLMs ...
apidays
 
apidays Singapore 2025 - What exactly are AI Agents by Aki Ranin (Earthshots ...
apidays Singapore 2025 - What exactly are AI Agents by Aki Ranin (Earthshots ...apidays Singapore 2025 - What exactly are AI Agents by Aki Ranin (Earthshots ...
apidays Singapore 2025 - What exactly are AI Agents by Aki Ranin (Earthshots ...
apidays
 
Ch01_Introduction_to_Information_Securit
Ch01_Introduction_to_Information_SecuritCh01_Introduction_to_Information_Securit
Ch01_Introduction_to_Information_Securit
KawukiDerrick
 
1022_ExtendEnrichExcelUsingPythonWithTableau_04_16+04_17 (1).pdf
1022_ExtendEnrichExcelUsingPythonWithTableau_04_16+04_17 (1).pdf1022_ExtendEnrichExcelUsingPythonWithTableau_04_16+04_17 (1).pdf
1022_ExtendEnrichExcelUsingPythonWithTableau_04_16+04_17 (1).pdf
elinavihriala
 
apidays New York 2025 - Spring Modulith Design for Microservices by Renjith R...
apidays New York 2025 - Spring Modulith Design for Microservices by Renjith R...apidays New York 2025 - Spring Modulith Design for Microservices by Renjith R...
apidays New York 2025 - Spring Modulith Design for Microservices by Renjith R...
apidays
 
LONGSEM2024-25_CSE3015_ETH_AP2024256000125_Reference-Material-I.pptx
LONGSEM2024-25_CSE3015_ETH_AP2024256000125_Reference-Material-I.pptxLONGSEM2024-25_CSE3015_ETH_AP2024256000125_Reference-Material-I.pptx
LONGSEM2024-25_CSE3015_ETH_AP2024256000125_Reference-Material-I.pptx
vemuripraveena2622
 
1-2. Lab Introduction to Linux environment.ppt
1-2. Lab Introduction to Linux environment.ppt1-2. Lab Introduction to Linux environment.ppt
1-2. Lab Introduction to Linux environment.ppt
Wahajch
 
BODMAS-Rule-&-Unit-Digit-Concept-pdf.pdf
BODMAS-Rule-&-Unit-Digit-Concept-pdf.pdfBODMAS-Rule-&-Unit-Digit-Concept-pdf.pdf
BODMAS-Rule-&-Unit-Digit-Concept-pdf.pdf
SiddharthSean
 
Alcoholic liver disease slides presentation new.pptx
Alcoholic liver disease slides presentation new.pptxAlcoholic liver disease slides presentation new.pptx
Alcoholic liver disease slides presentation new.pptx
DrShashank7
 
THE FRIEDMAN TEST ( Biostatics B. Pharm)
THE FRIEDMAN TEST ( Biostatics B. Pharm)THE FRIEDMAN TEST ( Biostatics B. Pharm)
THE FRIEDMAN TEST ( Biostatics B. Pharm)
JishuHaldar
 
Part Departement Head Presentation for Business
Part Departement Head Presentation for BusinessPart Departement Head Presentation for Business
Part Departement Head Presentation for Business
Rizki229625
 
apidays New York 2025 - The Challenge is Not the Pattern, But the Best Integr...
apidays New York 2025 - The Challenge is Not the Pattern, But the Best Integr...apidays New York 2025 - The Challenge is Not the Pattern, But the Best Integr...
apidays New York 2025 - The Challenge is Not the Pattern, But the Best Integr...
apidays
 
apidays New York 2025 - Computers are still dumb by Ben Morss (DeepL)
apidays New York 2025 - Computers are still dumb by Ben Morss (DeepL)apidays New York 2025 - Computers are still dumb by Ben Morss (DeepL)
apidays New York 2025 - Computers are still dumb by Ben Morss (DeepL)
apidays
 
Mining Presentation Online Courses for Student
Mining Presentation Online Courses for StudentMining Presentation Online Courses for Student
Mining Presentation Online Courses for Student
Rizki229625
 
apidays Singapore 2025 - Building Finance Innovation Ecosystems by Umang Moon...
apidays Singapore 2025 - Building Finance Innovation Ecosystems by Umang Moon...apidays Singapore 2025 - Building Finance Innovation Ecosystems by Umang Moon...
apidays Singapore 2025 - Building Finance Innovation Ecosystems by Umang Moon...
apidays
 
apidays New York 2025 - CIAM in the wild by Michael Gruen (Layr)
apidays New York 2025 - CIAM in the wild by Michael Gruen (Layr)apidays New York 2025 - CIAM in the wild by Michael Gruen (Layr)
apidays New York 2025 - CIAM in the wild by Michael Gruen (Layr)
apidays
 
Media_Literacy_Index_of_Media_Sector_Employees.pdf
Media_Literacy_Index_of_Media_Sector_Employees.pdfMedia_Literacy_Index_of_Media_Sector_Employees.pdf
Media_Literacy_Index_of_Media_Sector_Employees.pdf
OlhaTatokhina1
 
apidays New York 2025 - Building Agentic Workflows with FDC3 Intents by Nick ...
apidays New York 2025 - Building Agentic Workflows with FDC3 Intents by Nick ...apidays New York 2025 - Building Agentic Workflows with FDC3 Intents by Nick ...
apidays New York 2025 - Building Agentic Workflows with FDC3 Intents by Nick ...
apidays
 
MEDIA_LITERACY_INDEX_OF_EDUCATORS_ENG.pdf
MEDIA_LITERACY_INDEX_OF_EDUCATORS_ENG.pdfMEDIA_LITERACY_INDEX_OF_EDUCATORS_ENG.pdf
MEDIA_LITERACY_INDEX_OF_EDUCATORS_ENG.pdf
OlhaTatokhina1
 
apidays New York 2025 - Boost API Development Velocity with Practical AI Tool...
apidays New York 2025 - Boost API Development Velocity with Practical AI Tool...apidays New York 2025 - Boost API Development Velocity with Practical AI Tool...
apidays New York 2025 - Boost API Development Velocity with Practical AI Tool...
apidays
 
apidays New York 2025 - Why I Built Another Carbon Measurement Tool for LLMs ...
apidays New York 2025 - Why I Built Another Carbon Measurement Tool for LLMs ...apidays New York 2025 - Why I Built Another Carbon Measurement Tool for LLMs ...
apidays New York 2025 - Why I Built Another Carbon Measurement Tool for LLMs ...
apidays
 
apidays Singapore 2025 - What exactly are AI Agents by Aki Ranin (Earthshots ...
apidays Singapore 2025 - What exactly are AI Agents by Aki Ranin (Earthshots ...apidays Singapore 2025 - What exactly are AI Agents by Aki Ranin (Earthshots ...
apidays Singapore 2025 - What exactly are AI Agents by Aki Ranin (Earthshots ...
apidays
 
Ch01_Introduction_to_Information_Securit
Ch01_Introduction_to_Information_SecuritCh01_Introduction_to_Information_Securit
Ch01_Introduction_to_Information_Securit
KawukiDerrick
 
1022_ExtendEnrichExcelUsingPythonWithTableau_04_16+04_17 (1).pdf
1022_ExtendEnrichExcelUsingPythonWithTableau_04_16+04_17 (1).pdf1022_ExtendEnrichExcelUsingPythonWithTableau_04_16+04_17 (1).pdf
1022_ExtendEnrichExcelUsingPythonWithTableau_04_16+04_17 (1).pdf
elinavihriala
 
apidays New York 2025 - Spring Modulith Design for Microservices by Renjith R...
apidays New York 2025 - Spring Modulith Design for Microservices by Renjith R...apidays New York 2025 - Spring Modulith Design for Microservices by Renjith R...
apidays New York 2025 - Spring Modulith Design for Microservices by Renjith R...
apidays
 
LONGSEM2024-25_CSE3015_ETH_AP2024256000125_Reference-Material-I.pptx
LONGSEM2024-25_CSE3015_ETH_AP2024256000125_Reference-Material-I.pptxLONGSEM2024-25_CSE3015_ETH_AP2024256000125_Reference-Material-I.pptx
LONGSEM2024-25_CSE3015_ETH_AP2024256000125_Reference-Material-I.pptx
vemuripraveena2622
 
1-2. Lab Introduction to Linux environment.ppt
1-2. Lab Introduction to Linux environment.ppt1-2. Lab Introduction to Linux environment.ppt
1-2. Lab Introduction to Linux environment.ppt
Wahajch
 
BODMAS-Rule-&-Unit-Digit-Concept-pdf.pdf
BODMAS-Rule-&-Unit-Digit-Concept-pdf.pdfBODMAS-Rule-&-Unit-Digit-Concept-pdf.pdf
BODMAS-Rule-&-Unit-Digit-Concept-pdf.pdf
SiddharthSean
 
Alcoholic liver disease slides presentation new.pptx
Alcoholic liver disease slides presentation new.pptxAlcoholic liver disease slides presentation new.pptx
Alcoholic liver disease slides presentation new.pptx
DrShashank7
 
THE FRIEDMAN TEST ( Biostatics B. Pharm)
THE FRIEDMAN TEST ( Biostatics B. Pharm)THE FRIEDMAN TEST ( Biostatics B. Pharm)
THE FRIEDMAN TEST ( Biostatics B. Pharm)
JishuHaldar
 
Part Departement Head Presentation for Business
Part Departement Head Presentation for BusinessPart Departement Head Presentation for Business
Part Departement Head Presentation for Business
Rizki229625
 
apidays New York 2025 - The Challenge is Not the Pattern, But the Best Integr...
apidays New York 2025 - The Challenge is Not the Pattern, But the Best Integr...apidays New York 2025 - The Challenge is Not the Pattern, But the Best Integr...
apidays New York 2025 - The Challenge is Not the Pattern, But the Best Integr...
apidays
 
apidays New York 2025 - Computers are still dumb by Ben Morss (DeepL)
apidays New York 2025 - Computers are still dumb by Ben Morss (DeepL)apidays New York 2025 - Computers are still dumb by Ben Morss (DeepL)
apidays New York 2025 - Computers are still dumb by Ben Morss (DeepL)
apidays
 
Mining Presentation Online Courses for Student
Mining Presentation Online Courses for StudentMining Presentation Online Courses for Student
Mining Presentation Online Courses for Student
Rizki229625
 
apidays Singapore 2025 - Building Finance Innovation Ecosystems by Umang Moon...
apidays Singapore 2025 - Building Finance Innovation Ecosystems by Umang Moon...apidays Singapore 2025 - Building Finance Innovation Ecosystems by Umang Moon...
apidays Singapore 2025 - Building Finance Innovation Ecosystems by Umang Moon...
apidays
 

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