Saving / Showing Value of Variables in Genetic Algorithms

Hi there, I want to solve a problem using matlab.
It´s working great, I´m getting a solution after XXX generations.
But, it is only displaying me the FINAL solution for x(1) and x(2), the coordinates of the optimum solution. Let´s just say, I want to display the values of x(1) and x(2) after every iteration.
I want to show / save ALL values of x(1) and x(2) in the fitness function, so I can plot it in excel and show like: Generation 1 x(1) and x(2) was bad solution Generation 2 better etc etc Generation XXX x(1) and x(2) are optimal
Any idea? its driving me crazy, tried the iterative display, this is getting me nowhere.
Please be patient, I am a beginner with matlab...
Best regards, Marc

Antworten (4)

Alan Weiss
Alan Weiss am 26 Mai 2015

1 Stimme

Use an output function to get whatever you like from each iteration. There are examples using output functions in a different syntax here (careful, fmincon and ga use different syntax for their output functions, but the idea is the same).
Alan Weiss
MATLAB mathematical toolbox documentation

1 Kommentar

Hi Alan,
Do you have an example for this function for genetic algorithm? I have been trying to convert the fmincon syntax to ga syntax but it has not run. Your help would be truly appreciated.
Many thanks.
Precious Ogbeiwi

Melden Sie sich an, um zu kommentieren.

Geoff Hayes
Geoff Hayes am 25 Feb. 2015
Marc - according to Genetic Algorithm, all you need to do is include the output variable as an output parameter of the ga function. Something like
[x,fval,exitflag,output] = ga(fitnessfcn,nvars,...)
and output will be a structure that contains output from each generation and other information about the performance of the algorithm. Does this not include the optimal solution found at each generation?

4 Kommentare

Geoff Hayes
Geoff Hayes am 26 Feb. 2015
Bearbeitet: Geoff Hayes am 26 Feb. 2015
Marc's answer moved here
Hi Geoff,
unfortuneately it is not working, I tried this already.
example:
[x,fval,output]=ga(@simplefitness,2)
returns:
x =
44.8542 64.3680
fval =
3.7211e+04
output =
1
Now If I could just get it like:
Iteration / Generation
1 ---- x = 14 / 25,6
2 ---- x = 20 / 56
3 ---- x = 76 / 12
and so on....
Msrc - your output list has only three output parameters:
[x,fval,output]
While you may have named the third one output, this would correspond to the exitflag from my example as it isn't the name of the parameter that is important but its order in the list. Please try this again with four parameters, namely
[x,fval,exitflag,output]=ga(@simplefitness,2)
Marc's answer moved here
hey Geoff!
Thanks again for your answer, i reall appreciate it!
Still it does not work...
>> [x,fval,exitflag,output] = ga(@simplefitness,2) Optimization terminated: average change in the fitness value less than options.TolFun.
x =
44.8535 64.2461
fval =
3.7211e+04
exitflag =
1
output =
problemtype: 'unconstrained'
rngstate: [1x1 struct]
generations: 86
funccount: 4350
message: 'Optimization terminated: average change in the fitness value less than options.Tol...'
maxconstraint: []
Marc - the output structure is disappointing. I would have thought that it would include some basic statistics on each generation.
You may have to look at using an output function that fires at each generation. See ga options output functions for details. Based on the documentation, your output function would have the following signature
[state,options,optchanged] = myfun(options,state,flag)
where the input state structure has these properties which includes population and score which you could perhaps use to determine which member of the current population has the bet fitness. You could then write this data to file (or a persistent variable) for future analysis.

Melden Sie sich an, um zu kommentieren.

Sri
Sri am 26 Mai 2015

0 Stimmen

Marc did you find the solution? I have a similar problem. I want the values of x(1) and x(2) after optimization
Gifari Zulkarnaen
Gifari Zulkarnaen am 6 Apr. 2020

0 Stimmen

This guy here give an example output function to obtain variable in each iteration on a pattern search optimization. Maybe it is similar with genetic algorithm.

Gefragt:

am 24 Feb. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by