From 3231197877bfa0e12f1b2ad8ea4af995630997b6 Mon Sep 17 00:00:00 2001
From: Mauricio Zambrano-Bigiarini <hzambran@users.noreply.github.com>
Date: Mon, 11 Jun 2012 15:11:37 +0000
Subject: [PATCH] hydroPSO: the convergence of the algorithm controlled by
 'reltol' is now defined by 'reltol <= normalised swarm radius'

---
 DESCRIPTION   |  4 ++--
 NEWS          |  9 ++++++++-
 R/PSO_v2012.R | 51 ++++++++++++++++++++++++---------------------------
 3 files changed, 34 insertions(+), 30 deletions(-)

diff --git a/DESCRIPTION b/DESCRIPTION
index 831e166..3c019a7 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-55
-Date: 2012-05-11
+Version: 0.1-56
+Date: 2012-06-11
 Author: Mauricio Zambrano-Bigiarini [aut, cre] and Rodrigo Rojas [ctb]
 Author@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>
diff --git a/NEWS b/NEWS
index cefbfca..9780493 100755
--- a/NEWS
+++ b/NEWS
@@ -1,12 +1,19 @@
 NEWS/ChangeLog for hydroPSO
 --------------------------
 
+
+0.1-56	11-Jun-2012
+        o 'hydroPSO'    :  -) the convergence of the algorithm controlled by 'reltol' is now defined by : reltol <= normalised swarm radius, 
+                              instead of reltol <= [ gbest(iter) / ( gbest(iter-1) +reltol ) ]
+                           -) minor change: 'gbest.fit.prior' is now initialized in 'gbest.fit' instead of 'gbest.fit + 10*reltol'
+        
+
 0.1-55	11-May-2012
         o 'hydromod'     : -) 'sim' and 'obs' are now checked for time attributes when 'gof.Ini' and/or 'gof.Fin' is/are used 
                               (thanks to M. Leh for reporting !)
         o 'hydroPSO'     : -) fixed bug originated when 'gof.Ini' and/or 'gof.Fin' was/were not given as parameters for .model.FUN.args. 
                               (thanks to M. Leh for reporting !)
-                               This bug orginated the following error during the run of the model with the best parameter set:
+                               This bug originated the following error during the run of the model with the best parameter set:
                               "Error in as.Date.default(model.FUN.args[["gof.Ini"]]) : do not know how to convert 'model.FUN.args[["gof.Ini"]]' to class "Date" "
         o 'plot_results' : the default value for 'ftype' was changed from "dm" to "o", to avoid problems with time series with a resolution 
                            coarser than daily
