Why do I get Empty Plots during Optimization?
Inspired by custom plotting given here, for one-dimensional design variable case, I would like to generalize it to n-dimensional case. I wrote following code to achieve that:
function state = gaPlotRangeND(options, state, flag)
% gaPlotRangeND Plots the mean and the range of the population for n-dimensions.
% STATE = gaPlotRangeND(OPTIONS, STATE, FLAG) plots the mean and the range
% (highest and the lowest) of individuals for each variable.
generation = state.Generation;
population = state.Population;
numVars = size(population, 2);
M = mean(population);
L = M – min(population);
U = max(population) – M;
switch flag
case ‘init’
for i = 1:numVars
subplot(numVars, 1, i);
set(gca, ‘xlim’, [1, options.MaxGenerations + 1]);
plotRange = errorbar(generation, M(:, i), L(:, i), U(:, i));
set(plotRange, ‘Tag’, [‘Var_’ num2str(i)]);
title([‘Range of Population, Mean for Variable ‘ num2str(i)], ‘interp’, ‘none’)
xlabel(‘Generation’, ‘interp’, ‘none’)
end
case ‘iter’
for i = 1:numVars
subplot(numVars, 1, i);
plotRange = findobj(get(gca, ‘Children’), ‘Tag’, [‘Var_’ num2str(i)]);
newX = [get(plotRange, ‘Xdata’), generation];
newY = [get(plotRange, ‘Ydata’), M(:, i)];
newL = [get(plotRange, ‘Ldata’), L(:, i)];
newU = [get(plotRange, ‘Udata’), U(:, i)];
set(plotRange, ‘Xdata’, newX, ‘Ydata’, newY, ‘Ldata’, newL, ‘Udata’, newU);
end
end
end
When I run it for a simple two-dimensional test problem defined below, it does not work. It just outputs two empty subplots on top of each other during execution of genetic algorithm, and ends with a single empty plot. It is supposed to plot mean and range of population at each iteration for each variable, which is two in this case.
function y = booth_func(x)
y = (x(1) + 2 * x(2) – 7) ^ 2 + (2 * x(1) + x(2) – 5) ^ 2;
end
options = optimoptions(‘ga’, ‘PlotFcn’, @gaPlotRangeND);
[x, fval] = ga(@booth_func, 2, [], [], [], [], [], [], [], options);
How can I solve this issue? What do I miss here?Inspired by custom plotting given here, for one-dimensional design variable case, I would like to generalize it to n-dimensional case. I wrote following code to achieve that:
function state = gaPlotRangeND(options, state, flag)
% gaPlotRangeND Plots the mean and the range of the population for n-dimensions.
% STATE = gaPlotRangeND(OPTIONS, STATE, FLAG) plots the mean and the range
% (highest and the lowest) of individuals for each variable.
generation = state.Generation;
population = state.Population;
numVars = size(population, 2);
M = mean(population);
L = M – min(population);
U = max(population) – M;
switch flag
case ‘init’
for i = 1:numVars
subplot(numVars, 1, i);
set(gca, ‘xlim’, [1, options.MaxGenerations + 1]);
plotRange = errorbar(generation, M(:, i), L(:, i), U(:, i));
set(plotRange, ‘Tag’, [‘Var_’ num2str(i)]);
title([‘Range of Population, Mean for Variable ‘ num2str(i)], ‘interp’, ‘none’)
xlabel(‘Generation’, ‘interp’, ‘none’)
end
case ‘iter’
for i = 1:numVars
subplot(numVars, 1, i);
plotRange = findobj(get(gca, ‘Children’), ‘Tag’, [‘Var_’ num2str(i)]);
newX = [get(plotRange, ‘Xdata’), generation];
newY = [get(plotRange, ‘Ydata’), M(:, i)];
newL = [get(plotRange, ‘Ldata’), L(:, i)];
newU = [get(plotRange, ‘Udata’), U(:, i)];
set(plotRange, ‘Xdata’, newX, ‘Ydata’, newY, ‘Ldata’, newL, ‘Udata’, newU);
end
end
end
When I run it for a simple two-dimensional test problem defined below, it does not work. It just outputs two empty subplots on top of each other during execution of genetic algorithm, and ends with a single empty plot. It is supposed to plot mean and range of population at each iteration for each variable, which is two in this case.
function y = booth_func(x)
y = (x(1) + 2 * x(2) – 7) ^ 2 + (2 * x(1) + x(2) – 5) ^ 2;
end
options = optimoptions(‘ga’, ‘PlotFcn’, @gaPlotRangeND);
[x, fval] = ga(@booth_func, 2, [], [], [], [], [], [], [], options);
How can I solve this issue? What do I miss here? Inspired by custom plotting given here, for one-dimensional design variable case, I would like to generalize it to n-dimensional case. I wrote following code to achieve that:
function state = gaPlotRangeND(options, state, flag)
% gaPlotRangeND Plots the mean and the range of the population for n-dimensions.
% STATE = gaPlotRangeND(OPTIONS, STATE, FLAG) plots the mean and the range
% (highest and the lowest) of individuals for each variable.
generation = state.Generation;
population = state.Population;
numVars = size(population, 2);
M = mean(population);
L = M – min(population);
U = max(population) – M;
switch flag
case ‘init’
for i = 1:numVars
subplot(numVars, 1, i);
set(gca, ‘xlim’, [1, options.MaxGenerations + 1]);
plotRange = errorbar(generation, M(:, i), L(:, i), U(:, i));
set(plotRange, ‘Tag’, [‘Var_’ num2str(i)]);
title([‘Range of Population, Mean for Variable ‘ num2str(i)], ‘interp’, ‘none’)
xlabel(‘Generation’, ‘interp’, ‘none’)
end
case ‘iter’
for i = 1:numVars
subplot(numVars, 1, i);
plotRange = findobj(get(gca, ‘Children’), ‘Tag’, [‘Var_’ num2str(i)]);
newX = [get(plotRange, ‘Xdata’), generation];
newY = [get(plotRange, ‘Ydata’), M(:, i)];
newL = [get(plotRange, ‘Ldata’), L(:, i)];
newU = [get(plotRange, ‘Udata’), U(:, i)];
set(plotRange, ‘Xdata’, newX, ‘Ydata’, newY, ‘Ldata’, newL, ‘Udata’, newU);
end
end
end
When I run it for a simple two-dimensional test problem defined below, it does not work. It just outputs two empty subplots on top of each other during execution of genetic algorithm, and ends with a single empty plot. It is supposed to plot mean and range of population at each iteration for each variable, which is two in this case.
function y = booth_func(x)
y = (x(1) + 2 * x(2) – 7) ^ 2 + (2 * x(1) + x(2) – 5) ^ 2;
end
options = optimoptions(‘ga’, ‘PlotFcn’, @gaPlotRangeND);
[x, fval] = ga(@booth_func, 2, [], [], [], [], [], [], [], options);
How can I solve this issue? What do I miss here? plotting, subplot, optimization MATLAB Answers — New Questions