Get multiple solutions from genetic algorithm (ga)
Ältere Kommentare anzeigen
Hi
I'm using the genetic algorithm in the optimization toolbox. As for now I get one solution (the best) but is would like to get a range of solutions that is within a certain percentage of the best.
Is there a way to do that?
Example: Solution has a penalty score of 100. Return all solutions with a penalty socre less than 110.
Antworten (1)
Alan Weiss
am 12 Jun. 2013
1 Stimme
You can use an output function to get whatever you want. There are examples of output functions in the Optimization Toolbox documentation, and in MATLAB documentation. Be careful, those examples use a different syntax than an output function for ga.
Alan Weiss
MATLAB mathematical toolbox documentation
4 Kommentare
Craig Cowled
am 14 Jun. 2013
Jakob, I wrote an output function for a GA that saves only relevant information. It might help you ...
function [state, options, optchanged] = GAGenSave(options, state, flag)
global parastring
FileName = ['GenData_', parastring, '.mat'];
if (exist(FileName)) == 2
load(FileName);
if state.Generation == 0
iter = GenData.Generation;
else
iter = GenData.Generation + 1;
end
else
iter = 1;
end
GenData.x(iter, :) = state.Population(1, :);
GenData.Generation = iter;
GenData.FunEval(iter, :) = state.FunEval;
GenData.Population(:, :, iter) = state.Population;
GenData.Score(:, iter) = state.Score;
save(FileName, 'GenData');
% standard template code follows
optchanged = false;
switch flag
case 'init'
disp('Starting the algorithm');
case {'iter','interrupt'}
disp('Iterating ...')
case 'done'
disp('Performing final task');
end % GAGenSave()
Morteza
am 26 Okt. 2013
Hi Alan, How this code could be changed to give the best fitness value for each population? Thanks
Walter Roberson
am 27 Okt. 2013
Bearbeitet: Walter Roberson
am 14 Aug. 2017
Morteza commented,
works fine great code for output, the minor problem is that we need best fitness value in each generation. But this code gives score for each population! Anyway, it works fine Thanks Alan
Mostapha Dakhchoune
am 14 Aug. 2017
Bearbeitet: Walter Roberson
am 14 Aug. 2017
Thanks Alan and Craig for the help, anyone can help me why it gives this error while trying to run my GA coupled with Craig script? One more thing: I am interested only in x vectors so if I comment the other lines and run the code for a population of 20 it shows all the 7 variables (I am not sure also here because for the first half population of x vectors the last 3 variables are taken all as zeros), while if I run it, for instance, with a population of 50 it shows only the first 4 variables!
Thank you very much
Subscripted assignment dimension mismatch.
Error in GAGenSave (line 21)
GenData.Population(:, :, iter) = state.Population;
Error in gaoutput (line 39)
[state,optnew,changed] = feval(functions{i},opti |monospaced| ons.OutputPlotFcnOptions,state,flag,args{i}{:});
Error in galincon (line 31)
[state,options] = gaoutput(FitnessFcn,options,state,currentState);
Error in ga (line 374)
[x,fval,exitFlag,output,population,scores] = galincon(FitnessFcn,nvars, ...
Error in main_tem (line 47)
[x,fval,exitflag,output,population,scores]=ga(@delete6provadelete,7,[],[],[],[],lb,ub,[],opts);
Kategorien
Mehr zu Genetic Algorithm finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!