[R] Need advice on linear programming in R

Giorgio Garziano giorgio.garziano at ericsson.com
Sun Sep 4 13:51:45 CEST 2016


Hi Michael,

On top of all suggestions, I can mention the following packages for linear programming problems:


https://cran.r-project.org/web/packages/linprog/linprog.pdf


https://cran.r-project.org/web/packages/lpSolve/lpSolve.pdf


https://cran.r-project.org/web/packages/clue/clue.pdf

(see clue package solve_LSAP function)


https://cran.r-project.org/web/packages/adagio/adagio.pdf


As a reference book, "Using R for Numerical Analysis in Science and Engineering", CRC press:


https://www.crcpress.com/Using-R-for-Numerical-Analysis-in-Science-and-Engineering/Bloomfield/p/book/9781439884485



Further, I share with you the code implementing a greedy solution to your assignment problem:

set.seed(1023)
nbin <- 5
nrow <- 100
nvalue <- nbin*nrow

# generating random values
gMat2 <- matrix(as.integer(Mod(rnorm(nvalue, 5, 10))), nrow = nrow)
gMat2

# since you want to maximize the minimum bin sums value
# let us already find the maximum value per row and store its corresponding
# column id inside rowmax
rowmax <- apply(gMat2, 1, function(x) {which.max(x)})
rowmax

# maximum value per each row
max_per_row <- sapply(1:nrow, function(x) gMat2[x, rowmax[x]])
names(max_per_row) <- as.character(1:nrow)
max_per_row

# sorting max_per_row values in descreasing order
sorted <- base::sort(max_per_row, decreasing=TRUE)
sorted

# bin where to store sums
bin <- vector(mode="numeric", length = nbin)

# assignment output based on sorted vector
output <- c()

# looping on sorted and assign the bin with minimum current sum
for (i in seq_along(sorted)) {
  s <- sorted[i]
  min.bin <- which.min(bin)
  bin[min.bin] <- bin[min.bin] + s
  output <- c(output, min.bin)
}

df <- cbind(sorted, output)
ord <- order(as.integer(rownames(df)))

df2 <- df[ord,]
colnames(df2) <- c("value", "selected_bin")
df2 <- data.frame(selected_column = rowmax, df2)

# assignments table
df2

# resulting bins sum
bin

-----

Best,

GG



	[[alternative HTML version deleted]]



More information about the R-help mailing list