Error when I try to run genetic algorithm on peaks function.

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);

Answers (1)

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:
  • 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”.
In the peakPlotIterates” function, a new field named “iterations” is added to the Genetic Algorithm options objectoptimValues 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 Comments

Hi Abhinav,
Thank you for answering my question. Below is the part of the code that needs to be modified. Is there any way I can get access to the generation number from gaoptimset. I tried to look through the documentation but I could'nt find anything regarding generatio number.
function varargout = peaksPlotIterates(varargin)
% Output function that plots the iterates of the optimization algorithm.
% Copyright (c) 2010, The MathWorks, Inc.
% All rights reserved.
% Check if caller is from global or optimization toolbox
optimValues = varargin{2};
state = varargin{3};
if nargout > 1
if isfield(optimValues,'x') % simulated annealing options,optimvalues,flag
x = optimValues.x;
varargout{1} = false;
varargout{2} = x; % options field
varargout{3} = false;
else % pattern search optimvalues,options,flag,interval
optimValues = varargin{1};
state = varargin{3};
if isfield(optimValues,'x')
x = optimValues.x;
varargout{1} = false;
varargout{2} = x; % options field
varargout{3} = false;
else % gentic algorithm options,state,flag,interval
x = varargin{2}.Population;
optimValues.iteration = -1;
varargout{1} = varargin{2};
varargout{2} = varargin{1};
varargout{3} = false;
end
end
else
x = varargin{1};
varargout{1} = false;
end
% Check for state
switch state
case 'init'
% Plot objective function surface
PlotSurface(x,peaks(x(:,1),x(:,2)));
case 'iter'
if ~(optimValues.iteration == 0)
% Update surface plot to show current solution
PlotUpdate(x,peaks(x(:,1),x(:,2)));
end
case 'done'
if ~(optimValues.iteration == 0)
% After optimization, display solution in plot title
DisplayTitle(x,peaks(x(:,1),x(:,2)))
end
end
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)

Sign in to comment.

Products

Release

R2024a

Asked:

on 10 Apr 2024

Commented:

on 25 Apr 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!