필터 지우기
필터 지우기

Genetic algorithm fot multi differential equations

조회 수: 12 (최근 30일)
abdelghani msaad
abdelghani msaad 2021년 3월 14일
댓글: abdelghani msaad 2022년 7월 18일
good mornig i am working at a project in which i want to optimize the parameters of the 3 differential equations defined in the fun function, the parameters are 2, a = x(1) and b = x(2), i used the command ga to optimize them according to the available data that io already have, but i don't understand what i wrong because i get a lot of errors, also i don't know how to plot the final graph and compare it with the real one, here is the code:
clear all
N = 1e7;
dt = 1;
%% first data curve
Tmax = 99;
t = 1:dt:Tmax;
data = xlsread('MyData.xlsx', 'Foglio1', 'B1:B100'); %infected people per day
%% Loading data into arrays
y = data(:,1); %copy data into array
I = y'; % vector of infected people per day
I0 = I(1);
%% setting conditions for the ga
A = [];
b = [];
Aeq = [];
beq = [];
lb = [0 0];
ub = [5 5];
variables = 2;
%% defining the fitness function and the distance fitness function which will be inside the ga
fun = @(x) fitness_fun(x,Tmax,N,dt);
objFun = @(x) norm(fun(x)-I);
%% solution given by the ga
coeff = ga(objFun,variables,A,b,Aeq,beq,lb,ub);
%% graph of the solution compared withj the real one
axes();
plot(t, I, 'b+');
hold on
plot(t, fun(coeff), 'r-');
legend({'Data points', 'Fitted Curve'})
the following is the fitness function which contains the ode45 command for the fun function with the differential equations:
function y = fitness_fun(x,Tmax,N,dt)
tspan = 0:dt:Tmax;
y0 = [N 1 0];
[a,y] = ode45(@(x) fun, tspan, y0);
end
%% series of function for the ODE solver
function dydt = fun(x,y)
N = 1e4;
a = x(1);
b = x(2);
dydt(1) = -a*y(1)*y(2)/N; %-a*S*I
dydt(2) = a*y(1)*y(2)/N - b*y(2); % a*S*I
dydt(3) = b*y(2); % b*I
end

채택된 답변

Star Strider
Star Strider 2021년 3월 14일
The ‘fitness_fun’ function should probably be something like this:
function y = fitness_fun(x,t,N)
tspan = t;
y0 = [N 1 0];
[t,y] = ode45(@(x) fun, tspan, y0);
%% series of function for the ODE solver
function dydt = fun(t,y)
dydt = zeros(3,1);
N = 1e4;
a = x(1);
b = x(2);
dydt(1) = -a*y(1)*y(2)/N; %-a*S*I
dydt(2) = a*y(1)*y(2)/N - b*y(2); % a*S*I
dydt(3) = b*y(2); % b*I
end
end
Also, the time (or independent variable) vector for the data must be the ‘tspan’ argument, or it will not be possible to compare the fitted result with the data. I added the zeros call so that ‘fun’ will return a column vector, and added ‘t’ as its first argument. It will inherit the parameter vector ‘x’ from the outer function workspace.
I cannot test this, however my changes should get it closer to working. (I generally pass the initial conditions vector as the last elements of the parameter vector so that the optimisation routine can fit them as well.)

추가 답변 (4개)

abdelghani msaad
abdelghani msaad 2021년 3월 19일
a the end your suggestion was usefull but i wasn doing the difference between the same dimension matrix
objFun = @(x) norm(I - fun(x));
I was a vector and fun(x) a matrix
  댓글 수: 3
abdelghani takha
abdelghani takha 2021년 11월 7일
hi sir
how to use Genetic algorithm for this differential equations
dx1dt = a0*x(1) - w0*x(2);
dx2dt = a0*x(2) + w0*x(1);
dx3dt = - sum(ai.*dti.*exp(-0.5*(dti./bi).^2))- 1.0*(x(3) - zbase);
Star Strider
Star Strider 2021년 11월 7일
Post this as a new Question.

댓글을 달려면 로그인하십시오.


