1

I'm working on an optimsation problem using R's GA package with a 'permutation' type genetic algorithm. I need to introduce some parameters for how the initial population is generated before parents selection and crossover. The reason for this is: there is a general framework for arrangements of the genes in the chromosomes that can work at all, but at the same time, I do need a lot of randomization to find local maxima--not just test some suggested solutions using the suggestions argument of the ga() function.

If you check out the R GA package github repo, you can see there's a population generator function on line 576 that does the following:

gaperm_Population_R <- function(object)
{
  int <- seq.int(object@lower, object@upper)
  n <- length(int)
  population <- matrix(NA, nrow = object@popSize, ncol = n)
  for(i in 1:object@popSize)
     population[i,] <- sample(int, replace = FALSE)
  return(population)
}

I want to create a new function that is quite similar, but which takes some pre-calculated parameters pop_parms, and then call that function through the population argument of the ga() function, instead of using the default function, population = gaControl(type)$population.

My new function would look like this, with the new pop_parms argument:

gaperm_Feasible_Pop <- function(object, pop_parms)
{
  int <- seq.int(object@lower, object@upper)
  n <- length(int)
  population <- matrix(NA, nrow = object@popSize, ncol = n)
  for(i in 1:object@popSize)
    population[i,] <- sapply(pop_parms, function(x) sample(x, replace = FALSE)
      )
  return(population)
}

Of course, when I try to use this function, the package doesn't know how to pass through the object parameter.

Is there anyone who could help me get this function to work, or perhaps take a different approach?

kenmore17
  • 41
  • 3

2 Answers2

0

For it to work, you don't need to pass the "object" param, GA does it automatically. If you wanted to pass more params, you could pass in the ga method after the name of the function and separate by commas.

Example:

population = your_function, param1, param2, param3,

0

If it is just the initial population, you could do so by calculating it as you wish, and adding it as a suggested solution for the algorithm:

initial_pop <- (you define it)

GA <- ga(type = "permutation",
       fitness = fitness_function,
       suggestions = initial_pop,
       etc)

Note that the ga algorithm needs to have minimum the same number of population size as your suggested population rows. However, it can have a bigger population (e.g., you can have a population of 512, but suggest only 256).

brbe
  • 1