2016-01-06 1296 views
0

我想提出的优化算法slsqp其中目标函数取决于两个参数,一个循环 - 第二个参数是循环的变量:R:优化算法SLSQP

library(nloptr) 

example <- function(w, j){ return(sum((w + j)^2)) } 

heq_fun是平等的约束功能上宽:

heq_fun<-function(w){ 
Mat <- rbind(rep(1,length(w))) 
sum <- Mat %*% w 
return(sum-1)} 

循环:

sol_list <- list() 

for(j in 1:5){ 

sol_list[[j]]<-slsqp(fund_weights, fn = example, gr = NULL, lower = rep(0, 16), 
        upper = rep(1, 16), hin = NULL, hinjac = NULL, heq = heq_fun, 
        heqjac = NULL, nl.info = FALSE, control =list(stopval = -Inf, 
        xtol_rel = 1e-9, maxeval = 100000)) 
} 

我得到:

Error in fun(x, ...) : argument "j" is missing, with no default 

该算法不明白的功能,最大限度的第二个参数也是循环的变量...

你能帮助我吗?

回答

0

尝试在循环中创建一个新函数,只在一个参数上使用白色。这应该有帮助

sol_list <- list() 

for(j in 1:5){ 

example <- function(w){ return(sum((w + j)^2)) } 

sol_list[[j]]<-slsqp(fund_weights, fn = example, gr = NULL, lower = rep(0,16), 
       upper = rep(1, 16), hin = NULL, hinjac = NULL, heq = heq_fun, 
       heqjac = NULL, nl.info = FALSE, control =list(stopval = -Inf, 
       xtol_rel = 1e-9, maxeval = 100000)) 
}