从R中的nloptr输出写入CSV文件

时间:2021-11-27 14:07:23

I would like to write the optimal values of variables from the output of nloptr in R. For example I have this program, and how do I get the optimal value of the x vector from res and print it in a CSV file?

我想从R中的nloptr输出中写出变量的最佳值。例如,我有这个程序,如何从res获取x向量的最佳值并将其打印在CSV文件中?

library('nloptr')
eval_f <- function( x ) {
  yx = x[1]*x[4]*(x[1] + x[2] + x[3]) + x[3];
  return( list( "objective" = yx,
                "gradient" = c( x[1] * x[4] + x[4] * (x[1] + x[2] + x[3]),
                                x[1] * x[4],
                                x[1] * x[4] + 1.0,
                                x[1] * (x[1] + x[2] + x[3]) ) ) );
}
local_opts <- list( "algorithm" = "NLOPT_LD_MMA",
                    "xtol_rel" = 1.0e-7 );

opts <- list( "algorithm" = "NLOPT_LD_AUGLAG",
              "xtol_rel" = 1.0e-7,
              "maxeval" = 1000,
              "local_opts" = local_opts );

x0 <- c( 1, 5, 5, 1 );
lb <- c( 1, 1, 1, 1 );
ub <- c( 5, 5, 5, 5 );
res <- nloptr( x0=x0, eval_f=eval_f,lb=lb,ub=ub,opts=opts);
print(res)

This prints

## Call:
## nloptr(x0 = x0, eval_f = eval_f, lb = lb, ub = ub, opts = opts)
## 
## Minimization using NLopt version 2.4.2 
## 
## NLopt solver status: 3 ( NLOPT_FTOL_REACHED: Optimization stopped because
## ftol_rel or ftol_abs (above) was reached. )
## 
## Number of Iterations....: 6 
## Termination conditions:  xtol_rel: 1e-07 maxeval: 1000 
## Number of inequality constraints:  0 
## Number of equality constraints:    0 
## Optimal value of objective function:  4 
## Optimal value of controls: 1 1 1 1

I would like to write the vector [1,1,1,1] into a csv file. How to do this? thanks

我想将矢量[1,1,1,1]写入csv文件。这该怎么做?谢谢

1 个解决方案

#1


2  

The nloptr::nloptr() function returns a list, one of whose elements ($solution) is a vector of the values you want.

nloptr :: nloptr()函数返回一个列表,其中一个元素($ solution)是所需值的向量。

So if res is as above:

所以如果res如上:

# ...
x0 <- c(1, 5, 5, 1)
lb <- c(1, 1, 1, 1)
ub <- c(5, 5, 5, 5)
res <- nloptr(x0=x0, eval_f=eval_f, lb=lb, ub=ub, opts=opts)

Then you can see what it contains with str(res):

然后你可以看到它包含str(res):

str(res)
## ...
## $ iterations            : int 6
## $ objective             : num 4
## $ solution              : num [1:4] 1 1 1 1     <~~~ the thing we want
## $ version               : chr "2.4.2"
## - attr(*, "class")= chr "nloptr"

And then extract the values you want and write them as csv (if that's the format you want):

然后提取所需的值并将其写为csv(如果这是您想要的格式):

values <- res$solution
write.csv(values, "nloptr_soln.csv", row.names=FALSE)

#1


2  

The nloptr::nloptr() function returns a list, one of whose elements ($solution) is a vector of the values you want.

nloptr :: nloptr()函数返回一个列表,其中一个元素($ solution)是所需值的向量。

So if res is as above:

所以如果res如上:

# ...
x0 <- c(1, 5, 5, 1)
lb <- c(1, 1, 1, 1)
ub <- c(5, 5, 5, 5)
res <- nloptr(x0=x0, eval_f=eval_f, lb=lb, ub=ub, opts=opts)

Then you can see what it contains with str(res):

然后你可以看到它包含str(res):

str(res)
## ...
## $ iterations            : int 6
## $ objective             : num 4
## $ solution              : num [1:4] 1 1 1 1     <~~~ the thing we want
## $ version               : chr "2.4.2"
## - attr(*, "class")= chr "nloptr"

And then extract the values you want and write them as csv (if that's the format you want):

然后提取所需的值并将其写为csv(如果这是您想要的格式):

values <- res$solution
write.csv(values, "nloptr_soln.csv", row.names=FALSE)