jskoien / intamapInteractive

2 stars 0 forks source link

ssaOptim #1

Open OrdinarySK opened 4 years ago

OrdinarySK commented 4 years ago

I want to ask you some questions. counld you tell me how to achieve “spatial simulated annealing(SSA)”algorithm? I want to programmatically achieve ssa optimization of MKV for multiple goals. I hope you can tell us about the source code or website of SSA implementation.

jskoien commented 4 years ago

The SSA code here is implemented in the function ssaMap. The source code can be seen in R, and is visible here in GitHub: https://github.com/jskoien/intamapInteractive/blob/master/R/ssaMap.R Or did you mean something else than this?

OrdinarySK commented 4 years ago

Thank you very much. My object function is the weighting of multi-mkv. could you give me some advices to programming the function. Before this, I used the "ssaOptim“ function finshed calculation of single mkv. I also added some points to fix the suitable number of points. Can I define my own objective function based on your package to achieve my needs? Because the preparation before optimization based on the objective function is applicable based on the intamapInteractive package.

jskoien commented 4 years ago

The package as it is now is probably not flexible enough to achieve what I think you would like to do. What you could do is download the source code, then you could either:

  1. Modify the code of CalculateMukv to do what you would like it to do (easy - but would only work for your particular case)
  2. Make ssaMap more flexible, that it can take different functions as input (a bit more complicated - but would be useful for anyone with similar issues in the future) Then you can reinstall the package from your local copy instead of the CRAN version. If you follow path 2, I would be interested in seeing the code, as it could be a nice feature to integrate in the package.
OrdinarySK commented 4 years ago

I modified the code of CalculateMukv . Here are my object function code:

**__**`calculateMukv` <-
  function (observations, predGrid, model, formulaString, fun, ...)
  {
    if (!missing(fun) && is.function(fun)) {
      return(do.call(fun, list(observations, predGrid, model, formulaString, ...)))
    }
    if(length(model) !=3){
      stop("model must contain three variogram")
    }else{
      model1 = model[[1]]
      model2 = model[[2]]
      model3 = model[[3]]
    }
    prG = predGrid
    obs = observations_**

    if (missing(formulaString) || is.null(formulaString)) {
      eq1 = dum ~ 1
      eq2 = dum ~ 1
      eq3 = dum ~ 1
    } 
    if(length(formulaString) == 3){
      eq1 = formulaString[[1]] #lst
      eq2 = formulaString[[2]] #pre
      eq3 = formulaString[[3]] #twi

    }else{
      stop("must input three parameter")
    }
    ############ changed following 'if statement' 
    if (!"data.frame" %in% getSlots(class(obs)) &  all(all.vars(eq1)[-1] %in% dimnames(coordinates(obs))[[2]])
        &  all(all.vars(eq2)[-1] %in% dimnames(coordinates(obs))[[2]]) 
        &  all(all.vars(eq3)[-1] %in% dimnames(coordinates(obs))[[2]])) {
      obs = SpatialPointsDataFrame(obs, data = data.frame(dum = rep(1,
                                                                    dim(coordinates(obs))[1])))
      names(obs) = as.character("mutil-MKV")
    }
    lst_krige = krige(eq1, obs, prG, model1)
    pre_krige = krige(eq2, obs, preG, model2)
    soil_krige = krige(eq3, obs, preG, model3)
    lst_mkv = mean(lst_krige$var1.var)
    pre_mkv = mean(pre_krige$var1.var)
    soil_mkv = mean(pre_krige$var1.var)
    mutil_obj = lst_mkv / (3 * model1$psill[2]) + pre_mkv / (3 * model2$psill[2])
    + soil_mkv / (3 * model3$psill[2])

    'red_ann_gam_krig = krige(eq, obs, prG, model)'
    return(mutil_obj)
  }**_

My mutil-Mukv need three models and formulas. The parameter of model and formulaString I pass by default is a list containing three objects. But I meet new problem. I modified the parameter number of calculateMukv so that need to modify ssaOptim and ssaMap. Because of ssaOptim and ssaMap also used calculateMukv. ``