diff --git a/R/PSO_v2012.R b/R/PSO_v2012.R
index ab330ee..37ca7a8 100755
--- a/R/PSO_v2012.R
+++ b/R/PSO_v2012.R
@@ -1709,26 +1709,28 @@ hydroPSO <- function(
     rownames(X.best.iter) <- paste("iter.", 1:maxit, sep="")
     colnames(X.best.iter) <- param.IDs
 
-    ifelse(MinMax == "max", new.value <- -Inf, new.value <- +Inf)
-    pbest.fit <- rep(new.value, npart) 
-
-    pbest.fit.prior <- pbest.fit 
+    # Worst possible value defined for the objective function
+    ifelse(MinMax == "max", fn.worst.value <- -.Machine$double.xmax/2, 
+                            fn.worst.value <- +.Machine$double.xmax/2)
+                            
+    pbest.fit            <- rep(fn.worst.value, npart) 
+    
+    pbest.fit.iter       <- fn.worst.value
+    pbest.fit.iter.prior <- fn.worst.value*2
 
-    pbest.fit.part.iter           <- matrix(rep(new.value,npart*maxit), ncol=npart, nrow=maxit, byrow=TRUE)
+    pbest.fit.part.iter           <- matrix(rep(fn.worst.value, npart*maxit), ncol=npart, nrow=maxit, byrow=TRUE)
     rownames(pbest.fit.part.iter) <- paste("iter.", 1:maxit, sep="")
     colnames(pbest.fit.part.iter) <- paste("Part", 1:npart, sep="")
+			    
+    gbest.fit       <- fn.worst.value
 
-    ifelse(MinMax == "max", gbest.fit <- -.Machine$integer.max, 
-			    gbest.fit <- .Machine$integer.max )
-
-    gbest.fit.iter <- rep(gbest.fit, maxit)
+    gbest.fit.iter  <- rep(gbest.fit, maxit)
 
-    gbest.fit.prior <- gbest.fit + 10*reltol
+    gbest.fit.prior <- gbest.fit
 
     gbest.pos <- 1
 
-    ifelse(MinMax == "max", new.value <- -Inf, new.value <- +Inf)
-    Xt.fitness <- matrix(rep(new.value, maxit*npart), ncol=npart, nrow=maxit, byrow=TRUE)       
+    Xt.fitness <- matrix(rep(fn.worst.value, maxit*npart), ncol=npart, nrow=maxit, byrow=TRUE)       
     colnames(Xt.fitness) <- paste("Part", 1:npart, sep="")
     rownames(Xt.fitness) <- paste("iter.", 1:maxit, sep="")
 
@@ -1754,18 +1756,16 @@ hydroPSO <- function(
       colnames(X.neighbours) <- paste("Neigh", 1:nc, sep="")
     } # IF end 
 
-    ifelse(MinMax == "max", new.value <- -Inf, new.value <- +Inf)
-    LocalBest.fit <- rep(new.value, npart)
+    LocalBest.fit <- rep(fn.worst.value, npart)
 
     LocalBest.pos <- 1:npart
 
-    LocalBest.fit.part.iter <- matrix(rep(new.value, npart*maxit), ncol=npart, nrow=maxit, byrow=TRUE)
+    LocalBest.fit.part.iter <- matrix(rep(fn.worst.value, npart*maxit), ncol=npart, nrow=maxit, byrow=TRUE)
     rownames(LocalBest.fit.part.iter) <- paste("iter.", 1:maxit, sep="")
     colnames(LocalBest.fit.part.iter) <- paste("Part", 1:npart, sep="")
 
     if ( topology == "ipso") { 
-      ifelse(MinMax == "max", new.value <- -Inf, new.value <- +Inf)
-      ngbest.fit <- rep(new.value, ngbest)
+      ngbest.fit <- rep(fn.worst.value, ngbest)
 
       ngbest.pos <- rep(1, ngbest)
     } else {
@@ -1989,10 +1989,9 @@ hydroPSO <- function(
     } # IF 'write2disk' end
 
     ########################################################################
-    ifelse(MinMax == "max", new.value <- -Inf, new.value <- +Inf)
-    last.best.fits  <- rep(new.value, RG.miniter) 
+    last.best.fits  <- rep(fn.worst.value, RG.miniter) 
 
-    last.gbest.fits <- rep(new.value, RG.miniter)     
+    last.gbest.fits <- rep(fn.worst.value, RG.miniter)     
 
     GPbest.fit.rate <- Inf
 
@@ -2404,18 +2403,16 @@ hydroPSO <- function(
 
       ifelse(MinMax=="max", abstol.conv <- gbest.fit >= abstol, 
 			    abstol.conv <- gbest.fit <= abstol )
-
-      if (iter > 1) {
-	ifelse(MinMax=="max", reltol.conv <- abs((gbest.fit.prior+reltol)/gbest.fit) <= reltol,
-			      reltol.conv <- abs(gbest.fit/(gbest.fit.prior+reltol)) <= reltol )
-      } # IF end
+                     
+      reltol.conv <- ( NormSwarmRadius <= reltol )
+                     
+      pbest.fit.iter.prior <- pbest.fit.iter
 
       # Gbest was improved ?
       ifelse(gbest.fit.prior==gbest.fit, improvement <- FALSE, improvement <- TRUE) 
 
-      pbest.fit.prior <- pbest.fit
-
       gbest.fit.prior <- gbest.fit
+            
 
       if (abstol.conv ) {
 	end.type.stg  <- "Converged ('abstol' criterion)"
-- 
GitLab