我显然在这里做错了什么,但我无法理解。我有一个函数矩阵来最大化调用这里的排名(如果感兴趣的是男性和女性交配的预期后代表现)与一些约束 - 一切功能价值都是积极的。我运行它并获得解决方案。然后我为所有函数值添加一个常量(这里是10),我得到了一个不同的解决方案。我不是LP专家,但这没有任何意义 - 确定为一切添加一个常数不应该改变事情吗?
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