From 7abdfc08627d8a1665625788ebf97623e018406d Mon Sep 17 00:00:00 2001
From: Mauricio Zambrano-Bigiarini <hzambran@users.noreply.github.com>
Date: Mon, 24 Sep 2012 15:05:23 +0000
Subject: [PATCH] fixed 'v[t+1]' for 'reflecting' and 'damping' boundary
 conditions

---
 DESCRIPTION   |  4 ++--
 NEWS          | 11 ++++++++++-
 R/PSO_v2012.R | 12 ++++++------
 3 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/DESCRIPTION b/DESCRIPTION
index c55e340..ee24579 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-5
-Date: 2012-09-23
+Version: 0.1-58-6
+Date: 2012-09-24
 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 8ded1ed..268a8ea 100755
--- a/NEWS
+++ b/NEWS
@@ -33,6 +33,13 @@ NEWS/ChangeLog for hydroPSO
                             -) 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'
+                            -) fixed velocity equation for 'boundary.wall="reflecting"' :
+                                           ver <= 0.1-58   ->    ver >= 0.1-59
+                                 v[t+1] :   v[t]           ->      -v[t]                 (when x[t+1] > x_max | x[t+1] < x_min )
+                            -) changed velocity equation for 'boundary.wall="damping"' :
+                                           ver <= 0.1-58   ->    ver >= 0.1-59
+                                 v[t+1] :   v[t]           ->      -v[t]                 (when x[t+1] > x_max | x[t+1] < x_min )
+                              
 
         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:
@@ -40,11 +47,13 @@ NEWS/ChangeLog for hydroPSO
                             -) npart        : 10+2*[sqrt(n)]  ->   40
                             -) Vini.type    : 'lhs2007'       ->  'random2011'
                             -) 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)
                             -) TVlambda.type: 'non-linear'    ->  'linear' (no effect, because 'use.TVlambda=FALSE' by default)
 
+                            -) boundary.wall: 'reflecting' in  hydroPSO ver <= 0.1-58 is not longer equivalent to 'reflecting' in  hydroPSO ver >= 0.1-58
+
         o 'test_functions': -) new benchmark functions: shifted sphere ('ssphere'), shifted Rosenbrock ('srosenbrock'), shifted Rastrigin ('srastrigin'), 
                                shifted Griewank ('sgriewank') and shifted Ackley ('sackley') 
        
diff --git a/R/PSO_v2012.R b/R/PSO_v2012.R
index 042c465..3e78bd1 100755
--- a/R/PSO_v2012.R
+++ b/R/PSO_v2012.R
@@ -544,14 +544,14 @@ position.update.and.boundary.treatment <- function(x, v, x.MinMax, boundary.wall
          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]
+           v.new[byd.min.pos] <- -v[byd.min.pos]
       } else if ( boundary.wall == "invisible") {
              x.new[byd.min.pos] <- x[byd.min.pos]
              v.new[byd.min.pos] <- v[byd.min.pos]
         } else if ( boundary.wall == "damping") {
              L                  <- abs( x.min[byd.min.pos] - x.new[byd.min.pos] )
              x.new[byd.min.pos] <- x.min[byd.min.pos] + runif(1)*L
-             v.new[byd.min.pos] <- v[byd.min.pos]
+             v.new[byd.min.pos] <- -v[byd.min.pos]
         }# ELSE end
  } # IF end
       
@@ -565,14 +565,14 @@ position.update.and.boundary.treatment <- function(x, v, x.MinMax, boundary.wall
         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]
+           v.new[byd.max.pos] <- -v[byd.max.pos]
         } 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]
+              v.new[byd.max.pos] <- -v[byd.max.pos]
             }# ELSE end
  } # IF end
  
@@ -2308,13 +2308,13 @@ hydroPSO <- function(
 
       ##########################################################################  
       
-      if (normalise) X <- X * (upper.mat - lower.mat) + lower.mat
+      ifelse(normalise, Xn <- X * (upper.mat - lower.mat) + lower.mat, Xn <- X)
       
       # 3.a) Evaluate the particles fitness
       if ( fn.name != "hydromod" ) {
          
 	 # Evaluating an R Function 
-	 GoF <- apply(X, fn, MARGIN=1, ...)
+	 GoF <- apply(Xn, fn, MARGIN=1, ...)
 	 
          Xt.fitness[iter, 1:npart] <- GoF
          ModelOut[1:npart]         <- GoF  ###
-- 
GitLab