R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. > ### *
> ### > attach(NULL, name = "CheckExEnv") > assign(".CheckExEnv", as.environment(2), pos = length(search())) # base > ## add some hooks to label plot pages for base and grid graphics > setHook("plot.new", ".newplot.hook") > setHook("persp", ".newplot.hook") > setHook("grid.newpage", ".gridplot.hook") > > assign("cleanEx", + function(env = .GlobalEnv) { + rm(list = ls(envir = env, all.names = TRUE), envir = env) + RNGkind("default", "default") + set.seed(1) + options(warn = 1) + delayedAssign("T", stop("T used instead of TRUE"), + assign.env = .CheckExEnv) + delayedAssign("F", stop("F used instead of FALSE"), + assign.env = .CheckExEnv) + sch <- search() + newitems <- sch[! sch %in% .oldSearch] + for(item in rev(newitems)) + eval(substitute(detach(item), list(item=item))) + missitems <- .oldSearch[! .oldSearch %in% sch] + if(length(missitems)) + warning("items ", paste(missitems, collapse=", "), + " have been removed from the search path") + }, + env = .CheckExEnv) > assign("..nameEx", "__{must remake R-ex/*.R}__", env = .CheckExEnv) # for now > assign("ptime", proc.time(), env = .CheckExEnv) > grDevices::postscript("linprog-Examples.ps") > assign("par.postscript", graphics::par(no.readonly = TRUE), env = .CheckExEnv) > options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")) > options(warn = 1) > library('linprog') > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "print.solveLP" > > ### * print.solveLP > > flush(stderr()); flush(stdout()) > > ### Name: print.solveLP > ### Title: print.solveLP > ### Aliases: print.solveLP > ### Keywords: optimize > > ### ** Examples > > > ## 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.solveLP( res ) Results of Linear Programming / Linear Optimization Objective function (Maximum): [1] 93600 Iterations in phase 1: 0 Iterations in phase 2: 2 Basic Variables opt Milk 44.0 Bulls 24.0 S Land 0.8 Constraints max actual diff dual price dual.reg Land 40 39.2 0.8 0.0 0.8 Stable 90 90.0 0.0 240.0 15.0 Labor 2500 2500.0 0.0 28.8 1375.0 All Variables (including slack variables) opt c min c max c marg. marg.reg. Milk 44.0 1800 900 2400.000 NA NA Bulls 24.0 600 450 1200.000 NA NA Pigs 0.0 600 -Inf 1296.000 -696.0 6.25 S Land 0.8 0 NA 731.092 0.0 0.80 S Stable 0.0 0 NA NA -240.0 15.00 S Labor 0.0 0 NA NA -28.8 1375.00 > > > > cleanEx(); ..nameEx <- "readMps" > > ### * readMps > > flush(stderr()); flush(stdout()) > > ### Name: readMps > ### Title: read MPS files > ### Aliases: readMps > ### Keywords: optimize > > ### ** Examples > > > ## 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 ) ) > > ## 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 ) Warning in return(name = name, cvec = cvec, bvec = bvec, Amat = Amat, res = res) : multi-argument returns are deprecated > > ## Print the results > lp$res Results of Linear Programming / Linear Optimization Objective function (Maximum): [1] 93600 Iterations in phase 1: 0 Iterations in phase 2: 2 Basic Variables opt Milk 44.0 Bulls 24.0 S Land 0.8 Constraints max actual diff dual price dual.reg Land 40 39.2 0.8 0.0 0.8 Stable 90 90.0 0.0 240.0 15.0 Labor 2500 2500.0 0.0 28.8 1375.0 All Variables (including slack variables) opt c min c max c marg. marg.reg. Milk 44.0 1800 900 2400.000 NA NA Bulls 24.0 600 450 1200.000 NA NA Pigs 0.0 600 -Inf 1296.000 -696.0 6.25 S Land 0.8 0 NA 731.092 0.0 0.80 S Stable 0.0 0 NA NA -240.0 15.00 S Labor 0.0 0 NA NA -28.8 1375.00 > > > > > cleanEx(); ..nameEx <- "solveLP" > > ### * solveLP > > flush(stderr()); flush(stdout()) > > ### Name: solveLP > ### Title: solve Linear Programming / Optimization problems > ### Aliases: solveLP > ### Keywords: optimize > > ### ** Examples > > > ## 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 > solveLP( cvec, bvec, Amat, TRUE ) Results of Linear Programming / Linear Optimization Objective function (Maximum): [1] 93600 Iterations in phase 1: 0 Iterations in phase 2: 2 Basic Variables opt Milk 44.0 Bulls 24.0 S Land 0.8 Constraints max actual diff dual price dual.reg Land 40 39.2 0.8 0.0 0.8 Stable 90 90.0 0.0 240.0 15.0 Labor 2500 2500.0 0.0 28.8 1375.0 All Variables (including slack variables) opt c min c max c marg. marg.reg. Milk 44.0 1800 900 2400.000 NA NA Bulls 24.0 600 450 1200.000 NA NA Pigs 0.0 600 -Inf 1296.000 -696.0 6.25 S Land 0.8 0 NA 731.092 0.0 0.80 S Stable 0.0 0 NA NA -240.0 15.00 S Labor 0.0 0 NA NA -28.8 1375.00 > > ## example 1.1.3 of Witte, Deppe and Born (1975) > ## Not run: library( linprog ) > > ## 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 ) Results of Linear Programming / Linear Optimization Objective function (Minimum): [1] 10.4545 Iterations in phase 1: 3 Iterations in phase 2: 1 Basic Variables opt Feed1 1.81818 Feed2 2.95455 S Fibre 2.45455 Constraints max actual diff dual price dual.reg Protein -10.0 -10.00000 0.00000 -0.568182 3.60000 Fat -1.5 -1.50000 0.00000 -3.181818 1.35000 Fibre 12.0 9.54545 2.45455 0.000000 2.45455 All Variables (including slack variables) opt c min c max c marg. marg.reg. Feed1 1.81818 2.5 1.33333 5.000000 NA NA Feed2 2.95455 2.0 1.00000 3.750000 NA NA S Protein 0.00000 0.0 NA NA 0.568182 3.60000 S Fat 0.00000 0.0 NA NA 3.181818 1.35000 S Fibre 2.45455 0.0 -Inf 0.833333 0.000000 2.45455 > > ## 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 ) Results of Linear Programming / Linear Optimization Objective function (Minimum): [1] 10.4545 Iterations in phase 1: 3 Iterations in phase 2: 1 Basic Variables opt Feed1 1.81818 Feed2 2.95455 S Fibre 2.45455 Constraints max actual diff dual price dual.reg Protein -10.0 -10.00000 0.00000 -0.568182 3.60000 Fat -1.5 -1.50000 0.00000 -3.181818 1.35000 Fibre 12.0 9.54545 2.45455 0.000000 2.45455 All Variables (including slack variables) opt c min c max c marg. marg.reg. Feed1 1.81818 2.5 1.33333 5.000000 NA NA Feed2 2.95455 2.0 1.00000 3.750000 NA NA S Protein 0.00000 0.0 NA NA 0.568182 3.60000 S Fat 0.00000 0.0 NA NA 3.181818 1.35000 S Fibre 2.45455 0.0 -Inf 0.833333 0.000000 2.45455 > > > > cleanEx(); ..nameEx <- "summary.solveLP" > > ### * summary.solveLP > > flush(stderr()); flush(stdout()) > > ### Name: summary.solveLP > ### Title: summary.solveLP > ### Aliases: summary.solveLP > ### Keywords: optimize > > ### ** Examples > > > ## 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 > summary.solveLP( res ) Results of Linear Programming / Linear Optimization Objective function (Maximum): [1] 93600 Iterations in phase 1: 0 Iterations in phase 2: 2 Basic Variables opt Milk 44.0 Bulls 24.0 S Land 0.8 Constraints max actual diff dual price dual.reg Land 40 39.2 0.8 0.0 0.8 Stable 90 90.0 0.0 240.0 15.0 Labor 2500 2500.0 0.0 28.8 1375.0 All Variables (including slack variables) opt c min c max c marg. marg.reg. Milk 44.0 1800 900 2400.000 NA NA Bulls 24.0 600 450 1200.000 NA NA Pigs 0.0 600 -Inf 1296.000 -696.0 6.25 S Land 0.8 0 NA 731.092 0.0 0.80 S Stable 0.0 0 NA NA -240.0 15.00 S Labor 0.0 0 NA NA -28.8 1375.00 > > > > cleanEx(); ..nameEx <- "writeMps" > > ### * writeMps > > flush(stderr()); flush(stdout()) > > ### Name: writeMps > ### Title: write MPS files > ### Aliases: writeMps > ### Keywords: optimize > > ### ** Examples > > > ## 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 ) ) > > ## Write to MPS file > writeMps( "steinh.mps", cvec, bvec, Amat, "Steinhauser" ) > > > > ### *