diff --git a/R/PSO_v2012.R b/R/PSO_v2012.R index 189fe8a45e64c5a41399e90d63a9f6c0cd9ce8bb..be036362ec911cc2335ef8a52321d46e1811776a 100755 --- a/R/PSO_v2012.R +++ b/R/PSO_v2012.R @@ -1006,23 +1006,21 @@ UpdateLocalBest <- function(pbest.fit, localBest.pos, localBest.fit, x.neighbour # index of all the particles that are "neighbours" to the particle 'i' neighs.index <- x.neighbours[i,] - + # if one or more of the "neighbours" have a better fitness than the current Local Best if(MinMax == "max") { - better.index <- which( pbest.fit[neighs.index] > localBest.fit[i] ) - } else better.index <- which( pbest.fit[neighs.index] < localBest.fit[i] ) - - if (length(better.index) > 0) { - - if(MinMax == "max") { - localBest.pos[i] <- neighs.index[which.max( pbest.fit[neighs.index] )] + best.neigh.index <- which.max( pbest.fit[neighs.index] ) + if (pbest.fit[best.neigh.index] > localBest.fit[i] ) { + localBest.pos[i] <- neighs.index[best.neigh.index ] + localBest.fit[i] <- pbest.fit[localBest.pos[i]] + } # IF end + } else { + best.neigh.index <- which.min( pbest.fit[neighs.index] ) + if ( pbest.fit[neighs.index][best.neigh.index] < localBest.fit[i] ) { + localBest.pos[i] <- neighs.index[best.neigh.index] localBest.fit[i] <- pbest.fit[localBest.pos[i]] - } else { - localBest.pos[i] <- neighs.index[which.min( pbest.fit[neighs.index] )] - localBest.fit[i] <- pbest.fit[localBest.pos[i]] - } # ELSE end - } # |IF end - + } # IF end + } # ELSE end } # FOR end