luca-scr / GA

An R package for optimization using genetic algorithms
http://luca-scr.github.io/GA/
91 stars 29 forks source link

Argument names doesn't seem to work #31

Closed tamas-ferenci closed 5 years ago

tamas-ferenci commented 5 years ago

Consider the following example:

library( GA )

Rastrigin <- function(x1, x2)
{
  print(str(x1))
  print(str(x2))
  20 + x1^2 + x2^2 - 10*(cos(2*pi*x1) + cos(2*pi*x2))
}

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),
         names = c( "a", "b" ),
         popSize = 50, maxiter = 100,
         optim = TRUE)

I.e. x1 and x2 doesn't seem to be named, despite the option present.

luca-scr commented 5 years ago

It works as I intended to make it work. See

> summary(GA)
── Genetic Algorithm ─────────────────── 

GA settings: 
Type                  =  real-valued 
Population size       =  50 
Number of generations =  100 
Elitism               =  2 
Crossover probability =  0.8 
Mutation probability  =  0.1 
Search domain = 
          a     b
lower -5.12 -5.12
upper  5.12  5.12

GA results: 
Iterations             = 100 
Fitness function value = 0 
Solution = 
     a b
[1,] 0 0

You can see that the two decision variables are named a and b.

tamas-ferenci commented 5 years ago

Thank you very much @luca-scr ! I now see what's going on. However, what I actually wanted to achieve is to have names inside the fitness function, i.e., that the passed x is a named vector. Is it possible to achieve this somehow...?

luca-scr commented 5 years ago

Nope.

tamas-ferenci commented 5 years ago

Thanks!