wildart / Evolutionary.jl

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

Help wanted: solution to very simple problem? #73

Closed TheMEGuy closed 3 years ago

TheMEGuy commented 3 years ago

Hi,

thanks for the cool package.

I recently started to do some research within the field of Evolutionary Strategies.

I have a question regarding a very simple problem:

indiv = [0.0,0.0]
lowerbound = [0.0,0.0]
upperbound = [20.0,20.0]

function fit(x)
    if(sum(x) == 4.0)
        return -1
    else
        return +1
    end
end

Evolutionary.optimize(fit,lowerbound,upperbound,indiv,CMAES(),Evolutionary.Options(iterations = 1000))

>Status = success
>Candidate solution
> Minimzer = [6.216071822584697,2.911369416556109]
> Minimum = 1.0
> Iterations = 12

>Found with
> Algorihm: (15,30)-CMA-ES

I do not understand why the Algorithm yields infeasible solutions, returns the status "success" and always stops after 12 Iterations.

Another question:

Is there a way to mutate the genotype, so that the mutation is a multiple of a certain step/number? The Mutations should do something like this: step = 0.25 :[1.495763498,1.23478634] --> [1.75,1.23478634]. Is there such an Algorithm in this package?

I really hope I was able to explain my problems understandably.

Thanks

wildart commented 3 years ago

The fitness function is ill-defined. There is no variation in the resulting value it always +1 (the chance of getting -1 is close to 0 in the real domain).

So after the couple iterations the difference between previous and current fitness values is 0 which is less then the tolerance threshold, so the algorithm going to stop. There are additional 10 iterations of the optimization on successful tolerance condition, controlled by successive_f_tol option.

You may want to try the following fitness function fit(x) = abs(4.0 - sum(x))

julia> fit(x) = abs(4.0 - sum(x))
fit (generic function with 1 method)

julia> Evolutionary.optimize(fit,lowerbound,upperbound,indiv,CMAES(),Evolutionary.Options(iterations = 1000))

 * Status: success

 * Candidate solution
    Minimizer:  [2.2801910167618544, 1.719808983238146]
    Minimum:    0.0
    Iterations: 478

 * Found with
    Algorithm: (15,30)-CMA-ES