Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R optim:正确使用具有多个参数的函数_R_Optimization - Fatal编程技术网

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总是相同的(它们是从文件中读取的)

K
n\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)