Optimization with Genetic Algorithms working with vectors
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Vinicius Teixeira
am 19 Mär. 2021
Kommentiert: Star Strider
am 19 Mär. 2021
Hey guys!
I have the following problem, I have a vector referring to experimental data being it 1 column and 37 rows called P_i.
I intend to estimate the parameters of a curve that passes through these points of the vector P_i.
I am using the matlab GA toolbox to determine the parameters of that curve whose objective function is this.
function [error]=Func_obj_error(x)
%Four-Parameter Logistic Expression:
load ('P_i.mat'); % load experimental data
a=x(1); % 1° parameter of the logistic expression
b=x(2); % 2° parameter of the logistic expression
c=x(3); % 3° parameter of the logistic expression
d=x(4); % 4° parameter of the logistic expression
Y = a*(1+b*exp(-x/c)/(1+d*exp(-x/c)));
error= (P-Y).^2; % I intend to minimize the mean square error in relation to the experimental data
end
Matlab returns the following error
Error using makeState (line 56)
Your fitness function must return a scalar value.
Error in galincon (line 17)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 374)
[x,fval,exitFlag,output,population,scores] = galincon(FitnessFcn,nvars, ...
Error in GA_rv01 (line 45)
[X(n_repete,:),Y(n_repete,:),exitflag(n_repete,:),output,population,score] = ga(Func,nvars,A,b,Aeq,beq,lb,ub,[],[],options);
I need the x that goes into the Func_obj_error function to be the same size as my experimental data to compare point by point and then minimize the error. However the x that enters the Func_obj_error function has only one row and 4 columns and not 4 columns and 37 rows. The main algorithm of Ga is below.
clc, clear all, close all hidden
load('V_i_e_P_i.mat');
%% Problem formulation
Func = @(x)Func_obj_error(x);
nvars = 4; % Number of variables
A = [];
b = [];
Aeq = [];
beq = [];
ub = [ 0.06751 , 2.325 , -1.023 , 0.002009]; % Uper Boundary
lb = [-0.04746 , -1.679 , -1.248 , 0.0003645]; % Lower Boundary
run=1;
n_pop=length(P_i);
%%
for n_=1:run
%% Start with the default options
options = gaoptimset;
%% Modify options setting
options = gaoptimset(options,'PopulationSize', n_pop);
options = gaoptimset(options,'PopulationType', 'doubleVector');
options = gaoptimset(options,'SelectionFcn', @selectionroulette);
options = gaoptimset(options,'TolFun', 1e-20);
options = gaoptimset(options,'StalltimeLimit', 60);
options = gaoptimset(options,'StallGenLimit', 100);
options = gaoptimset(options,'TimeLimit', 180);
options = gaoptimset(options,'Generations', 1000)
options = gaoptimset(options,'MutationFcn', {@mutationadaptfeasible});
options = gaoptimset(options,'CrossoverFcn',@crossoverheuristic);
options = gaoptimset(options,'CrossoverFraction', 0.8);
options = gaoptimset(options,'PlotFcns', { @gaplotbestf, @gaplotbestindiv, @gaplotstopping});
%% Solver
[X(n_repete,:),Y(n_repete,:),exitflag(n_repete,:),output,population,score] = ga(Func,nvars,A,b,Aeq,beq,lb,ub,[],[],options);
PG(n_repete,:)=sum(X(n_repete,:));
end
%%
a=X(1);
b=X(2);
c=X(3);
d=X(4);
x=v_i;
fitness_GA = (a.*(1+b.*exp(-x./c)./(1+d.*exp(-x./c))));
figure(777)
plot(v_i,P_i,'-k')
hold on
plot(v_i,fitness_GA,'-r')
legend('P_expimental','P_GA')
0 Kommentare
Akzeptierte Antwort
Star Strider
am 19 Mär. 2021
error= sum((P(:)-Y(:)).^2); % I intend to minimize the mean square error in relation to the experimental data
That should work.
1 Kommentar
Star Strider
am 19 Mär. 2021
It should produce a scalar value for ‘error’, and that is the substance of the error message your code threw.
It would be most efficient to read you data in once, then pass it to your fitness function to compare the model with the dat. Your fitness function then returns the scalar ‘error’ value (in your code) that ga then uses as the fitness value for its optimisation.
The ‘fitness_GA’ function should be something like this:
function [error]=fitness_GA(x,y)
%Four-Parameter Logistic Expression:
a=x(1); % 1° parameter of the logistic expression
b=x(2); % 2° parameter of the logistic expression
c=x(3); % 3° parameter of the logistic expression
d=x(4); % 4° parameter of the logistic expression
Y = a*(1+b.*exp(-x/c)./(1+d.*exp(-x./c)));
error= sum((y(:)-Y(:)).^2); % I intend to minimize the mean square error in relation to the experimental data
end
with the data loades separately:
y = load ('P_i.mat'); % load experimental data;
with the ga call as:
B = ga(@(x)fitness_GA(x,y), ...other_arguments...);
What am I supposed to do with an image of your data set?
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Genetic Algorithm finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!