wildart / Evolutionary.jl

Evolutionary & genetic algorithms for Julia
Other
328 stars 59 forks source link

ga fails to minimize functions with negative range #30

Open fredcallaway opened 5 years ago

fredcallaway commented 5 years ago

Example:

best, = ga(x->-sum(x), 
          5,
          initPopulation=n->rand(Bool, n),
          iterations=10000,
          mutationRate = 0.2,
          selection = roulette
          )

I think this is because you maximize the inverse of the provided objective function. Is there a reason to do this rather than just directly minimizing the function? (Or I guess, maximizing the negation of the function)

fredcallaway commented 5 years ago

Looking at the code more, lots of stuff won't make sense with negative objective values. Is this just a standard assumption for genetic algorithms? (I'm no expert!)

apeano commented 5 years ago

I guess it is because the selection methods need to sort the population basing on the fitness. So the ga method should take in input a max/min parameter to drive the sorting phases. Maybe this is a limitation of this first version.

phelipe commented 5 years ago

You need to rewrite your problem to fit the package.

wildart commented 5 years ago

This package performs an objective function minimization by maximizing its inverse. Shape your problem to fit the package behavior.

fredcallaway commented 5 years ago

OK sure that's fine. But I think it would be good to include the constraint that the objective function be positive in the documentation.

Also, it's worth noting that taking the inverse has scaling effects that may be undesirable. In particular, for values near 0, very small differences in the objective function will lead to large differences in the inverse objective function, which could reduce population diversity if using selection functions such as roulette or SUS.