首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R中的非线性有界优化

R中的非线性有界优化
EN

Stack Overflow用户
提问于 2020-07-15 12:30:47
回答 1查看 283关注 0票数 1

我试图在R中运行一个有界约束的非线性优化。我已经知道NlcOptim & roptim可以用来优化非线性目标函数,并且我已经通过了下面提到的例子https://cran.r-project.org/web/packages/NlcOptim/NlcOptim.pdf like one (ex1);

代码语言:javascript
复制
require(NlcOptim)
require(MASS)
objfun=function(x){
  return(exp(x[1]*x[2]*x[3]*x[4]*x[5]))
}
#constraint function
confun=function(x){
  f=NULL
  f=rbind(f,x[1]^2+x[2]^2+x[3]^2+x[4]^2+x[5]^2-10)
  f=rbind(f,x[2]*x[3]-5*x[4]*x[5])
  f=rbind(f,x[1]^3+x[2]^3+1)
  #return(list(ceq=f,c=NULL))
  return(list(ceq=f,c=NULL))
}
x0=c(-2,2,2,-1,-1)
solnl(x0,objfun=objfun,confun=confun)

理解:在objfun和confun中使用的x是一个包含x(i)的向量,i=1(1)5 x0是起始值(在这种情况下,我有点困惑,因为我们在这里没有定义x1,..,x5的界限,而只定义了初始值)

我试图为我的实际问题复制这个例子,我构建的目标函数如下;

代码语言:javascript
复制
Maximize P= (x*y*z)-(cost1 + cost2 + cost3 + cost4 + cost5 + cost6) 
where
cost1 = 5000
cost2 = (0.23*cost1) + (0.67*x*y) 
cost3 = 0.2* cost1+ (0.138*x*y)
cost4 = 0.62*cost1
cost5 = 0.12* cost1
cost6 = 0.354*x
Boundaries for the variables are as follow;
200<=x=>350
17<=y=>60
964<=z=>3000

有了这个问题,我试着把它写成代码;

代码语言:javascript
复制
x <- runif(2037,200,350)
y <- runif(2037,17,60)
z <- seq(964,3000,1)  # z is having highest length of 2037. But not sure if this the way to define bounds!!
data_comb <- cbind(x,y,z)
mat <- as.matrix(data_comb)

cost1 <- 5000
cost2 <- (0.23*cost1) + (0.67* mat[,1])* (mat[,2]) 
cost3 <- 0.2* cost1+ (0.138* mat[,1])* (mat[,2])
cost4 <- rep(0.62*cost1, dim(mat)[1])
cost5 <- rep(0.12* cost1, dim(mat)[1])
cost6 <- 0.354* mat[,1]
#Objective function
objfun <- function(mat){
  return((mat[,1]*mat[,2]*mat[,3]) - (cost1 + cost2 + cost3 + cost4 + cost5 + cost6))
}
#Constraints
confun=function(mat){
  f=NULL
  f=rbind(f,(0.23*cos1) + (0.67*mat[,1])* (mat[,2]))
  f=rbind(f,(0.2*cost1) + (0.138*mat[,1])*(mat[,2]))
  f=rbind(f,0.354*mat[,1])
  return(list(ceq=f,c=NULL))
}
x0 <- c(200,17,964)
solnl(x0,objfun=objfun,confun=confun)

这给了我一个错误

代码语言:javascript
复制
Error in mat[, 2] : subscript out of bounds

我有一种感觉,不知何故,我没有正确地复制我的问题的例子,但同时又不能理解我错过了什么。我不知道我是否正确地定义了边界,也不知道如何在函数中包含多元边界。请帮我解决这个优化问题。

提亚

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-25 08:55:09

没有非线性约束,只有盒子约束,因此没有理由应用专门的包或函数。

代码语言:javascript
复制
obj <- function(v) {
    x <- v[1]; y <- v[2]; z <- v[3]
    cost1 <- 5000
    cost2 <- (0.23*cost1) + (0.67*x*y) 
    cost3 <- 0.2* cost1+ (0.138*x*y)
    cost4 <- 0.62*cost1
    cost5 <- 0.12* cost1
    cost6 <- 0.354*x
    w <- (x*y*z) - (cost1 + cost2 + cost3 + cost4 + cost5 + cost6)
    return(-w)
}

o <- optim(c(200,17,964), obj, method = "L-BFGS-B",
           lower = c(200,17,964), upper = c(350,60,3000))

o$par; -o$value
## [1]  350   60 3000
## [1] 62972058

为了使用NlcOptim:solnl(),您可以使用lbub选项参数来定义框约束,就像上面一样。

代码语言:javascript
复制
NlcOptim::solnl(c(200,17,964), obj,
                lb = c(200,17,964), ub = c(350,60,3000))

同样的结果,表明您的函数没有真正的内部最大值。或者,您可以将边界集成到约束函数中

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62907641

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档