R optim:正确使用具有多个参数的函数
我正在使用R optim:正确使用具有多个参数的函数,r,optimization,R,Optimization,我正在使用optim编写一个最小化函数。该任务是在循环中解决一些类似的优化任务 # K and k are always the same (they are read from a file) K <- matrix(data = c(1, 2, 1, 2, 1, 2, 16, 2, 1, 2, 1, 2, 8, 2, 1, 2, 1, 2, 16, 2,
optim
编写一个最小化函数。该任务是在循环中解决一些类似的优化任务
# K and k are always the same (they are read from a file)
K <- matrix(data = c(1, 2, 1, 2, 1,
2, 16, 2, 1, 2,
1, 2, 8, 2, 1,
2, 1, 2, 16, 2,
1, 2, 1, 2, 32),
nrow = 5, ncol = 5, byrow = TRUE)
k <- c(-2, 4, 12, 0, 2)
# j will be changed
minimize <- function(beta){ #function to minimize (for beta)
value <- (1/2)*(t(beta)%*%K%*%beta) - t(k)%*%beta + j*abs(sum(beta)-n_s)
return(value)
}
myfunc <- function(K, k, m) #K is matrix, k is vector
{
j_values <- 10^seq(-5, 5, length = m)
for (i in 1:m)
{
current_j_value <- j_values[i]
#I want to set j in minimize function as current_j_value (and also my k and K from file)
# and then minimize it
myans <- optim(c(0, 0, 0, 0, 0), minimize) # using minimize(K, k, j) doesn't work
print(myans$par)
}
}
myfunc(K, k, 5)
#K和K总是相同的(它们是从文件中读取的)
K如果您想在最小化函数中包含额外的参数,您可以在optim
调用中添加这些参数,如文档所示,请参见?optim
点(…
)
。。。要传递给fn和gr的其他参数
因此,将j
、k
、k
和n_s
纳入最小化范围
minimize <- function(beta, j, k, K, n_s){ #function to minimize (for beta)
value <- (1/2)*(t(beta)%*%K%*%beta) - t(k)%*%beta + j*abs(sum(beta)-n_s)
return(value)
}
那么运行这个,
> myfunc(K, k, 5)
[1] -6.7956860 0.7999990 1.9999999 0.5333326 0.1290324
[1] -6.7911329 0.7996483 2.0000002 0.5329818 0.1290322
[1] -5.3512894 0.6889257 1.9999436 0.4222287 0.1290095
[1] -2.80295781 0.61426579 1.95348934 0.24715200 -0.01194974
[1] -1.2999142 0.4313710 1.3088572 -0.5764644 0.1361504
把所有代码放在一起
# K and k are always the same (they are read from a file)
K <- matrix(data = c(1, 2, 1, 2, 1,
2, 16, 2, 1, 2,
1, 2, 8, 2, 1,
2, 1, 2, 16, 2,
1, 2, 1, 2, 32),
nrow = 5, ncol = 5, byrow = TRUE)
k <- c(-2, 4, 12, 0, 2)
# j will be changed
minimize <- function(beta, j, k, K, n_s){ #function to minimize (for beta)
value <- (1/2)*(t(beta)%*%K%*%beta) - t(k)%*%beta + j*abs(sum(beta)-n_s)
return(value)
}
myfunc <- function(K, k, m) #K is matrix, k is vector
{
j_values <- 10^seq(-5, 5, length = m)
for (i in 1:m)
{
current_j_value <- j_values[i]
#I want to set j in minimize function as current_j_value (and also my k and K from file)
# and then minimize it
myans <- optim(c(0, 0, 0, 0, 0), minimize, j = current_j_value, k = k, K = K, n_s = 0) # using minimize(K, k, j) doesn't work
print(myans$par)
}
}
myfunc(K, k, 5)
#K和K总是相同的(它们是从文件中读取的)
Kn\u s
指的是什么?
# K and k are always the same (they are read from a file)
K <- matrix(data = c(1, 2, 1, 2, 1,
2, 16, 2, 1, 2,
1, 2, 8, 2, 1,
2, 1, 2, 16, 2,
1, 2, 1, 2, 32),
nrow = 5, ncol = 5, byrow = TRUE)
k <- c(-2, 4, 12, 0, 2)
# j will be changed
minimize <- function(beta, j, k, K, n_s){ #function to minimize (for beta)
value <- (1/2)*(t(beta)%*%K%*%beta) - t(k)%*%beta + j*abs(sum(beta)-n_s)
return(value)
}
myfunc <- function(K, k, m) #K is matrix, k is vector
{
j_values <- 10^seq(-5, 5, length = m)
for (i in 1:m)
{
current_j_value <- j_values[i]
#I want to set j in minimize function as current_j_value (and also my k and K from file)
# and then minimize it
myans <- optim(c(0, 0, 0, 0, 0), minimize, j = current_j_value, k = k, K = K, n_s = 0) # using minimize(K, k, j) doesn't work
print(myans$par)
}
}
myfunc(K, k, 5)