Laboratorio-de-Pedometria / spsann-package

Optimization of Spatial Samples via Simulated Annealing
8 stars 5 forks source link

plotting and objective function tracking #12

Closed AlexandreWadoux closed 5 years ago

AlexandreWadoux commented 5 years ago

I have a problem to return the objective function values (track=T) and for the plotting (plotit=T). I tested on both function optimUSER and optimCLHS and it happens the same. Find below a reproducible example:

data(meuse.grid, package = "sp") schedule <- scheduleSPSANN( chains = 100, initial.temperature = 10, temperature.decrease = 0.5, x.max = 1540, y.max = 2060, x.min = 0, y.min = 0, cellsize = 0)

set.seed(2001) res <- optimCLHS( points = 20, candi = meuse.grid[,c('x', 'y')], covars = meuse.grid[, c(1,5)], use.coords = F, schedule = schedule, track=T, plotit = F)

or:

data(meuse.grid, package = "sp")

schedule <- scheduleSPSANN( chains = 100, initial.temperature = 10, temperature.decrease = 0.5, x.max = 1540, y.max = 2060, x.min = 0, y.min = 0, cellsize = 0)

set.seed(2001) res <- optimCLHS( points = 20, candi = meuse.grid[,c('x', 'y')], covars = meuse.grid[, c(1,5)], use.coords = F, schedule = schedule, track=F, plotit = T)

Both return an error at the end of during the optimization: Error in rbind(deparse.level, ...) : numbers of columns of arguments do not match

But when I set the weights weights = list(O1 = 0.5, O3 = 0.5) then this is solved.

samuel-rosa commented 5 years ago

I am looking into it!

samuel-rosa commented 5 years ago

If you do not set the weights, then optimCLHS automatically uses weights = list(O1 = 1/3, O2 = 1/3, O3 = 1/3). Note that it is not smart enough to see that one could be providing only continuous covariates -- I see that this is your case. Then it creates a data.frame with four columns to store the objective function values (O1, O2, O3, and their weighted sum).

optimCLHS gets smarter up ahead. It checks if one provided continuous, categorical or both types of covariates and guesses which objective functions to use. Because you provided only continuous covariates, optimCLHS creates a three-column data.frame to store the three objective function values (O1, O3, and their weighted sum).

When trying to fill the tracking data.frame it created in the beginning of the exercise, optimCLHS finds out that the the two data.frames ha different numbers of columns. Thus the error:

Error in rbind(deparse.level, ...) : numbers of columns of arguments do not match

This should be easy to fix.

AlexandreWadoux commented 5 years ago

Please note that by setting track=Fand plotit=F, the problem doesn't occur, even without specifying the weights!

data(meuse.grid, package = "sp")
schedule <- scheduleSPSANN( chains = 100, initial.temperature = 10, temperature.decrease = 0.5, x.max = 1540, y.max = 2060, x.min = 0, y.min = 0, cellsize = 0)

set.seed(2001)
res <- optimCLHS( points = 20, candi = meuse.grid[,c('x', 'y')], covars = meuse.grid[, c(1,5)], use.coords = F, schedule = schedule, track=F, plotit = F)
samuel-rosa commented 5 years ago

Yes, I am aware. I am thinking of changing the status of the argument weights from optional do mandatory. First, this would solve the issue. Second, it would guarantee that the user is aware of what s/he is doing.

AlexandreWadoux commented 5 years ago

I think it is a good solution, it would indeed solve the problem.

samuel-rosa commented 5 years ago

Done! Argument weights now is a mandatory argument.