From b4ab64c28302104a2de83fb307c138dc6de5aefa Mon Sep 17 00:00:00 2001
From: Mauricio Zambrano-Bigiarini <hzambran@users.noreply.github.com>
Date: Sun, 23 Sep 2012 23:21:55 +0000
Subject: [PATCH] hydroPSO: new 'boundary.wall' possible: 'absorbing2011'

---
 DESCRIPTION     |  4 ++--
 NEWS            | 19 ++++++++++++++-----
 R/PSO_v2012.R   | 36 +++++++++++++++++++++++-------------
 man/hydroPSO.Rd |  6 +++---
 4 files changed, 42 insertions(+), 23 deletions(-)

diff --git a/DESCRIPTION b/DESCRIPTION
index e0644d2..c55e340 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-4
-Date: 2012-09-22
+Version: 0.1-58-5
+Date: 2012-09-23
 Author: Mauricio Zambrano-Bigiarini [aut, cre] and Rodrigo Rojas [ctb]
 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>
diff --git a/NEWS b/NEWS
index 473710b..8ded1ed 100755
--- a/NEWS
+++ b/NEWS
@@ -14,13 +14,22 @@ NEWS/ChangeLog for hydroPSO
                             -) argument 'method'. The old (default) value 'pso' was replaced by the new (default) value 'spso2011'
                             -) argument 'Vini.type' now allows the following 2 new values: 'random2011' and 'lhs2011', which set the initial velocity 
                                values for the particles to random values according to the equation specified in the SPSO 2011, with a uniform distribution 
-                               or with a Latin-hypercube sampling, respectively. The old values 'random' and 'lhs' were replaced by the new 
-                               Default ? (so far, SPSO 2011)
+                               or with a Latin-hypercube sampling, respectively.
                             -) argument 'Vini.type'. The old values 'random' and 'lhs' were replaced by 'random2007' and 'lhs2007', in order to make 
-                               clear that the follow the equation described in SPSO 2007.
+                               clear that it follows the equation described in SPSO 2007.
+                            -) argument 'Vini.type', when missing its value depends on the value of the 'method' argument:
+                               method == 'spso2007' => Vini.type='random2007' 
+                               method != 'spso2007' => Vini.type='random2011'
                             -) argument 'npart', when missing its value depends on the value of the 'method' argument:
                                method == 'spso2007' => npart=10+2*[sqrt(n)] 
                                method != 'spso2007' => npart=40
+                            -) argument 'boundary.wall' now allows the following new values: 'absorbing2011', which set 'boundary.wall' to the absorbing condition
+                               specified in the SPSO 2011
+                            -) argument 'boundary.wall'. The old value 'absorbing' was replaced by 'absorbing2007', in order to make clear that is follows 
+                               the equation described in SPSO 2007.
+                            -) argument 'boundary.wall', when missing its value depends on the value of the 'method' argument:
+                               method == 'spso2007' => boundary.wall='absorbing2007' 
+                               method != 'spso2007' => boundary.wall='absorbing2011'
                             -) in the documentation, default values are now mentioned for each argument of 'control'.
                             -) the 'PSO_logfile.txt' now contains information about: 'best.update', 'random.update', 'Xini.type', 'Vini.type'.
                                In addition, information about 'IW.type' and 'IW.exp' is now written only when 'length(IW.w) > 1'
@@ -28,9 +37,9 @@ NEWS/ChangeLog for hydroPSO
         o Running 'hydroPSO' >= 0.1-59 with default settings will produce DIFFERENT RESULTS from those obtained with 'hydroPSO' <= 0.1-58, due to the 
                                following changes in default values:
                                               ver <= 0.1-58   ->    ver >= 0.1-59
-                            -) npart        : 10+2*[sqrt(n)]  ->  40
+                            -) npart        : 10+2*[sqrt(n)]  ->   40
                             -) Vini.type    : 'lhs2007'       ->  'random2011'
-                            -) boundary.wall: 'reflecting'    ->  'absorbing'
+                            -) boundary.wall: 'reflecting'    ->  'absorbing2011'
 
                             -) TVc1.type    : 'non-linear'    ->  'linear' (no effect, because 'use.TVc1=FALSE' by default)
                             -) TVc2.type    : 'non-linear'    ->  'linear' (no effect, because 'use.TVc2=FALSE' by default)
