# How to automate small increments in function for Genetic Algorithm ?

1 view (last 30 days)
Ankur Shah on 30 Aug 2021
Commented: Star Strider on 31 Aug 2021
Hello everyone,
I have prepared code in matlab for genetic algorithm from toolbox for number of iteration (nit)
The function code is very long. In summary, the fitness function is
function [objfun]=numericalbeam(phi)
objfun=AA+BB+(y*CC)
end
y = 0.01 to 1 with small increments of 0.005...
GA code is prepared from toolbox.. The code ends as below
for i=1:nit
[x,fval,exitflag,output,population,score] =
GAcode(nvars,lb,ub,InitialPopulationRange_Data,PopulationSize_Data,EliteCount_Data,CrossoverFraction_Data,
MaxGenerations_Data,FunctionTolerance_Data,ConstraintTolerance_Data);
H1(i,1)=fval;
H2(i,:)=x;
end
H1 give function value and H2 gives unknowns for all iterations.
The code works perfectly fine when single value of y is taken for e.g. 0.01
Is it possible to apply a loop such that it automates small increment of 'y' and gives results of function and unknowns at end for different values of y ?

Star Strider on 30 Aug 2021
I am not certain what you want to do.
It would certainly be appropriate to put the ga call in a loop and have the function pass ‘y’ as an extra argument so that it updates the fitness function between ga calls. It is not a good idea to increment ‘y’ while ga is running, since this creates a ‘moving target’ that any optimisation routine will have problems working with.
I am not certain what you are doing (and the text is running off the right edge of the browser window), however I would do something like this:
fitness_function = @(b,y) exp(b*y);
y = 0.01:0.005:1;
for k = 1:numel(y)
out{k} = ga(@(b)fitness_function(b,y(k)))
end
figure
plot(y, out)
grid
Or something similar. The ga output ‘out’ does not have to be a cell array, however I created it as such here because I have no idea what the dimensions are of the problem you are optimising.
.
##### 2 CommentsShowHide 1 older comment
Star Strider on 31 Aug 2021
That is not possible.
The best you can hope for is to provide a new value for ‘y’ in each call to ga, wait for ga to converge, store that result, and solve for the next value of ‘y’ and so on for all the values of ‘y’. There are no alternative options.
.