Variations of Genetic Algorithms

I am working with the R programming language. I am trying to learn about different optimization algorithms such as the "Genetic Algorithm" (e.g. A quick tour of GA) vs. the "Evolutionary Grammar Algorithm" (gramEvol: Grammatical Evolution in R | Noorian | Journal of Statistical Software).

For instance, I can use the Genetic Algorithm to optimize the following function ("Rastrigin") :

#PART 1: Optimize Rastrigin Function with the Genetic Algorithm

    library(GA)
    
#define function
Rastrigin <- function(x1, x2)
{
  20 + x1^2 + x2^2 - 10*(cos(2*pi*x1) + cos(2*pi*x2))
}

#plot

x1 <- x2 <- seq(-5.12, 5.12, by = 0.1)
f <- outer(x1, x2, Rastrigin)
persp3D(x1, x2, f, theta = 50, phi = 20, col.palette = bl2gr.colors)

#run optimization algorithm and plot results

GA <- ga(type = "real-valued", 
         fitness =  function(x) -Rastrigin(x[1], x[2]),
         lower = c(-5.12, -5.12), upper = c(5.12, 5.12), 
         popSize = 50, maxiter = 1000, run = 100)

plot(GA)

summary(GA)

Fitness function value = -2.502466e-07 
Solution = 
               x1           x2
[1,] 3.341508e-05 1.203355e-05

enter image description here Now, I am using the "Evolutionary Grammar Algorithm" to optimize the same function:

#PART 2: Optimize Rastrigin Function with the Evolutionary Grammar Algorithm:

library(gramEvol)

ruleDef <- list(expr     = gsrule("<der.expr><op><der.expr>"),
                der.expr = grule(func(var), var),
                func     = grule(log, exp, sin, cos),
                op       = gsrule("+", "-", "*"),
                var      = grule(x1, x2, n),
                n        = grule(1, 2, 3, 4))

# Creating the grammar object
grammarDef <- CreateGrammar(ruleDef)

#redine the same function in a format acceptable to the "gramEvol" library

Rastrigin  <- function(expr) {
  # expr: a string containing a symbolic expression
  # returns: Symbolic regression Error
  x1 <- c(5.12, 5.12)
  x2 <- c(5.12, 5.12)
  
  result <- eval(as.expression(expr))
  

  err <- 20 + x1^2 + x2^2 - 10*(cos(2*pi*x1) + cos(2*pi*x2))
  
  return(err)
}

#run optimization 
ge <- GrammaticalEvolution(grammarDef, Rastrigin  , terminationCost = 0.001)

# print results 
print(ge, sequence = TRUE)

There were 50 or more warnings (use warnings() to see the first 50)

Grammatical Evolution Search Results:
  No. Generations:  675 
  Best Expression:  log(1) + x2 
  Best Cost:        57.8494274515718 

Question: Can someone please tell me if I have done PART 2 correctly? For some reason, I don't think that PART 1 and PART 2 produced the same answer, nor did they attempt to even solve the same problem. PART 1 actually returns a coordinate of "x1 and x2" that corresponds to the minimum point of the Rastrigin function, whereas I am not sure what the output of PART 2 corresponds to. Is it possible that the "grammatical evolution algorithm" is not a good choice for this problem? Apparently the "grammatical evolution algorithm" is said to work well when the optimization problem has "logical constraints" that can not be specified in the traditional way that "optimization constraints" are usually specified. Is this true?

Thanks!

Source: GrammaticalEvolution: Grammatical Evolution in gramEvol: Grammatical Evolution for R

gramEvol is for attempting to find expressions i.e. mathematical formulas , not an attempt to find the minimum of such functions/formulas.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.