hzambran / hydroPSO

Model-Independent Particle Swarm Optimisation for Environmental Models
https://cran.r-project.org/package=hydroPSO
GNU General Public License v2.0
36 stars 18 forks source link

Error when running with a single parameter #4

Closed cjzilverberg closed 8 years ago

cjzilverberg commented 8 years ago

Using R version 3.2.2 hydroPSO version 0.3-4 Windows 7

When trying to optimize a single parameter, I received an error that subsequently did not occur when I optimized 2 parameters. The message was:

[ 4) Computing the goodness of fit ... ]

[NSE= -3.152]

[Iter: 1/10. Particle: 4/4. Finished !. GoF: -3.152331e+00]

                                 |  
                                 |  

Error in X[j, ] <- out[["x.new"]] : number of items to replace is not a multiple of replacement length

I tracked this error to the position.update.and.boundary.treatment function. I don’t believe it is indexing the vectors correctly in this function. For instance, when optimizing only 1 parameter, my x.new was a vector of length 4, and x.min was a vector of length 1. Byd.min.pos was a vector of length 1, and its value was 2. Then, when boundary.wall == ‘absorbing2011’), x.new[2] <- x.min[2], but x.min is only of length 1, so it creates the error copied above.

As I pointed out, I could probably get around the problem by always optimizing at least 2 parameters, but I’m afraid this error may cause the program to execute in a way it was not intended to.

Here are the parameter settings and function call that I used:

model.drty = sim.wd setwd(model.drty) model.FUN.args=list( model.drty=getwd(), param.files='ParamFiles.txt', exe.fname=exe.fname, verbose=TRUE, #FALSE, stderr="", out.FUN=out.FUN, out.FUN.args=out.FUN.args, gof.FUN=gof.FUN, gof.FUN.args=gof.FUN.args, obs=obs )

maxit=10 par.type <- 'none' #"parallelWin" #"none" par.nnodes <- max(detectCores()-2,2) #4 par.pkgs <- c("hydroPSO","lhs","plyr","ggplot2","gridExtra","zoo","parallel","LaF")

drty.out = "PSO_Hays"

drty.out = ""

hydroPSO(fn="hydromod" , method="spso2011" , model.FUN="hydromod" , model.FUN.args=model.FUN.args , control=list( drty.in=model.drty , drty.out=drty.out , MinMax="max" #"min" #max" , maxit=maxit #50 #25 #100 , npart=4 #should be more than 4. Can set to NA for the default value of 40

, c1=2.05

       #, c2=2.05
       #, use.IW=FALSE
       #, use.CF=TRUE
       , abstol=0.95 #0.001 #0.95
       , reltol=1E-8 #1E-5 #1E-8 #0.0005 #1e-20
       #, Xini.type="lhs"
       #, Vini.type="lhs2011"
       #, best.update="sync"
       #, boundary.wall="absorbing2011"  
       #, topology="random"
       #, K=11
       #, use.TVlambda=TRUE
       #, TVlambda.rng=c(1, 0.5)
       #, plot=FALSE
       #, write2disk=TRUE
       , REPORT=1 #25
       , parallel=par.type
       , par.nnodes=par.nnodes
       , par.pkgs=par.pkgs
    )

)

azzaea commented 8 years ago

Hi! Apologies for double posting, but I only found this repo now... This problem is generated at the initialization phase of PSO in the case of 1D optimization. The way to go is to replace this line (2076 in PSO_v2013.R file): V = V <- t(apply(V, MARGIN = 1, FUN = velocity.boundary.treatment, vmax = Vmax))

with: V = matrix(apply(V, MARGIN = 1, FUN = velocity.boundary.treatment, vmax = Vmax), nrow=npart,byrow=T)

hzambran commented 8 years ago

Thanks @cjzilverberg for reporting and @zo0z for proving a solution. This issue has been fixed in the current under-development version (0.3-4-5).

You may try:

D <- 1

lower <- rep(-5.12, D)
upper <- rep(5.12, D)
setwd("~")

set.seed(100)
hydroPSO(fn=rastrigin, lower=lower, upper=upper, control=list(write2disk=FALSE) )