# Is it possible to make create custom InitialPopulation for N simulation iterates of genetic algorithm w/o resetting optimoptions N times?

1 view (last 30 days)
Nick Rowe on 9 Feb 2022
Commented: Nick Rowe on 10 Feb 2022
I would like to use the genetic algorithm to simulate N nonlinear minimization problems with nonlinear constraints. As the problem is constrained, I get better results when I am able to feed the genetic algorithm a custom creation function/initial population. Is there a way to create N custom subpopulations without resetting optimoptions N times?
Further background/context: I had hoped to use the custom creation function to index the evaluation of the creation function to the particular simulation iteration of the objective function; i.e., on simulation j = 542 of 2,000,000, call a creation functio that accepts the index j as a variable so that the created index j facilitates the creation of a new initial population, something like the following:
function Population = myfun(K,obj2,options,START,ilb,iub,j,G)
Population = (repmat(ilb(j,:),G,1)+repmat(iub(j,:)-ilb(j,:),G,1).*START);
where iub, ilb are custom bounds for each simulation iterate j, G the populzation size, K the dimension of a population member, and START a G-by-K matrix of preallocated uniform random variables.
Thus far, I haven't had any luck. Using the the for-loop index j is no good, as creating a function handle for the creation function freezes the for-loop index at whatever variable existed at the time the handle was created. Additionally, one cannot seemingly make the for-loop index a variable of the creation function, as the genetic algorithm seems to only know how to call on creation functions with the variables (GenomeLength, FitnessFcn, options). I would appreciate any insight available on this matter.

Alan Weiss on 10 Feb 2022
You can use a custom creation function. As you probably know, this function can have the syntax
function Population = myfun(GenomeLength, FitnessFcn, options,params)
where params can be a parameter vector or structure. You set the function in ga using
options = optimoptions("ga","CreationFcn",...
@(GenomeLength, FitnessFcn, options)myfun(GenomeLength, FitnessFcn, options,params));
You can set params as you like. But, as you say, you might need to recreate these options each time you change params.
You can also check out using a nested function for passing parameters in Passing Extra Parameters. That way you would not have to call optimoptions each time you update the parameters.
Alan Weiss
MATLAB mathematical toolbox documentation
Nick Rowe on 10 Feb 2022
Thank you!

### Categories

Find more on Genetic Algorithm in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by