From 46e5441477348e411fc2385a95cfb0f29dd57f55 Mon Sep 17 00:00:00 2001
From: Mauricio Zambrano-Bigiarini <hzambran@users.noreply.github.com>
Date: Mon, 22 Oct 2012 15:10:25 +0000
Subject: [PATCH] fixed some (very unlikely) error when 'IW.type=aiwf'

---
 DESCRIPTION   |  4 ++--
 NEWS          |  1 +
 R/PSO_v2012.R | 21 +++++++++++----------
 3 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/DESCRIPTION b/DESCRIPTION
index e6ad88e..5235674 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-12
-Date: 2012-10-17
+Version: 0.1-58-13
+Date: 2012-10-22
 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 528a9cd..845223d 100755
--- a/NEWS
+++ b/NEWS
@@ -46,6 +46,7 @@ NEWS/ChangeLog for hydroPSO
                             -) when the control argument 'out.with.fit.iter' (not used so far) is set to TRUE, the number of iterations returned
                                correspond to the effective number of iterations carried out, not the maximum defined by 'maxit'
                             -) now it is able to correctly write the observed values for sub-daily models
+                            -) fixed some (very unlikely) error when 'IW.type="aiwf"'
                               
 
         o Running 'hydroPSO' >= 0.1-59 with default settings will produce DIFFERENT RESULTS from those obtained with 'hydroPSO' <= 0.1-58, due to the 
diff --git a/R/PSO_v2012.R b/R/PSO_v2012.R
index 6deb5eb..34f810a 100755
--- a/R/PSO_v2012.R
+++ b/R/PSO_v2012.R
@@ -363,10 +363,11 @@ compute.value.with.iter <- function(iter, niter, nexp, val.ini, val.fin) {
 ##                         compute.w.aiwf Function                            ##
 ##                 Adaptive inertial weight factor (AIWF)                     ##
 ################################################################################
-# Author : Mauricio Zambrano-Bigiarini
+# Author : Mauricio Zambrano-Bigiarini                                        ##
 ################################################################################
-# Started: 2008
-# Updates: 24-Nov-2011
+# Started: 2008                                                               ##
+# Updates: 24-Nov-2011                                                        ##
+#          22-Oct-2012                                                        ##
 ################################################################################
 # Reference:
 # According to Liu et al., 2005 ("Improved Particle Swarm Combined with Caos")
@@ -389,14 +390,14 @@ compute.w.aiwf <- function(iter.fit, particle.pos, gbest.fit, w.max, w.min, MinM
   f <- iter.fit[particle.pos]
   
   # 'f.avg': mean fitness value of all the particles in the current iteration
-  f.avg <- mean(iter.fit, na.rm=TRUE)
+  f.avg <- mean(iter.fit, na.rm=TRUE) 
   
   # 'f.min': best fitness value of all the particles in the current iteration
   ifelse(MinMax == "min", f.min <- min(iter.fit, na.rm=TRUE), f.min <- max(iter.fit, na.rm=TRUE) )
-   
+  
   ifelse(MinMax == "min", to.apply <- (f <= f.avg), to.apply <- (f >= f.avg) )
   
-  if (to.apply) {
+  if ( (to.apply) & (abs(f.avg - f.min)!=0) ) {
     w <- w.min + ( ( (w.max - w.min) * abs(f - f.min) ) / abs(f.avg - f.min) )
   } else w <- w.max
   
@@ -1547,9 +1548,9 @@ hydroPSO <- function(
 
     if (!is.null(n)) {
        if (length(lower) != n)
-	 stop( paste( "Invalid argument: 'length(lower) != nparam (", length(lower), "!=", n, ")'", sep="" ) )             
+	 stop( "Invalid argument: 'length(lower) != nparam (", length(lower), "!=", n, ")'" )             
        if (length(upper) != n)
-	 stop( paste( "Invalid argument: 'length(upper) != nparam (", length(lower), "!=", n, ")'", sep="" ) )
+	 stop( "Invalid argument: 'length(upper) != nparam (", length(lower), "!=", n, ")'" )
     } else n <- length(lower)      
 
     ############################################################################
@@ -1718,13 +1719,13 @@ hydroPSO <- function(
 	drty.in <- paste( getwd(), "/", drty.in, sep="")
 
       if ( !file.exists( file.path(drty.in) ) )
-	 stop( paste("Invalid argument: The directory '", drty.in, "' doesn't exist !", sep="") )
+	 stop( "Invalid argument: The directory '", drty.in, "' doesn't exist !" )
 
       if (param.ranges == basename(param.ranges) )
 	 param.ranges <- paste( file.path(drty.in), "/", param.ranges, sep="")
 
       if ( !file.exists( param.ranges ) )
-	 stop( paste("Invalid argument: The file '", param.ranges, "' doesn't exist !", sep="") ) 
+	 stop( "Invalid argument: The file '", param.ranges, "' doesn't exist !" ) 
 
       if ( is.null(model.FUN) ) {
 	stop( "'model.FUN' has to be defined !" )
-- 
GitLab