recmapGA {recmap} | R Documentation |
Genetic Algorithm Wrapper Function for recmap
Description
higher-level function for recmap
using a Genetic Algorithm as
metaheuristic.
Usage
recmapGA(
V,
fitness = .recmap.fitness,
pmutation = 0.25,
popSize = 10 * nrow(Map),
maxiter = 10,
run = maxiter,
monitor = if (interactive()) {
gaMonitor
} else FALSE,
parallel = FALSE,
...
)
Arguments
V |
defines the input map regions formatted as |
fitness |
the fitness function, any allowable R function which takes as input an individual |
pmutation |
the probability of mutation in a parent chromosome. Usually mutation occurs with a small probability, and by default is set to 0.1. |
popSize |
the population size. |
maxiter |
the maximum number of iterations to run before the GA search is halted. |
run |
the number of consecutive generations without any improvement in the best fitness value before the GA is stopped. |
monitor |
a logical or an R function which takes as input the current state of the |
parallel |
An optional argument which allows to specify if the Genetic Algorithm should be run sequentially or in parallel. For a single machine with multiple cores, possible values are:
In all the cases described above, at the end of the search the cluster is automatically stopped by shutting down the workers. If a cluster of multiple machines is available, evaluation of the fitness function can be executed in parallel using all, or a subset of, the cores available to the machines belonging to the cluster. However, this option requires more work from the user, who needs to set up and register a parallel back end.
In this case the cluster must be explicitly stopped with |
... |
additional arguments to be passed to the fitness function. This allows to write fitness functions that keep some variables fixed during the search. |
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. doi:10.18637/jss.v053.i04.
Examples
## 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)
## https://github.com/luca-scr/GA/issues/52
if (Sys.info()['machine'] == "arm64") GA::gaControl(useRcpp = FALSE)
res <- recmapGA(V = 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(V = getUS_map(), maxiter = 5)
op <- par(ask = TRUE)
plot(res)
par(op)
summary(res)
## End(Not run)