Title: | Linear Programming / Optimization |
---|---|
Description: | Can be used to solve Linear Programming / Linear Optimization problems by using the simplex algorithm. |
Authors: | Arne Henningsen |
Maintainer: | Arne Henningsen <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.9-4 |
Built: | 2024-10-30 02:43:45 UTC |
Source: | https://github.com/r-forge/linprog |
This method prints the results of the Linear Programming algorithm.
## S3 method for class 'solveLP' print( x, digits=6, ...)
## S3 method for class 'solveLP' print( x, digits=6, ...)
x |
an object returned by |
digits |
number of digits to print. |
... |
currently ignored. |
print.solveLP
invisibly returns the object given in argument x
.
Arne Henningsen
solveLP
, summary.solveLP
,
readMps
, writeMps
## example of Steinhauser, Langbehn and Peters (1992) ## Not run: library( linprog ) ## Production activities cvec <- c(1800, 600, 600) # gross margins names(cvec) <- c("Milk","Bulls","Pigs") ## Constraints (quasi-fix factors) bvec <- c(40, 90, 2500) # endowment names(bvec) <- c("Land","Stable","Labor") ## Needs of Production activities Amat <- rbind( c( 0.7, 0.35, 0 ), c( 1.5, 1, 3 ), c( 50, 12.5, 20 ) ) ## Maximize the gross margin res <- solveLP( cvec, bvec, Amat, TRUE ) ## print the results print( res )
## example of Steinhauser, Langbehn and Peters (1992) ## Not run: library( linprog ) ## Production activities cvec <- c(1800, 600, 600) # gross margins names(cvec) <- c("Milk","Bulls","Pigs") ## Constraints (quasi-fix factors) bvec <- c(40, 90, 2500) # endowment names(bvec) <- c("Land","Stable","Labor") ## Needs of Production activities Amat <- rbind( c( 0.7, 0.35, 0 ), c( 1.5, 1, 3 ), c( 50, 12.5, 20 ) ) ## Maximize the gross margin res <- solveLP( cvec, bvec, Amat, TRUE ) ## print the results print( res )
This function reads MPS files - the standard format for Linear Programming problems.
readMps( file, solve=FALSE, maximum=FALSE )
readMps( file, solve=FALSE, maximum=FALSE )
file |
a character string naming the file to read. |
solve |
logical. Should the problem be solved after reading it
from the file (using |
maximum |
logical. Should we maximize or minimize (the default)? |
Equality constraints and 'greater than'-bounds are not implemented yet.
readMps
returns a list containing following objects:
name |
the name of the Linear Programming problem. |
cvec |
vector |
bvec |
vector |
Amat |
matrix |
res |
if |
Arne Henningsen
## example of Steinhauser, Langbehn and Peters (1992) ## Production activities cvec <- c(1800, 600, 600) # gross margins names(cvec) <- c("Cows","Bulls","Pigs") ## Constraints (quasi-fix factors) bvec <- c(40, 90, 2500) # endowment names(bvec) <- c("Land","Stable","Labor") ## Needs of Production activities Amat <- rbind( c( 0.7, 0.35, 0 ), c( 1.5, 1, 3 ), c( 50, 12.5, 20 ) ) ## Write to MPS file writeMps( "steinh.mps", cvec, bvec, Amat, "Steinhauser" ) ## delete all LP objects rm( cvec, bvec, Amat ) ## Read LP data from MPS file and solve it. lp <- readMps( "steinh.mps", TRUE, TRUE ) ## Print the results lp$res ## remove the MPS file file.remove( "steinh.mps" )
## example of Steinhauser, Langbehn and Peters (1992) ## Production activities cvec <- c(1800, 600, 600) # gross margins names(cvec) <- c("Cows","Bulls","Pigs") ## Constraints (quasi-fix factors) bvec <- c(40, 90, 2500) # endowment names(bvec) <- c("Land","Stable","Labor") ## Needs of Production activities Amat <- rbind( c( 0.7, 0.35, 0 ), c( 1.5, 1, 3 ), c( 50, 12.5, 20 ) ) ## Write to MPS file writeMps( "steinh.mps", cvec, bvec, Amat, "Steinhauser" ) ## delete all LP objects rm( cvec, bvec, Amat ) ## Read LP data from MPS file and solve it. lp <- readMps( "steinh.mps", TRUE, TRUE ) ## Print the results lp$res ## remove the MPS file file.remove( "steinh.mps" )
Minimizes (or maximizes) , subject to
and
.
Note that the inequality signs <=
of the individual linear constraints
in can be changed with argument
const.dir
.
solveLP( cvec, bvec, Amat, maximum = FALSE, const.dir = rep( "<=", length( bvec ) ), maxiter = 1000, zero = 1e-9, tol = 1e-6, dualtol = tol, lpSolve = FALSE, solve.dual = FALSE, verbose = 0 )
solveLP( cvec, bvec, Amat, maximum = FALSE, const.dir = rep( "<=", length( bvec ) ), maxiter = 1000, zero = 1e-9, tol = 1e-6, dualtol = tol, lpSolve = FALSE, solve.dual = FALSE, verbose = 0 )
cvec |
vector |
bvec |
vector |
Amat |
matrix A (of dimension |
maximum |
logical. Should we maximize or minimize (the default)? |
const.dir |
vector of character strings giving the directions of the constraints: each value should be one of "<," "<=," "=," "==," ">," or ">=". (In each pair the two values are identical.) |
maxiter |
maximum number of iterations. |
zero |
numbers smaller than this value (in absolute terms) are set to zero. |
tol |
if the constraints are violated by more than this number,
the returned component |
dualtol |
if the constraints in the dual problem are violated by more than this number, the returned status is non-zero. |
lpSolve |
logical. Should the package 'lpSolve' be used to solve the LP problem? |
solve.dual |
logical value indicating if the dual problem should also be solved. |
verbose |
an optional integer variable to indicate how many intermediate results should be printed (0 = no output; 4 = maximum output). |
This function uses the Simplex algorithm of George B. Dantzig (1947)
and provides detailed results (e.g. dual prices, sensitivity analysis
and stability analysis).
If the solution is not feasible, a 2-phase procedure is
applied.
Values of the simplex tableau that are actually zero might get small
(positive or negative) numbers due to rounding errors, which might
lead to artificial restrictions. Therefore, all values that are smaller
(in absolute terms) than the value of zero
(default is 1e-10) are
set to 0.
Solving the Linear Programming problem by the package lpSolve
(of course) requires the installation of this package, which is available
on CRAN (https://cran.r-project.org/package=lpSolve).
Since the lpSolve
package uses C-code and this (linprog
)
package is not optimized for speed, the former is much faster.
However, this package provides more detailed results (e.g. dual values,
stability and sensitivity analysis).
This function has not been tested extensively and might not solve all
feasible problems (or might even lead to wrong results). However, you can
export your LP to a standard MPS file via writeMps
and check
it with other software (e.g. lp_solve
, see
http://lpsolve.sourceforge.net/5.5/).
Equality constraints are not implemented yet.
solveLP
returns a list of the class solveLP
containing following objects:
opt |
optimal value (minimum or maximum) of the objective function. |
solution |
vector of optimal values of the variables. |
iter1 |
iterations of Simplex algorithm in phase 1. |
iter2 |
iterations of Simplex algorithm in phase 2. |
basvar |
vector of basic (=non-zero) variables (at optimum). |
con |
matrix of results regarding the constraints: |
allvar |
matrix of results regarding all variables (including slack variables): |
status |
numeric. Indicates if the optimization did succeed: |
lpStatus |
numeric. Return code of |
dualStatus |
numeric. Return code from solving the dual problem
(only if argument |
maximum |
logical. Indicates whether the objective function was maximized or minimized. |
Tab |
final 'Tableau' of the Simplex algorith. |
lpSolve |
logical. Has the package 'lpSolve' been used to solve the LP problem. |
solve.dual |
logical. Argument |
maxiter |
numeric. Argument |
Arne Henningsen
Dantzig, George B. (1951), Maximization of a linear function of variables subject to linear inequalities, in Koopmans, T.C. (ed.), Activity analysis of production and allocation, John Wiley \& Sons, New York, p. 339-347.
Steinhauser, Hugo; Cay Langbehn and Uwe Peters (1992), Einfuehrung in die landwirtschaftliche Betriebslehre. Allgemeiner Teil, 5th ed., Ulmer, Stuttgart.
Witte, Thomas; Joerg-Frieder Deppe and Axel Born (1975), Lineare Programmierung. Einfuehrung fuer Wirtschaftswissenschaftler, Gabler-Verlag, Wiesbaden.
## example of Steinhauser, Langbehn and Peters (1992) ## Production activities cvec <- c(1800, 600, 600) # gross margins names(cvec) <- c("Cows","Bulls","Pigs") ## Constraints (quasi-fix factors) bvec <- c(40, 90, 2500) # endowment names(bvec) <- c("Land","Stable","Labor") ## Needs of Production activities Amat <- rbind( c( 0.7, 0.35, 0 ), c( 1.5, 1, 3 ), c( 50, 12.5, 20 ) ) ## Maximize the gross margin solveLP( cvec, bvec, Amat, TRUE ) ## example 1.1.3 of Witte, Deppe and Born (1975) ## Two types of Feed cvec <- c(2.5, 2 ) # prices of feed names(cvec) <- c("Feed1","Feed2") ## Constraints (minimum (<0) and maximum (>0) contents) bvec <- c(-10, -1.5, 12) names(bvec) <- c("Protein","Fat","Fibre") ## Matrix A Amat <- rbind( c( -1.6, -2.4 ), c( -0.5, -0.2 ), c( 2.0, 2.0 ) ) ## Minimize the cost solveLP( cvec, bvec, Amat ) # the same optimisation using argument const.dir solveLP( cvec, abs( bvec ), abs( Amat ), const.dir = c( ">=", ">=", "<=" ) ) ## There are also several other ways to put the data into the arrays, e.g.: bvec <- c( Protein = -10.0, Fat = -1.5, Fibre = 12.0 ) cvec <- c( Feed1 = 2.5, Feed2 = 2.0 ) Amat <- matrix( 0, length(bvec), length(cvec) ) rownames(Amat) <- names(bvec) colnames(Amat) <- names(cvec) Amat[ "Protein", "Feed1" ] <- -1.6 Amat[ "Fat", "Feed1" ] <- -0.5 Amat[ "Fibre", "Feed1" ] <- 2.0 Amat[ "Protein", "Feed2" ] <- -2.4 Amat[ "Fat", "Feed2" ] <- -0.2 Amat[ "Fibre", "Feed2" ] <- 2.0 solveLP( cvec, bvec, Amat )
## example of Steinhauser, Langbehn and Peters (1992) ## Production activities cvec <- c(1800, 600, 600) # gross margins names(cvec) <- c("Cows","Bulls","Pigs") ## Constraints (quasi-fix factors) bvec <- c(40, 90, 2500) # endowment names(bvec) <- c("Land","Stable","Labor") ## Needs of Production activities Amat <- rbind( c( 0.7, 0.35, 0 ), c( 1.5, 1, 3 ), c( 50, 12.5, 20 ) ) ## Maximize the gross margin solveLP( cvec, bvec, Amat, TRUE ) ## example 1.1.3 of Witte, Deppe and Born (1975) ## Two types of Feed cvec <- c(2.5, 2 ) # prices of feed names(cvec) <- c("Feed1","Feed2") ## Constraints (minimum (<0) and maximum (>0) contents) bvec <- c(-10, -1.5, 12) names(bvec) <- c("Protein","Fat","Fibre") ## Matrix A Amat <- rbind( c( -1.6, -2.4 ), c( -0.5, -0.2 ), c( 2.0, 2.0 ) ) ## Minimize the cost solveLP( cvec, bvec, Amat ) # the same optimisation using argument const.dir solveLP( cvec, abs( bvec ), abs( Amat ), const.dir = c( ">=", ">=", "<=" ) ) ## There are also several other ways to put the data into the arrays, e.g.: bvec <- c( Protein = -10.0, Fat = -1.5, Fibre = 12.0 ) cvec <- c( Feed1 = 2.5, Feed2 = 2.0 ) Amat <- matrix( 0, length(bvec), length(cvec) ) rownames(Amat) <- names(bvec) colnames(Amat) <- names(cvec) Amat[ "Protein", "Feed1" ] <- -1.6 Amat[ "Fat", "Feed1" ] <- -0.5 Amat[ "Fibre", "Feed1" ] <- 2.0 Amat[ "Protein", "Feed2" ] <- -2.4 Amat[ "Fat", "Feed2" ] <- -0.2 Amat[ "Fibre", "Feed2" ] <- 2.0 solveLP( cvec, bvec, Amat )
These methods prepare and print summary results of the Linear Programming algorithm.
## S3 method for class 'solveLP' summary(object,...) ## S3 method for class 'summary.solveLP' print(x,...)
## S3 method for class 'solveLP' summary(object,...) ## S3 method for class 'summary.solveLP' print(x,...)
object |
an object returned by |
x |
an object returned by |
... |
currently ignored. |
summary.solveLP
returns an object of class summary.solveLP
.
print.summary.solveLP
invisibly returns the object given
in argument x
.
Arne Henningsen
solveLP
, print.solveLP
,
readMps
, writeMps
## example of Steinhauser, Langbehn and Peters (1992) ## Not run: library( linprog ) ## Production activities cvec <- c(1800, 600, 600) # gross margins names(cvec) <- c("Milk","Bulls","Pigs") ## Constraints (quasi-fix factors) bvec <- c(40, 90, 2500) # endowment names(bvec) <- c("Land","Stable","Labor") ## Needs of Production activities Amat <- rbind( c( 0.7, 0.35, 0 ), c( 1.5, 1, 3 ), c( 50, 12.5, 20 ) ) ## Maximize the gross margin res <- solveLP( cvec, bvec, Amat, TRUE ) ## prepare and print the summary results summary( res )
## example of Steinhauser, Langbehn and Peters (1992) ## Not run: library( linprog ) ## Production activities cvec <- c(1800, 600, 600) # gross margins names(cvec) <- c("Milk","Bulls","Pigs") ## Constraints (quasi-fix factors) bvec <- c(40, 90, 2500) # endowment names(bvec) <- c("Land","Stable","Labor") ## Needs of Production activities Amat <- rbind( c( 0.7, 0.35, 0 ), c( 1.5, 1, 3 ), c( 50, 12.5, 20 ) ) ## Maximize the gross margin res <- solveLP( cvec, bvec, Amat, TRUE ) ## prepare and print the summary results summary( res )
This function writes MPS files - the standard format for Linear Programming problems.
writeMps( file, cvec, bvec, Amat, name="LP" )
writeMps( file, cvec, bvec, Amat, name="LP" )
file |
a character string naming the file to write. |
cvec |
vector |
bvec |
vector |
Amat |
matrix |
name |
an optional name for the Linear Programming problem. |
The exported LP can be solved by running other software on this MPS file
(e.g. lp_solve
, see http://lpsolve.sourceforge.net/5.5/).
Arne Henningsen
## example of Steinhauser, Langbehn and Peters (1992) ## Production activities cvec <- c(1800, 600, 600) # gross margins names(cvec) <- c("Cows","Bulls","Pigs") ## Constraints (quasi-fix factors) bvec <- c(40, 90, 2500) # endowment names(bvec) <- c("Land","Stable","Labor") ## Needs of Production activities Amat <- rbind( c( 0.7, 0.35, 0 ), c( 1.5, 1, 3 ), c( 50, 12.5, 20 ) ) ## Write to MPS file writeMps( "steinh.mps", cvec, bvec, Amat, "Steinhauser" ) ## remove the MPS file file.remove( "steinh.mps" )
## example of Steinhauser, Langbehn and Peters (1992) ## Production activities cvec <- c(1800, 600, 600) # gross margins names(cvec) <- c("Cows","Bulls","Pigs") ## Constraints (quasi-fix factors) bvec <- c(40, 90, 2500) # endowment names(bvec) <- c("Land","Stable","Labor") ## Needs of Production activities Amat <- rbind( c( 0.7, 0.35, 0 ), c( 1.5, 1, 3 ), c( 50, 12.5, 20 ) ) ## Write to MPS file writeMps( "steinh.mps", cvec, bvec, Amat, "Steinhauser" ) ## remove the MPS file file.remove( "steinh.mps" )