R/setup.R

Defines functions initialize_parents

Documented in initialize_parents

###########################################################################################
# Function: initialize_parents
#
#' Generate the first generation of models.
#'
#' @param feature_count Number of features given
#' @param P Number of parents in the generation
#' @return A list of selected models, with each specified by the indices of selected columns.
#' @examples
#' parents <- initialize_parents(10, P = 10, number_features=4)

initialize_parents <- function(feature_count, P=2*feature_count) {

  # Sanitize input, ensure everything is an integer
  feature_count <- as.integer(feature_count)
  P <- as.integer(P)

  # Ensure input is sanitized correctly
  if (!is.integer(feature_count)) {
    stop("Feature count is not an integer")
  }
  if (!is.integer(P)) {
    stop("Generation count is not an integer")
  }

  # index_list (returns list of features that are included)
  # binary_list (returns 1 if feature is included, otherwise 0)
  index_list <- list()

  # creates all the parents in the generation
  while (length(index_list) < P) {

    # samples a fixed number of features from the total features
    indexes <- sort(sample(c(1:feature_count), size = sample(0:feature_count, 1)))
    index_length <- length(index_list)
    index_list[[index_length + 1]] <- indexes

    # Check for duplicates!
    index_list <- unique(index_list)
  }

  return (list("index" = index_list))
}
kunaljaydesai/GA documentation built on May 28, 2019, 7:38 a.m.