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 Running hydroPSO #6

Closed mlnjsh closed 4 years ago

mlnjsh commented 7 years ago

I have following function to maximize with 18<=x1<=20; 4000<=x2<=4800; 5<=x3<=13 ####################################################### f<-function(x1,x2,x3){ -53.06453125+5.146767857x1+0.001306786x2+0.25028125x3-0.00019125x1x2- 0.00005594x2x3-0.111714286x1^2+0.00000032*x2^2 } ################################################################ I am running following code lower<-c(18,4000,5) upper<-c(20,4800,13) out<-hydroPSO(fn=f, lower=lower, upper=upper, control=list(write2disk=FALSE) )

But i am getting the following Error Error in FUN(newX[, i], ...) : argument "x2" is missing, with no default Any help would be Appriciated

jthurner commented 7 years ago

From the documentation:

When fn!='hydromod', the first argument of fn has to be a vector of parameters over which optimisation is going to take place.

hydroPSO will call f() with a single vector containing all parameters (corresponding to upper/lower) as first argument. You have to unpack them inside your function, e.g.:

f <- function(parameters) {
  x1 <- parameters[1]
  x2 <- parameters[2]
  x3 <- parameters[3]
  # evaluate and return result
  # ...
}