How to incorporate write "options" into the main code (GA algorithm) for beginners
1 view (last 30 days)
I am a beginner to writing matlab code for global optimization using GA algorithm. I can use the live editor to perform the optimization. However, I noticed more than one peak from the resulting plots. It means that there may be more than one optimum conditions. So I would like to write a code that can change number of parents, and at each number of parents, the GA optimization should give me a new set of solutions.
To accomplish this, I wen to this website, https://www.mathworks.com/help/gads/genetic-algorithm-options.html#f6593, and found that I should use options to pass these extra information to my code. This is where the problem started. I don't understand how to incorporate the inforamtion in the link above to my existing main code.
I know that I should write the following two lines:
options = optimoptions('ga','Option1','value1','Option2','value2'); (1)
[x,fval] = ga(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options) (2)
To change number of parents, the link above said...
A function handle enables you to write your own selection function.
options = optimoptions('ga','SelectionFcn',@myfun); (3)
Your selection function must have the following calling syntax:
function parents = myfun(expectation, nParents, options) (4)
ga provides the input arguments expectation, nParents, and options. Your function returns the indices of the parents.
To get x and fval, should start with Line 3, followed by Lines 4 and 2?
Alan Weiss on 21 Jan 2022
I think that you have a misconception about How the Genetic Algorithm Works. ga always takes exactly two parents for creating a child, except when it takes one for a mutation child.
There may indeed be more than one optimum, but it doesn't mean that you should do any parameter tuning to run your optimization. Please read up on this and other topics in the genetic algorithm documentation such as
MATLAB mathematical toolbox documentation