Optimization with Genetic Algorithms working with vectors
Show older comments
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')
Accepted Answer
More Answers (0)
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!