Learn R Programming

recmap (version 1.0.9)

recmapGA: Genetic Algorithm Wrapper Function for recmap

Description

higher-level function for recmap using a Genetic Algorithm as metaheuristic.

Usage

recmapGA(Map, 
  fitness = .recmap.fitness, 
  pmutation = 0.25, 
  popSize = 10 * nrow(Map), 
  maxiter = 10, 
  run = maxiter, 
  monitor = if (interactive()) { gaMonitor } else FALSE, 
  parallel = FALSE, 
  …)

Arguments

Map

defines the input map regions formatted as data.frame having the column names c('x', 'y', 'dx', 'dy', 'z', 'name') as described above.

fitness

a fitness function function(idxOrder, Map, ...) returning a number which as to be maximized.

pmutation

see docu of ga.

popSize

see docu of ga.

maxiter

see docu of ga.

run

see docu of ga.

monitor

see docu of ga.

parallel

see docu of ga.

passed through the ga method.

Value

returns a list of the input Map, the solution of the ga function, and a recmap object containing the cartogram.

References

Luca Scrucca (2013). GA: A Package for Genetic Algorithms in R. Journal of Statistical Software, 53(4), 1-37. 10.18637/jss.v053.i04.

See Also

  • recmap - Compute a Rectangular Statistical Cartogram

  • ga - Genetic Algorithms

Examples

Run this code
# NOT RUN {
## The default fitnes function is currently defined as
function(idxOrder, Map, ...){

  Cartogram <- recmap(Map[idxOrder, ])
  # a map region could not be placed; 
  # accept only feasible solutions!
  
  if (sum(Cartogram$topology.error == 100) > 0){return (0)}
  
  1 / sum(Cartogram$relpos.error)
}


## use Genetic Algorithms (GA >=3.0.0) as metaheuristic
set.seed(1)
res <- recmapGA(Map = checkerboard(4), pmutation = 0.25)

op <- par(mfrow = c(1, 3))
plot(res$Map, main = "Input Map") 
plot(res$GA, main="Genetic Algorithm")
plot(res$Cartogram, main = "Output Cartogram")


## US example
getUS_map <- function(){
  usa <- data.frame(x = state.center$x, 
  y = state.center$y, 
  # make the rectangles overlapping by correcting 
  # lines of longitude distance.
  dx = sqrt(state.area) / 2 
    / (0.8 * 60 * cos(state.center$y * pi / 180)), 
  dy = sqrt(state.area) / 2 / (0.8 * 60), 
  z = sqrt(state.area),
  name = state.name)
      
  usa$z <- state.x77[, 'Population']
  US.Map <- usa[match(usa$name, 
    c('Hawaii', 'Alaska'), nomatch = 0)  == 0, ]

  class(US.Map) <- c('recmap', 'data.frame')
  US.Map
}

# }
# NOT RUN {
# takes 34.268 seconds on CRAN
res <- recmapGA(getUS_map(), maxiter = 5)
op <- par(ask = TRUE)
plot(res)
par(op)
summary(res)
# }

Run the code above in your browser using DataLab