From 560178f76a37939229674d6a70dc732d82049432 Mon Sep 17 00:00:00 2001 From: Mauricio Zambrano-Bigiarini <hzambran@users.noreply.github.com> Date: Mon, 29 Oct 2012 13:16:45 +0000 Subject: [PATCH] hydroPSO.R: fixed small bug in 'UpdateLocalBest' --- DESCRIPTION | 4 ++-- NEWS | 3 ++- R/PSO_v2012.R | 25 ++++++++++++------------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 1a89830..cedea15 100755 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: hydroPSO Type: Package Title: Model-Independent Particle Swarm Optimisation for Environmental Models -Version: 0.1-58-15 -Date: 2012-10-28 +Version: 0.1-58-16 +Date: 2012-10-29 Authors@R: c(person("Mauricio", "Zambrano-Bigiarini", email="mzb.devel@gmail.com", role=c("aut","cre")), person("Rodrigo", "Rojas", email="Rodrigo.RojasMujica@gmail.com", role=c("ctb")) ) Maintainer: Mauricio Zambrano-Bigiarini <mzb.devel@gmail.com> Description: This package implements a state-of-the-art version of the Particle Swarm Optimisation (PSO) algorithm (SPSO-2011 and SPSO-2007 capable), with a special focus on the calibration of environmental models. hydroPSO is model-independent, allowing the user to easily interface any model code with the calibration engine (PSO). It includes a series of controlling options and PSO variants to fine-tune the performance of the calibration engine to different calibration problems. An advanced sensitivity analysis function together with user-friendly plotting summaries facilitate the interpretation and assessment of the calibration results. Bugs reports/comments/questions are very welcomed. diff --git a/NEWS b/NEWS index a942a10..46657ac 100755 --- a/NEWS +++ b/NEWS @@ -8,7 +8,8 @@ NEWS/ChangeLog for hydroPSO * new confinement of the velocity ( V[t+1] = -0.5 * V[t], when x[t+1] > x_max | x[t+1] < x_min ) * optional normalisation of parameter values - -) improved performance (~ 33% faster, tested on 10-D benchmark functions with 'write2disk=FALSE') + -) improved performance. ~ 33% faster for 32-bit machines and 38% faster for 64-bit machines. + Tested on 10-, 20- and 30-D benchmark functions with 'write2disk=FALSE'. -) 'fn' argument now can be any R function or a character. In the latter case, it can be "hydromod" or the name of a valid R function. In previous versions of 'hydroPSO' only a character type was accepted. -) now it handles models with sub-daily time step and with sub-daily observations (thanks to O. Rakovec !). diff --git a/R/PSO_v2012.R b/R/PSO_v2012.R index 7ca22f8..189fe8a 100755 --- a/R/PSO_v2012.R +++ b/R/PSO_v2012.R @@ -246,8 +246,6 @@ compute.veloc <- function(x, v, w, c1, c2, CF, Pbest, part.index, gbest, } else Gr <- (x + p) / 2 vn <- CF * (w*v + alea.sphere( G=Gr, radius= enorm(Gr-x) ) - x ) - - #vn <- CF * (w*v + gx + alea.sphere(G=gx + x, radius=enorm(gx) ) ) } else if ( method %in% c("spso2007", "canonical") ) { @@ -983,7 +981,7 @@ InitializateV <- function(npart, param.IDs, x.MinMax, v.ini.type, Xini) { # Started: 24-Dec-2010 ## # Updates: 29-Dec-2010 ; ## # 14-Nov-2011 ; 27-Jan-2011 ## -# 28-Oct-2012 ## +# 28-Oct-2012 ; 29-Oct-2012 ## ################################################################################ # Purpose: Function for computing the best value in the neighbourhood of each # particle @@ -1013,17 +1011,18 @@ UpdateLocalBest <- function(pbest.fit, localBest.pos, localBest.fit, x.neighbour 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 there are some particles that have a better fitness value - if (length(better.index) > 0) #{ ??? - if(MinMax == "max") { - localBest.fit[i] <- max( pbest.fit[neighs.index], na.rm=TRUE ) - } else localBest.fit[i] <- min( pbest.fit[neighs.index], na.rm=TRUE ) + + if (length(better.index) > 0) { - if(MinMax == "max") { - localBest.pos[i] <- neighs.index[which.max( pbest.fit[neighs.index] )] - } else localBest.pos[i] <- neighs.index[which.min( pbest.fit[neighs.index] )] - #} ??? + if(MinMax == "max") { + localBest.pos[i] <- neighs.index[which.max( pbest.fit[neighs.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 + } # FOR end -- GitLab