MATLAB Answers

0

Why is ga optimization ridiculously slow to solve a problem ?

Asked by Pierre Lonfat on 13 Mar 2018
Latest activity Edited by 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?

  0 Comments

Sign in to comment.

1 Answer

Answer by Stephen Cobeldick on 13 Mar 2018
Edited by Stephen Cobeldick on 13 Mar 2018
 Accepted Answer

"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.

  8 Comments

Thank you John. Indeed, my code can be improved, I just discovered that rolling average formula exist on Matlab. I am actually doing a lot of unnecessary loops... But with my actual level, I have better confidence using them because in my mind, they are more representative of what the code is doing ;)
@Pierre Lonfat: keep in mind that with MATLAB:
  • numeric operations and functions often work very efficiently on whole arrays of data without requiring any loops (this is what vectorized code takes advantage of), and
  • inbuilt operations and functions are usually quite optimized for speed: they are often written using C, or call highly optimized linear algebra libraries.
Both of these mean that using whole arrays with inbuilt functions is a good starting point for writing efficient code, that can be tailored later as required. It is also a good rule of thumb to not write code twice: if MATLAB already has a function that does what you want, then use it: it will be well tested by many users, well documented, and most likely more efficient.
You might like to read these:
Keep in mind that the solution to slow code (e.g. your question) is not to throw another even slower operation at it (e.g. using ga), but to understand why that code is slow, and fix it at the source. The more you read about and practice writing efficient MATLAB code, then easier this will be for you.
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 !

Sign in to comment.