Linear programming (lpsolve) gives a different solution when constant is added to function

230 views Asked by At

I'm obviously doing something wrong here but I can't understand what. I have a matrix of the function to maximise called here rank (if interested the expected progeny performance from the mating of a male and female) with some constraints - everything function value is positive. I run it and get a solution. Then I add a constant (here 10) to all the function values and I get a different solution. I'm not a LP expert but this makes no sense - surely adding a constant to everything should not change things?

library(lpSolve)
rank <- matrix(c(1, 2, 3, 4, 5,6, 7, 8), nrow=2, ncol=4,byrow=T)
rank<- rank+10  # switch on and off
rank
exclusions<-  matrix(c(0,0,0,0,0,0,0,0), nrow=2, ncol=4,byrow=T)
sires <- matrix(c(2,2), nrow=1, ncol=2,byrow=T)
m <- NROW(rank) # n of males
f <- NCOL(rank) # n of females
obj <- as.numeric(rank)    # objective function
nMalePerFemaleRhs <- rep(1,f)    # one mating per female 
nMalePerFemaleSign <- rep("<=",f)   # every female must gets no more than 1 mating 
nMalePerFemaleConstr <- matrix(0,nrow=f,ncol=m*f)

for(i in 1:f){
  nMalePerFemaleConstr[i,(i-1)*m+(1:m)] <- 1    # cows are rows and the number of columns are bulls times cows
}

nFemalePerMaleRhs <- sires[1,] # number of matings per sire
nFemalePerMaleSign <- rep("<=",m)  
nFemalePerMaleConstr <- matrix(0,nrow=m,ncol=m*f)

for(i in 1:m){
  nFemalePerMaleConstr[i,seq(from=i,length.out=f,by=m)] <- 1
}


######################################################
#         Sibling exclusions constraint              #
######################################################

siblingConstr <- t(as.numeric(exclusions))
siblingRhs <- 0
siblingSign <- '='


######################################################
#           Solve the linear program                 #
######################################################

res <- lp(direction='max',
          objective.in=obj,
          const.mat = rbind
(nMalePerFemaleConstr,nFemalePerMaleConstr,siblingConstr),
          const.dir = c(nMalePerFemaleSign,nFemalePerMaleSign,siblingSign),
          const.rhs = c(nMalePerFemaleRhs,nFemalePerMaleRhs,siblingRhs),
          all.int = TRUE
          )

solutionLP <- matrix(res$solution,nrow=m)

solutionLP 
0

There are 0 answers