resuming and starting from new line in genetic algorithm after specific generations
2 次查看(过去 30 天)
显示 更早的评论
Hamid
2014-11-5
Hi,
I want to resume and start from new line in genetic algorithm after specific generations.
Has anyone know How is impossible??
Any suggestion may help a lot.
Thank you guys.
7 个评论
Geoff Hayes
2014-11-7
Hamid - please clarify what you mean by wanting to start from new line in genetic algorithm after specific generations. What is a new line?
Hamid
2014-11-7
Deae Geoff.
I want to use genetic algorithm from 0-100 generations for my project then from 101-200 generations for my project with a little change in it and so on until generations 800.
In fact I want these simulations will be continuos and connencted.
My m file commands some input at the first, These changes after specific generations comes to one of them and I don't how to this.
I attached my project and the changes will happen in f-matrix in GAmodule (GAmodule is main function).
Thank you Geoff.
Geoff Hayes
2014-11-8
Hamid - rather than attaching an unknown zip file, consider attaching the m files instead.
Geoff Hayes
2014-11-9
Hamid - what type of change do you want to inject into the population at the start of generation 101? It may be that the population has converged to a solution prior to generation 100, so continuing the genetic algorithm for another 700 iterations may not lead to a better solution. (I noticed that currently you use 5 generations only...)
Hamid
2014-11-9
编辑:Hamid
2014-11-9
I want to change f-matrix every 100 generations and I'm partly sure that a solution better happen at the end of generation 700, beside I don't have another choice and I have to inject all these 7 conditions.
for example : nn=20 nb=4 nl=6
case 1 (0-100 generations) : f(13)=800 f(14)=0 f(15)=-1000 and so on
case 2 (101-200 generations) : f(13)=0 f(14)=700 f(15)=-500 and so on
also I used 5 for an experiment and the right one is 700 generations.
Thank you very much Geoff for helping me.
Hamid
2014-11-9
Dear Geoff - my final goal is to reaching to best solution under all 7 conditions together, these conditions only difference in f-matrix.
What do you recommend?????
I'm so confused and I'm gonna be crazy.
回答(1 个)
Geoff Hayes
2014-11-10
Hamid - I don't think that I can give a valid recommendation because I don't know enough of what you are trying to do. f seems to be a matrix used in the fitness function, but I can't guess as to its impact or that of changing a handful of its values. But if you want to try and re-use the population after 100 iterations with a slight change to the f matrix, then you could change your return from the ga function of
[x,fval]=ga(@(x) penalty2(x,available,bc,Dof,E...),Nvars,options);
to
[x,fval,exitflag,output,population] = ga(@(x)penalty2(x,available,bc,Dof, E...),Nvars,options);
The population output parameter will be the final population for (say) 100 iterations of the algorithm. You then update the f matrix, and re-call
% re-use the population as the initial population for the next 100 generations
options = gaoptimset('InitialPopulation', population);
% run the algorithm
[x,fval,exitflag,output,population] = ga(@(x)penalty2(x,available,bc,Dof, E...),Nvars,options);
for 100 more generations. Then change f to whatever you want. You can put this in a loop that iterates from 1 through to 7, and you would change f on each iteration.
However, this method may not work as expected especially if the GA has converged to a premature solution before generation 100. If all members of the population are identical (or near-identical) then there will not be enough genetic diversity in the next round of 100 generations to produce any new members even though you have changed the f matrix.
19 个评论
Hamid
2014-11-10
Dear Geoff
[x,fval,exitflag,output,population]=ga(@(x)penalty2(x,...,options);
Time=toc;
% re-use the population as the initial population for the next 100 generations
options = gaoptimset('InitialPopulation', population);
% run the algorithm
[x,fval,exitflag,output,population]=ga(@(x)penalty2(x,...,options);
I write these in my m file but It gets an error :
??? Error using ==> bin2dec at 36
Binary string must be 52 bits or less.
I think I don't get that you thought me and this is bad writing.
please write the whole script that I must change including loop to me Geoff.
Thank you very very much.
Geoff Hayes
2014-11-10
Hamid - please post/attach your updated code with comments indicating what changes you have made.
Hamid
2014-11-10
Geoff - I only add these lines to my file (line 168-171)
% re-use the population as the initial population for the next 100 generations
options = gaoptimset('InitialPopulation', population);
% run the algorithm
[x,fval,exitflag,output,population]=ga(@(x)penalty2(x,...,options);
rest of the file is remained unchanged.
Thanks.
Geoff Hayes
2014-11-10
Hamid - compare the matrix population with Initpop with respect to their sizes. Do they both have the same number of rows and columns?
I noticed that you didn't try to change the f matrix when running the second set of generations. Why not?
Geoff Hayes
2014-11-11
population, which is one of the return values from the updated call to ga should be a matrix. I'm not referring to your local variable of Population which is the number of organisms in your population (you should name this variable popSize or something similar). So please verify again what this output from the ga function is.
Unfortunately, I don't have the Global Optimization Toolbox so I can't run your code. Since you want to change the f matrix at after each batch of 100 generations then it seems to me that you need to do the following:
- Run the GA for the first 100 generations.
- Change the f matrix.
- Run the GA for the next 100 generations.
- Repeat steps 2 to 3 until the f matrix has been updated the required number of times.
Hamid
2014-11-11
Dear Geoff - you were right and popSize and Initpop are the same size.
I know what you saying but I can't fulfill it correctly.
these are my codes with new changes :
[x,fval,exitflag,output,popSize] = ga(@(x) penalty2(x,available,bc,Dof,E,f,fixedcoord,na,nb,neft,nevt,nl,nn,numbitCoordX,numbitCoordY,numbitCoordZ,numbitEdof,numbitEp,sq,TOPGSM,xspan,yspan,zspan),Nvars,options);
Time=toc;
% re-use the population as the initial population for the next 100 generations
options = gaoptimset('InitialPopulation', popSize);
f=zeros(3*nn,1);
for i=nb+1:nb+nl
f(3*i-2)=input(['For load node ',num2str(i-nb),' please specify the X-resultant of the load! (N) ']);
f(3*i-1)=input(['For load node ',num2str(i-nb),' please specify the Y-resultant of the load! (N) ']);
f(3*i)=input(['For load node ',num2str(i-nb),' please specify the Z-resultant of the load! (N) ']);
end
% run the algorithm
[x,fval,exitflag,output,popSize] = ga(@(x) penalty2(x,available,bc,Dof,E,f,fixedcoord,na,nb,neft,nevt,nl,nn,numbitCoordX,numbitCoordY,numbitCoordZ,numbitEdof,numbitEp,sq,TOPGSM,xspan,yspan,zspan),Nvars,options);
but I kept this error again :
??? Error using ==> bin2dec at 36
Binary string must be 52 bits or less.
please write the codes for me.
You are the best Geoff, thanks a lot.
Geoff Hayes
2014-11-11
编辑:Geoff Hayes
2014-11-11
Hamid - where is the bin2dec being called from? There must be more to the error than that which you are showing. Please copy and paste all of the red text that corresponds to this error into a comment.
Hamid
2014-11-12
??? Error using ==> bin2dec at 36
Binary string must be 52 bits or less.
Error in ==> bintranslate at 137
if ceil(bin2dec(num2str(chromosome4(i-(numbitEp-1):i))))>na
Error in ==> penalty2 at 38
[Coord,Edof2,Ep2]=bintranslate(available,chromosome,Dof,fixedcoord,na,nb,neft,nevt,nl,nn,numbitCoordX,numbitCoordY,numbitCoordZ,numbitEdof,numbitEp,xspan,yspan,zspan);
Error in ==>
GAmodule>@(x)penalty2(x,available,bc,Dof,E,f,fixedcoord,na,nb,neft,nevt,nl,nn,numbitCoordX,numbitCoordY,numbitCoordZ,numbitEdof,numbitEp,sq,TOPGSM,xspan,yspan,zspan)
at 177
[x,fval,exitflag,output,popSize] = ga(@(x)
penalty2(x,available,bc,Dof,E,f,fixedcoord,na,nb,neft,nevt,nl,nn,numbitCoordX,numbitCoordY,numbitCoordZ,numbitEdof,numbitEp,sq,TOPGSM,xspan,yspan,zspan),Nva
Error in ==> validate>@(x)fitness(x,FitnessFcnArgs{:}) at 135
fitness = @(x) fitness(x,FitnessFcnArgs{:});
Error in ==> fcnvectorizer at 14
y(i,:) = feval(fun,(pop(i,:)));
Error in ==> stepGA at 45
nextScore = fcnvectorizer(nextPopulation,FitnessFcn,1,options.SerialUserFcn);
Error in ==> gaunc at 85
[score,population,state] = stepGA(score,population,options,state,GenomeLength,FitnessFcn);
Error in ==> ga at 279
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in ==> GAmodule at 177
[x,fval,exitflag,output,popSize] = ga(@(x)
penalty2(x,available,bc,Dof,E,f,fixedcoord,na,nb,neft,nevt,nl,nn,numbitCoordX,numbitCoordY,numbitCoordZ,numbitEdof,numbitEp,sq,TOPGSM,xspan,yspan,zspan),Nva
Caused by:
Failure in user-supplied fitness function evaluation. GA cannot continue.
Geoff Hayes
2014-11-12
Before running your code, type the following in the Command Window
dbstop if error
Now re-run your code. When the error is encountered, the debugger will pause at the line that is generating the error. So that should be at line
if ceil(bin2dec(num2str(chromosome4(i-(numbitEp-1):i))))>na
Look at
chromosome4(i-(numbitEp-1):i)
What is the value of this chromosome? What is i? What is numbitEp? Why is it a binary string? Once converted to a string, it must have more than 52 bits. Does this make sense for the problem that you are trying to solve?
Hamid
2014-11-12
Dear Geoff - I attached chromosome4 value as a jpeg file.
numbitEp is number of bits required in the different segments of the bit string and calculated:
numbitEp=ceil(log2(na));
na is always 256, then numbitEp is 8.
binary string because I need the obtained nodes coordinates.
No this is not matter and this does NOT make sense.
Thanks Geoff for helping me out.
Geoff Hayes
2014-11-13
Hamid - you are going to have step through the code and determine why you are observing invalid values. Determine when the error occurs, i.e. which iteration/generation of the second batch. Is it because the f matrix has changed? What happens if you use the same f matrix from the previous 100 generations - do you still observe an error? Use breakpoints and the debugger to help.
Hamid
2014-11-13
Geoff - with the same f I still have that error and this is not because of f matrix change.
When i is maximum for example i=80 ;
num2str(chromosome4(i-(numbitEp-1):i))= 1 1 0 0 1 0 1 1
It is less than 52 but I keep that error, What's wrong?
Thanks Geoff.
Geoff Hayes
2014-11-14
Hamid - you will have to work backwards from the above in order to determine what the problem is. Consider how nevt and neft are initialized and/or updated.
另请参阅
类别
在 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!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 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 (한국어)