Why does my code bug down during while loop?
3 views (last 30 days)
Show older comments
Hello everyone,
I am working on an genetic algorithm optimization problem and I have created a function which should create inital random values of variables. There are 661 instances each requiring a set of randomly generated variables. For some odd reason the code quickly generates first 100, but totally bugs down on 101-st. When I try the function outside of the entire code and for the 101-st particular instance everything works fine. But in the loop it simply does not work.
I have tried two options:
- unifrnd which, after I terminate code execution, gives "Operation terminated by user during unifrnd"
- for loop which also bugs down on the line where for is written
Does anyone understand why is this happening?
I have attached everything you need to try it out.
0 Comments
Answers (1)
Benjamin Thompson
on 1 Jul 2022
Have you tried the Profiler tool with your code? This can quickly highlite which parts take the most time. In this case line 24 of population.m uses quite a bit of the time. It is always recommended to create vectors with the desired size at the beginning instead of adding to its size on each iteration of a loop. The rand function also accepts size arguments and this will execute much faster than pulling scalar random numbers one by one.
x = zeros(N, 1);
rvalues = rand(N,1);
0 Comments
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!