- The video mentioned in the question utilizes MATLAB R2015a for the example and in turn uses the “gaoptimset” function which returns the genetic algorithm options as a “struct”.
- In the given implementation above in MATLAB R2024a, you have used the “optimoptions” with “ga” as the solver, which is the alternative provided in the documentation for “gaoptimset”. It is to be noted that for “ga” solver, “optimoptions” returns an object with set of options for “ga”.
Error when I try to run genetic algorithm on peaks function.
3 次查看(过去 30 天)
显示 更早的评论
Below is my code for applying genetic algorithm to peaks function. my intent is to see what points are added in each iteration.
peaks
problem.solver = 'ga';
problem.fitnessfcn = @(x)peaks(x(1),x(2));
problem.nvars = 2;
problem.lb = [-3 -3];
problem.ub = [3 3];
problem.options = optimoptions('ga', ...
'OutputFcn', @peaksPlotIterates, ...
'Display', 'iter', ...
'PopInitRange', [-3 -3;3 3]);
x = ga(problem);
I tried to replicate what I saw in a youtube video (https://www.youtube.com/watch?v=1i8muvzZkPw) published by MAtlab. However, I get the following error in the console:
Peaks_GA
z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ...
- 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ...
- 1/3*exp(-(x+1).^2 - y.^2)
Single objective optimization:
2 Variables
Options:
CreationFcn: @gacreationuniform
CrossoverFcn: @crossoverscattered
SelectionFcn: @selectionstochunif
MutationFcn: @mutationadaptfeasible
Unrecognized property 'iteration' for class 'optim.options.GaOptions'.
Error in peaksPlotIterates (line 24)
optimValues.iteration = -1;
Error in gaoutput (line 56)
[state,optnew,changed] = feval(functions{i},options.OutputPlotFcnOptions,state,flag,args{i}{:});
Error in galincon (line 55)
[state,options] = gaoutput(FitnessFcn,options,state,currentState);
Error in ga (line 420)
[x,fval,exitFlag,output,population,scores] = galincon(FitnessFcn,nvars, ...
Error in Peaks_GA (line 13)
x = ga(problem);
0 个评论
回答(1 个)
Abhinav Aravindan
2024-4-23
I see that you are attempting to apply the genetic algorithm to the peaks function and monitor the progress of points added in each iteration. I assume that you are using the “peakPlotIterates” function mentioned in the following link:
Based on the error message you've encountered, it seems there is an issue with the “peaksPlotIterates” function trying to modify the “optimValues” structure, by adding an “iteration” field.
There are a few points to note here:
In the “peakPlotIterates” function, a new field named “iterations” is added to the Genetic Algorithm options object “optimValues” causing this error while using “optimoptions”. A possible workaround to resolve this error is to modify “peakPlotIterates” function by creating a new local variable “iteration” and replace all “optimValues.iteration” with “iteration” to avoid modifying “optimValues”.
Hope this helps resolve the issue !
Please refer to the below documentation for your reference.
3 个评论
Abhinav Aravindan
2024-4-25
The use of "gaoptimset" is no longer recommended, you can utilize "optimoptions" as written in your code. The current generation number can be obtained from "varargin{2}" as follows:
currentGen = varargin{2}.Generation
The number of generations computed can be found from the fourth output argument of "ga"
[x, fval, exitflag, out] = ga(problem);
disp(out.generations)
另请参阅
类别
在 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!