Skip to content
Snippets Groups Projects
Commit c7eb141a authored by Mauricio Zambrano-Bigiarini's avatar Mauricio Zambrano-Bigiarini
Browse files

hydroPSO.R: removed 'param.IDs' as argumnt of 'InitializeX' and 'InitializeV'

parent a1d02005
No related branches found
No related tags found
No related merge requests found
...@@ -8,7 +8,7 @@ NEWS/ChangeLog for hydroPSO ...@@ -8,7 +8,7 @@ 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 ) * 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 * optional normalisation of parameter values
-) improved performance. ~ 33% faster for 32-bit machines and 38% faster for 64-bit machines. -) 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'. 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 -) '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. valid R function. In previous versions of 'hydroPSO' only a character type was accepted.
......
...@@ -138,8 +138,8 @@ alea.normal <- function(mean=0, sd=1) { ...@@ -138,8 +138,8 @@ alea.normal <- function(mean=0, sd=1) {
# Created: 19-Sep-2012 # # Created: 19-Sep-2012 #
# Updates: # # Updates: #
################################################################################ ################################################################################
# Purpose : It generates a random point inside the hypersphere centered around# # Purpose : It generates a random point inside the hypersphere centered #
# G with radius = r # # around G with radius = r #
################################################################################ ################################################################################
# Output : numeric vector with the location of a random point inside the # # Output : numeric vector with the location of a random point inside the #
# hypersphere around G with radius = r # # hypersphere around G with radius = r #
...@@ -868,8 +868,6 @@ decrease.search.space <- function(Lmin, x.MinMaxCurrent, x.MinMaxRange, x.best, ...@@ -868,8 +868,6 @@ decrease.search.space <- function(Lmin, x.MinMaxCurrent, x.MinMaxRange, x.best,
# of all the particles in the swarm # # of all the particles in the swarm #
################################################################################ ################################################################################
# -) npart : number of particles # -) npart : number of particles
# -) param.IDs : character, with the ID of each parameter/dimension.
# It has 'n' elements, where 'n' is the number of dimensions
# -) X.MinMax : Matrix with the minimum and maximum values for each dimension # -) X.MinMax : Matrix with the minimum and maximum values for each dimension
# during the current iteration # during the current iteration
# -) Rows = 'n' (number of parameters) # -) Rows = 'n' (number of parameters)
...@@ -879,10 +877,7 @@ decrease.search.space <- function(Lmin, x.MinMaxCurrent, x.MinMaxRange, x.best, ...@@ -879,10 +877,7 @@ decrease.search.space <- function(Lmin, x.MinMaxCurrent, x.MinMaxRange, x.best,
# 'init.type' : character, indicating how to carry out the initialization # 'init.type' : character, indicating how to carry out the initialization
# of the position of all the particles in the swarm # of the position of all the particles in the swarm
# valid values are in c('random', 'lhs') # valid values are in c('random', 'lhs')
InitializateX <- function(npart, param.IDs, x.MinMax, x.ini.type) { InitializateX <- function(npart, x.MinMax, x.ini.type) {
# Number of parameters
n <- length(param.IDs)
# 'X' # # 'X' #
# Matrix of unknown parameters. # Matrix of unknown parameters.
...@@ -910,8 +905,6 @@ InitializateX <- function(npart, param.IDs, x.MinMax, x.ini.type) { ...@@ -910,8 +905,6 @@ InitializateX <- function(npart, param.IDs, x.MinMax, x.ini.type) {
# of all the particles in the swarm # # of all the particles in the swarm #
################################################################################ ################################################################################
# -) npart : number of particles # -) npart : number of particles
# -) param.IDs : character, with the ID of each parameter/dimension.
# It has 'n' elements, where 'n' is the number of dimensions
# -) X.MinMax : Matrix with the minimum and maximum values for each dimension # -) X.MinMax : Matrix with the minimum and maximum values for each dimension
# during the current iteration # during the current iteration
# -) Rows = 'n' (number of parameters) # -) Rows = 'n' (number of parameters)
...@@ -921,10 +914,10 @@ InitializateX <- function(npart, param.IDs, x.MinMax, x.ini.type) { ...@@ -921,10 +914,10 @@ InitializateX <- function(npart, param.IDs, x.MinMax, x.ini.type) {
# 'v.ini' : character, indicating how to carry out the initialization # 'v.ini' : character, indicating how to carry out the initialization
# of the velocitites of all the particles in the swarm # of the velocitites of all the particles in the swarm
# valid values are in c('zero', 'random2007', 'lhs2007', 'random2011', 'lhs2011') # valid values are in c('zero', 'random2007', 'lhs2007', 'random2011', 'lhs2011')
InitializateV <- function(npart, param.IDs, x.MinMax, v.ini.type, Xini) { InitializateV <- function(npart, x.MinMax, v.ini.type, Xini) {
# Number of parameters # Number of parameters
n <- length(param.IDs) n <- nrow(x.MinMax)
# 'V' # # 'V' #
# Matrix of velocities for each particle and iteration. # Matrix of velocities for each particle and iteration.
...@@ -1135,6 +1128,22 @@ RegroupingSwarm <- function(x, ...@@ -1135,6 +1128,22 @@ RegroupingSwarm <- function(x,
#rf <- 6/(5*RG.thr) # Evers & Ghalia #rf <- 6/(5*RG.thr) # Evers & Ghalia
#rf <- (1/RG.thr)/2 # MZB #rf <- (1/RG.thr)/2 # MZB
## # Removing possible attributes
## gbest <- as.numeric( gbest )
## x.min.rng <- as.numeric( x.Range[ ,1] )
## x.max.rng <- as.numeric( x.Range[ ,2] )
##
## xmin <- x.min.rng + rf*abs(x.min.rng)
## xmax <- x.max.rng - rf*abs(x.max.rng)
## x.MinMax <- cbind(xmin, xmax)
##
## x <- InitializateX(npart, x.MinMax, x.ini.type="lhs")
## x <- x+gbest
##
## # Maximum length of the parameter space in each dimension
## Lmax <- x.max.rng - x.min.rng
## Lnew <- Lmax
# name of each parameter # name of each parameter
param.IDs <- row.names(x.Range) param.IDs <- row.names(x.Range)
...@@ -1909,10 +1918,9 @@ hydroPSO <- function( ...@@ -1909,10 +1918,9 @@ hydroPSO <- function(
Vmax <- lambda*Lmax Vmax <- lambda*Lmax
X <- InitializateX(npart=npart, param.IDs=param.IDs, X <- InitializateX(npart=npart, x.MinMax=X.Boundaries, x.ini.type=Xini.type)
x.MinMax=X.Boundaries, x.ini.type=Xini.type) V <- InitializateV(npart=npart, x.MinMax=X.Boundaries, v.ini.type=Vini.type,
V <- InitializateV(npart=npart, param.IDs=param.IDs, Xini=X)
x.MinMax=X.Boundaries, v.ini.type=Vini.type, Xini=X)
V <- t(apply(V, MARGIN=1, FUN=velocity.boundary.treatment, vmax=Vmax)) V <- t(apply(V, MARGIN=1, FUN=velocity.boundary.treatment, vmax=Vmax))
if (!missing(par)) { if (!missing(par)) {
...@@ -2607,7 +2615,7 @@ hydroPSO <- function( ...@@ -2607,7 +2615,7 @@ hydroPSO <- function(
if (do.RandomGeneration) { if (do.RandomGeneration) {
if (topology == "gbest") { if (topology %in% c("gbest", "random") ) {
gbest.fit.bak <- gbest.fit gbest.fit.bak <- gbest.fit
gbest.pos.bak <- gbest.pos gbest.pos.bak <- gbest.pos
x.bak <- X[gbest.pos,] x.bak <- X[gbest.pos,]
...@@ -2632,7 +2640,7 @@ hydroPSO <- function( ...@@ -2632,7 +2640,7 @@ hydroPSO <- function(
X <- tmp[["X"]] X <- tmp[["X"]]
if (topology == "gbest") { if (topology %in% c("gbest", "random") ) {
X[gbest.pos,] <- x.bak X[gbest.pos,] <- x.bak
#V[gbest.pos,] <- v.bak #V[gbest.pos,] <- v.bak
gbest.fit <- gbest.fit.bak gbest.fit <- gbest.fit.bak
...@@ -2645,9 +2653,8 @@ hydroPSO <- function( ...@@ -2645,9 +2653,8 @@ hydroPSO <- function(
gbest.pos <- gbest.pos.bak gbest.pos <- gbest.pos.bak
} # IF end } # IF end
V <- InitializateV(npart=npart, param.IDs=param.IDs, V <- InitializateV(npart=npart, x.MinMax=X.Boundaries,
x.MinMax=X.Boundaries, v.ini.type=Vini.type, v.ini.type=Vini.type, Xini=X)
Xini=X)
GPbest.fit.rate <- +Inf GPbest.fit.rate <- +Inf
if (MinMax=="max") { if (MinMax=="max") {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment