# Genetic Algorithm: Find best input combinations that will give global maximum of the output of a non-linear ODE mathematical model using Genetic algorithm

8 vues (au cours des 30 derniers jours)
Darshna Joshi le 12 Jan 2022
Modifié(e) : Torsten le 8 Jan 2024
The problem is to find the optimum(maximum) value of x3 in range of (-8e-4 to 2e-4) by varying kst,x1,x5 and xo)
x5=5 %Input 2 (Input 2 is a state variable and could vary in range of 4 to 15 while performing optimization)
kst=1 %Input 3 (Input 3 is in terms of rate constant, it could vary from 0.1 to 2)
xo=4 %Input 4 (Input 4 is a state variable and could vary in range of 4 to 10)
x1=1e-7 %Input 1 could vary from 1e-9 to 1e-6
%MATLAB CODE%
% Function file for ODE solution
options = odeset('InitialStep',0.0001,'RelTol',1e-09);
[T,Y]=ode15s(@Scrpt1,[0 60],[9e-13,0],options);
X3= Y(:,2);
plot(T,X3)
%Script file
function rest = Scrpt1(t,X)
x2 = X(1);
x3 = X(2);
%Parameters
if t<15
x1 = 1e-7; %Input 1 could vary from 1e-9 to 1e-6
else
x1 = 0;
end
x5=5; %Input 2 (Input 2 is a state variable and could vary in range of 4 to 15 while performing optimization)
kst=1; %Input 3 (Input 3 is in terms of rate constant, it could vary from 0.1 to 2)
xo=4; %Input 4 (Input 4 is a state variable and could vary in range of 4 to 10)
k1 = 6e7;
km1 = 0.20;
km4 = 0.003;
k3 = 2500.00;
k4 = km4/9;
km3 = km1;
LAP=1.5;
%Differential equations
dx2dt = km1*x3 + km3*LAP - k1*x1*x2 + km4*x3 - k4*x2;
dx3dt = k1*x1*x2 - km1*(x3+x5+xo) - k3*x3*kst;
rest = [dx2dt; dx3dt];
end
How to use Genetic Algorithm for this to solve the mentioned optimization problem of finding maximum value of x3. For which values of x5,kst,xo,x1 we get maximum x3?
My objective function is Maximize(x3).
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Torsten le 8 Jan 2024
Maximum X3 after 60 time units ? Maximum value within the complete interval from 0 to 60 time units ? Maximum mean value in the interval from 0 to 60 time units ?

Connectez-vous pour commenter.

### Réponses (1)

VINAYAK LUHA le 8 Jan 2024
Modifié(e) : Torsten le 8 Jan 2024
Hi Darshna,
I understand that you want to find the maximum value of the variable "x3" using Genetic Algorithm in MATLAB.
Go through the following steps to achieve the above objective.
1. Define the objective function that the GA will optimize
2. Set Up the GA Options and Constraints
3. Run the GA
4. Retrieve the Optimal Parameters and Maximum Value of x3
Please note that the GA inherently seeks to minimize the objective function. To find the maximum, we'll need to focus on the negative of the objective function. Additionally, you might need to adjust the GA settings or impose extra constraints to achieve optimal outcomes for your particular scenario.
Below is a sample code for implementing the steps mentioned:
% Define the bounds for the variables x1, x5, kst, xo
lb = [1e-9, 4, 0.1, 4];
ub = [1e-6, 15, 2, 10];
% Set up the GA options
options = optimoptions('ga','PopulationSize', 100,'MaxGenerations', 100,'FunctionTolerance', 1e-6,'Display', 'iter');
% Run the GA
[x_opt, fval] = ga(@objectiveFunction, 4, [], [], [], [], lb, ub, [], options);
Single objective optimization: 4 Variables Options: CreationFcn: @gacreationuniform CrossoverFcn: @crossoverscattered SelectionFcn: @selectionstochunif MutationFcn: @mutationadaptfeasible Best Mean Stall Generation Func-count f(x) f(x) Generations 1 200 -0 -0 0 2 295 -0 -0 1 3 390 -0 -0 2 4 485 -0 -0 3 5 580 -0 -0 4 6 675 -0 -0 5 7 770 -0 -0 6 8 865 -0 -0 7 9 960 -0 -0 8 10 1055 -0 -0 9 11 1150 -0 -0 10 12 1245 -0 -0 11 13 1340 -0 -0 12 14 1435 -0 -0 13 15 1530 -0 -0 14 16 1625 -0 -0 15 17 1720 -0 -0 16 18 1815 -0 -0 17 19 1910 -0 -0 18 20 2005 -0 -0 19 21 2100 -0 -0 20 22 2195 -0 -0 21 23 2290 -0 -0 22 24 2385 -0 -0 23 25 2480 -0 -0 24 26 2575 -0 -0 25 27 2670 -0 -0 26 28 2765 -0 -0 27 29 2860 -0 -0 28 30 2955 -0 -0 29 Best Mean Stall Generation Func-count f(x) f(x) Generations 31 3050 -0 -0 30 32 3145 -0 -0 31 33 3240 -0 -0 32 34 3335 -0 -0 33 35 3430 -0 -0 34 36 3525 -0 -0 35 37 3620 -0 -0 36 38 3715 -0 -0 37 39 3810 -0 -0 38 40 3905 -0 -0 39 41 4000 -0 -0 40 42 4095 -0 -0 41 43 4190 -0 -0 42 44 4285 -0 -0 43 45 4380 -0 -0 44 46 4475 -0 -0 45 47 4570 -0 -0 46 48 4665 -0 -0 47 49 4760 -0 -0 48 50 4855 -0 -0 49 51 4950 -0 -0 50 ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
% Retrieve the optimal parameters
optimal_x1 = x_opt(1);
optimal_x5 = x_opt(2);
optimal_kst = x_opt(3);
optimal_xo = x_opt(4);
% Maximum value of x3
max_x3 = -fval
max_x3 = 0
% Display the results
fprintf('Optimal x1: %g\n', optimal_x1);
Optimal x1: 1e-09
fprintf('Optimal x5: %g\n', optimal_x5);
Optimal x5: 4
fprintf('Optimal kst: %g\n', optimal_kst);
Optimal kst: 0.1
fprintf('Optimal xo: %g\n', optimal_xo);
Optimal xo: 4
fprintf('Maximum value of x3: %g\n', max_x3);
Maximum value of x3: 0
% Objective Function for the Genetic Algorithm
function x3_max = objectiveFunction(params)
x1 = params(1);
x5 = params(2);
kst = params(3);
xo = params(4);
function rest = Scrpt1(t, X)
x2 = X(1);
x3 = X(2);
if t < 15
x1_val = x1;
else
x1_val = 0;
end
k1 = 6e7;
km1 = 0.20;
km4 = 0.003;
k3 = 2500.00;
k4 = km4 / 9;
km3 = km1;
LAP = 1.5;
dx2dt = km1*x3 + km3*LAP - k1*x1_val*x2 + km4*x3 - k4*x2;
dx3dt = k1*x1_val*x2 - km1*(x3+x5+xo) - k3*x3*kst;
rest = [dx2dt; dx3dt];
end
options = odeset('InitialStep',0.0001,'RelTol',1e-09);
[T,Y] = ode15s(@(t, X) Scrpt1(t, X), [0 60], [9e-13, 0], options);
x3_max = -max(Y(:,2));
end
Additionally, You may refer to the following documentations for more details about the used functions
I hope this clarifies how to apply the Genetic Algorithm in MATLAB to obtain the maximum value of "x3" for your case.
Regards,
Vinayak Luha
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Genetic Algorithm dans Help Center et File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by