0

我正在使用alabama 包来优化非线性约束优化问题。

问题是:

 minimise:  -(0.653*x[1]+ 0.234* x[1]*x[1]+ 0.437 * x[2] + 0.769 * x[3]
 +0.453 * x[4] + 0.744 * x[5] + 0.476 * x[5]* x[5])

等式约束为:

x[1]+ x[2]+x[3]+x[4]+x[5] = 2600

不等式约束为:

x[1]> 900
x[1] < 1100
x[2] > 400
x[2] < 600
x[3] > 250
x[3] < 350
x[4] > 175
x[4] < 225
x[5] > 295
x[5] < 305

这是我正在尝试的:

fn <- function() {
   -(0.653*x[1]+ 0.234* x[1]*x[1]+ 0.437 * x[2] + 0.769 * x[3] +
    0.453 * x[4] + 0.744 * x[5] + 0.476 * x[5]* x[5])
 }



 heq <- function(x) { x[1] + x[2] + x[3] + x[4] +x[5] - 2600  }

hin <- function(x) {
h <- rep(NA, 1)
h[1] <- x[1] - 900
h[2] <- 1100 - x[1]
h[3] <- x[2] - 400
h[4] <- 600 - x[2]
h[5] <- x[3] - 250
h[6] <- 350 - x[3]
h[7] <- x[4] - 175
h[8] <- 225 - x[4]
h[9] <- x[5] - 295
h[10] <- 305 - x[5]
h
}

以下是我在使用不同的 par 值时面临的各种问题:

情况1:

ans <- auglag(par= NULL,fn=fn, gr=NULL,hin=hin, heq=heq) 
Error in h[1] <- x[1] - 900 : replacement has length zero

案例2:

ans <- auglag(par= c(1,1,1,1,1),fn=fn,hin=hin, heq=heq)
Error in h[1] <- x[1] + x[2] + x[3] + x[4] + x[5] - 2600 : 
  object 'h' not found

案例3:

 ans <- auglag(par= c(1000,500,300,200,300),fn=fn,hin=hin, heq=heq)
Error in h[1] <- x[1] + x[2] + x[3] + x[4] + x[5] - 2600 : 
  object 'h' not found

案例4:

ans <- auglag(par=NULL,fn=fn, gr=NULL,hin=hin,heq=heq) 
Error in h[1] <- x[1] - 900 : replacement has length zero

应用 augrag 或 constrOptim.nl 的正确方法是什么?我尝试通过solve.QP解决它,但无法理解要传递哪些参数。

在@Hong Ooi 进行编辑后,以下是新错误:

> ans <- auglag(par= NULL,fn=fn, gr=NULL,hin=hin, heq=heq) 
Error in h[1] <- x[1] - 900 : replacement has length zero
> ans <- auglag(par= c(1,1,1,1,1),fn=fn,hin=hin, heq=heq)
Error in fn(par, ...) : unused argument(s) (par)
> ans <- auglag(par= c(1000,500,300,200,300),fn=fn,hin=hin, heq=heq)
Error in fn(par, ...) : unused argument(s) (par)
> ans <- auglag(par=NULL,fn=fn, gr=NULL,hin=hin,heq=heq) 
Error in h[1] <- x[1] - 900 : replacement has length zero
4

1 回答 1

1

我有这个工作。感谢 Hao Ooi 对我的指导。

我的问题是:

我正在使用:

fn <- function() {
   -(0.653*x[1]+ 0.234* x[1]*x[1]+ 0.437 * x[2] + 0.769 * x[3] +
    0.453 * x[4] + 0.744 * x[5] + 0.476 * x[5]* x[5])
 }

相反,我应该使用:

fn <- function(x) {
       -(0.653*x[1]+ 0.234* x[1]*x[1]+ 0.437 * x[2] + 0.769 * x[3] +
        0.453 * x[4] + 0.744 * x[5] + 0.476 * x[5]* x[5])
     }
于 2013-06-22T05:20:03.533 回答