Skip to content
Prev 309129 / 398506 Next

Optimization in R similar to MS Excel Solver

On 26-10-2012, at 12:50, Richard James wrote:

            
Actually the G and H arguments are wrong. They should be

G=diag(1,4)
H=rep(0,4)

since the weights should be >= 0
Maybe this will do what you need

Nrep <- 10  # fors testing

I think this is what you need

### start code

kRow <- 1
bmat <- matrix(DistAll[kRow,],ncol=4,byrow=FALSE)
bmat
target <- c("Ca in river(%)"=0.33,"Mg in river (%)"=0.0114)
target 

lsei(A=bmat,B=target,E=matrix(rep(1,4),ncol=4),F=1,G=diag(1,4),H=rep(0,4),fulloutput=TRUE)

gen.single <- function(k,Distall,target) {
    bmat <- matrix(DistAll[k,],ncol=4,byrow=FALSE)
    z <- lsei(A=bmat,B=target,E=matrix(rep(1,4),ncol=4),F=1,G=diag(1,4),H=rep(0,4),fulloutput=TRUE)
    # insert tests of  output of lsei to see if all is ok etc.
    z$X # weights
}

set.seed(2001)
t(sapply(1:Nrep, FUN=gen.single,Distall=Distall,target=target))

### end of code


And now you can do whatever you wish with the columns of the output matrix

Berend