R for solving linear programming problems
Asked Answered
I

2

7

I am searching for a nice R package to solve linear programming models. I'm quite happy with the default lpSolve::lp, but there's no way to get the shadow and reduced prices. I need these, together with integrality constraints.

Sample model:

A = rbind(
    c(0.5, 0.2, 0.2),
    c( -1,   1,   0),
    c(  0,   1,  -1),
    c( -1,  -1,  -1),
    c( -1,   0,   0),
    c(  0,  -1,   0),
    c(  0,   0,  -1)
)
b = c(5, 0, 0, -13, 0, 0, 0)
c_ = c(8.4, 6, 9.2)
(signs = c('=', rep('<=', 6)))

res = lpSolve::lp('min', c_, A, signs, b,  all.int = TRUE)

# Objective function
res
# Variables
res$solution

# Shadow prices???
# Reduced prices???
Illdefined answered 13/4, 2013 at 16:27 Comment(4)
Sorry, what's shadow and reduced prices?Ergosterol
@Ergosterol it's a dual variable - see en.wikipedia.org/wiki/Shadow_priceIlldefined
Page 4 in this documentation talks about dual values for constraints. Is this what you're looking for?Ergosterol
@Ergosterol yep, that's the one. Make an answer and I'll accept it.Illdefined
E
4

As said under comments, page 4 of the documentation talks about this. Here's an excerpt from the documentation:

# Get sensitivities
lp ("max", f.obj, f.con, f.dir, f.rhs, compute.sens=TRUE)$sens.coef.from
## Not run: [1] -1e+30 2e+00 -1e+30
lp ("max", f.obj, f.con, f.dir, f.rhs, compute.sens=TRUE)$sens.coef.to
## Not run: [1] 4.50e+00 1.00e+30 1.35e+01

# Right now the dual values for the constraints and the variables are
# combined, constraints coming first. So in this example...

lp ("max", f.obj, f.con, f.dir, f.rhs, compute.sens=TRUE)$duals
## Not run: [1] 4.5 0.0 -3.5 0.0 -10.5

# ...the duals of the constraints are 4.5 and 0, and of the variables,
# -3.5, 0.0, -10.5. Here are the lower and upper limits on these:

lp ("max", f.obj, f.con, f.dir, f.rhs, compute.sens=TRUE)$duals.from
## Not run: [1] 0e+00 -1e+30 -1e+30 -1e+30 -6e+00
lp ("max", f.obj, f.con, f.dir, f.rhs, compute.sens=TRUE)$duals.to
## Not run: [1] 1.5e+01 1.0e+30 3.0e+00 1.0e+30 3.0e+00
Ergosterol answered 14/4, 2013 at 13:18 Comment(0)
N
4

A search with

sos::findFn("integer shadow reduced")

... returns links to the Benchmarking package stating that it will deliver dual values (shadow prices).

http://finzi.psych.upenn.edu/R/library/Benchmarking/html/Benchmarking-package.html

Not answered 13/4, 2013 at 17:36 Comment(0)
E
4

As said under comments, page 4 of the documentation talks about this. Here's an excerpt from the documentation:

# Get sensitivities
lp ("max", f.obj, f.con, f.dir, f.rhs, compute.sens=TRUE)$sens.coef.from
## Not run: [1] -1e+30 2e+00 -1e+30
lp ("max", f.obj, f.con, f.dir, f.rhs, compute.sens=TRUE)$sens.coef.to
## Not run: [1] 4.50e+00 1.00e+30 1.35e+01

# Right now the dual values for the constraints and the variables are
# combined, constraints coming first. So in this example...

lp ("max", f.obj, f.con, f.dir, f.rhs, compute.sens=TRUE)$duals
## Not run: [1] 4.5 0.0 -3.5 0.0 -10.5

# ...the duals of the constraints are 4.5 and 0, and of the variables,
# -3.5, 0.0, -10.5. Here are the lower and upper limits on these:

lp ("max", f.obj, f.con, f.dir, f.rhs, compute.sens=TRUE)$duals.from
## Not run: [1] 0e+00 -1e+30 -1e+30 -1e+30 -6e+00
lp ("max", f.obj, f.con, f.dir, f.rhs, compute.sens=TRUE)$duals.to
## Not run: [1] 1.5e+01 1.0e+30 3.0e+00 1.0e+30 3.0e+00
Ergosterol answered 14/4, 2013 at 13:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.