Is there a way to reduce execution time of the following because it took 6 hours and still is executing?
Show older comments
I have attached a file 'main.m'. I started to run main.m at 1:50pm today and still its going on executing and I don't know when will it stop its execution? So, is there a way to reduce its execution time so that instead of taking too much time it is complted in less time?
1 Comment
Jan
on 20 Feb 2023
Use the profiler to find the bottleneck. If it is the exp() calculation, an acceleration might be possible. But it is not worth to try it before clarifying, if a relevant amount of time is spent there.
Answers (1)
Are you sure you can use "ga" with stochastic inputs that change from iteration to iteration ? I doubt it.
Look up "stochastic optimization" instead.
26 Comments
Sadiq Akbar
on 20 Feb 2023
Torsten
on 20 Feb 2023
Yes, I have been using it with stochastic inputs.
Yes, one can use it, but if one should use it for stochastic optimization is a different question. I wouldn't.
Walter Roberson
on 20 Feb 2023
ga() will not produce valid output with stochastic functions.
Sadiq Akbar
on 21 Feb 2023
Walter Roberson
on 21 Feb 2023
To make it deterministic (enough) you would need to put in a call to rng() with a constant seed just before the call to awgn()
Sadiq Akbar
on 21 Feb 2023
Steven Lord
on 21 Feb 2023
Sadiq Akbar
on 22 Feb 2023
Sadiq Akbar
on 22 Feb 2023
Sadiq Akbar
on 22 Feb 2023
Walter Roberson
on 22 Feb 2023
Each of those lines completely overwrites the previous options. To add additional options you should be using
options = optimoptions(options,'MaxGenerations', MaxGenerations_Data);
options = optimoptions(options,'FunctionTolerance', FunctionTolerance_Data);
and so on. Use 'ga' as the first parameter in the first optimoptions call, but use the previous options as the first parameter for the remaining calls.
Note that you can add multiple options in the same call.
Sadiq Akbar
on 23 Feb 2023
Walter Roberson
on 23 Feb 2023
Please post your current options setting code.
You should also consider that you are setting specific creation and crossover and mutation functions; those are not necessarily going to be better for your particular problem.
Sadiq Akbar
on 23 Feb 2023
Edited: Sadiq Akbar
on 23 Feb 2023
Walter Roberson
on 23 Feb 2023
That is a very small tolerance unless your function values are on the order of 1e-12
Sadiq Akbar
on 23 Feb 2023
Sadiq Akbar
on 23 Feb 2023
Walter Roberson
on 23 Feb 2023
Please document this section of code in more detail:
%%%%%%%%%%%%%%%%%%%%%%
% Swapping
%%%%%%%%%%%%%%%%%%%%%%
[~, ix] = sort(u);
[~, ix1(ix)] = sort(twov(nn,:));
two(nn,:) = twov(nn,ix1);
along with
%%%%%%%%%%%%%%%%%%%%
% Swapping vector b
%%%%%%%%%%%%%%%%%%%%
[~, ix] = sort(u);
[~, ix1(ix)] = sort(b);
b = b(ix1);
u is a constant vector so the order of u is always the same. It is not clear why you want to do all of that. The only different the order makes is that for part of your calculation, you only use the first half of u .
Sadiq Akbar
on 24 Feb 2023
Walter Roberson
on 24 Feb 2023
As 'GA' generates random vectors of same size as u is.
ga() generates in the same order as lb / ub so if you want a different order then reorder your lb / ub and the statements that access the entries of the vector.
Sadiq Akbar
on 25 Feb 2023
Edited: Sadiq Akbar
on 25 Feb 2023
Sadiq Akbar
on 25 Feb 2023
Walter Roberson
on 25 Feb 2023
I copied your code and tested. I had to add definitions for mm and Noise but I could run after that.
Just as I expected, the generated values passed in as the first parameter to Vectorized are in the same order as the constraints expressed in lb / ub. All of your lb are the same as each other, and all of your ub are the same as each other.
If you need random values that match in order with what is in u then what you should do is
u=[-34, 34, -50, 50, -45, 45, -65, 65];
u = sort(u);
A = eye(8) + diag(-ones(1,7),1);
A = A(1:7,:);
B = zeros(7,1);
[B,fval] = ga(@(b)Vectorized(b,u,Noise(mm)), dim, A, B, [], [], lb, ub, [], options)
The A B is to construct a matrix similar to
1 -1 0 0 0 0 0 0
0 1 -1 0 0 0 0 0
0 0 1 -1 0 0 0 0
which is being used to express b(K) - b(K+1) <= 0 -- which is another way to write b(K) <= b(K+1) which is the requirement that the b entries are in increasing order and so matching the now-sorted u values.
You currently have
i=1:K;
and things like ao=exp(1j*2*pi*(ho-1)*d.*sind(bro(i))); % ao calculation which selects particular elements of bro (which is in turn the input u) . That is in the context where your original u is alternating pairs negative and positive. I do not understand why you are extracting only the first half of u for those purposes -- but if you need to preserve that kind of sub-selection of u then you can calculate the appropriate indices ahead of time and pass them into Vectorized .
Sadiq Akbar
on 25 Feb 2023
Walter Roberson
on 25 Feb 2023
Okay, go ahead and do that. Be advised that your approach is likely to have poor performance. But if you need your code to be written that way in order for you to understand it, then go with it.
Categories
Find more on Univariate Discrete Distributions 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!