genetic algorithm with different results at each run

27 views (last 30 days)
I tried to run the code, but unfortunatelly I found different solutions at each run. How to solve the issue, although I am sure about the objective function.
This is for your kind support please.

Accepted Answer

Walter Roberson
Walter Roberson on 20 Jan 2019
Use rng() to set the random number generator to a known value.
  3 Comments
Walter Roberson
Walter Roberson on 20 Jan 2019
rng() sets the seed not the limits of random generation. Roughly speaking, it affects the order that random numbers are generated in.

Sign in to comment.

More Answers (2)

John D'Errico
John D'Errico on 19 Jan 2019
A genetic algorithm is a stochastic algorithm, based on random sampling! What do you seriously expect?
No matter what, you will see small differences, even for a function that has a nice global min. So you can think of the results as all being effectively the same within a tolerance.
If you get different solutinos, you still need to accept that for a function that has multiple local minima, the solver can get trapped in different solutions. Sometimes they may all be equivalent solutions. For example, use any solver to minimize sin(x). Depending on the route to the solution and the start points, you will find different local minima, all of which are equivalent, none are better than the others.
  3 Comments
Walter Roberson
Walter Roberson on 20 Jan 2019
Remember that ga never promises to find the global minima, just to use strategies that are sometimes useful in finding minima. Getting caught in local minima is something you should expect with ga.
John D'Errico
John D'Errico on 20 Jan 2019
Exacty. There is no absolute assurance that a genetic algorithm, or any optimization, will always result in the same solution, or the best possible solution.
The genetic algorithm is designed to improve that characteristic, as are other optimizers, like simulated annealing, PSO, etc. But they cannot assure you will get the same result always. And this is the behavior you are seeing.
As Walter said, you can set the random seed. But that will not assure the globally optimal solution results, merely that you get the same solution.

Sign in to comment.


Abdullah Al Shereiqi
Abdullah Al Shereiqi on 20 Jan 2019
Thanks a lot for your support.
It works fine with me
Regards,
A.Shereiqi
  2 Comments
kam Pal
kam Pal on 18 Oct 2021
Did you solve the issue by using random number generator?
Or did you use any other approach? From what I understood, the problem of getting different solutions is trapping in local minimas. I am having a similar issue. Would you please throw some light on this?
Kam
Walter Roberson
Walter Roberson on 18 Oct 2021
They solved the problem of getting different solutions each time by using rng so that they will get the same results each time.
However there is no possible solution to the problem of ga or pso getting caught in local minima. Consider the function
f(x) = sin(x) - 2*(x==42971372.1673492437)
The global minimum is at that constant, and no numeric evaluation at any other location can possibly give any hint of the location of the minimum: if the evaluation does not happen to test exactly that one location then it will never find the minimum.
You need non-numeric methods such as calculus to solve such functions. But it can also be proven that even calculus is not enough.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!