Curve Fitting Problem using Genetic Algorithm
12 views (last 30 days)
Show older comments
Hi i have two vectors of data as Input and output of a system; I want to model a function ( Which I call It mapping function) which maps input vector to output vector by use of Genetic algorithm the problem is I don't know how should I write Fitness function for genetic algorithm (because I need to have The mapping function for future use) and generally does genetic algorithm can be applied to this family of problems
2 Comments
Answers (2)
Janarthanan
on 21 Oct 2014
Curve fit can be done in matlab as follows:
Create two files GA.m and curvefit_multiobjective.m and run GA.m in the command window.
GA.m
%%First read the expt data 'Data' to fit from a file the content of which is shown below...
%e.g. y=a*exp(-b*x); where a= 10, b=0.2; and x: 0 to 10
%0.000 10.000
%1.000 8.187
%2.000 6.703
%3.000 5.488
%4.000 4.493
%5.000 3.679
%6.000 3.012
%7.000 2.466
%8.000 2.019
%9.000 1.653
%10.000 1.353
Data_exp=fopen('Data','r');
exp_data=fscanf(Data_exp,'%f',[2 inf]);
fclose(Data_exp);
exp_data=exp_data';
FitnessFunction = @(x)curvefit_multiobjective(x,exp_data); % Function handle to the fitness function
numberOfVariables = 2; % Number of decision variables
lb = []; % Lower bound
ub = []; % Upper bound
A = []; % No linear inequality constraints
b = []; % No linear inequality constraints
Aeq = []; % No linear equality constraints
beq = []; % No linear equality constraints
hybridopts = optimoptions('fminunc','Display','iter','Algorithm','quasi-newton','MaxFunEvals',100);
options = gaoptimset('CreationFcn',{@gacreationlinearfeasible},'TolFun',1e-4,'Generations',300,'StallGenLimit',200,'PopulationSize',200,'MutationFcn',{@mutationgaussian},'EliteCount',1,'CrossoverFraction',0.8,'CrossoverFcn',{@crossovertwopoint},'HybridFcn',{@fminunc,hybridopts});
[x,Fval,exitFlag,Output] = gamultiobj(FitnessFunction,numberOfVariables,A,b,Aeq,beq,lb,ub,options);
fprintf('The number of points on the Pareto front was: %d\n', size(x,1));
fprintf('The number of generations was : %d\n', Output.generations);
% x: the fit parameters, Fval: The minimized objective function
curvefit_multiobjective.m
function y = curvefit_multiobjective(x,exp_data)
%KUR_MULTIOBJECTIVE Objective function for a multiobjective problem.
% The Pareto-optimal set for this two-objective problem is nonconvex as
% well as disconnected. The function KUR_MULTIOBJECTIVE computes two
% objectives and returns a vector y of size 2-by-1.
%
% Reference: Kalyanmoy Deb, "Multi-Objective Optimization using
% Evolutionary Algorithms", John Wiley & Sons ISBN 047187339
% Copyright 2007 The MathWorks, Inc.
% Initialize for two objectives
% Compute first objective
[row,col]=size(exp_data');
y = zeros(2,1);
for i = 1:row
y(1) = y(1)+power(1-(x(1)*exp(-(x(2)*exp_data(i,1)))/exp_data(i,2)),2);
y(2) = y(2)+power(1-(x(1)*exp(-(x(2)*exp_data(i,1)))/exp_data(i,2)),2);
end
Good luck! Jana_bio
0 Comments
Frederico Severgnini
on 9 Jun 2017
This is super old thread but I'd like to thank Janarthanan on the great answer. One question: In other curve fitting solvers (like lsqlin) we need to offer an initial guess for the curve parameters, which will be used as initial point in matlab calculations. Is there something similar in genetic algorithm applications for fitting problems? It seems ga has it's own set of technical terms, so if this concept existis here, I'm not sure what is the name for it.
Regards,
Fred
0 Comments
See Also
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!