Skip to content

Commit

Permalink
refactor mergePopulations. now much more efficient
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobbossek committed Feb 13, 2015
1 parent edf9aea commit de18099
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
19 changes: 0 additions & 19 deletions R/makePopulation.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,3 @@ makePopulation = function(individuals, fitness = NULL) {
classes = c("ecrPopulation", "setOfIndividuals")
)
}

# Helper for merging populations.
#
# @param .. [\code{list}]\cr
# List of objects of type \code{setOfIndividuals}.
# @return [\code{setOfIndividuals}]
mergePopulations = function(...) {
populations = list(...)
individuals = data.frame()
fitness = c()
for (i in 1:length(populations)) {
individuals = rbind(individuals, populations[[i]]$individuals)
fitness = c(fitness, populations[[i]]$fitness)
}
makePopulation(
individuals = as.matrix(individuals),
fitness = fitness
)
}
31 changes: 31 additions & 0 deletions R/mergePopulations.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Helper for merging populations.
#
# @param ... [\code{list}]\cr
# List of objects of type \code{setOfIndividuals}.
# @return [\code{setOfIndividuals}]
mergePopulations = function(...) {
populations = list(...)
# get n.params
n.params = ncol(populations[[1]]$individuals)

# summarize over all population sizes
n.pop = sum(sapply(populations, function(x) length(x$fitness)))

# allocate space
fitness = numeric(n)
individuals = matrix(NA, ncol = m, nrow = n)

# now iterate over populations and generate merged population
#FIXME: maybe better implement this in C++
start = 1L
for (i in 1:length(populations)) {
j = nrow(populations[[i]]$individuals)
individuals[start:(start + j - 1), ] = populations[[i]]$individuals
fitness[start:(start + j - 1)] = populations[[i]]$fitness
start = start + j
}
makePopulation(
individuals = individuals,
fitness = fitness
)
}

0 comments on commit de18099

Please sign in to comment.