Why does my code bug down during while loop?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Benjamin Thompson
2022-7-1
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Genetic Algorithm 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!