Code
#install.packages("lpSolve")
library(lpSolve)Tony Duan
October 12, 2023

using two differnct package to solve Linear Programming Problem lpSolve and linprog
How many hectares of each habitat should be conserved? The decision is not so simple because you have a number of decision constraints to contend with. These constraints contain bits of information that need to be included in the decision problem (such as the cost per hectare or the total new plant species that are added with each new hectare conserved).
• Budget: The maximum budget is 24,000 pickles.
• Total unique plant species by habitat:
– valley = 350
– highland = 240
• Number of new species conserved for each additional hectare purchased:
– valley = 5
– highland = 3
• Edge effects
– valley = 15
– highland = 25
• Political Considerations
– valley = 2
– highland = 2
• Social upheaval per hectare
– valley = 5
– highland = 2
• Cost per hectare
– valley = 300
– highland = 200
Max(5v + 3h)
1v + 0h >= 15 (edge effects for valley)
0v + 1h >= 25 (edge effects for highland)
1v - 2h <= 0 (politics constraining valley purchase)
-2v + 1h <= 0 (politics constraining highland purchase)
5v + 2h <= 400 (social upheaval constraint)
300v + 200h <= 24000 (budge constraint)
1v + 0h <= 70 (species richness valley constraint)
0v + 1h <= 60 (species richness highland constraint)
How many v and how many h ?
lpSolve# first, let's define the coefficients for the function to be optimized
f.obj <- c(5, 3)
# Set matrix corresponding to coefficients of constraints by rows
# Do not consider the non-negative constraint; it is automatically assumed
f.con <- matrix(c(1, 0, 0, 1, 1, -2, -1, 1, 5, 2, 300, 200, 1, 0, 0, 1),
nrow = 8,
byrow = TRUE)
# Set unequality signs
f.dir <- c(">=", ">=", "<=", "<=", "<=", "<=", "<=", "<=")
# Set right hand side coefficients
f.rhs <- c(15, 25, 0, 0, 400, 24000, 70, 60)1v + 0h >= 15 (edge effects for valley)
0v + 1h >= 25 (edge effects for highland)
1v - 2h <= 0 (politics constraining valley purchase)
-2v + 1h <= 0 (politics constraining highland purchase)
5v + 2h <= 400 (social upheaval constraint)
300v + 200h <= 24000 (budge constraint)
1v + 0h <= 70 (species richness valley constraint)
0v + 1h <= 60 (species richness highland constraint)
so buy 5 valley(60) and buy 3 highland(30)
linprog# first, let's define the coefficients for the function to be optimized
f.obj <- c(5, 3)
# Set matrix corresponding to coefficients of constraints by rows
# Do not consider the non-negative constraint; it is automatically assumed
f.con <- matrix(c(1, 0, 0, 1, 1, -2, -1, 1, 5, 2, 300, 200, 1, 0, 0, 1),
nrow = 8,
byrow = TRUE)
# Set unequality signs
f.dir <- c(">=", ">=", "<=", "<=", "<=", "<=", "<=", "<=")
# Set right hand side coefficients
f.rhs <- c(15, 25, 0, 0, 400, 24000, 70, 60)
Results of Linear Programming / Linear Optimization
Objective function (Maximum): 390
Iterations in phase 1: 3
Iterations in phase 2: 1
Solution
opt
1 60
2 30
Basic Variables
opt
1 60
2 30
S 1 45
S 2 5
S 4 30
S 5 40
S 7 10
S 8 30
Constraints
actual dir bvec free dual dual.reg
1 60 >= 15 45 0.00000 45
2 30 >= 25 5 0.00000 5
3 0 <= 0 0 0.12500 48
4 -30 <= 0 30 0.00000 30
5 360 <= 400 40 0.00000 40
6 24000 <= 24000 0 0.01625 4000
7 60 <= 70 10 0.00000 10
8 30 <= 60 30 0.00000 30
All Variables (including slack variables)
opt cvec min.c max.c marg marg.reg
1 60 5 4.500000 Inf NA NA
2 30 3 -10.000000 3.333333 NA NA
S 1 45 0 -0.500000 Inf 0.00000 NA
S 2 5 0 -13.000000 0.333333 0.00000 NA
S 3 0 0 -Inf 0.125000 -0.12500 48
S 4 30 0 -0.200000 Inf 0.00000 NA
S 5 40 0 NA 0.250000 0.00000 NA
S 6 0 0 -Inf 0.016250 -0.01625 4000
S 7 10 0 NA 0.500000 0.00000 NA
S 8 30 0 -0.333333 13.000000 0.00000 NA
Same result: buy 5 valley(60) and buy 3 highland(30)
https://blog.uvm.edu/tdonovan-vtcfwru/files/2020/08/LinearProgramming.pdf
https://github.com/gaborcsardi/lpSolve
https://www.youtube.com/watch?v=E72DWgKP_1Y
---
title: "Linear Programming Problem (LPP) in R"
author: "Tony Duan"
date: "2023-10-12"
categories: [R]
execute:
warning: false
error: false
format:
html:
toc: true
toc-location: left
code-fold: show
code-tools: true
number-sections: true
code-block-bg: true
code-block-border-left: "#31BAE9"
---
{width="300"}
using two differnct package to solve Linear Programming Problem `lpSolve` and `linprog`
## problem:
How many hectares of each habitat should be conserved? The decision is not so simple because you have a
number of decision constraints to contend with. These constraints contain bits of information that need to be
included in the decision problem (such as the cost per hectare or the total new plant species that are added
with each new hectare conserved).
• Budget: The maximum budget is 24,000 pickles.
• Total unique plant species by habitat:
– valley = 350
– highland = 240
• Number of new species conserved for each additional hectare purchased:
– valley = 5
– highland = 3
• Edge effects
– valley = 15
– highland = 25
• Political Considerations
– valley = 2
– highland = 2
• Social upheaval per hectare
– valley = 5
– highland = 2
• Cost per hectare
– valley = 300
– highland = 200
## putting to LP
### direction and objective:
Max(5v + 3h)
- variable
valley
highland
### constraint:
1v + 0h >= 15 (edge effects for valley)
0v + 1h >= 25 (edge effects for highland)
1v - 2h <= 0 (politics constraining valley purchase)
-2v + 1h <= 0 (politics constraining highland purchase)
5v + 2h <= 400 (social upheaval constraint)
300v + 200h <= 24000 (budge constraint)
1v + 0h <= 70 (species richness valley constraint)
0v + 1h <= 60 (species richness highland constraint)
### Question:
How many v and how many h ?
## solve in `lpSolve`
### package
```{r}
#install.packages("lpSolve")
library(lpSolve)
```
### define
```{r}
# first, let's define the coefficients for the function to be optimized
f.obj <- c(5, 3)
# Set matrix corresponding to coefficients of constraints by rows
# Do not consider the non-negative constraint; it is automatically assumed
f.con <- matrix(c(1, 0, 0, 1, 1, -2, -1, 1, 5, 2, 300, 200, 1, 0, 0, 1),
nrow = 8,
byrow = TRUE)
# Set unequality signs
f.dir <- c(">=", ">=", "<=", "<=", "<=", "<=", "<=", "<=")
# Set right hand side coefficients
f.rhs <- c(15, 25, 0, 0, 400, 24000, 70, 60)
```
### formula
1v + 0h >= 15 (edge effects for valley)
0v + 1h >= 25 (edge effects for highland)
1v - 2h <= 0 (politics constraining valley purchase)
-2v + 1h <= 0 (politics constraining highland purchase)
5v + 2h <= 400 (social upheaval constraint)
300v + 200h <= 24000 (budge constraint)
1v + 0h <= 70 (species richness valley constraint)
0v + 1h <= 60 (species richness highland constraint)
### left side of the constrain
```{r}
f.con
```
### sign of the constrain
```{r}
f.dir
```
### right side of the constrain
```{r}
f.rhs
```
### run LP
```{r}
# Now run
results=lp(direction = "max",
objective.in = f.obj,
const.mat = f.con,
const.dir = f.dir,
const.rhs = f.rhs,
all.int = T
)
```
### result
```{r}
results
```
```{r}
# Variables final values
results$objval
```
```{r}
# Variables final values
results$solution
```
```{r}
60 *5+30*3
```
so buy 5 valley(60) and buy 3 highland(30)
## solve in `linprog`
```{r}
#install.packages("linprog")
library("linprog")
```
### define
```{r}
# first, let's define the coefficients for the function to be optimized
f.obj <- c(5, 3)
# Set matrix corresponding to coefficients of constraints by rows
# Do not consider the non-negative constraint; it is automatically assumed
f.con <- matrix(c(1, 0, 0, 1, 1, -2, -1, 1, 5, 2, 300, 200, 1, 0, 0, 1),
nrow = 8,
byrow = TRUE)
# Set unequality signs
f.dir <- c(">=", ">=", "<=", "<=", "<=", "<=", "<=", "<=")
# Set right hand side coefficients
f.rhs <- c(15, 25, 0, 0, 400, 24000, 70, 60)
```
### run LP
```{r}
linprog_results=solveLP(cvec = f.obj,
bvec = f.rhs,
Amat = f.con,
maximum = TRUE,
const.dir = f.dir)
```
### result
```{r}
linprog_results
```
Same result: buy 5 valley(60) and buy 3 highland(30)
## Reference
https://blog.uvm.edu/tdonovan-vtcfwru/files/2020/08/LinearProgramming.pdf
https://github.com/gaborcsardi/lpSolve
https://www.youtube.com/watch?v=E72DWgKP_1Y