Last active
October 31, 2015 02:48
-
-
Save mvw/914b07eb3edff1ab7dfb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # lp-alloc.R | |
| # | |
| # Allocate 3 employes to 4 departments | |
| # | |
| # usage: | |
| # source('lp-alloc.R') | |
| # help: | |
| # http://lpsolve.r-forge.r-project.org/ | |
| # ?make.lp | |
| # ls("package:lpSolveAPI") | |
| # installation: | |
| # install.packages("lpSolveAPI") | |
| library(lpSolveAPI) | |
| #lprec <- make.lp(0,30, "full") | |
| lprec <- make.lp(0,30) | |
| lp.control(lprec,sense="max") | |
| # objective function "R" | |
| R = c( | |
| 550, 1000, 300, 1200, | |
| 0.5*(550+1000), 0.5*(550+300), 0.5*(550+1200), | |
| 0.5*(1000+300), 0.5*(1000+1200), | |
| 0.5*(300+1200), | |
| 900, 1250, 200, 900, | |
| 0.5*(900+1250), 0.5*(900+200), 0.5*(900+900), | |
| 0.5*(1250+200), 0.5*(1250+900), | |
| 0.5*(200+900), | |
| 1000, 1250, 325, 450, | |
| 0.5*(1000+1250), 0.5*(1000+325), 0.5*(1000+450), | |
| 0.5*(1250+325), 0.5*(1250+450), | |
| 0.5*(325+450) | |
| ) | |
| set.objfn(lprec,R) | |
| # A | |
| add.constraint(lprec, | |
| c( | |
| 1,1,1,1,1, 1,1,1,1,1, | |
| 0,0,0,0,0, 0,0,0,0,0, | |
| 0,0,0,0,0, 0,0,0,0,0 | |
| ), | |
| "=", 1) | |
| add.constraint(lprec, | |
| c( | |
| 0,0,0,0,0, 0,0,0,0,0, | |
| 1,1,1,1,1, 1,1,1,1,1, | |
| 0,0,0,0,0, 0,0,0,0,0 | |
| ), | |
| "=", 1) | |
| add.constraint(lprec, | |
| c( | |
| 0,0,0,0,0, 0,0,0,0,0, | |
| 0,0,0,0,0, 0,0,0,0,0, | |
| 1,1,1,1,1, 1,1,1,1,1 | |
| ), | |
| "=", 1) | |
| # B | |
| add.constraint(lprec, | |
| c( | |
| 1, 0, 0, 0, | |
| 1, 1, 1, | |
| 0, 0, | |
| 0, | |
| 1, 0, 0, 0, | |
| 1, 1, 1, | |
| 0, 0, | |
| 0, | |
| 1, 0, 0, 0, | |
| 1, 1, 1, | |
| 0, 0, | |
| 0 | |
| ), | |
| ">=", 1) | |
| add.constraint(lprec, | |
| c( | |
| 0, 1, 0, 0, | |
| 1, 0, 0, | |
| 1, 1, | |
| 0, | |
| 0, 1, 0, 0, | |
| 1, 0, 0, | |
| 1, 1, | |
| 0, | |
| 0, 1, 0, 0, | |
| 1, 0, 0, | |
| 1, 1, | |
| 0 | |
| ), | |
| ">=", 1) | |
| add.constraint(lprec, | |
| c( | |
| 0, 0, 1, 0, | |
| 0, 1, 0, | |
| 1, 0, | |
| 1, | |
| 0, 0, 1, 0, | |
| 0, 1, 0, | |
| 1, 0, | |
| 1, | |
| 0, 0, 1, 0, | |
| 0, 1, 0, | |
| 1, 0, | |
| 1 | |
| ), | |
| ">=", 1) | |
| add.constraint(lprec, | |
| c( | |
| 0, 0, 0, 1, | |
| 0, 0, 1, | |
| 0, 1, | |
| 1, | |
| 0, 0, 0, 1, | |
| 0, 0, 1, | |
| 0, 1, | |
| 1, | |
| 0, 0, 0, 1, | |
| 0, 0, 1, | |
| 0, 1, | |
| 1 | |
| ), | |
| ">=", 1) | |
| RowNames <- | |
| c( | |
| "A1", "A2", "A3", | |
| "B1", "B2", "B3", "B4" | |
| ) | |
| ColNames <- | |
| c( | |
| "X1A", "X1B", "X1C", "X1D", | |
| "X1AB", "X1AC", "X1AD", | |
| "X1BC", "X1BD", | |
| "X1CD", | |
| "X2A", "X2B", "X2C", "X2D", | |
| "X2AB", "X2AC", "X2AD", | |
| "X2BC", "X2BD", | |
| "X2CD", | |
| "X3A", "X3B", "X3C", "X3D", | |
| "X3AB", "X3AC", "X3AD", | |
| "X3BC", "X3BD", | |
| "X3CD" | |
| ) | |
| dimnames(lprec) <- list(RowNames, ColNames) | |
| set.type(lprec, | |
| c( | |
| 1,2,3,4,5,6,7,8,9,10, | |
| 11,12,13,14,15,16,17,18,19,20, | |
| 21,22,23,24,25,26,27,28,29,30 | |
| ), | |
| "binary" | |
| ) | |
| print("saving model as model.txt ..") | |
| write.lp(lprec, "model.txt") | |
| solve(lprec) | |
| print("Objective Function:") | |
| print(R) | |
| print("Objective:") | |
| print(get.objective(lprec)) | |
| print(ColNames) | |
| print(get.variables(lprec)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment