How to automate small increments in function for Genetic Algorithm ?

4 views (last 30 days)
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 ?

Accepted Answer

Star Strider
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 Comments
Ankur Shah
Ankur Shah on 31 Aug 2021
Thank you for your reply.
There are 3 files which have been created
1. Function file - The one defining function - Given above
function [objfun]=numericalbeam(phi)
objfun=AA+BB+(y*CC)
end
2. GA code- Generated from optimization toolbox. The end of which is as below
[x,fval,exitflag,output,population,score] = ...
ga(@numericalbeam,nvars,[],[],[],[],lb,ub,[],[],options);
3. Final Code - From where GA is called -
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
Definitiely i want to run GA one after another. to get fitness and vairables i.e. for particular value of y= 0.01 than y= 0.015 and so on. There are 10 variables and also for each ' y' ga is run ten times (nit=10).
I am trying to automate the process, i.e. rather than entering single value of 'y' each time and finding variable and fitness value, I can loop it and at end get values of fitness and variable for all value of 'y' i.e. y=0.01,0.015 etc.
Could you help by showing in example of code as above ?
Star Strider
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.
.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!