循环问题

时间:2018-01-14 16:30:14

标签: r optimization

我正在尝试编写一个pso片段。代码是pso算法的单次迭代。我对某个循环有问题:

#update swarm - ERROR
for(i in 1:n){
  v[i,]=omega*(swarm[i,]-old_swarm[i,])+
    fip*rop*(pBest[i,]-swarm[i,])
  +fig*rog*(gBest-swarm[i,])
  print(v[i,])
  swarm[i,]=swarm[i,]+v[i,]
}

我从循环接收的速度向量v全为零,而当我在控制台中输入代码时,我得到一些值:

v[1,]=omega*(swarm[1,]-old_swarm[1,])+fip*rop*(pBest[1,]-swarm[i,])+fig*rog*(gBest-swarm[1,])

整个代码:

# Objective function
myFun = function(x){
  return(x[1]^2 + exp(x[1])*1/10*x[2]^2)
}

f=myFun
n=10
x_length = 2
omega =.1
fip = .1
fig = .2

#f - objective function
#n - number of particels
#x_length - length of x
#omega, fip, fig - velocities

#initiate swarm
swarm = matrix(nrow = n,ncol = x_length)
for(i in 1:n){
  swarm[i,]=runif(n=x_length,min=-4,max=4)
}

#initiate result
result <- list(g_best = globalBest(swarm,f), y_best = f(g_best), iter = 0)

#initial parameters
pBest <- swarm
gBest <- globalBest(swarm,f)
old_swarm <- swarm
v <- swarm

#the main part - normally inside a loop
#update velocity
rop = runif(n=1,min=0,max=1)
rog = runif(n=1,min=0,max=1)
#update personal best
for(i in 1:n){
  if(f(swarm[i,])<f(pBest[i,]))
    pBest[i,]=swarm[i,]
}
#update global best
if(f(globalBest(swarm,f))<f(gBest))
  gBest <- globalBest(swarm,f)

#update swarm - ERROR
for(i in 1:n){
  v[i,]=omega*(swarm[i,]-old_swarm[i,])+
    fip*rop*(pBest[i,]-swarm[i,])
  +fig*rog*(gBest-swarm[i,])
  print(v[i,])
  swarm[i,]=swarm[i,]+v[i,]
}
result$g_best <- rbind(result$g_best, gBest)
result$y_best <- rbind(result$y_best, f(gBest))


#function calculating global best
globalBest = function(swarm,f){
  n = dim(swarm)[1]
  x_length = dim(swarm)[2]

  x_best = swarm[1,]
  y_best <-f(x_best)

  for(i in 2:n){
    if(y_best > f(swarm[i,])){
      x_best <-swarm[i,] 
      y_best <-f(x_best)
    }
  }

  return(x_best)
}

1 个答案:

答案 0 :(得分:1)

我重新格式化了您的代码,现在它应该可以正常工作,希望按照您的预期方式:

# Objective function
myFun <- function(x){
  x[1]^2 + exp(x[1])*1/10*x[2]^2
}

#function calculating global best
globalBest <- function(swarm, f) {
  n <- dim(swarm)[1]
  x_length <- dim(swarm)[2]

  x_best <- swarm[1, ]
  y_best <- f(x_best)

  for (i in 2:n) {
    si <- swarm[i, ]
    if (y_best > f(si)) {
      x_best <- si
      y_best <- f(x_best)
    }
  }
  x_best
}

f <- myFun
n <- 10
x_length <- 2
omega <- .1
fip <- .1
fig <- .2


#initiate swarm
swarm <- matrix(nrow = n, ncol = x_length)
for (i in 1:n) {
  swarm[i, ] <- runif(n = x_length, min = -4, max = 4)
}

#initiate result
gg <- globalBest(swarm, f)
result <- list(g_best = gg, y_best = f(gg), iter = 0)

#initial parameters
pBest <- swarm
gBest <- globalBest(swarm, f)
old_swarm <- swarm
v <- swarm

#the main part - normally inside a loop
#update velocity
rop <- runif(n = 1, min = 0, max = 1)

#update personal best
for (i in 1:n) {
  si <- swarm[i,]
  if (f(si) < f(pBest[i,]))
    pBest[i, ] <- si
}

#update global best
if (f(globalBest(swarm, f)) < f(gBest))
  gBest <- globalBest(swarm, f)

#update swarm - ERROR
for (i in 1:n) {
  si <- swarm[i, ]
  v[i, ] <- omega*(si - old_swarm[i, ]) + fip*rop*(pBest[i, ] - si) +
    fig*rog*(gBest - si)
  print(v[i, ])
  swarm[i, ] <- si + v[i, ]
}
result$g_best <- rbind(result$g_best, gBest)
result$y_best <- rbind(result$y_best, f(gBest))
result

您的一个代码问题是+处于新行。我建议你读: https://google.github.io/styleguide/Rguide.xmlhttp://adv-r.had.co.nz/Style.html