How to access the intermediate calculation in genetic algorithm optimisation?

Hi, I would like to use my own data instead of a function when using the genetic algorithm optimisation. In other words, instead of defining a mathematical function, I would like to enter for each generation the values for the fitness manually. I would like to plot how thi fitness is improving over the generation. I am also trying to see and plot the solutions that Aspen is testing for each generation. The reason for doing this is that I am doing experimental work with no known matematica model and the netic algorithm is guiding me to choose the right x solutions and the f(x) is the output of the experiment that I need to enter manually.

Antworten (1)

You can do this by writing a fitness function that prints the location x and waits for your input:
function y = myfun(x)
disp(x)
y = input('What is f(x)?')
HOWEVER, I think that you will find it much better to use patternsearch instead of ga. If you look at the comparison of several solvers, you see that patternsearch finds a minimum in many, many fewer function evaluations. And if, as in the example, you want to find a better minimum, then start patternsearch from initial random positions, as indicated in the documentation. And patternsearch is much easier to tune than ga, if you should decide to do so.
Alan Weiss
MATLAB mathematical toolbox documentation

3 Kommentare

Hi Alan, This is exacly what I was looking for. If I have multiple x. Is the floowing correct:
function y = myfun(x1, x2,x3) disp(x1,x2,x3) y = input('What is f(x1,x2,x3)?')
Do you think this function will be run n times (n is the number of samples for each generation?
Is this applicable to patternsearch? and why is GA is more popular? Thanks, Zied
Typically, x is a row vector (for ga, it can be any orientation for patternsearch). So you don't have three inputs, you just have one, a vector whose first part can be x1, the next part x2, the last part x3.
I don't understand what you mean when you ask if it will be run n times. MATLAB will ask you for function evaluations many times. Take a look at the examples in the documentation to get an idea of how many times. It may ask you for the same point more than once, so you might want to record the points and have them available, or use the patternsearch 'Cache' option, set to 'on', to avoid repeated evaluations of a point.
Yes, of course it is applicable to patternsearch. As to why ga is more popular than patternsearch, I don't know, but it is not a sensible thing, as patternsearch is usually much better in almost every respect.
Alan Weiss
MATLAB mathematical toolbox documentation
Hi Alain, Thank for the reply. I tried the following function in patternsearch with RMSE
% if true
function F = patternsearch1(x)
g= [1 1 1 1];
F= (sum((x-g).^2)/4)^0.5;
end
This solver run with initial values -10000 5000 55 999
The solver found these values 1.0060319876161277 1.002031669976748 1.000009409533599 1.0001629848123759
This is good.No? after only 54 iterations
Now I cangd my function to the following:
if true
function F = patternsearch2(x)
disp(x)
target =10;
m = input('What is f(x)?')
disp('target -y')
F =(sum((target-m).^2)/4)^0.5;
end
Do you see anything wrong with this function (I am trying to see the values of X and enter manually the value of the m then I measure the RMSE). My objective now is not to run a population p of x vectors instead of one and enter p values for m from the experimental work. Is this possible to change in my script?
Thanks, Zied

Melden Sie sich an, um zu kommentieren.

Produkte

Gefragt:

am 5 Jul. 2018

Bearbeitet:

am 30 Jul. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by