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

14 次查看(过去 30 天)
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
Walter Roberson 2022-5-18
编辑:Walter Roberson 2022-5-18
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 个评论
Jacob Child
Jacob Child 2022-5-19
That worked! Thank you! I did read something about how the fitness function is occasionally called without the full population. Do you know under what circumstances the ga solver does this? Documentation
Here is the quote "The fitness function, and any nonlinear constraint function, must accept an arbitrary number of rows to use the Vectorize option. ga sometimes evaluates a single row even during a vectorized calculation."
Walter Roberson
Walter Roberson 2022-5-22
Unfortunately at the moment I do not know the circumstances under which a single row is evaluated.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by