[S] Q on an minimization

Mikio Kaihara (mkaihara@ichinoseki.ac.jp)
Fri, 10 Jul 1998 09:13:25 +0900


Dear S-PLUS users,

I (beginner from May, Win 95;S-PLUS Ver.4.0) am now fabricating a simple optimization
program, but there was an error. I struggled for 2 days in vain.
Would you please help me ?
I would like to minimize the function,
(x1 - 1)^2 + (x2 - 2)^2 + (x3 - 3)^2 + (x4 - 4)^2 + 1 by the simplex method.

---------------------------------------------------------------------------
> sim(1)
Error in uu[, 5]: Array subscript (5) out of bounds, should be at most 1
Dumped
---------------------------------------------------------------------------
--- sim ---
function(ite)
{
sim1()
for(i in 1:5) {
tt <<- ab[, , i]
sim2()
uu[i, 1] <<- u
}
sim3()
while(ite > 0) {
sim4()
sim3()
ite <- ite - 1
}
}

--- sim1 ---
function()
{
ab <<- array(c(2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1,
1), dim = c(2, 2, 5))
uu <<- matrix(rep(0, 5), ncol = 1)
}
--- sim2 ---
function()
{
tt <- ab[, , 1]
uu[, 1] <<- (tt[1, 1] - 1)^2 + (tt[1, 2] - 2)^2 + (tt[2, 1] - 3)^2 + (
tt[2, 2] - 4)^2 + 1
}
--- sim3 ---
function()
{
ord <- rev(order(uu[, 1]))
newuu <- uu[ord, 1, drop = F]
newab <- ab[, , ord]
uu[, 1] <<- newuu
ab[, , 1:5] <<- newab
}
--- sim4 ---
function()
{
alpha <- 0.5
beta <- 0.5
gamma <- 1.5
ttt <- matrix(rep(0, 4), ncol = 2)
ab[, , 1] <<- (((1 + alpha) * (ab[, , 2] + ab[, , 3] + ab[, , 4] +
ab[, , 5]))/4) - alpha * ab[, , 1]
ttt <- ab[, , 1]
sim2()
if(uu[, 1] < uu[, 5])
ab[, , 1] <<- gamma * ttt + (1 - gamma) * ttt
if(uu[, 1] >= uu[, 5])
ab[, , 1] <<- bata * ttt + ((1 - bata) * (1 - beta) * (ab[, ,
2] + ab[, , 3] + ab[, , 4] + ab[, , 5]))/4
}
---------------------------------------------------------------------------

------------------------------
Mikio kaihara
Dep. Chem. Engineer.
INCT
Ichinoseki,021-8511, Japan
------------------------------
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news