diff --git a/R/PSO_v2012.R b/R/PSO_v2012.R
index 2e01260..042c465 100755
--- a/R/PSO_v2012.R
+++ b/R/PSO_v2012.R
@@ -495,7 +495,8 @@ velocity.boundary.treatment <- function(v, vmax ) {
 # Author : Mauricio Zambrano-Bigiarini                                         #
 ################################################################################
 # Started: 2008                                                                #
-# Updates:                                                                     #
+# Updates: Nov-2011                                                            #
+#          23-Sep-2012                                                         #
 ################################################################################
 # 'x'             : vector of 'n' parameters, corresponding to one particle
 # 'X.MinMax'      : string indicating if PSO have to find a minimum or a maximum 
@@ -535,10 +536,13 @@ position.update.and.boundary.treatment <- function(x, v, x.MinMax, boundary.wall
  
  byd.min.pos <- which(x.new < x.min)
  if ( length(byd.min.pos) > 0) { 
-    if ( boundary.wall == "absorbing") {     
+    if ( boundary.wall == "absorbing2011") {     
        x.new[byd.min.pos] <- x.min[byd.min.pos]
        v.new[byd.min.pos] <- -0.5*v[byd.min.pos]      
-    } else if ( boundary.wall == "reflecting") {    
+    } else if ( boundary.wall == "absorbing2007") {     
+         x.new[byd.min.pos] <- x.min[byd.min.pos]
+         v.new[byd.min.pos] <- 0*v[byd.min.pos]      
+      } else if ( boundary.wall == "reflecting") {    
            x.new[byd.min.pos] <- 2*x.min[byd.min.pos] - x.new[byd.min.pos] 
            v.new[byd.min.pos] <- v[byd.min.pos]
       } else if ( boundary.wall == "invisible") {
@@ -553,20 +557,23 @@ position.update.and.boundary.treatment <- function(x, v, x.MinMax, boundary.wall
       
  byd.max.pos <- which( x.new > x.max )
  if ( length(byd.max.pos) > 0 ) {	 
-    if ( boundary.wall == "absorbing") { 
+    if ( boundary.wall == "absorbing2011") { 
        x.new[byd.max.pos] <- x.max[byd.max.pos]
        v.new[byd.max.pos] <- -0.5*v[byd.max.pos] 
-    } else if ( boundary.wall == "reflecting") {
+    } else if ( boundary.wall == "absorbing2007") { 
+        x.new[byd.max.pos] <- x.max[byd.max.pos]
+        v.new[byd.max.pos] <- 0*v[byd.max.pos] 
+      } else if ( boundary.wall == "reflecting") {
            x.new[byd.max.pos] <- 2*x.max[byd.max.pos] - x.new[byd.max.pos] 
            v.new[byd.max.pos] <- v[byd.max.pos]
-      } else if ( boundary.wall == "invisible") {
+        } else if ( boundary.wall == "invisible") {
              x.new[byd.max.pos] <- x[byd.max.pos]
              v.new[byd.max.pos] <- v[byd.max.pos]
-        } else if ( boundary.wall == "damping") {
-             L                  <- abs( x.new[byd.max.pos] - x.max[byd.max.pos])
-             x.new[byd.max.pos] <- x.max[byd.max.pos] - runif(1)*L
-             v.new[byd.max.pos] <- v[byd.max.pos]
-        }# ELSE end
+          } else if ( boundary.wall == "damping") {
+              L                  <- abs( x.new[byd.max.pos] - x.max[byd.max.pos])
+              x.new[byd.max.pos] <- x.max[byd.max.pos] - runif(1)*L
+              v.new[byd.max.pos] <- v[byd.max.pos]
+            }# ELSE end
  } # IF end
  
  out <- list(x.new=x.new, v.new=v.new)		
@@ -1283,7 +1290,7 @@ hydromod.eval <- function(part, Particles, iter, npart, maxit,
 #          15-Jan-2012 ; 23-Jan-2012 ; 30-Jan-2012 ; 23-Feb-2012 ; 23-Mar-2012 #
 #          14-Jun-2012 ; 15-Jun-2012 ; 03-Jul-2012 ; 06-Jul-2012               #
 #          11-Jul-2012 ; 17-Jul-2012 ; 18-Jul-2012 ; 13-Sep-2012; 14-Sep-2012  #
-#          17-Sep-2012 ;                                                       #                          
+#          17-Sep-2012 ; 23-Sep-2012                                           #                          
 ################################################################################
 # 'lower'           : minimum possible value for each parameter
 # 'upper'           : maximum possible value for each parameter
@@ -1569,7 +1576,7 @@ hydroPSO <- function(
 	    Vini.type=c(NA, "random2011", "lhs2011", "random2007", "lhs2007",  "zero"), 
 	    best.update=c("sync", "async"),
 	    random.update=TRUE,
-	    boundary.wall=c("absorbing", "reflecting", "damping", "invisible"),
+	    boundary.wall=c(NA, "absorbing2011", "absorbing2007", "reflecting", "damping", "invisible"),
 	    topology=c("random", "gbest", "lbest", "vonNeumann"), K=3, 
 	    iter.ini=0, # only used when 'topology=lbest'   
 	    ngbest=4,   # only used when 'method=ipso'   
@@ -1598,6 +1605,9 @@ hydroPSO <- function(
                             Vini.type)    
     best.update   <- match.arg(control[["best.update"]], con[["best.update"]]) 
     boundary.wall <- match.arg(control[["boundary.wall"]], con[["boundary.wall"]]) 
+    boundary.wall <- ifelse(is.na(boundary.wall), 
+                            ifelse(method=="spso2007", "absorbing2007", "absorbing2011"),
+                            boundary.wall)
     topology      <- match.arg(control[["topology"]], con[["topology"]]) 
     IW.type       <- match.arg(control[["IW.type"]], con[["IW.type"]])
     TVc1.type     <- match.arg(control[["TVc1.type"]], con[["TVc1.type"]]) 
diff --git a/man/hydroPSO.Rd b/man/hydroPSO.Rd
index 9df5adc..c018e57 100755
--- a/man/hydroPSO.Rd
+++ b/man/hydroPSO.Rd
@@ -145,7 +145,7 @@ character, indicates how to initialise the particles' velocities in the swarm. V
 -) \kbd{random2007}: random initialisation of velocities within \code{lower} and \code{upper} using the \sQuote{half-diff} method defined in SPSO 2007 (\samp{Vini=[U(lower, upper)-Xini]/2}) (see Clerc, 2012, 2010) \cr
 -) \kbd{lhs2007}: same as in \kbd{random2007}, but using a Latin Hypercube initialisation with \code{npart} number of strata instead of a random uniform distribution for each parameter (\samp{Vini=[LHS(lower, upper)-Xini]/2}) (see Clerc, 2010). \bold{It requires the \pkg{lhs} package} \cr
 -) \kbd{zero}: all the particles are initialised with zero velocity \cr
-By default \code{Vini.type='random2011'}
+By default \code{Vini.type=NA}, which means that \code{Vini.type} depends on the value of \code{method}: when \kbd{method='spso2007'} \code{Vini.type='random2007'}, or \code{Vini.type='random2011'} otherwise
 }
   \item{best.update}{
 character, indicates how (when) to update the global/neighbourhood and personal best. Valid values are: \cr
@@ -158,8 +158,8 @@ logical, if \code{TRUE} the particles are processed in random order to update th
 By default \code{random.update=TRUE}
 }
   \item{boundary.wall}{ 
-character, indicates the type of boundary condition to be applied during optimisation. Valid values are in \code{c('absorbing', 'reflecting', 'damping', 'invisible')} \cr
-By default \code{boundary.wall='absorbing'}\cr
+character, indicates the type of boundary condition to be applied during optimisation. Valid values are in \code{c(NA, 'absorbing2011', 'absorbing2007', 'reflecting', 'damping', 'invisible')} \cr
+By default \code{boundary.wall=NA}, which means that \code{boundary.wall} depends on the value of \code{method}: when \kbd{method='spso2007'} \code{boundary.wall='aborbing2007'}, or \code{boundary.wall='aborbing2011'} otherwise\cr
 
 Experience has shown that Clerc's constriction factor and the inertia weights do not always confine the particles within the solution space. To address this problem, Robinson and Rahmat-Samii (2004) and Huang and Mohan (2005) propose different boundary conditions, namely, \kbd{reflecting}, \kbd{damping}, \kbd{absorbing} and \kbd{invisible} to define how particles are treated when reaching the boundary of the searching space (see Robinson and Rahmat-Samii (2004) and Huang and Mohan (2005) for further details)
 }
-- 
GitLab