이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Is there a way to reduce execution time of the following because it took 6 hours and still is executing?
조회 수: 4 (최근 30일)
이전 댓글 표시
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
Jan
2023년 2월 20일
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.
답변 (1개)
Torsten
2023년 2월 20일
편집: Torsten
2023년 2월 20일
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
Sadiq Akbar
2023년 2월 20일
Torsten
2023년 2월 20일
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.
Sadiq Akbar
2023년 2월 21일
Thanks to both of you dear Torsten and Walter Roberson for your kind responses. But my function is not stochastic one. Its deterministic as ca be seen. Then what's the reason?
Walter Roberson
2023년 2월 21일
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
2023년 2월 22일
Thanks to both of you dear Walter Roberson and Steven Lord for your kind responses. When I use, rng(0) before awgn(), then I don't get different values of 'one' and 'two' and in that case, I cannot sketch the plot of one which is fitness vs runs? In that case it stays only at one value which is not good. The graph of one vs different runs should be a decreasing graph but here it satys only on a single number and is not changing. So, this is not the solution.
Sadiq Akbar
2023년 2월 22일
You had developed the above code for me. It works but it takes a lot of time. I had to shut it down before it completed because it took round about 10 hours but was still executing. If we do pre-allocation in this, I hope it will take less time. I did pre-allocation at the beginning of the code like this:
Runs=100;
onev=zeros(Runs,1);
tow11=zeros(Runs,dim);
twov=zeros(Runs,dim);
two=zeros(Runs,dim);
But then instead of getting 21 values of one, two, I get 100 values and also they are wrong. How can I do pre-allocation in the above code, so that I get total of 21 values (from noise=-10 to Noise=10)? and the code becomes speedy?
Sadiq Akbar
2023년 2월 22일
Further, the profile on and viewer tells me that the following lines are not used:
options = optimoptions('ga','PopulationSize', PopulationSize_Data);
options = optimoptions('ga','MaxGenerations', MaxGenerations_Data);
options = optimoptions('ga','FunctionTolerance', FunctionTolerance_Data);
options = optimoptions('ga','ConstraintTolerance', ConstraintTolerance_Data);
options = optimoptions('ga','CreationFcn', @gacreationuniform);
options = optimoptions('ga','CrossoverFcn', { @crossoverheuristic [] });
options = optimoptions('ga','MutationFcn', @mutationadaptfeasible);
But I want to use them because i want to tell 'ga' these these are the options, run according to these options. So what to do so that these are also in use?
Walter Roberson
2023년 2월 22일
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
2023년 2월 23일
Thanks a lot for your kind response dear Walter Roberson. Yes, now it works. But now it gives another problem. In the 1st case, it gives good results but when I use your code to include other options also, then the results are degraded. Why it is so? Usually in GA builtin tool in older Matlab, when I inluded other options, then the result was improving but here it is degrading. why is it so? Or there is something wrong?
Walter Roberson
2023년 2월 23일
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
2023년 2월 23일
편집: Sadiq Akbar
2023년 2월 23일
PopulationSize_Data=100;
MaxGenerations_Data=500;
FunctionTolerance_Data=1e-25;
ConstraintTolerance_Data=1e-25;
options = optimoptions('ga','PopulationSize', PopulationSize_Data);
options = optimoptions(options,'MaxGenerations', MaxGenerations_Data);
options = optimoptions(options,'FunctionTolerance', FunctionTolerance_Data);
options = optimoptions(options,'FunctionTolerance', FunctionTolerance_Data);
options = optimoptions(options,'ConstraintTolerance', ConstraintTolerance_Data);
options = optimoptions(options,'CreationFcn', @gacreationuniform);
options = optimoptions(options,'CrossoverFcn', { @crossoverheuristic [] });
options = optimoptions(options,'MutationFcn', @mutationadaptfeasible);
options = optimoptions(options,'Display', 'off');
Walter Roberson
2023년 2월 23일
That is a very small tolerance unless your function values are on the order of 1e-12
Sadiq Akbar
2023년 2월 23일
Thanks a lot for your kind response dear Walter Roberson. I don't understand it. You mean I should make it large like 1e-6 or 1e-5 etc.?
Sadiq Akbar
2023년 2월 23일
I changed the values of:
FunctionTolerance_Data=1e-25;
ConstraintTolerance_Data=1e-25;
to the following:
FunctionTolerance_Data=1e-6;
ConstraintTolerance_Data=1e-6;
But still the results are degraded.
Walter Roberson
2023년 2월 23일
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
2023년 2월 24일
%%%%%%%%%%%%%%%%%%%%%%
% Swapping
%%%%%%%%%%%%%%%%%%%%%%
[~, ix] = sort(u);
[~, ix1(ix)] = sort(twov(nn,:));
two(nn,:) = twov(nn,ix1);
This re-arranges the elements of vector twov in the same order as is the order of vector u. I do this because usually the elements of vector twov are not in the same order as is my vector u.
%%%%%%%%%%%%%%%%%%%%
% Swapping vector b
%%%%%%%%%%%%%%%%%%%%
[~, ix] = sort(u);
[~, ix1(ix)] = sort(b);
b = b(ix1);
As 'GA' generates random vectors of same size as u is. So the order of the elements of those random vectors are not the same as is of u. So I use this code to make the order of each random vector of GA like that of u. Because if you see inside the function 'Vectorized.m', you will see the root mean sqaure error formula as:
e=norm(yo-ye).^2/(M*N);
If the sent random vector by GA to the function 'Vectorized.m' is not of the same order as u, then definitely the order of yo and ye are also different. So in that case the calculated root mean sqaure error value will be wrong. So to avoid this 1st I re-arrange the sent vector by GA like that of vector u.
Walter Roberson
2023년 2월 24일
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
2023년 2월 25일
편집: Sadiq Akbar
2023년 2월 25일
ga() generates in the same order as lb / ub: I don't agree to this statement. I doubt you take the meaning of order as 'Rows x Colmns'. But I don't mean that. By order I mean the arrangemet of elements. For demonstration purpose, you just type the following commands in a script file say for example 'abc.m'.
u=[-34, 34, -50, 50, -45, 45, -65, 65];
dim=length(u);
lb=-90*ones(1,dim);
ub=90*ones(1,dim);
PopulationSize_Data=100;
MaxGenerations_Data=500;
FunctionTolerance_Data=1e-20;
ConstraintTolerance_Data=1e-20;
options = optimoptions('ga','PopulationSize', PopulationSize_Data);
options = optimoptions('ga','MaxGenerations', MaxGenerations_Data);
options = optimoptions('ga','FunctionTolerance', FunctionTolerance_Data);
options = optimoptions('ga','ConstraintTolerance', ConstraintTolerance_Data);
[B,fval] = ga(@(b)Vectorized(b,u,Noise(mm)), dim,[],[],[],[],lb,ub,[],options)
and also make a separate m-file for the following function:
%%%%%%%%%%%%%%%%%%%%%%
% Vectorized function
%%%%%%%%%%%%%%%%%%%%%%
function e=Vectorized(b,u,Noise)
M = 10;%Constant1
N = 10;%Constant2
d = 0.5;%Constant3
K = length(u)/2;%Constant4
alpha=ones(1,K);%Constant5
%%%%%%%%%%%%%%%%%%%%
% yo calculation
%%%%%%%%%%%%%%%%%%%
ho=(1:M)';
po=(1:N)';
i=1:K;
bro = u(:).';
ao=exp(1j*2*pi*(ho-1)*d.*sind(bro(i))); % ao calculation
fo=exp(1j*2*pi*(po-1)*d.*sind(bro(K+i))); % fo calculation
co = reshape(reshape(ao,1,[],K).*reshape(fo,[],1,K),[],K);% co calculation
yo=co*alpha'; % yo calculation
yo=awgn(yo,Noise);% ao calculation with noise
%%%%%%%%%%%%%%%%%%%%%%
% ye calculation
%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%
% Swapping vector b
%%%%%%%%%%%%%%%%%%%%
% [~, ix] = sort(u);
% [~, ix1(ix)] = sort(b);
% b = b(ix1);
bre = b(:).';
ae=exp(1j*2*pi*(ho-1)*d.*sind(bre(i)));
fe=exp(1j*2*pi*(po-1)*d.*sind(bre(K+i)));
ce = reshape(reshape(ae,1,[],K).*reshape(fe,[],1,K),[],K);
ye=ce*alpha';
%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%%
e=norm(yo-ye).^2/(M*N);
end
Now run the script file 'abc.m' and compare the result of 'B' with u. Do you see that B is in the same order as is u. Again by order I mean the arrangement of elements.
Likewise, run the script file 'abc.m' for the 2nd time and again compare B with u.Do you see that B and u have same order. I mean the arrangement of elements in both B and u is the same? Definitly not. you will see that some time the order(arrangement of elelents) is same and some time different. So to ensure it that they are alwasy in the same order(arrangement), I use the mentioned pice of code.
Sadiq Akbar
2023년 2월 25일
Consider the following:
u=[10 20 30 40];
B=[9.9 20.1 30.01 39.9];
Error=u-B;
MSE=mean(Error.^2)
MSE=0.0075
Now if B=[39.9 30.01 20.1 9.9], then the Error and the mean sqaure error are as:
u=[10 20 30 40];
B=[39.9 30.01 20.1 9.9];%B=[9.9 20.1 30.01 39.9];
Error=u-B;
MSE=mean(Error.^2)
MSE=499.5575
See that there is a lot of difference between the two MSE. That's why I use the mentioned piece of code to be ensured that both u and B are having the same order i.e., arrangement.
Walter Roberson
2023년 2월 25일
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
2023년 2월 25일
Thanks a lot for your kind response dear Walter Roberson. Actually 'GA' generates its random population of vectors having the same size as u.And then it sends each member of that generated population to the function 'Vectorized.m' .So, we don't know in advance each memebr. That's why we re-arrange them inside the function 'Vectorized.m'. Further, I am not talking about lb and ub. They are passed as they are.No problem in that. I am re-stating that it is the generated vector by GA whose order (i.e., arrangement of elements) is some times different and some time same as u (not lb or ub). However, what you are saying is beyond my understanding. If you can do it, then please do it for me.
If you need random values that match in order with what is in u : Yes, I want that but I don't want to generate it myself. I am interested in those values which are gnerated by GA itself.
which is the requirement that the b entries are in increasing order and so matching the now-sorted u values: Here you are misunderstanding it. The values of u are not in increasing order. These values may be any real number like:
u=[-34, 34, -50, 50, -45, 45, -65, 65];
u=[3, 4, 0, 5, -4, 5, -5, 6];
u=[0, 4, -5, 0, -45, 59, -65, 65];
u=[1,, 2, 0, 5, -4, 45, 5, 15];
etc.
What I want?
I want that whatever values are put in vector u, the same values or nearly the same values and in the same arrangement should be returned by GA to me.
Walter Roberson
2023년 2월 25일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Preprocessing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)