Skip to content

Instantly share code, notes, and snippets.

@rsalaza4
Last active April 8, 2022 17:49
Show Gist options
  • Save rsalaza4/7e1877918545342e30cc8e6849767a2f to your computer and use it in GitHub Desktop.
Save rsalaza4/7e1877918545342e30cc8e6849767a2f to your computer and use it in GitHub Desktop.
# Import lpSolve package
library(lpSolve)
# Set coefficients of the objective function
f.obj <- c(5, 7)
# 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,
2, 3,
1, 1), nrow = 3, byrow = TRUE)
# Set unequality signs
f.dir <- c("<=",
"<=",
"<=")
# Set right hand side coefficients
f.rhs <- c(16,
19,
8)
# Final value (z)
lp("max", f.obj, f.con, f.dir, f.rhs)
# Variables final values
lp("max", f.obj, f.con, f.dir, f.rhs)$solution
# Sensitivities
lp("max", f.obj, f.con, f.dir, f.rhs, compute.sens=TRUE)$sens.coef.from
lp("max", f.obj, f.con, f.dir, f.rhs, compute.sens=TRUE)$sens.coef.to
# Dual Values (first dual of the constraints and then dual of the variables)
# Duals of the constraints and variables are mixed
lp("max", f.obj, f.con, f.dir, f.rhs, compute.sens=TRUE)$duals
# Duals lower and upper limits
lp("max", f.obj, f.con, f.dir, f.rhs, compute.sens=TRUE)$duals.from
lp("max", f.obj, f.con, f.dir, f.rhs, compute.sens=TRUE)$duals.to
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment