# Why is ga optimization ridiculously slow to solve a problem ?

20 views (last 30 days)
Pierre Lonfat on 13 Mar 2018
Edited: Pierre Lonfat on 14 Mar 2018
If you consider the following code, you can see that I have only one integer variable with [10 16] interval.
Therefore, in my mind, ga should look up for the minimal value by trying to set the variable with 10 11 12 13 14 15 16 only.
The function take roughly a few second to run when I am doing it manually. Then why is this optimization so long? Any guess what i am doing wrong?

Stephen23 on 13 Mar 2018
Edited: Stephen23 on 13 Mar 2018
"Therefore, in my mind, ga should look up for the minimal value by trying to set the variable with 10 11 12 13 14 15 16 only."
Nope. That is not how the ga algorithm works.
"Any guess what i am doing wrong?"
You are using the wrong tool for the task.
A global solver like ga does not work by simply defining a list of all possible values and trying each of them in turn. All global optimization routines have overhead because they have to set up whatever variables (e.g. sample populations, etc) depending on the algorithm and options selected, and then they run the algorithm until some conditions are met. The fitness function will be called many times during this process, because that is how global optimizers work. For example the conditions for ga include the population size, which according to the ga documentation for one variable and a mixed integer problem will be 40... so your (apparently slow) function gets called for each of those 40 population members on each step of the algorithm, and will be quite few steps until the conditions are met such that the solver stops. Just because your particular domain only has seven possible values does not mean that this sample population is reduced, or that the other conditions magically disappear: there is no shortcut, no special case where the ga algorithm will decide to use some other different algorithm (e.g. the one that you apparently want to use "try each one and see which is best"). You decided to use a ga algorithm, which involves creating a large population and calling the function for each population member while changing the population values. That is what the ga algorithm does.
If you want to want to find a global maximum then read about the different algorithms, and see how they work, keeping in mind that there is no "perfect" optimizer because different ones work well in different situations:
If you want a simple "try each value" algorithm, then why not just use a for loop anyway? Using any optimization tools for this seems like a waste of time.
Pierre Lonfat on 14 Mar 2018
Thank you very much Stephen for your detailed answers ! I am starting to work now on improving my code efficiency. As I am a beginner, it is not straight forward to me to write vectorized code with cell variables. If you are willing to help me again, I just asked a question right there because I couldn't find any detailed example: https://ch.mathworks.com/matlabcentral/answers/388205-if-function-with-cellfun-i-e-vectorized-code-instead-of-ridiculously-slow-loop
Thanks again !

### Community Treasure Hunt

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

Start Hunting!

Translated by