我有一个整数向量作为输入值(optim par的起始值)
my.data.var <- c(10,0.25,0.25,0.25,0.25,0.25,
10,0.25,0.25,0.25,0.25,0.25,
10,0.25,0.25,0.25,0.25,0.25,
10,0.25,0.25,0.25,0.25,0.25)
优化问题是一分钟。问题。
误差函数计算diff之间的平方根之和 两个矩阵(给定值矩阵与计算矩阵)
my.data.var.mat <- matrix(my.data.var,nrow = 4,ncol = 6,byrow = TRUE)
我必须引入的约束是colSum(my.data.var.mat) <=1
将optim定义为
sols<-optim(my.data.var,Error.func,method="L-BFGS-B",upper=c(Inf,1,1,1,1,1,Inf,1,1,1,1,1,Inf,1,1,1,1,1,Inf,1,1,1,1,1),
lower=c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
错误功能定义为
Error.func <- function(my.data.var){
my.data.var.mat <- matrix(my.data.var,nrow = ncol(my.data.matrix.prod),ncol = ncol(my.data.matrix.inj)+1,byrow = TRUE)
Calc.Qjk.Value <- Qjk.Cal.func(my.data.timet0,my.data.qo,my.data.matrix.time,
my.data.matrix.inj, my.data.matrix.prod,my.data.var,my.data.var.mat)
diff.values <- my.data.matrix.prod-Calc.Qjk.Value #FIND DIFFERENCE BETWEEN CAL. MATRIX AND ORIGINAL MATRIX
Error <- ((colSums ((diff.values^2), na.rm = FALSE, dims = 1))/nrow(my.data.matrix.inj))^0.5 #sum of square root of the diff
Error_total <- sum(Error,na.rm=FALSE)/ncol(my.data.matrix.prod) # total avg error
Error_total
}
给定数据集:my.data.matrix.prod
,my.data.timet0, my.data.qo, my.data.matrix.time, my.data.matrix.inj
所以,我的问题是我应该如何以及在何处引入矩阵col sum约束?或者另外一种方式来说明OPTIM如何在Matrix col sum约束下改变整数向量?
答案 0 :(得分:1)
我意识到nloptr
比optim
更好,因为我的问题包括“不平等约束”。
我修改了实现,正如我在这篇文章中解释的那样。 "multiple inequality constraints" - Minimization with R nloptr package
因此,关闭此主题。