# how can i use the output of one genetic algorithm program in another GA programme.

3 views (last 30 days)
harsh Brar on 11 Mar 2022
Answered: Prateekshya on 18 Jan 2024
lets say we have the equation
t = 122.14-0.84.*x(1)-0.42.*x(2)+0.34.*x(3)-0.09.*x(4)+363.77.*x(5);
It have 5 variables and have some lower and upper bounds. i perform genetic algorithm and got optimum values as answers. now whatever value I got for these 5 variables. I want to use them in another equation which is
s = - 463.21-3.05x(1) + 5.21x(2)+0.54x(3) +0.11x(4)-6541.17x(5) + 41.67x(6).
It have 6 variables ( variable x(1) to x(5) are same ) and x(6) is unknown and have some lower and upper bounds . here, i want the output from my previous equation(i.e. equation1 ) as an input of x(1) to x(5) variable in 2nd equation and x(6) will be treated as unknown variables having some bounds ofc.
Inshort, i want to use the answer of 5 variable from 1st equation in 2nd equation.
ps:- Since whenever I run a genetic algorithm, the output of first equation is going to change, i want your help in the code in which the output of x(1) to x(5) variable from 1st equation will automatically get assigned in my 2nd equation. now, i hope you got it. thank you for taking interest in question.
this general coding for 1st equation
function t=strength(x)
t = 122.14-0.84.*x(1)-0.42.*x(2)+0.34.*x(3)-0.09.*x(4)+363.77.*x(5);
end
function [state,options,optchanged] = stop40_80(options,state,flag)
optchanged = false;
switch flag
case 'init'
case 'iter'
% Find the best objective function, and stop if it is right range
mask = state.Score >= 40 & state.Score <= 80;
state.StopFlag = 'range statisfied';
state.Best(end) = state.Score(end);
end
case 'done'
end
end
this is my calling function of GA for 1st equation:-
options = optimoptions('ga','MutationFcn', @mutationadaptfeasible, 'CrossoverFcn', {@crossoverintermediate, 0.7}, 'SelectionFcn', {@selectiontournament,3}, 'PlotFcn', {@gaplotselection, @gaplotbestf, @gaplotbestindiv, @gaplotrange}, 'FitnessLimit', -inf, 'MaxGenerations', 1e5);
A = []; b = []; Aeq = []; beq = []; lb = [29 160 30 0 0.040]; ub = [50 180 60 20 0.069]; nonlcon = [];
obj = @(x) (strength(x)-80).^2;
bestx = ga(obj, 5, A, b, Aeq, beq, lb, ub, nonlcon, options);
display(bestx)
display(strength(bestx))
All I want is use this data (output) from 1st equation( Basically output of variables x(1) to x(5)) for finding the answer of 2nd equation whose fitness function is
s= -463.21-3.05.*x(1)+5.21.*x(2)+0.54.*x(3)+0.11.*x(4)-6541.17.*x(5)+41.67.*x(6);
here variable x(1) to x(5) variable are same from 1st equation
lower and upper bound of x(6)-
lb=1.50
ub=9.0

Prateekshya on 18 Jan 2024
Hi Harsh,
I understand that you want to pass the result of one Genetic Algorithm execution to another. Here's how you can do it:
Create a function that will calculate "s".
function s = second_equation(x6, bestx)
x = [bestx, x6]; % Combine the known best values with the new variable x6
s = -463.21 - 3.05.*x(1) + 5.21.*x(2) + 0.54.*x(3) + 0.11.*x(4) - 6541.17.*x(5) + 41.67.*x(6);
end
This function should be called after your first execution of Genetic Algorithm is over. The results of that execution should be passed as the argument "bestx".
Now "s" contains only one unknown variable i.e. "x(6)". You can use this result for the next execution of the Genetic Algorithm in a way similar to the first execution.
I hope this helps!

### Categories

Find more on Genetic Algorithm in Help Center and File Exchange

R2018a

### Community Treasure Hunt

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

Start Hunting!