# How can I plot the population before evaluating fitness in Matlab GA?

1 view (last 30 days)
Jacob Child on 18 May 2022
Commented: Walter Roberson on 22 May 2022
Problem Summary:
(See TLDR below if you want to skip this)
I am attempting to plot the current population before evaluating the fitness of said population. The population is made up of "shapes" that the user evaluates and assigns a fitness value to. I have the fitness function working and asking the user to evaluate, however the output function plots don't show up until after the fitness evaluation. For other reasons it is much more beneficial to plot the whole population at once, and not just one individual at a time which would be very easy from within the fitness function.
What I've tried:
I have an output function that works and plots the current population using state.Population, the problem is the Output function runs after the fitness function. How can I plot before the fitness function. I tried following the "passing extra parameters" documentation, but can't figure out how to do it correctly. I figured the easiest way to solve the problem would be pass the state structure into the fitness function, just like in the Output function, but couldn't get it to work with this code-
FitnessFunction = @(x) MyFitnessTest2(x,state);
[solution,objectiveValue] = ga(FitnessFunction,a,[],[],[],[],lowerbound,...
upperbound,[],[],options);
It throws an error on state saying I need a different toolbox for it, which I think means it probably is not yet defined as nothing has yet been evaluated? I don't know much about structures, or the state structure
Succinct Question (TLDR;)-
How do I plot the current population before running the fitness function, or pass the state structure/current population into the Fitness Function (and then I can plot it in there)?

Walter Roberson on 18 May 2022
Edited: Walter Roberson on 18 May 2022
You cannot do that using ga() . ga() will never pass the state to the fitness function.
You are also assuming that ga() is creating the entire new population before evaluating any of it, but ga() never promises that.
You could potentially take advantage of the fact that the mutation function is passed the state. However the mutation is just one step in creating the new population, and unless you had reason to believe that it was the last step, you might not be receiving the final population.
However.... if you set UseVectorized to true, then your fitness function will be passed all of the population at once, and you could plot there before doing the cost evaluation.
##### 2 CommentsShowHide 1 older comment
Walter Roberson on 22 May 2022
Unfortunately at the moment I do not know the circumstances under which a single row is evaluated.

R2020a

### Community Treasure Hunt

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

Start Hunting!