Info

此问题已关闭。 请重新打开它进行编辑或回答。

Problem with Output function in Genetic Algorithm (GA)

2 次查看(过去 30 天)
This is my main script, I have a problem in the output function which have to save a mat file with the hystory of each generation. The "Output function" (@myfun in the code) that I have written save the hystory of the first and the second ga but in the third ga it only saves the first and the last value.
Main function
clear all, close all, clc
dt = 0.001;
popsize = 10;
MaxGenerations = 10;
s = tf('s');
G = (1/(1+5*s))*exp(-s)
rng(1,'twister') % for reproducibility
population_PID = rand(popsize,3);
population_I_PD = rand(popsize,3);
population_PI_D = rand(popsize,3);
%% PID
options_PID = optimoptions(@ga,'PopulationSize',popsize,'MaxGenerations',MaxGenerations,'InitialPopulation',population_PID,'OutputFcn',@myfun_pid);
[x_PID,fval_PID,exitflag,output,population_PID,scores] = ga(@(K)pidtest(G,dt,K),3,-eye(3),zeros(3,1),[],[],[],[],[],options_PID);
fval_PID
%% I-PD
options_I_PD = optimoptions(@ga,'PopulationSize',popsize,'MaxGenerations',MaxGenerations,'InitialPopulation',population_I_PD,'OutputFcn',@myfun_ipd);
[x_I_PD,fval_I_PD,exitflag,output,population_I_PD,scores] = ga(@(K)ipdtest_gapid(G,dt,K),3,-eye(3),zeros(3,1),[],[],[],[],[],options_I_PD);
fval_I_PD
%% PI-D
options_PI_D = optimoptions(@ga,'PopulationSize',popsize,'MaxGenerations',MaxGenerations,'InitialPopulation',population_PI_D,'OutputFcn',@myfun_pi_d);
[x_PI_D,fval_PI_D,exitflag,output,population_PI_D,scores] = ga(@(K)dpitest(G,dt,K),3,-eye(3),zeros(3,1),[],[],[],[],[],options_PI_D);
fval_PI_D
My fun PID
All the output function are the same but only in the last ga that I run it doesn't save in the correct way.
function [state, options,optchanged] = myfun_pid(options,state,flag)
persistent history_pid
persistent cost_pid
optchanged = false;
switch flag
case 'init'
history_pid(:,:,1) = state.Population;
cost_pid(:,1) = state.Score;
case {'iter','interrupt'}
ss = size(history_pid,3);
history_pid(:,:,ss+1) = state.Population;
cost_pid(:,ss+1) = state.Score;
case 'done'
ss = size(history_pid,3);
history_pid(:,:,ss+1) = state.Population;
cost_pid(:,ss+1) = state.Score;
save history_pid.mat history_pid cost_pid
end
hystory_pid.mat
My fun PI-D
function [state_PI_D, options_PI_D,optchanged] = myfun_pi_d(options_PI_D,state_PI_D,flag_PI_D)
persistent history_dpi
persistent cost_dpi
optchanged = false;
switch flag_PI_D;
case 'init'
history_dpi(:,:,1) = state_PI_D.Population;
cost_dpi(:,1) = state_PI_D.Score;
case {'iter','interrupt'}
ss = size(history_dpi,1);
history_dpi(:,:,ss+1) = state_PI_D.Population;
cost_dpi(:,ss+1) = state_PI_D.Score;
case 'done'
ss = size(history_dpi,1);
history_dpi(:,:,ss+1) = state_PI_D.Population;
cost_dpi(:,ss+1) = state_PI_D.Score;
save history_pi_d.mat history_pi_d cost_pi_d
end
hystory_pi_d.mat
As I said, the functions are the same but in this case the output function of the genetic algorithm is wrong

回答(0 个)

此问题已关闭。

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by