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!