0
我正在使用alabama package來優化非線性約束優化問題。二次函數的約束優化
的問題是:
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
}
這裏是我面對面值的不同值的各種問題:
案例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
CASE4:
ans <- auglag(par=NULL,fn=fn, gr=NULL,hin=hin,heq=heq)
Error in h[1] <- x[1] - 900 : replacement has length zero
什麼是應用auglag或constrOptim.nl的正確方法?我試着通過solve.QP來解決這個問題,但是無法解決要傳遞的參數。
通過@Hong大井所做的編輯後,這裏有新的錯誤:
> 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
,而無需使用'alabama',我猜你想你的等式約束函數爲'function(x){x [1] + x [2] + x [3] + x [4] + x [5] - 2600}''。 –
正確!編輯它 – Arc
您沒有正確編輯它。將'heq'定義爲我在上面輸入的內容。 –