abdelghani msaad
abdelghani msaad 2021년 11월 7일
Here i prepare the array with the real data in might be something else in your case
y = letturaExcel(t3(i),t3(i+1));
%% defining the fitness function and the distance fitness function which will be inside the ga
Here i hold the fitness function in the variable fun, then i i hold in the variable objFun the real aim for my optimization which is the norm between the real data and the values calculated by the model in the fitness_fun
fun = @(x) fitness_fun(x,t,N,y0);
objFun = @(x) norm(fun(x) - y(:,2));
here you prepare some details to give to the ga so it will help it to work correclty form more info look at the ga documentation.
%% solution given by the ga
pop = 50;
maxGen = 30;
opts = optimoptions('ga', 'PopulationSize',pop, 'TolFun',1e-5,'MaxGenerations',maxGen, 'PlotFcn',@gaplotbestf, 'PlotInterval',1);
i save x wich is the array of coefficients optimized by ga and fval which is a value taht gives you an idea on how good is the optimization.
[x,fval] = ga(objFun,variables,A,b,Aeq,beq,lb,ub,[],[],opts);
[J,Jv] = fitness_fun(x,t,N,y0);
% this last command you can decide...
here there is the fitness function
function [J,Jv] = fitness_fun(x,t,N,y0)
[T,Jv] = ode45(@fun, t, y0);
%differential equations to ode45
function dY = fun(t,y)
dydt = zeros(3,1);
a = x(1);
b = x(2);
dydt(1) = -a*y(1)*y(2)/N; %-a*S*I/N
dydt(2) = a*y(1)*y(2)/N - b*y(2); % a*S*I/N - b*I
dydt(3) = b*y(2); % b*I
dY = dydt;
end
J = Jv(:,2);
end
  댓글 수: 6
Omima Musa Mohmed Abusil
Omima Musa Mohmed Abusil 2022년 7월 16일
I do have this code but the ga has failed at initialize the value and I do not know why? so will you help me to locate the issue and thanks
here is my code
function [J,Jv]=paramfun(theta,t,bt0)
% Monod Model for PPB growth
% dx/dt = Mumax*inhibition factor*x - kd*x
% ds/dt = -(1/y)*Mumax*inhibition factor*x
% with
% variable b(1) = x, b(2) = s
% parameter theta(1) = Mumax, theta(2) = inhibition factor, theta(3) = decay, theta(4)= yield
[T,Jv] = ode45(@fun,t,bt0);
function dC = fun(t,b);
dcdt = zeros(2,1);
dcdt(1)= theta(1)*theta(2)*b(1)-theta(3)*b(1);
dcdt(2) = -(1/theta(4))*theta(1)*theta(2)*b(1);
dC=dcdt;
end
J=Jv(:,2);
end
clear all
clc
t = [0 2.1 4 5 22.5 24.5 26.5 28.5 29.5 46.5 48.5 50.5 52.5 53.5 70.5 73.25 75.5]';
x = [19.5 23.57 24.33 24.33 80.6 100.76 142.2 174.14 188.21 321.3 331.18 324.33 322.432 322.432 327.755 332.052 319.77]';
s = [999.957 996.4 982.012 968.86 495.17 459.42 429.2 403.65 392.43 292.94 287.5 282.74 278.55 276.64 256.49 254.6 253.22]';
b = [x s];
% optimization
lb = [0,0,0,0];
ub = [0.04,0.5,0.03,1];
%%
A = [];
b = [];
Aeq = [];
beq = [];
%%
nvar = 4%;%number of variable in the objective function
%% to call the objective function
fun =@(b)paramfun(theta,t,bt0);
objFun = @(b)norm(fun(b)-b);
%fitnessF = @(theta,t,bt0)Mysim(theta,t,bt0);
%% defining the iteration option
options = optimoptions('ga','Display','iter','MaxGenerations',1000*nvar,'PopulationSize',55,'FunctionTolerance',1e-6,'PlotFcn',@gaplotbestindiv);
%% optimization
[b,fval]=ga(objFun,nvar,A,b,Aeq,beq,lb,ub,[],[],options);
%% drawing model data and real one
axes()
plot(t,b,'o'); hold on;
plot(t,fun([b,fval]),'-bo'); hold off;
waiting for your feedback urgently
abdelghani msaad
abdelghani msaad 2022년 7월 18일
Hi man, im sorry but i am not an active user here, and it has been a while since i have not used more GAs, im not trained, what i can suggest you both is to post you questions as new Questions and not here in the comments so other people will be also able to see the and help you.
@Star Strider answers if the question is new to get prices in Mathworks so post a new question on the page.

댓글을 달려면 로그인하십시오.


abdelghani takha
abdelghani takha 2021년 11월 7일
hi sir
In the case of variables :
ai = [-60 -15 0 15 90];
bi = [1.2 -5 30 -7.5 0.75];
ci = [0.25 0.1 0.1 0.1 0.4];
How do I write Ib and Ub

abdelghani takha
abdelghani takha 2021년 11월 9일

카테고리

Help CenterFile Exchange에서 Genetic Algorithm에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by